Edit existing world data?

Discussion in 'Plugin Development' started by xDeeKay, Jun 22, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    xDeeKay

    Is there a way to edit an existing worlds data such as the seed, type (default, flat, etc), and things like that? I've seen some threads on reflections, but they all seem outdated. Is this still possible within Bukkit?
     
  2. I don't think this is possible, and even if it is why would you even want that? It would have 1 part of the world using a seed and generator, but the other parts are another type, it would look messy.
     
  3. Offline

    xDeeKay

    @megamichiel Sigh.. The seed was just an example.. I don't plan on changing the seed. We have worlds that are supposed to be generating as flat worlds, but are being generated with terrain. Somewhere down the line something messed up and I wish to fix it. I could use mcedit to do this, but I was just checking to see if this was possible.

    Messy or not, the purpose of this isn't to make anything look pretty.
     
  4. @xDeeKay
    The seed I mentioned was also an example, I just wondered why you would want to do it because terrain changing would be a bit weird. I'm not sure how one used to do it, but one way to do it is unloading the world, and then re-generating it using another generator. I can have a look at some nms stuff to find info about the generator.
     
  5. Offline

    xDeeKay

    @megamichiel I've seen threads that talk about accessing the CraftWorld world data fields, making it accessible, then setting the field to whatever. Not sure how reliable or safe this is though, might just be better off using mcedit.
     
  6. @xDeeKay
    Actually I just found something :p. In the NMS class World there is a field named 'generator' of type ChunkGenerator from the bukkit package. I can give you an example code on how to change it, I am not sure if this will make it work, but you're gonna have to test it out.
    Code:
    public void setGenerator(World world, ChunkGenerator gen) {
      try {
        Object handle = world.getClass().getDeclaredMethod("getHandle").get(world); //No private method
        String version = Bukkit.getServer().getClass().getPackage().getName();
        version = version.substring(version.lastIndexOf('.') + 1);
        Class<?> worldClass = Class.forName("net.minecraft.server." + version + ".World");
        worldClass.getDeclaredField("generator").set(handle, gen); //Not a private field
      } catch (Exception ex) { //Some reflect exceptions, shouldn't be thrown
        ex.printStackTrace();
      }
    }
     
  7. Offline

    xDeeKay

    Thanks for that, will definitely try it out and let you know how it went.

    Edit: @megamichiel Apparently on line 3 .get() is undefined. Also, I'm unsure how to use ChunkGenerator here, what does it return and how would I tell it to use a flat generation?

    Edit2: Getting this error:
    Code:
    [15:29:04] [Server thread/INFO]: xDeeKay issued server command: /worldgenflat
    [15:29:04] [Server thread/WARN]: java.lang.IllegalArgumentException: Can not set org.bukkit.generator.ChunkGenerator field net.minecraft.server.v1_8_R3.World.generator to java.lang.reflect.Method
    [15:29:04] [Server thread/WARN]:     at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
    [15:29:04] [Server thread/WARN]:     at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
    [15:29:04] [Server thread/WARN]:     at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(Unknown Source)
    [15:29:04] [Server thread/WARN]:     at sun.reflect.UnsafeObjectFieldAccessorImpl.set(Unknown Source)
    [15:29:04] [Server thread/WARN]:     at java.lang.reflect.Field.set(Unknown Source)
    [15:29:04] [Server thread/WARN]:     at net.dkcraft.opticore.commands.WorldGenFlat.setGenerator(WorldGenFlat.java:24)
    [15:29:04] [Server thread/WARN]:     at net.dkcraft.opticore.commands.WorldGenFlat.onCommand(WorldGenFlat.java:42)
    [15:29:04] [Server thread/WARN]:     at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    [15:29:04] [Server thread/WARN]:     at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140)
    [15:29:04] [Server thread/WARN]:     at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:621)
    [15:29:04] [Server thread/WARN]:     at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1079)
    [15:29:04] [Server thread/WARN]:     at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:939)
    [15:29:04] [Server thread/WARN]:     at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:37)
    [15:29:04] [Server thread/WARN]:     at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:9)
    [15:29:04] [Server thread/WARN]:     at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13)
    [15:29:04] [Server thread/WARN]:     at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    [15:29:04] [Server thread/WARN]:     at java.util.concurrent.FutureTask.run(Unknown Source)
    [15:29:04] [Server thread/WARN]:     at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19)
    [15:29:04] [Server thread/WARN]:     at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:676)
    [15:29:04] [Server thread/WARN]:     at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335)
    [15:29:04] [Server thread/WARN]:     at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:632)
    [15:29:04] [Server thread/WARN]:     at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:540)
    [15:29:04] [Server thread/WARN]:     at java.lang.Thread.run(Unknown Source)
     
    Last edited: Jun 24, 2015
Thread Status:
Not open for further replies.

Share This Page