Deleting and regenerating a World?

Discussion in 'Plugin Development' started by HyrulesLegend, Dec 17, 2013.

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

    HyrulesLegend

    Does anyone know how I should go about deleteing and generating a world?

    I think this may work, not tested though (For deleting worlds):
    Code:java
    1. Bukkit.getServer().getWorld("world").getWorldFolder().delete();


    Using this to create worlds:
    Code:java
    1. Bukkit.createWorld(new WorldCreator("world"));
     
  2. Offline

    tommycake50

    You can't delete worlds from a plugin.
    If you unload a world Bukkit still hangs on to some resources and you can't delete it.
    You have to start an external script after the server is fully stopped.
     
  3. Offline

    JRL1004

    tommycake50 How does Multiverse delete worlds? "/mvdelete <worldname>"

    EDIT: tommycake50 Max_The_Link_Fan
    Would this work for erasing a world?
    Code:java
    1. public void deleteWorld(World world) {
    2. Bukkit.unloadWorld(world, false);
    3. File folder = world.getWorldFolder();
    4. if(!folder.isDirectory()) return;
    5. for(File file : folder.listFiles()) {
    6. file.delete();
    7. }
    8. folder.delete();
    9. Bukkit.getWorlds().remove(world);
    10. }
     
  4. Offline

    The_Doctor_123

    JRL1004
    You know, there's been lots of long topics regarding unloading a world. Apparently unloadWorld() doesn't fully unload the world, making the folder not editable. I'm not sure if that's changed since..
     
  5. Offline

    JRL1004

    The_Doctor_123 I wasn't aware of that. Hopefully it's been patched so that the world will completely unload. If the method I put above does not work then I will make a loop to go through all aspects of the world (Chunks, Entities, etc..) and unload / remove them individually so that the world can be deleted.
     
  6. Offline

    HyrulesLegend

    Then how would I go about after finishing my game, it stops the game. Kicks everyone, and creates a new world?
     
  7. Offline

    rbrick

    As tommycake50 said, you need an external script. every plugin that has world deleting as a feature as some kind of script that accomplishes this. And in order to do this automatically, you can make a script that is called when the game is completed, then shuts down the server, deletes the world files and restarts it. i believe you can do this within a .bat script. you could probably do it in python as well. but until worldUnload() gets fix you need a script. sorry.
     
  8. Offline

    EcMiner

    Max_The_Link_Fan likes this.
  9. Offline

    tommycake50

    Don't use .bat, Most servers are run on linux so an sh script would be better.
    A external JAR would be ideal though I guess.
     
  10. Offline

    EcMiner

    There is only one issue and that is that you can't delete your main world. With main world i mean the world that is defined in the server.properties. I hope you don't mind that :)
    Code:java
    1. public void resetmap() {
    2.  
    3. for (Player p : Bukkit.getOnlinePlayers()) {
    4. p.kickPlayer("Kick reason....");
    5. }
    6.  
    7. if (Bukkit.unloadWorld("worldname", true)) {
    8. System.out.println("Unloaded world");
    9. } else {
    10. System.err.println("Couldn't unload world");
    11. }
    12. if (delete(new File("worldname"))){
    13. System.out.println("Deleted world!");
    14. WorldCreator wc = new WorldCreator("worldname");
    15. wc.environment(Environment.NORMAL);
    16. wc.createWorld();
    17. Bukkit.reload();
    18. } else {
    19. System.err.println("Couldn't delete world");
    20. }
    21. }
    22.  
    23. private boolean delete(File file) {
    24. if (file.isDirectory())
    25. for (File subfile : file.listFiles())
    26. if (!delete(subfile))
    27. return false;
    28. if (!file.delete())
    29. return false;
    30. return true;
    31. }


    I'm not sure if you really need Bukkit.reload(), i do Bukkit.reload() just to make sure that the world is loaded again.
     
  11. Offline

    rbrick

    tommycake50 hehe i know. i have it setup on my linux system to run .bat and .sh files...i just went with the one most people are familiar with.
     
  12. Offline

    Ultimate_n00b

    If I can find the code we use on Minecade, I'll toss it at ya.
     
  13. Offline

    tommycake50

    Aaaand bash is 10x as powerful.
    (Why cant windows users just get MinGW).
     
Thread Status:
Not open for further replies.

Share This Page