Rollback an Entire World

Discussion in 'Plugin Development' started by PogoStick29, Nov 18, 2012.

?

Should the developer release the code as a plugin?

  1. Yes!

    4 vote(s)
    80.0%
  2. No.

    1 vote(s)
    20.0%
Thread Status:
Not open for further replies.
  1. Offline

    PogoStick29

    I am working on a plugin that will allow players to join worlds, play on them, then leave. When the player leaves the world, I need the world to be rolled back 100% (blocks, chest contents, sign data, entities, etc.) I must be 100% automated, This could be done one of two ways:

    1. Log all changes to a world then undo them when the player leaves.
    2. Replace the world with a backup when the player leaves.

    If anyone can help, I will give them a staff position on my server (this plugin will run the server).
     
  2. Offline

    Rprrr

    I need this too for a plugin I'm making.. I searched a bit but couldn't find anything. What I did find, was that it seemed to be very hard to replace a world (it's not possible to fully unload a world, and this may lead to memory leak, at least that's what someone said).

    But if anyone knows how to do this I would be interested.
     
    PogoStick29 likes this.
  3. Offline

    LaxWasHere

    Multiverse has that option.
     
  4. Offline

    PogoStick29

    I know MultiVerse has the ability to unload/load worlds, but how could I make it delete the old one, copy the backup, and load it?
     
  5. Offline

    fireblast709

    unload the world, and make sure this returns true. If it is unloaded, then you can delete the world, replace it with a backup, and load it again. Note that this does not work with the default world, nether and end
     
  6. Offline

    PogoStick29

    How can I unload the world, delete the world folder, copy the backup, and replace it, and load it.
    Also, This does not need to be done for the default world.
     
  7. Offline

    LaxWasHere

  8. Offline

    PogoStick29

    LaxWasHere You are my savior!!!!!
    I will discuss a rank with you privately ;)
     
  9. Offline

    fireblast709

    Code:java
    1.  
    2. if(Bukkit.getServer().unloadWorld("worldname",false))
    3. {
    4. // Delete, not sure if correct
    5. new File("location of world folder, like serverfolder/worldname").delete();
    6. // Copy
    7. // [url]http://www.mkyong.com/java/how-to-copy-directory-in-java/[/url]
    8. // Loading the world
    9. new WorldCreator("worldname").createWorld();
    10.  
    11. }
    12. else
    13. {
    14. // Could not unload world
    15. }

    kind of a design
     
    PogoStick29 likes this.
  10. Offline

    Rprrr

    I'm now trying this:
    Code:java
    1. public static void copyFolder(File src, File dest)
    2. throws IOException{
    3.  
    4. if(src.isDirectory()){
    5.  
    6. //if directory not exists, create it
    7. if(!dest.exists()){
    8. dest.mkdir();
    9. System.out.println("Directory copied from "
    10. + src + " to " + dest);
    11. }
    12.  
    13. //list all the directory contents
    14. String files[] = src.list();
    15.  
    16. for (String file : files) {
    17. //construct the src and dest file structure
    18. File srcFile = new File(src, file);
    19. File destFile = new File(dest, file);
    20. //recursive copy
    21. copyFolder(srcFile,destFile);
    22. }
    23.  
    24. }else{
    25. //if file, then copy it
    26. //Use bytes stream to support all file types
    27. OutputStream out = new FileOutputStream(dest);
    28.  
    29. byte[] buffer = new byte[1024];
    30.  
    31. int length;
    32. //copy the file content in bytes
    33. while ((length = in.read(buffer)) > 0){
    34. out.write(buffer, 0, length);
    35. }
    36.  
    37. in.close();
    38. out.close();
    39. System.out.println("File copied from " + src + " to " + dest);
    40. }
    41. }
    42.  
    43. public static void replaceWorld(String worldname, String backupworldname){
    44.  
    45. World world = Bukkit.getServer().getWorld(worldname);
    46. if(Bukkit.getServer().unloadWorld(world,false)) {
    47.  
    48.  
    49. // Delete
    50. File tobereplaced = new File(plugin.getServer().getWorldContainer() + File.separator + worldname);
    51. tobereplaced.delete();
    52.  
    53. File backup = new File(plugin.getServer().getWorldContainer() + File.separator + "worldbackups" + File.separator + backupworldname);
    54.  
    55. try {
    56. copyFolder(backup, plugin.getServer().getWorldContainer());
    57. } catch (IOException e) {
    58. // TODO Auto-generated catch block
    59. e.printStackTrace();
    60. }
    61.  
    62. // Load new world
    63. new WorldCreator("crownconquest").createWorld();
    64.  
    65. }
    66.  
    67. else
    68. {
    69. // Could not unload world
    70. }
    71.  
    72.  
    73. }
    74.  


    That puts all the files in the raw server folder.. so use this:
    Code:java
    1. public static void copyFolder(File src, File dest)
    2. throws IOException{
    3.  
    4. if(src.isDirectory()){
    5.  
    6. //if directory not exists, create it
    7. if(!dest.exists()){
    8. dest.mkdir();
    9. System.out.println("Directory copied from "
    10. + src + " to " + dest);
    11. }
    12.  
    13. //list all the directory contents
    14. String files[] = src.list();
    15.  
    16. for (String file : files) {
    17. //construct the src and dest file structure
    18. File srcFile = new File(src, file);
    19. File destFile = new File(dest, file);
    20. //recursive copy
    21. copyFolder(srcFile,destFile);
    22. }
    23.  
    24. }else{
    25. //if file, then copy it
    26. //Use bytes stream to support all file types
    27.  
    28. byte[] buffer = new byte[1024];
    29.  
    30. int length;
    31. //copy the file content in bytes
    32. while ((length = in.read(buffer)) > 0){
    33. out.write(buffer, 0, length);
    34. }
    35.  
    36. in.close();
    37. out.close();
    38. System.out.println("File copied from " + src + " to " + dest);
    39. }
    40. }
    41.  
    42. public static void replaceWorld(String worldname, String backupworldname){
    43.  
    44. World world = Bukkit.getServer().getWorld(worldname);
    45. if(Bukkit.getServer().unloadWorld(world,false)) {
    46.  
    47.  
    48. // Delete, not sure if correct
    49. File tobereplaced = new File(plugin.getServer().getWorldContainer() + File.separator + worldname);
    50. tobereplaced.delete();
    51.  
    52. File backup = new File(plugin.getServer().getWorldContainer() + File.separator + "worldbackups" + File.separator + backupworldname);
    53.  
    54. File folder = new File (plugin.getServer().getWorldContainer() + File.separator + worldname);
    55. try {
    56. copyFolder(backup, folder);
    57. } catch (IOException e) {
    58. // TODO Auto-generated catch block
    59. e.printStackTrace();
    60. }
    61.  
    62. // Load new world
    63. new WorldCreator("crownconquest").createWorld();
    64.  
    65. }
    66.  
    67. else
    68. {
    69. Bukkit.getServer().getLogger().info("Could not unload " + worldname + " ! Failed to replace.");
    70. }
    71.  
    72.  
    73. }
    74.  

    Kinda worked for me, although it did screw up some chunks (I think because I have too little RAM).

    (@404Ninja : Okay, lol. :p I censored my own post.)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  11. Offline

    404Ninja

    Rprrr Please do not swear on the forums. Judging from the last when will 1.x.x be released threads, we have a large amount of 8 year olds on the forums.
     
Thread Status:
Not open for further replies.

Share This Page