Completely Unloading A World

Discussion in 'Spigot Discussion' started by Donkelyn, Apr 6, 2020.

  1. How do I completely unload a world. I am trying to delete it then copy the world over from another map. This works as I have it work onEnable() but whenever I use the code while the server is online it doesn't actually unload the world meaning the files aren't deleted. Code Below:
    Code (Text):
                    World delete = Bukkit.getWorld("Ragnarokworld");
                    File deleteFolder = delete.getWorldFolder();
                    Bukkit.unloadWorld("Ragnarokworld", false);
                    Chunk[] chunks = delete.getLoadedChunks();
                    for (Chunk chunk : chunks) {
                        chunk.unload(false);
                    }
                    deleteWorld(deleteFolder);
                   
                    System.gc();
                   
                    Bukkit.getServer().createWorld(new WorldCreator("Ragnarokclassic"));
                    World source = Bukkit.getWorld("Ragnarokclassic");
                    File sourceFolder = source.getWorldFolder();

                    World target = Bukkit.getWorld("Ragnarokworld");
                    File targetFolder = target.getWorldFolder();

                    copyWorld(sourceFolder, targetFolder);
                    Bukkit.broadcastMessage(ChatColor.GOLD + "Reset Complete");
     
  2. Your method to delete the world and replace it with another, you have to put it in the onDisable
     
  3. Will that unload the world while the server is online tho?
     
  4. As far as I know a world is finish unloadet some ticks after you call Bukkit.unloadWorld.

    This means you must unlod the world -> wait some ticks -> delete files
     
  5. what's in your deleteWorld method? I think this will lead to the cause of all problems.
     
  6. Why don't you just close the server for a sec, delete the files and move te new files into the server. Then startup the server.
    The method "unloadchunks" does only unload the chunk from the server's memory, it does not delete the actual file. Besides, if there are players on the server, chunks around the players will automatically be loaded again.
     
  7. The issue isn't actually deleting the world as it works when I turn the server off. The issue is unloading the world so the files can be manipulated. I empty the world and then start the deleteworld() but it doesn't delete cause the files are loaded and cannot be changed
     
  8. To unload a world simply use the following:
    Code (Java):
    // unload and save world
    Bukkit.unloadWorld(world, true);
    You can also choose to not save the world by passing on false. It might be wise to kick all players (teleport them out of the world) before unloading.

    Edit: to delete the world folder after unloading use:
    Code (Text):
    FileUtils.deleteDirectory(worldFolder);
    To get the world folder simply use:
    Code (Text):
    File worldFolder = new File(Bukkit.getServer().getWorldContainer() + File.separator + world.getName());
     
    • Like Like x 1
    • Useful Useful x 1