How to fully delete worlds!!

Discussion in 'Plugin Development' started by Theminebench, Oct 29, 2014.

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

    Theminebench

    After doing some looking around and I did not find any simple way to delete a world, so I wrote this:
    Code:java
    1. private void deleteWorld(String worldName) {
    2.  
    3. World world = Bukkit.getWorld(worldName);
    4. Bukkit.broadcastMessage(world.getName());
    5. //TODO kick all players on world, or move them to a different world
    6. if (Bukkit.getServer().unloadWorld(world, false)) {
    7. logger.info("Successfully unloaded " + worldName);
    8. } else {
    9. logger.severe("COULD NOT UNLOAD " + worldName);
    10. }
    11.  
    12. File wFile = world.getWorldFolder();
    13. try {
    14.  
    15. deleteFile(wFile);
    16.  
    17. } catch (IOException e) {
    18. e.printStackTrace();
    19. }
    20.  
    21. }
    22.  
    23.  
    24. public void deleteFile(File f) throws IOException {
    25. if (f.isDirectory()) {
    26. for (File c : f.listFiles())
    27. deleteFile(c);
    28. }
    29. if (!f.delete())
    30. throw new FileNotFoundException("Failed to delete file: " + f);
    31. }
    32.  


    This seems to leave the region file, anyone know how to do it completely??
     
  2. Offline

    Gamecube762

    So ... are you asking for help on deleting a world or ... are you trying to help us?

    If you need help, then what's your problem?
    If you are trying to show this, wrong section. Add some more information to the post and move it to Resources.
     
  3. Offline

    Theminebench

    Gamecube762 I'm asking for help. (I edited it to make it more clear)
     
  4. Offline

    CraftCreeper6

    Theminebench
    Delete the region file...

    Code:java
    1. File f = new File(path, "regions.fileExtension");
    2. f.delete();
     
    Theminebench likes this.
  5. Offline

    Theminebench

    CraftCreeper6
    In the method deleteFile shouldn't it automaticly do that?
     
  6. Offline

    CraftCreeper6

    Theminebench likes this.
  7. Offline

    Theminebench

    CraftCreeper6 I can not figure this out, sometimes it deletes the whole thing and sometimes it does not. Now not only does it leave the region, but also session.lock and uid.dat. I have no idea where to go from here
     
  8. Offline

    Funergy

    Theminebench What you should try is add some time between the unloading and the deleting.
    Thats how I've done it.
    A world needs to save and sometimes is says its unloaded but its actually not. so just add a little delay between the unloading and deleting and everything should be fine ;)
     
    Theminebench likes this.
  9. Offline

    Freelix2000

    I think you missed the bottom text in this thread.
     
    Theminebench likes this.
  10. Offline

    Funergy

    Theminebench likes this.
  11. Offline

    Gamecube762

    Theminebench Make sure the world is fully unloaded and wait a tick to make sure. I also suggest to run the deletes on an async thread so it doesn't slow down the server.

    I didn't, post was edited by OP.
     
    Theminebench likes this.
  12. Offline

    Funergy

    Gamecube762 Like what I've said some posts ago :p
     
    Theminebench likes this.
  13. Offline

    Freelix2000

    CraftCreeper6
    I noticed something in your signature... It seems your "Intellij" class requires an Intellij to create one, which means you cannot possibly create an Intellij object without using null, which would likely throw a NullPointerException if it actually does something. Is that an intentional paradox, or did you mean to use the provided Intellij instance rather than creating a new one with the provided one? =P
     
  14. Offline

    CraftCreeper6

    Freelix2000
    Ah, you see. Signatures are not meant to be serious ;)
     
    Theminebench likes this.
  15. Offline

    Freelix2000

    CraftCreeper6
    Yes, that is why I asked if it was intentional because I don't get it. =P
     
    Theminebench likes this.
  16. Offline

    Theminebench

  17. Offline

    Theminebench

    So... After using this code to delete worlds
    Code:java
    1. public static void deleteWorld(final String mapName) {
    2. final Plugin plugin = MixedArcade.getPlugin();
    3.  
    4.  
    5. final File file;
    6.  
    7.  
    8.  
    9. World world = Bukkit.getServer().getWorld(mapName);
    10.  
    11. if (world == null) {
    12. System.out.print("The world " + mapName + " is not loaded!");
    13. file = new File(plugin.getServer().getWorldContainer(), mapName);
    14.  
    15. // Bukkit.broadcastMessage("Path: " +
    16. // file.getAbsolutePath());
    17.  
    18. if (!file.exists()) {
    19. System.out.print("Could not find file at: " + file.getPath() + " Something can't be deleted if its not there.");
    20. return;
    21. }
    22. try {
    23. FileUtils.deleteFile(file);
    24. } catch (IOException e) {
    25. e.printStackTrace();
    26. return;
    27. }
    28. System.out.print("Deleted the files found at: " + file.getPath() + " though.");
    29.  
    30. return;
    31.  
    32. } else {
    33. file = world.getWorldFolder();
    34. }
    35.  
    36. if (Bukkit.getServer().unloadWorld(world, false)) {
    37. System.out.print("Successfully unloaded " + world.getName());
    38. } else {
    39. System.out.print("COULD NOT UNLOAD " + world.getName());
    40. return;
    41. }
    42.  
    43. new BukkitRunnable() {
    44.  
    45. @Override
    46. public void run() {
    47. try {
    48. FileUtils.deleteFile(file);
    49. System.out.print("Successfully deleted " + mapName);
    50. return;
    51. } catch (IOException e) {
    52. System.out.print("COULD NOT DELETE " + mapName + "!");
    53. e.printStackTrace();
    54. }
    55.  
    56. }
    57. }.runTaskLater(plugin, 20);
    58.  
    59. return;
    60. }

    And the method deleteFile

    Code:java
    1. public static void deleteFile(File file) throws IOException {
    2.  
    3. if (file.isDirectory()) {
    4. for (File c : file.listFiles())
    5. deleteFile(c);
    6. }
    7. if (!file.delete())
    8. throw new FileNotFoundException("Failed to delete file: " + file);
    9. }


    This works most of the time, but sometimes it gives this error

    Code:
    2:58:55 PM [INFO] Successfully unloaded SpleefMap
    2:58:56 PM [INFO] COULD NOT DELETE SpleefMap!
    2:58:56 PM [WARNING] java.io.FileNotFoundException: Failed to delete file: .\SpleefMap\region\r.-1.-1.mca
    2:58:56 PM [WARNING]    at me.theminebench.mixedarcade.utils.FileUtils.deleteFile(FileUtils.java:20)
    2:58:56 PM [WARNING]    at me.theminebench.mixedarcade.utils.FileUtils.deleteFile(FileUtils.java:17)
    2:58:56 PM [WARNING]    at me.theminebench.mixedarcade.utils.FileUtils.deleteFile(FileUtils.java:17)
    2:58:56 PM [WARNING]    at me.theminebench.mixedarcade.utils.WorldUtils$1.run(WorldUtils.java:88)
    2:58:56 PM [WARNING]    at org.bukkit.craftbukkit.v1_7_R4.scheduler.CraftTask.run(CraftTask.java:71)
    2:58:56 PM [WARNING]    at org.bukkit.craftbukkit.v1_7_R4.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350)
    2:58:56 PM [WARNING]    at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:641)
    2:58:56 PM [WARNING]    at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289)
    2:58:56 PM [WARNING]    at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584)
    2:58:56 PM [WARNING]    at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490)
    2:58:56 PM [WARNING]    at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628)
    2:59:05 PM [INFO] We found a file at: .\SpleefMap Please delete this file!
    2:59:10 PM [SEVERE] Map SpleefMap not loaded!
    
     
  18. Offline

    PreFiXAUT

    This error is caused when the File doesn't exist. So you're tying to delte a deleted File.
    Check if the File exists first, than delete it.
     
  19. Freelix2000 CraftCreeper6 Off-topic but might be beneficial to learn something about Java so I'll say it anyway :p

    Freelix2000, his signature wouldn't necessarily have to pass null. Classes can have more than one constructor, so he could have (for example) a blank constructor as well, make the object using the blank constructor, and then pass it that. For example:

    Code:
    new IntelliJ(new InteliiJ());
    I wouldn't think that there are many real life use cases that would utilise this, but maybe it could be something to enforce a parent-child relationship between the two objects *shrugs*

    In this case, however, the code would have a StackOverflowError if usingEclipse is false at the time of creation, since then the code would loop infinitely... even if the while loop was just an if statement, or the while loop had a break in it, this would be the case because creating a new IntelliJ object using the constructor in the signature would create a new IntelliJ object, which creates a new one, and a new one, and so on. :)
     
  20. Offline

    RainoBoy97

    What I figured out when doing this a couple months back:
    Code:
    Bukkit.unloadWorld("worldname", true); <- Notice "true"
    
    I don't know why it works if you save the world first, but if you don't it will not work. I assume it "unlocks" the region files while saving or something like that :p
     
    ChipDev likes this.
  21. Offline

    ChipDev

    True, thats why you always see 'Saving world' before the server ends; It saves the world before stopping then pretty much unlocks the files, which you can unload it with.
     
Thread Status:
Not open for further replies.

Share This Page