Create a new world, game ends, delete and create another.

Discussion in 'Plugin Development' started by Kazzababe, Oct 30, 2012.

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

    Kazzababe

    I'm attempting to develop a new plugin. For the plugin I need a way to randomly generate a world where the players will start the match and then once the match is over, I have to delete the map and generate a new one.

    Is there a way to do that? I basically want to restart the server, and when it starts back up, you have the new world. I also want the world to be named the same thing everytime. Help?
     
  2. Offline

    Haribo98

    You may be able to use:
    Code:
    Bukkit.createWorld()
    Then when the game ends:
    Code:
    List<Player> players = Bukkit.getWorld(worldname).getPlayers();
    for (Player worldPlayers : players) {
        worldPlayers.teleport(loc);
    }
    EDIT: To delete the world I've not got a clue :p
     
  3. Offline

    JazzaG

    You can't delete the world folder while it is in use by the CraftBukkit process. Either programmatically disable them or; end the process, start a separate process, delete the world and start CraftBukkit back up. Sound simple? :p
     
  4. Offline

    Hoolean

    I believe you can teleport everyone out of the world (into another), unload it, delete the files, then create a new one!
     
  5. Offline

    Kazzababe

    Do you know how to go about that?
     
  6. Offline

    LucasEmanuel

    Using server.unloadWorld(); doesnt seem to close all connections to the world files, so you cant just delete the files while the server is running. Someone on this forum posted a solution to this once, cant remember what thread or who it was though.
     
  7. Offline

    Kazzababe

    What exactly does unloadWorld() do? If I unload the world and then load it, will that generate a new world?
     
  8. Offline

    LucasEmanuel

    No that will just load the world as it where when you unloaded it. From what i can tell by reading the sourcecode in unloadWorld() it doesnt actually do that much, it calls an UnloadWorldEvent, saves the world, calls an WorldSaveEvent, then removes the world from its lists of worlds.

    Lists:
    CraftServer:
    https://github.com/Bukkit/CraftBukk.../org/bukkit/craftbukkit/CraftServer.java#L147
    MinecraftServer:
    https://github.com/Bukkit/CraftBukk...net/minecraft/server/MinecraftServer.java#L75

    Source: https://github.com/Bukkit/CraftBukk.../org/bukkit/craftbukkit/CraftServer.java#L745
     
  9. Offline

    Kazzababe

    How would I go about this then?
     
  10. Offline

    LucasEmanuel

    Well for my SurvivalGamesMultiverse, i simply log each change to the world then reset those changes at the end of each round.

    But you want to create an entirely new world after each round, you could have a script remove the world files when the server stops then restart the server, but this would mean that you had to restart the server after each round.

    You could create a new world, teleport all players to that world and keep the old one, then restart the server every 5 rounds or so and then remove all the 5 worlds you created. But this would still require server restarts.
     
  11. Offline

    NinjaWAffles

    If he creates a new world, teleports all players there. Could he potentially remove the first world from the list, and remove it's folder, or is it still being used?
     
  12. Offline

    FTWinston

  13. Offline

    ZeusAllMighty11

    try { delete(new File(plugin.getServer().getWorldContainer() + File.separator + name)); } catch ( Exception e ) {

    Code:
    try {
    delete(new File(plugin.getServer().getWorldContainer() + File.separator + name));
    } catch ( Exception e ) {
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  14. Offline

    FTWinston

  15. Offline

    LucasEmanuel

  16. Offline

    FTWinston

    Oh thanks, umm... where?

    The only async task in that class handles file deletion then schedules some sync tasks again to continue the plugin logic... no?
     
  17. Offline

    Kazzababe

    I added e.printStackTrace() to show if there was an error and it doesnt show an error, but it also doesnt work.
     
  18. Offline

    FTWinston

    If it's in that block at all (i.e. it caught an error) ... then there was an error.
     
  19. Offline

    Deleted user

    I actually accomplished to do this on my Hunger Games plugin.. all without restarting the server. I suppose I could leak the code if you ask nicely.
     
  20. Offline

    Kazzababe

    You're asking me to beg? No.
     
    Cirno and JazzaG like this.
  21. Offline

    LucasEmanuel

    This task is a WorldDeleter runnable which calls deleteWorld here, deleteWorld calls clearWorldReference here, clearWorldReference calls regionfiles here in an unsynchronized way and then calls refField here thats also unsynchronized, then it modifies regionfiles unsynchronized here.

    Either you add "synchronized" like this:
    Code:
    @SuppressWarnings("rawtypes")
    private synchronized boolean clearWorldReference(String worldName)
    {
        //Method body
    }
    or you surround every call to those objects i pointed out with a synchronized statement like:
    Code:
    synchronized {
      object.dosomething();
    }
    :)
     
    FTWinston likes this.
  22. Offline

    FTWinston

    LucasEmanuel
    Ah, excellent catch on the regionfiles map. Thanks!
     
  23. Offline

    LucasEmanuel

    np :)
     
  24. Offline

    Kazzababe

    So, I'm glad you guys got your problem solved but I have yet to find a solution for my problem.
     
  25. Offline

    FTWinston

    Well, I was recommending base it on my code... Lucas just found a potential bug in it.

    When the plugin starts, you want to bind the region file cache as done in bindRegionFiles. Then when you want to remove a world:
    1. Teleport/kick all players out of your world
    2. Unload the world (I use my own copy of the unloadWorld function, because I find the built-in one fails sometimes when it shouldn't.
    3. Clear any references to this world's files in the region cache
    4. Actually delete the files & folder
    5. After an appropriate delay, create a new world.
     
  26. Offline

    LucasEmanuel

    This might be the best way of doing it while the server is running, but this depends on that any extra plugin on the server is listening on the WorldUnloadEvent and are clearing their references as well. :)
     
  27. Offline

    FTWinston

    True, if another plugin is trying to do something with a deleted world, it'll likely crash. I wouldn't expect other plugins to have references to files in the world folders though - you know of any that do that?
     
  28. Offline

    dark navi

    Sorry for the bump, but I am also having this issue/ FTWinston, think you could host the source again? It seems to be missing form GitHub.
     
    recon88 likes this.
  29. Offline

    recon88

    Same here.
     
    dark navi likes this.
  30. Offline

    FTWinston

    recon88 likes this.
Thread Status:
Not open for further replies.

Share This Page