How can I duplicate a world

Discussion in 'Plugin Development' started by LugnutsK, Jun 20, 2014.

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

    LugnutsK

    I'm not talking about backing up or creating archives of worlds. I want to have one world loaded in my server, then run a code they will duplicate it, so the exact world will have two copies loaded (with different players in them, etc).
    I first tried completely duplicating the world folder and making a copy. Then I called
    Code:java
    1. Bukkit.createWorld(new WorldCreator(<name of new world>));

    Bukkit complained about duplicate UIDs, requesting I delete the UID.dat file. So I did that. However, when running the code it was obvious that the world was being regenerated with a different seed. Entering the world confirmed that they were indeed, different. (They also had a lot of non-matching chunks, which makes sense).

    So my question: how can I programmatically clone worlds, and load both versions?
    Thanks!
     
  2. Offline

    TheHandfish

    I expect you could use Apache's CommonsIO to copy the folder, but I'm not entirely sure. My idea sounds like it could cause problems, however.
     
    LugnutsK likes this.
  3. Offline

    LugnutsK

    I used Java's FileUtils.copyDirectory(); to copy the world folder. Getting it into Bukkit as a different world was where I was having trouble; it asks for the UID.dat file to be removed because of the duplicate UIDs, but that seems to hold some important info (possibly the seed). After loading the copied world it just ends up creating new chunks instead of using the existing saved files.
     
  4. Offline

    LugnutsK

    Here is the relevant code I am using. If anyone could point me in the right direction, that would be great:
    Code:java
    1. if (Bukkit.getWorld("<FIRST WORLD NAME>") == null) {
    2. sender.sendMessage(ChatColor.RED + "World does not exist.");
    3. return true;
    4. }
    5. File uid;
    6. try {
    7. FileUtils.copyDirectory(new File(Bukkit.getWorldContainer().getCanonicalPath() + "/" + "<FIRST WORLD NAME>"),
    8. new File(Bukkit.getWorldContainer().getCanonicalPath() + "/" + "<NEW WORLD NAME>"));
    9. uid = new File(Bukkit.getWorldContainer().getCanonicalPath() + "/" + "<NEW WORLD NAME>" + "/uid.dat");
    10. }
    11. catch (Exception e) {
    12. sender.sendMessage(ChatColor.RED + "Invalid world(s).");
    13. return true;
    14. }
    15. if (uid.exists())
    16. uid.delete();
    17. Bukkit.createWorld(new WorldCreator("<NEW WORLD NAME>"));
    18. return true;
     
  5. Offline

    unrealdesign

    LugnutsK likes this.
  6. LugnutsK It's a shame there's literally nothing in the Bukkit API to do this.

    Wait, what's this?
     
  7. Offline

    LugnutsK

    Sorry for the late reply, and thank you all for your help.

    unrealdesign Thank you for the link, this is very helpful. There is only one issue though, specifically:
    Code:java
    1. // The world to overwrite when copying
    2. World target = Bukkit.getWorld("NewWorld");
    3. File targetFolder = target.getWorldFolder();

    I don't want to overwrite a world, I want to create a new one. The above code will always cause a NPE. I have the code I tried a while ago above:
    Code:java
    1. Bukkit.createWorld(new WorldCreator("<NEW WORLD NAME>"));

    The problem is that this code starts generating new chunks and whatnot, overwriting the copied files.

    .

    AdamQpzm Thanks for trying, but that code creates a WorldCreator, and I'm not interested in generating new chunks, which would reset any user-built structures.

    .

    I've also been looking at Multiverse's source to see their method, I'll post it here if I get it working.
     
Thread Status:
Not open for further replies.

Share This Page