Util Unload, Delete & Copy Worlds

Discussion in 'Resources' started by ThunderWaffeMC, Oct 13, 2013.

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

    blue1

    Whoop whoop! Used your delete method. Works like a charm. :)
     
  2. Offline

    PhantomUnicorns

    @ThunderWaffeMC Btw this
    Code:
       public boolean deleteWorld(File path) {
            if (path.exists()) {
                File files[] = path.listFiles();
                for (int i = 0; i < files.length; i++) {
                    if (files[i].isDirectory()) {
                        deleteWorld(files[i]);
                    } else {
                        files[i].delete();
                    }
                }
            }
            return (path.delete());
        }
    
    can be changed to this
    Code:
        public boolean deleteWorld(String worldName, boolean loaded) {
            if (loaded) {
                unloadWorld(worldName);
            }
            File path = new File (worldName);
            if (path.exists()) {
                for (File file : path.listFiles()) {
                    if (file.isDirectory()) {
                        deleteWorld(worldName + "\\" + file.getName(), false);
                    } else {
                        file.delete();
                    }
                }
            }
            return (path.delete());
        }
    
    Just a little shorter and to unload a world:
    Code:
        public boolean unloadWorld(String worldName) {
            if (Bukkit.getWorld(worldName) != null) {
                Bukkit.getServer().unloadWorld(Bukkit.getWorld(worldName), true);
                return true;
            }
            return false;
        }
    
    And copy world to:
    Code:
        public void copyWorld(String worldName, String newLocation, String... ignore) {
            ArrayList<String> ignored = new ArrayList<String>(Arrays.asList(ignore));
            for (File file : new File(worldName).listFiles()) {
                if (!ignored.contains(file.getName())) {
                    if (file.isDirectory()) {
                        new File(newLocation + "\\" + file.getName()).mkdirs();
                        copyWorld(worldName + "\\" + file.getName(), newLocation + "\\" + file.getName() + "\\", ignore);
                    } else {
                        try {
                            InputStream in = new FileInputStream(file.getAbsolutePath());
                            OutputStream out = new FileOutputStream(newLocation.endsWith("\\")
                                    ? newLocation + file.getName() : newLocation + "\\" + file.getName());
                            byte[] buffer = new byte[1024];
                            int length;
                            while ((length = in.read(buffer)) > 0)
                                out.write(buffer, 0, length);
                            in.close();
                            out.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    
    And here is a load world function:
    Code:
        public World loadWorld(String worldName) {
            WorldCreator worldCreater = new WorldCreator(worldName);
            Bukkit.getServer().createWorld(worldCreater);
            return Bukkit.getWorld(worldName);
        }
    
    I have used them all, they all work for me, here is an example:
    Code:
    copyWorld ("world", "newWorld", "uid.dat");
    World world = loadWorld("newWorld");
    Bukkit.getConsoleSender().sendMessage(world.getName());
    
     
    Last edited: Nov 4, 2016
  3. Offline

    blue1

    I'd like to ask you about your copy method. I have a copy method that does successfully copy the world, but if I try to load and use the world, an error is thrown in the console that the world is already being used in another instance of Minecraft. Do you ever get that error? I'm needing something that doesn't require a server reload every time a world is copied.
     
  4. Offline

    PhantomUnicorns

    Hmm, I believe you would use the ignored part of my copy method and ignore what the original post owner ignored, if that doesn't work then tag and tell me and I will find the answer for you!
    He ignored: "uid.dat" and "session.dat"
     
    Last edited: Oct 30, 2016
  5. Offline

    blue1

    Well, it copies the world file, so far as I know. However, it still throws the error. The error is as follows:

    [21:19:31 ERROR]: Couldn't save chunk; already in use by another instance of Min
    ecraft?
    net.minecraft.server.v1_10_R1.ExceptionWorldConflict: The save for world located
    at .\ThreeCopy is being accessed from another location, aborting
    at net.minecraft.server.v1_10_R1.WorldNBTStorage.checkSession(WorldNBTSt
    orage.java:78) ~[spigot-1.10.jar:git-Spigot-6016ac7-d5ecbd0]
    at net.minecraft.server.v1_10_R1.World.checkSession(World.java:2820) ~[s
    pigot-1.10.jar:git-Spigot-6016ac7-d5ecbd0]
    at net.minecraft.server.v1_10_R1.ChunkRegionLoader.a(ChunkRegionLoader.j
    ava:124) ~[spigot-1.10.jar:git-Spigot-6016ac7-d5ecbd0]
    at net.minecraft.server.v1_10_R1.ChunkProviderServer.saveChunk(ChunkProv
    iderServer.java:229) [spigot-1.10.jar:git-Spigot-6016ac7-d5ecbd0]
    at net.minecraft.server.v1_10_R1.ChunkProviderServer.unloadChunks(ChunkP
    roviderServer.java:302) [spigot-1.10.jar:git-Spigot-6016ac7-d5ecbd0]
    at net.minecraft.server.v1_10_R1.WorldServer.doTick(WorldServer.java:234
    ) [spigot-1.10.jar:git-Spigot-6016ac7-d5ecbd0]
    at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:
    788) [spigot-1.10.jar:git-Spigot-6016ac7-d5ecbd0]
    at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:
    399) [spigot-1.10.jar:git-Spigot-6016ac7-d5ecbd0]
    at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:
    672) [spigot-1.10.jar:git-Spigot-6016ac7-d5ecbd0]
    at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.jav
    a:571) [spigot-1.10.jar:git-Spigot-6016ac7-d5ecbd0]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_101]

    And it repeats it about 30 times in the space of a second, and then does so again a few seconds later.
    Here's how I referenced your method:
    Code:
    copyWorld(oldWorldFolder.getName(), newWorldFolder.getName(), "uid.dat", "session.dat");
     
  6. Offline

    PhantomUnicorns

    Ok so the error was my bad coding :p, sorry for taking your time I've updated my code to be correct w/ an example usage!
     
  7. Offline

    blue1

    Oooooh so you're saying you use your copy method, and THEN you create the world! Well that makes sense. :p I guess it was also my bad coding here. I (mindlessly) created the world, then unloaded it, and then copied.
    Thanks for your help. :) I'm not in a hurry with this plugin, as it's for my own server and I've been trying to complete it for months anyways. xP

    EDIT: Ok, so I tried it out and it didn't throw the error anymore. :) However, it also does not copy the blocks that were recently placed, even if I first use getWorld(oldWorld.getName()).save(); Do you know any way to fix that, or am I missing something?
     
    Last edited: Oct 31, 2016
  8. Offline

    PhantomUnicorns

    Hmm, I'm guessing you need to wait a little after you save it (copying/saving a world is no small feat, it takes quite a bit of time) and I don't think saving pauses the code, so you would have to save and then use Bukkit's scheduler (no going to spoonfeed it but if you need more I will tell) and inside the Bukkit scheduler copy it.
     
  9. Offline

    blue1

    Trust me, I don't need spoonfeeding. :p I've made a total of 63 plugins so far, some for bigger servers like TerraMiningMC. But I've done very very little with worlds, so thanks for your help here.
    I'll try out the scheduler and see how it pans out.
     
  10. Offline

    PhantomUnicorns

    Ok, if it doesn't work I'll research alittle and see (but I'm pretty sure that the problem is that it is not saving in time for the copy)
     
  11. Offline

    PhantomUnicorns

    @blue1 I updated the functions to return some values, and I added one more function that loads a world (given that it is not null!)
     
  12. Offline

    blue1

    Sir, you have saved my life. I finally got time to try it (I've been busy installing solar panels :p ), and it worked like a charm. You have my gratitude. :3

    For anyone else who might come along and isn't sure what they're doing yet, here's the code I used.

    Code:
    public void copyWorld(World w) {
        File old = w.getWorldFolder();
        Bukkit.getServer().getWorld(old.getName()).save();
        Main.scheduler.scheduleSyncDelayedTask(Main.plugin, new Runnable(){
            @Override
            public void run() {
                Bukkit.getConsoleSender().sendMessage("World has been saved!");
                //COPY YOUR WORLD HERE USING ABOVE METHOD BY PhantomUnicorns
        }, (long)200);
    }
     
  13. Offline

    PhantomUnicorns

    That would take 10seconds everytime it is ran... because my method uses recursion. Just questioning about the amount of ticks you used.
     
  14. Offline

    blue1

    Indeed it does, but I actually use the 10 seconds as a timer for another event. It certainly doesn't take the 10 seconds I use, but it was a handy place to put it.
     
Thread Status:
Not open for further replies.

Share This Page