Util Unload, Delete & Copy Worlds

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

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

    ThunderWaffeMC

    Here's something you can use for unloading, deleting and copying worlds in a simple manor.

    Unloading a world (open)

    Add this to the end of your class:

    Code:java
    1.  
    2. public void unloadWorld(World world) {
    3. this.world = Bukkit.getWorld("");
    4. if(!world.equals(null)) {
    5. Bukkit.getServer().unloadWorld(world, true);
    6. }
    7. }
    8.  


    At the top of your class, add this variable:

    Code:java
    1.  
    2. private World world;
    3.  


    Now define the world to unload with: (and replace <your world name>)

    Code:java
    1.  
    2. World unload = Bukkit.getWorld("<your world name>");
    3.  


    Now simply use:

    Code:java
    1.  
    2. unloadWorld(unload);
    3.  



    Deleting a world (open)

    Add this to the end of your class:

    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());
    }
    Now define the world to delete with the variable "delete"

    Code:java
    1.  
    2. World delete = Bukkit.getWorld("somerandomworld");
    3. File deleteFolder = delete.getWorldFolder();
    4.  


    Now simply use:

    Code:java
    1.  
    2. deleteWorld(deleteFolder);
    3.  



    Copying a world (open)

    Add this to the end of your class:

    Code:java
    1.  
    2. public void copyWorld(File source, File target){
    3. try {
    4. ArrayList<String> ignore = new ArrayList<String>(Arrays.asList("uid.dat", "session.dat"));
    5. if(!ignore.contains(source.getName())) {
    6. if(source.isDirectory()) {
    7. if(!target.exists())
    8. target.mkdirs();
    9. String files[] = source.list();
    10. for (String file : files) {
    11. File srcFile = new File(source, file);
    12. File destFile = new File(target, file);
    13. copyWorld(srcFile, destFile);
    14. }
    15. } else {
    16. InputStream in = new FileInputStream(source);
    17. OutputStream out = new FileOutputStream(target);
    18. byte[] buffer = new byte[1024];
    19. int length;
    20. while ((length = in.read(buffer)) > 0)
    21. out.write(buffer, 0, length);
    22. in.close();
    23. out.close();
    24. }
    25. }
    26. } catch (IOException e) {
    27.  
    28. }
    29. }
    30.  


    Now define the source world and the target world

    Code:java
    1.  
    2. // The world to copy
    3. World source = Bukkit.getWorld("world");
    4. File sourceFolder = source.getWorldFolder();
    5.  
    6. // The world to overwrite when copying
    7. World target = Bukkit.getWorld("NewWorld");
    8. File targetFolder = target.getWorldFolder();
    9.  


    Now simply use:

    Code:java
    1.  
    2. copyWorld(sourceFolder, targetFolder);
    3.  



    Hope it helps!
     
  2. Offline

    Ty Cleere

    Does this delete the whole world? Including the spawn chucks? and everything?
     
  3. Offline

    MrSparkzz

    Ty Cleere
    This will delete the whole world file.

    ThunderWaffeMC
    This is pretty cool! I'll be sure to use it in my ServerControl plugin! :D
     
    ThunderWaffeMC likes this.
  4. Offline

    Gamecube762

    Looks nice, I remember trying to help you on a subject similar to this XD
     
    ThunderWaffeMC likes this.
  5. Offline

    ThunderWaffeMC

    Yeah I got it all working, Gamecube762 and since I couldn't find anything like it, I worked it out and posted it here! But I thank you very much for the help!

    MrSparkzz That's great to hear!
     
  6. Offline

    Garris0n

    This looks great, however it's "manner" not "manor".
     
    ThunderWaffeMC and Ultimate_n00b like this.
  7. Offline

    ThunderWaffeMC

    Oops. Well, I'm not going to change it otherwise the syntax will go wrong. Thanks anyway.
     
  8. Offline

    Garris0n

    copy-paste it first.
     
  9. Offline

    koencraft2002

    Do you have to import java.oi or org.bukkit?
     
  10. Offline

    ThunderWaffeMC

    Sorry for late reply; it's org.bukkit and anything todo with files is java imports.
     
  11. Offline

    Ultimate_n00b

    How would you load a world?
     
  12. Offline

    Skyost

  13. Offline

    unenergizer

    So would a practical use for this be a spleef map for a mini game? For instance, you are in the spleef arena, you destroy it, game is over players teleported out of map, then map is deleted, and a new copy is put in place from a backup/original source.

    Does this sound about right?
     
  14. Offline

    L33m4n123


    thats kinda what I use it for. Not for spleef but same idea
     
  15. Offline

    Qwerty89983nr2

    There is a much more easy way to copy a world:
    Code:java
    1. WorldCreator w = new WorldCreator("new_world");
    2. w.copy(Bukkit.getWorld("world"));
    3. w.createWorld();

    This will copy the world "world" and then load it.

    EDIT:
    I just tested it with a flat world and thought it copies the world but it just copies the generator settings of the world... Sorry ^^
     
    Skyost likes this.
  16. Offline

    GPSforLEGENDS

    The copy function does not work for me... i get a lot of errors in the console
    Code:java
    1. World source = Bukkit.getWorld("world");
    2. File sourceFolder = source.getWorldFolder();
    3.  
    4. // The world to overwrite when copying
    5. World target = Bukkit.getWorld("NewWorld");
    6. File targetFolder = target.getWorldFolder();
    7. @Override
    8. public void onEnable() {
    9. try{
    10. System.out.println("[FishBattle] saving the world...");
    11. copyWorld(sourceFolder, targetFolder);
    12. } catch(Exception e){
    13.  
    14. }
    15.  
    16. }
    17.  
    18. @Override
    19. public void onDisable() {
    20. try{
    21. copyWorld(targetFolder, sourceFolder);
    22. } catch(Exception e){
    23.  
    24. }
    25. }

    where is my fault?
     
  17. Offline

    viper_monster

  18. Offline

    GPSforLEGENDS

    ok i found the error... but i still have a problem: at onEnable it take a copy and save it but onDisable he does not copy the world back... but if i take the file from the "newWorld" and put them in world it do a copy

    Code:
    [19:24:30 ERROR]: Encountered an unexpected exception
    net.minecraft.server.v1_7_R3.ExceptionWorldConflict: The save is being accessed
    from another location, aborting
            at net.minecraft.server.v1_7_R3.WorldNBTStorage.checkSession(WorldNBTSto
    rage.java:72) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.World.G(World.java:2634) ~[craftbukkit.j
    ar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.WorldServer.a(WorldServer.java:790) ~[cr
    aftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.WorldServer.save(WorldServer.java:763) ~
    [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.saveChunks(MinecraftServ
    er.java:369) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    76) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    [19:24:30 ERROR]: This crash report has been saved to: C:\Users\Rob1998\Desktop\
    Alles\Plugins\TestServer 1.7.2\.\crash-reports\crash-2014-08-07_19.24.30-server.
    txt
    >
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 29, 2015
  19. Offline

    MiniDigger

    GPSforLEGENDS this problem is because when minecraft (the internal server) checks the session of the map, it notifies that the session is wrong. I usually avoid that by deleting the session file and creating a new one:
    Code:java
    1. File session = new File(Bukkit.getWorldContainer(), name + "/session.lock");
    2. session.delete();
    3. try {
    4. session.createNewFile();
    5. DataOutputStream dataoutputstream = new DataOutputStream(new FileOutputStream(session));
    6.  
    7. try {
    8. dataoutputstream.writeLong(System.currentTimeMillis());
    9. } finally {
    10. dataoutputstream.close();
    11. }
    12. } catch (IOException ioexception) {
     
  20. Offline

    KingFaris11

    Nice! If I hadn't made my own (my one is more (arguably less) certain it'll work since I use Reflection to force unload the world as Bukkit.unloadWorld() sometimes doesn't work properly or doesn't unload instantly) I'd have used this.
    Though...
    Code:
    if(!world.equals(null)) {
    [​IMG]
    Wouldn't that itself just throw a NullPointerException since you're calling ".equals" on a possibly null object. You should do: if (world != null).
     
    Last edited: Feb 10, 2015
    MCMastery likes this.
  21. Offline

    InfamousSheep

    Where do I place this in the copyWorld() method? Thanks in advance.
     
  22. Offline

    MiniDigger

    InfamousSheep I don't know your copy method but you should do this after the word is copied
     
  23. Offline

    metincasper

    @Qwerty89983nr2
    How to i replace a exiting world with the copy world?
     
  24. Offline

    TehVoyager

    @ThunderWaffeMC
    This does not seem to be working for me. Can you provide some code on how to use it.
     
  25. Offline

    RW_Craft

    Thanks! I'ma use it to unload worlds when people aren't on to optimise RAM usage!
     
  26. Offline

    teeter11

    I get a null pointer exception for

    Code:
        World source = Bukkit.getWorld("world");
    
        File sourceFolder = source.getWorldFolder();
    
        // The world to overwrite when copying
    
        World target = Bukkit.getWorld("NewWorld");
    
        File targetFolder = target.getWorldFolder();

    can you help me?
     
  27. Offline

    MiniDigger

    @teeter11 which line do you get the npe for? Do you have "world" and "NewWorld" loaded?
     
  28. Offline

    KingFaris11

    Because NewWorld probably doesn't exist or is not loaded yet. To get the folder of target, you should do:
    File targetFolder = new File(Bukkit.getWorldContainer(), "NewWorld");
     
  29. Offline

    Slendy_man19

    static World delete = Bukkit.getWorld("WORLD");
    static File deleteFolder = delete.getWorldFolder();

    This returns nullpointerexception
     
  30. Offline

    Mrs. bwfctower

    @Slendy_man19 You can't initialize 'delete' before Bukkit is initialized. Move the initialization to the onEnable method.
     
Thread Status:
Not open for further replies.

Share This Page