On server restart, create new world.

Discussion in 'Plugin Development' started by onVoid, Nov 15, 2014.

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

    onVoid

    I am trying to develop a gamemode where the map needs to be reset every time the server is restarted. I have looked at various other threads on ways of achieving this and none of them worked. Any updated solutions to this? Thanks.
     
  2. Offline

    teej107

    onVoid When the plugin gets loaded, generate a world with your desired seed.
     
  3. Offline

    onVoid

    I am looking for a random world, not a seeded one. Also, I have tried doing that but I can't figure out how because most things dont work.
     
  4. Offline

    teej107

    onVoid Random worlds have seeds, which you can create also. If your code doesn't work, post it.
     
  5. Offline

    xTrollxDudex

    Delete the world directory when the server is stopped and when the server starts, a new world will generate in place of the old one for you.
     
    Konato_K likes this.
  6. Offline

    Rocoty

    This. And if you need it automated, use a shell script!
     
    xTrollxDudex likes this.
  7. Offline

    xTrollxDudex

    Or batch
     
    ZanderMan9 and Rocoty like this.
  8. Offline

    onVoid

    xTrollxDudex

    Code:java
    1. try
    2. {
    3. World mainWorld = this.getServer().getWorld("worldeh");
    4. File worldFolder = mainWorld.getWorldFolder();
    5. this.getServer().unloadWorld(mainWorld, true);
    6. worldFolder.delete();
    7. } catch (Exception ex) {
    8. this.getLogger().log(Level.SEVERE, "Main world could not be deleted!");
    9. }

    That is what I have in my ondisable but it doesn't work.
    Rocoty
    How do you use a shell script? I am looking to make it automated.
     
  9. Offline

    Creeoer

    onVoid
    It wouldn't work because after the first time the default level name would be world, and if world doesn't exist your code can't run. Did you change the level name to "worldeh."
     
  10. Offline

    onVoid

    Creeoer
    Yes. Should I rename it back to world?
     
  11. Offline

    Creeoer

    It probally wouldn't make a difference, if you went into the actual server config and changed the level name, not too sure.
     
  12. Offline

    teej107

    The delete method for File only deletes the directory when there are no files inside it. You'll need a recursive method to delete all the files in the folders and then delete the world file.
     
    Konato_K likes this.
  13. Offline

    onVoid

    teej107
    How would I loop through all the files?
     
  14. Offline

    mythbusterma

    onVoid

     
  15. Offline

    onVoid

  16. Offline

    ZanderMan9

    Batch, as mentioned above:
    Code:
    @ECHO OFF
    java (enter your normal parameters here) -jar craftbukkit.jar
    echo Press any key to continue to delete world and exit.
    PAUSE 1>NUL
    rd /S/Q world
     
  17. Offline

    onVoid

    ZanderMan9
    I run off of a host so I don't know if that is possible. Also, how would you automate that?
     
  18. Offline

    xTigerRebornx

  19. Offline

    ZanderMan9

    onVoid If it's hosted most likely it is Linux, not Windows, therefore you'd need Bash, not Batch.
     
  20. Offline

    teej107

    Konato_K likes this.
  21. Offline

    onVoid

    Okay so this is my current onDisable:
    Code:java
    1. @Override
    2. public void onDisable() {
    3. this.saveDefaultConfig();
    4. for (Player p : Bukkit.getServer().getWorld("worldeh").getPlayers()){
    5. p.kickPlayer(ChatColor.RED + "Restarting!");
    6. }
    7. World mainWorld = this.getServer().getWorld("worldeh");
    8. Bukkit.getServer().getWorlds().remove(mainWorld);
    9. Bukkit.getServer().unloadWorld(mainWorld, false);
    10. File worldFolder = mainWorld.getWorldFolder();
    11. //
    12. deleteAllInsideFolder(worldFolder);
    13. //
    14. if (new File(worldFolder, "/region").exists()){
    15. File regionFolder = new File(worldFolder, "/region");
    16. if (regionFolder.listFiles() != null){
    17. deleteAllInsideFolder(regionFolder);
    18. }
    19. clear(regionFolder);
    20. }
    21. if (new File(worldFolder, "/playerdata").exists()){
    22. File regionFolder = new File(worldFolder, "/playerdata");
    23. clear(regionFolder);
    24. }
    25. if (new File(worldFolder, "/data").exists()){
    26. File regionFolder = new File(worldFolder, "/data");
    27. clear(regionFolder);
    28. }
    29. if (worldFolder.exists()){
    30. clear(worldFolder);
    31. }
    32. }

    It deletes everything inside of the world folder except for the regions folder. If I put a .txt file inside the regions folder, though, it deletes that. I think the regions folder might not be deleting because the world is still running but I unloaded it... Anyone know why and how to fix?
     
  22. Offline

    teej107

    onVoid um..... idk how recursive your "deleteAllInsideFolder" method is, but regardless of that, your code after that is not recursive. If you made your "deleteAllInsideFolder" right, you shouldn't be needing those checks afterward.
     
  23. Offline

    guitargun

    onVoid that code you posted can be wayyyy smaller.

    nice stepplan:
    use a enhanced for loop to loop through all your files inside the worlddirectory
    1. check if the file is a director
      1. loop through all the files inside that second directory. with a repeating if as mention above
    2. delete the file
    made is once a time ago but I deleted it since I scrapped the project.
     
  24. Offline

    onVoid

    My deleteAllInsideFolder code:
    Code:java
    1. public void deleteAllInsideFolder(File wF){
    2. for (File p : wF.listFiles()){
    3. if (!p.isDirectory()){
    4. clear(p);
    5. } else {
    6. deleteAllInsideFolder(p);
    7. continue;
    8. }
    9. }
    10. }
    11.  

    My clear code:
    Code:java
    1. public static void clear(File file)
    2. {
    3. if (!file.exists())
    4. return;
    5. if (file.isFile()) {
    6. file.delete();
    7. } else {
    8. for (File f : file.listFiles())
    9. clear(f);
    10. file.delete();
    11. }
    12. }

    teej107
    I am going with a for loop rather than a recursive method

    guitargun
    That's basically what I did (Plus I'm not looking for amount of space I'm looking for function.
     
  25. Offline

    onVoid

  26. Offline

    teej107

    onVoid Here is the recursive delete files method that I use. Since I don't like spoonfeeding, I'm going to massively comment it.
    PHP:
    public static boolean delete(File src)
    {
        
    //If source is directory, loop through the directory and call this same method.
        // Otherwise, just delete the file
        
    if (src.isDirectory())
        {
            
    //Looping through the directory.
            
    for (File f src.listFiles())
            {
                
    //Calling this same method recursively, repeating steps above.
                
    delete(f);
            }
        }
        
    //This is for you should remove this part of the code.
        
    System.exit(0);
        
    //Delete the file in the end. If this is a directory, since this is called last,
        //all the files inside should have been deleted already, executing a successful delete.
        
    return src.delete();
    }
     
  27. Offline

    onVoid

    teej107
    That method did the same as mine. Gave me an error and didn't delete the region file.
     
  28. Offline

    Skionz

    onVoid Then post the error?
     
  29. Offline

    onVoid

    Skionz
    Code:
    [19:17:06 WARN]:        at net.minecraft.server.v1_7_R2.ChunkRegionLoader.a(Chun
    kRegionLoader.java:132)
    [19:17:06 WARN]:        at net.minecraft.server.v1_7_R2.ChunkProviderServer.save
    Chunk(ChunkProviderServer.java:228)
    [19:17:06 WARN]:        at net.minecraft.server.v1_7_R2.ChunkProviderServer.unlo
    adChunks(ChunkProviderServer.java:321)
    [19:17:06 WARN]:        at net.minecraft.server.v1_7_R2.WorldServer.doTick(World
    Server.java:192)
    [19:17:06 WARN]:        at net.minecraft.server.v1_7_R2.MinecraftServer.v(Minecr
    aftServer.java:631)
    [19:17:06 WARN]:        at net.minecraft.server.v1_7_R2.DedicatedServer.v(Dedica
    tedServer.java:250)
    [19:17:06 WARN]:        at net.minecraft.server.v1_7_R2.MinecraftServer.u(Minecr
    aftServer.java:548)
    [19:17:06 WARN]:        at net.minecraft.server.v1_7_R2.MinecraftServer.run(Mine
    craftServer.java:459)
    [19:17:06 WARN]:        at net.minecraft.server.v1_7_R2.ThreadServerApplication.
    run(SourceFile:618)
    [19:17:06 WARN]: net.minecraft.server.v1_7_R2.ExceptionWorldConflict: Failed to
    check session lock, aborting
    [19:17:06 WARN]:        at net.minecraft.server.v1_7_R2.WorldNBTStorage.checkSes
    sion(WorldNBTStorage.java:78)
    [19:17:06 WARN]:        at net.minecraft.server.v1_7_R2.World.G(World.java:2544)
     
  30. Offline

    Skionz

Thread Status:
Not open for further replies.

Share This Page