Copying and Deleting Worlds

Discussion in 'Plugin Development' started by BungeeTheCookie, Apr 13, 2014.

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

    BungeeTheCookie

    Hello people of the Bukkit community! I am creating a minigame plugin where there is only one world per arena, and I want the world to reset back to its original state from a folder, so this would require copying and deleting worlds. How would I get a world file inside a folder that is inside my plugin folder and load it as the main world, and when it is done, the world is deleting and the players are teleported back to another world (lobby)? Thanks!

    El Bump.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  2. BungeeTheCookie
    This is an example of how I backup worlds. You can re-purpose this for your needs.

    Code:java
    1. private void startBackup(){
    2. DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
    3. Date dateTime = new java.util.Date();
    4. String date = dateFormat.format(dateTime);
    5.  
    6. List<World> worlds = Bukkit.getWorlds();
    7. Object[] theWorlds = worlds.toArray();
    8. String path = new File("").getAbsolutePath();
    9. int fileCount = 0;
    10.  
    11. for(int i=0; i<theWorlds.length; i++){
    12. World w = (World) theWorlds[i];
    13. try {
    14. w.save();
    15. } catch (Exception e) {}
    16.  
    17. String wNam = w.getName();
    18. File srcFolder = new File(path + File.separator + wNam);
    19. File dir = new File(Main.getInstance().getDataFolder().getAbsolutePath() +
    20. File.separator + "World Backups");
    21. File backup = new File(dir + File.separator + date);
    22. File destFolder = new File(backup + File.separator + wNam);
    23.  
    24. destFolder.mkdirs();
    25.  
    26. if(srcFolder.exists()){
    27. try{
    28. Copier.copyFolder(srcFolder,destFolder);
    29. }catch(IOException e){}
    30. }
    31. }
    32. }
    33. }
    34. class Copier{
    35. public static void copyFolder(File src, File dest) throws IOException{
    36. if(src.isDirectory()){
    37. if(!dest.exists())
    38. dest.mkdir();
    39. String[] files = src.list();
    40. for (String file : files) {
    41. File srcFile = new File(src, file);
    42. File destFile = new File(dest, file);
    43. copyFolder(srcFile,destFile);
    44. }
    45. }else{
    46. OutputStream out = new FileOutputStream(dest);
    47. byte[] buffer = new byte[1024];
    48. int length;
    49.  
    50. while ((length = in.read(buffer)) > 0)
    51. out.write(buffer, 0, length);
    52. in.close();
    53. out.close();
    54. }
    55. }
    56. }
    57. [/i]
     
  3. Offline

    BungeeTheCookie

    I want to know how to copy just one folder from a directory, paste it into another, and then delete that world. Would I still use the copyFolder() method, because to me it looks like you are backing up all worlds.

    Bump.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  4. Ah, I have a plugin that does just that. Let me get the code..

    http://pastie.org/private/on3jltu7jsp5yfzfd23kq

    Please note that I used FileUtils (from Apache Commons IO) to copy the folder, but in this code I quickly took a copy directory code, not sure if it will work.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  5. Offline

    1Rogue

    You don't set it as the main world, you dynamically load/unload them and simply delete the instance once unloaded:

    Code:java
    1. /**
    2.   * Loads a world from a provided {@link File} location
    3.   *
    4.   * @since 1.0.0
    5.   * @version 1.0.0
    6.   *
    7.   * @param baseWorld The {@link File} to load
    8.   * @return A new {@link World} instance
    9.   * @throws IOException Failure to copy world folder
    10.   */
    11. private World loadAnonymousWorld(File baseWorld) throws IOException {
    12. if (baseWorld == null || !baseWorld.isDirectory()) {
    13. }
    14. String name = "world_" + System.nanoTime();
    15. File newLocation = new File(plugin.getServer().getWorldContainer(), name);
    16. while (newLocation.exists()) {
    17. name = name + "_";
    18. newLocation = new File(plugin.getServer().getWorldContainer(), name);
    19. }
    20. this.copyDirectory(baseWorld, newLocation);
    21. return WorldCreator.name(name).environment(World.Environment.NORMAL).generator(this.gen).createWorld();
    22. }


    Code:java
    1. /**
    2.   * Unloads a {@link World}
    3.   *
    4.   * @since 1.0.0
    5.   * @version 1.0.0
    6.   *
    7.   * @param baseWorld The {@link World} to unload
    8.   * @param save Whether or not to save chunks
    9.   * @throws IOException
    10.   */
    11. private void unloadWorld(World baseWorld, boolean save) {
    12. if (baseWorld == null) {
    13. }
    14. boolean success = this.plugin.getServer().unloadWorld(baseWorld, save);
    15. if (!success) {
    16. this.plugin.getLogger().log(Level.SEVERE, "Failed to unload world " + baseWorld.getName() + "!", new RuntimeException());
    17. }
    18. }


    From the second method, you can simply delete the world folder that you had loaded/generated.
     
  6. Offline

    BungeeTheCookie

    KingFaris11
    What is getGameWorld() and pluginSettings? And could you please provide the import for FileUtils?

    1Rogue
    Where did you get this.gen from?
     
  7. Offline

    1Rogue


    this.gen is just my custom void generator, you don't need to include that method if you want a default generator.
     
  8. Oh - I still have those? It's from my CTF plugin - getGameWorld() is the world's name and pluginSettings returns the Settings class (custom) that handles all config settings - ignore it.

    FileUtils is from Apache Commons IO: http://mirror.ox.ac.uk/sites/rsync.apache.org//commons/io/source/commons-io-2.4-src.zip
     
  9. Offline

    BungeeTheCookie

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

Share This Page