Solved Fully delete a world - Doesnt work :s

Discussion in 'Plugin Development' started by RainoBoy97, Sep 21, 2013.

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

    RainoBoy97

    Howdy!

    I'm trying to completely delete a world, while the server is running. But the issue I'm having is that it deletes everything except the /region folder inside the world folder. I have different delete methods, with no luck.
    Any help would be appreciated :D

    Relevant code: http://hastebin.com/hayoxubohu.coffee

    Thanks :) -Raino
     
  2. Offline

    L33m4n123

    you would need to unload the world completely first before you can delete it
     
  3. Offline

    Janmm14

    RainoBoy97
    These files may not be deleted because tge server is using them.
     
  4. Offline

    RainoBoy97

    Janmm14
    Is there any possibility to unload them from that?
     
  5. Offline

    Janmm14

    RainoBoy97
    No possibility of unloadjng, but you can do world.regenerateChunk
     
  6. Offline

    RainoBoy97

    Janmm14
    I want to fully delete the world, not regenerate it.
     
  7. Offline

    Janmm14

    RainoBoy97
    No possibility with bukkit methods.
     
  8. Offline

    ThunderWaffeMC

    Using basic java:

    Code:
    public static boolean deleteDirectory(File path) {
      if( path.exists() ) {
          File files[] = path.listFiles();
          for(int i=0; i<files.length; i++) {
              if(files[i].isDirectory()) {
          deleteDirectory(files[i]);
          }
              else {
                  files[i].delete();
              } //end else
          }
          }
      return( path.delete() );
      }
    and then use:

    Code:java
    1.  
    2. World worldOne = Bukkit.getWorld("the world to delete");
    3. Bukkit.getServer().unloadWorld(worldOne, true);
    4. if(worldOne.equals(null)) { //is unloaded
    5. File folder = worldOne.getWorldFolder();
    6. deleteDirectory(folder);
    7. }
    8.  
     
  9. Offline

    Janmm14

    ThunderWaffeMC
    didn't knew the method unloadWorld() from Bukkit

    But I ask you: Why do you use Bukkit.getServer()????? just use Bukkit
     
  10. Can't fully delete a world. r.0.0.mca and r.0.-1.mca are the spawn chunks and can't be unloaded.
     
  11. Offline

    RainoBoy97

    ThunderWaffeMC
    I got it to delete, the problem is the region files.. Those wont delete...

    RealmsofValendor
    Is there any way you could force unload them so you can delete them?
     
  12. Offline

    Shevchik

    You can't delete main world files while your server is running.
    I have seen some method using NMS, but not sure that it worked.
     
  13. Offline

    andre111

    Well it is possible, but only using NMS code and somec hacky code which you should not use when you don't know what it is exactly doing. It also breaks with certain plugins. (e.g.: When LWC is running and protecting a Block in the world, it cann't be deleted.)

    But here is the code, you will have to edit it for your own plugin:
    Code:java
    1. public static void resetMainWorld(final World w) {
    2. if(w!=null) {
    3. final String wname = w.getName();
    4. w.setAutoSave(false);
    5.  
    6. for ( Player player : w.getPlayers() ) {
    7. InventoryHandler.clearInv(player, false);
    8. player.teleport(Bukkit.getServer().getWorlds().get(0).getSpawnLocation());
    9. player.sendMessage(ConfigManager.getLanguage().getString("string_tp_reset", "World is resetting - You have been teleported to the Lobby"));
    10. }
    11.  
    12. //do not register a task/delete the world on serverstop
    13. if(!Bukkit.getPluginManager().isPluginEnabled(DvZ.instance)) return;
    14.  
    15. Bukkit.getScheduler().scheduleSyncDelayedTask(DvZ.instance, new Runnable() {
    16. public void run() {
    17. if(w!=null) {
    18. File wf = new File(Bukkit.getServer().getWorldContainer().getPath()+"/"+ConfigManager.getStaticConfig().getString("world_prefix", "DvZ_")+"Main"+id+"/");
    19.  
    20. DynamicClassFunctions.bindRegionFiles();
    21. DynamicClassFunctions.forceUnloadWorld(w);
    22. DynamicClassFunctions.clearWorldReference(wname);
    23.  
    24. FileHandler.deleteFolder(wf);
    25.  
    26. }
    27. }
    28. }, 40);
    29. }
    30. }


    You also need some methods from this class:
    WARNING: This class circumvents Bukkits "Versioning system" and is disacuraged to use.
    https://github.com/andre111/Dwarves...1/dvz/volatileCode/DynamicClassFunctions.java
    This needs to be called on plugin load:
    Code:java
    1. // Dynamic package detection
    2. if (!DynamicClassFunctions.setPackages()) {
    3. logger.log(Level.WARNING, "NMS/OBC package could not be detected, using " + DynamicClassFunctions.nmsPackage + " and " + DynamicClassFunctions.obcPackage);
    4. }
    5. DynamicClassFunctions.setClasses();
    6. DynamicClassFunctions.setMethods();
    7. DynamicClassFunctions.setFields();


    So as a conclusing: Yes it is possible to delete a world while the server is running(But I never tried it with one of the main worlds and I guess it would fail). but it is really fragile code that can break very easily and you should only use it when you know how it works.
     
  14. Offline

    RainoBoy97

    andre111
    Thanks! :D
    I'll try it out and see if it works :)

    EDIT: Works!!! Awesome, thanks! :D

    Shevchik
    I was not trying to delete the main world :p But another world I created with another commands ;P

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
Thread Status:
Not open for further replies.

Share This Page