Copying World

Discussion in 'Plugin Development' started by Dreeass, May 17, 2012.

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

    Dreeass

    So I want to have a world and later multiple worlds that will get deleted and then copied again from the default worldfile. I didn't write the deleting yet because if I got this fixed, then I probably won't be having problems with deleting it.

    Code:java
    1. import java.io.File;
    2. import java.io.FileInputStream;
    3. import java.io.FileOutputStream;
    4. import java.io.IOException;
    5. import java.io.InputStream;
    6. import java.i:confused:utputStream;
    7. import java.util.logging.Logger;
    8.  
    9. import org.bukkit.entity.Player;
    10.  
    11. public class CopyWorld {
    12.  
    13. public static Main plugin;
    14.  
    15. public CopyWorld(Main instance) {
    16. plugin = instance;
    17. }
    18.  
    19. private static Logger log = plugin.log;
    20.  
    21. public static void main(Player player)
    22. {
    23. String name = player.getName();
    24. File srcFolder = new File(plugin.getConfig().getString("Config.CopyFromWorld"));
    25. File destFolder = new File(plugin.getConfig().getString("Config.CopyToWorld"));
    26.  
    27. //make sure source exists
    28. if(!srcFolder.exists()){
    29.  
    30. plugin.MessageConvert(player, "Messages.Start/Stop.CopyFromWorld_DoesNotExist");
    31. plugin.ServerMessageConvert(name, "Messages.Start/Stop.CopyFromWorld_DoesNotExist");
    32.  
    33. }else{
    34.  
    35. try{
    36. copyFolder(srcFolder,destFolder);
    37. }catch(IOException e){
    38. e.printStackTrace();
    39. plugin.MessageConvert(player, "Messages.Start/Stop.Error");
    40. plugin.ServerMessageConvert(name, "Messages.Start/Stop.Error");
    41. }
    42. }
    43.  
    44. log.info("Done");
    45. }
    46.  
    47. public static void copyFolder(File src, File dest)
    48. throws IOException{
    49.  
    50. if(src.isDirectory()){
    51.  
    52. // If directory not exists, create it
    53. if(!dest.exists()){
    54. dest.mkdir();
    55. log.info("Directory copied from "
    56. + src + " to " + dest);
    57. }
    58.  
    59. // List all the directory contents
    60. String files[] = src.list();
    61.  
    62. for (String file : files) {
    63. // Construct the src and dest file structure
    64. File srcFile = new File(src, file);
    65. File destFile = new File(dest, file);
    66. // Recursive copy
    67. copyFolder(srcFile,destFile);
    68. }
    69.  
    70. }else{
    71. // If file, then copy it
    72. // Use bytes stream to support all file types
    73.  
    74. byte[] buffer = new byte[1024];
    75.  
    76. int length;
    77. // Copy the file content in bytes
    78. while ((length = in.read(buffer)) > 0){
    79. out.write(buffer, 0, length);
    80. }
    81.  
    82. in.close();
    83. out.close();
    84. log.info("File copied from " + src + " to " + dest);
    85. }
    86. }
    87. }
    88.  

    Messageconvert gets the path from config and does some stuff with it like colors and sends it to the player. Servermessageconvert does the same but sends it to the console.
    But what's wrong here? And these are the worlds:
    Code:java
    1. config.addDefault("Config.CopyFromWorld", "/world_gamedefault");
    2. config.addDefault("Config.CopyToWorld", "/world_game");
     
  2. if(!dest.exists()){
    dest.mkdir();
    and then you're calling mkdir for all files and folders, so it may create dirs, but never copy files (as either dest doesn't exist or it is a dir from a previous run).
     
  3. Offline

    Dreeass

    // If file, then copy it
    // Use bytes stream to support all file types

    Nvm, the problem is the location of the worldfolders, what's wrong with that?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  4. Because the VM doesn't know where the server folder is, you need to specify that as well.
     
  5. Offline

    Dreeass

    Do I have to start from the C drive or is there a way that I can use the location where the Craftbukkit jar is?
     
  6. Code:
    String path = new File(".").getAbsolutePath();
    That will give you the current path which should be the path to the folder where craftbukkit/server is in.
     
    Dreeass likes this.
  7. Offline

    Dreeass

    Thanks man!
     
Thread Status:
Not open for further replies.

Share This Page