1.16.5 Deleting world files after unloading

Discussion in 'Spigot Plugin Development' started by Herobrine0819, Feb 16, 2021.

Thread Status:
Not open for further replies.
  1. I'm trying to delete a world, but I have to unload it first. So I have this code for unloading:

    Code (Text):

    public void unloadIsland(World world) {
           
            if(world != null) {
               
                Bukkit.getServer().unloadWorld(world, true);
               
            }
           
    }
     
    and I have this one to delete all the world files:

    Code (Text):

    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());
    }
     
    and I have this code to trigger both of them:

    Code (Text):

    public DeleteIsland(File path, World world) {
           
            unloadIsland(world);
           
            deleteWorld(path);
           
    }
     
    the problem is, it actually deletes all the world files before the server unloads, so it does not actually DELETE the map from it's memory until I restart the server or unload, but unloading does not work if the files does not exist

    how can I run deleteWorld(path) automatically after it is finished unloading?

    Any help is appreciated!
     
  2. Try
    if(world != null) {
    if(path.exists()) {
    File files[] = path.listFiles();
    for(int i=0; i<files.length; i++) {
    if(files.isDirectory()) {
    deleteWorld(files);
    } else {
    files.delete();
    }
    }
    }
     
  3. You can just simply do:?

    Code (Java):
            if (Bukkit.getServer().unloadWorld(world, true)){
                // TODO: Delete world files.
            }
    According to the Spigot Docs it will return a boolean based on if the world has been unloaded or not.
    How you will handle your try/catch is on you tho.

    Another approuch depends on where you are placing your code.
    1. If you are programming it on the main thread, you can make a runnable each second and check if the world has been unloaded, if so delete the files.
    2. If you use it onDisable, you can then just simply use Java Recursion and then deleting the files once it has been unloaded.
     
Thread Status:
Not open for further replies.