Copying worlds from a plugin folder.

Discussion in 'Plugin Development' started by Theminebench, Oct 29, 2014.

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

    Theminebench

    I'm trying to copy a world from /plugins/<Plugin name>/worlds/<World>
    to the server folder.

    onEnable I have:
    Code:java
    1. this.getDataFolder().mkdir();
    2.  
    3. File worlds = new File(this.getDataFolder(), "worlds");
    4. if (worlds.exists()) {
    5. logger.info("file " + worlds.getAbsolutePath() + " already exists!");
    6. } else {
    7. worlds.mkdir();
    8. logger.info("Created " + worlds.getAbsolutePath() + "!");
    9. }
    10.  


    Then before I start the server I put the world in the worlds folder.
    Then run this method:

    Code:java
    1. public void copyWorldIntoServer(String worldName) {
    2. File worldFile = new File(new File(this.getDataFolder(), "worlds"), worldName);
    3.  
    4. File destFile = new File(this.getServer().getWorldContainer(), worldName);
    5.  
    6. try {
    7. copyFile(worldFile, destFile);
    8. } catch (IOException e) {
    9. // TODO Auto-generated catch block
    10. e.printStackTrace();
    11. }
    12. Bukkit.getServer().createWorld(new WorldCreator(worldName));
    13.  
    14. }
    15.  
    16.  
    17. public void copyFile(File sourceFile, File destFile) throws IOException {
    18. if (!destFile.exists()) {
    19. destFile.createNewFile();
    20. }
    21.  
    22. FileChannel source = null;
    23. FileChannel destination = null;
    24.  
    25. try {
    26. source = new FileInputStream(sourceFile).getChannel();
    27. destination = new FileOutputStream(destFile).getChannel();
    28. destination.transferFrom(source, 0, source.size());
    29. } finally {
    30. if (source != null) {
    31. source.close();
    32. }
    33. if (destination != null) {
    34. destination.close();
    35. }
    36. }
    37. }


    And I get this in the console:




    Code:
    4:42:18 PM [INFO] TheMineBench issued server command: /copyspleef
    4:42:18 PM [WARNING] java.io.FileNotFoundException: plugins\TestPlugin\worlds\SpleefMap (Access is denied)
    4:42:18 PM [WARNING]    at java.io.FileInputStream.open(Native Method)
    4:42:18 PM [WARNING]    at java.io.FileInputStream.<init>(Unknown Source)
    4:42:18 PM [WARNING]    at me.theminebench.testplugin.TestPlugin.copyFile(TestPlugin.java:184)
    4:42:18 PM [WARNING]    at me.theminebench.testplugin.TestPlugin.copyWorldIntoServer(TestPlugin.java:164)
    4:42:18 PM [WARNING]    at me.theminebench.testplugin.TestPlugin.onCommand(TestPlugin.java:63)
    4:42:18 PM [WARNING]    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    4:42:18 PM [WARNING]    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181)
    4:42:18 PM [WARNING]    at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:767)
    4:42:18 PM [WARNING]    at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerConnection.java:1043)
    4:42:18 PM [WARNING]    at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:880)
    4:42:18 PM [WARNING]    at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28)
    4:42:18 PM [WARNING]    at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat.java:65)
    4:42:18 PM [WARNING]    at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:186)
    4:42:18 PM [WARNING]    at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81)
    4:42:18 PM [WARNING]    at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:734)
    4:42:18 PM [WARNING]    at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289)
    4:42:18 PM [WARNING]    at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584)
    4:42:18 PM [WARNING]    at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490)
    4:42:18 PM [WARNING]    at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628)
    4:42:18 PM [SEVERE] null
    4:42:18 PM org.bukkit.command.CommandException: Unhandled exception executing command 'copyspleef' in plugin TestPlugin v0.1
    4:42:18 PM    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
    4:42:18 PM    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) ~[spigot.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
    4:42:18 PM    at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:767) ~[spigot.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
    4:42:18 PM    at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerConnection.java:1043) [spigot.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
    4:42:18 PM    at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:880) [spigot.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
    4:42:18 PM    at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) [spigot.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
    4:42:18 PM    at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat.java:65) [spigot.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
    4:42:18 PM    at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:186) [spigot.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
    4:42:18 PM    at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [spigot.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
    4:42:18 PM    at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:734) [spigot.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
    4:42:18 PM    at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [spigot.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
    4:42:18 PM    at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [spigot.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
    4:42:18 PM    at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [spigot.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
    4:42:18 PM    at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
    4:42:18 PM Caused by: java.lang.IllegalArgumentException: File exists with the name 'SpleefMap' and isn't a folder
    4:42:18 PM    at org.bukkit.craftbukkit.v1_7_R4.CraftServer.createWorld(CraftServer.java:965) ~[spigot.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
    4:42:18 PM    at me.theminebench.testplugin.TestPlugin.copyWorldIntoServer(TestPlugin.java:170) ~[?:?]
    4:42:18 PM    at me.theminebench.testplugin.TestPlugin.onCommand(TestPlugin.java:63) ~[?:?]
    4:42:18 PM    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-1.7.9-R0.2-204-g534549b]
    4:42:18 PM    ... 13 more


    I think it might be important to note that a file with the same name as the world is added to the server folder, but instead of being a "File folder" its just a "File".
     
  2. Offline

    fireblast709

    Theminebench you create a new file with the world's name. Instead of createNewFile(), use mkdir().
     
  3. Offline

    Theminebench

    Now it just generates a new world, it does not copy the one I had

    Code:
    5:45:24 PM [INFO] TheMineBench issued server command: /copyspleef
    5:45:24 PM [WARNING] java.io.FileNotFoundException: plugins\TestPlugin\worlds\SpleefMap (Access is denied)
    5:45:24 PM [WARNING]    at java.io.FileInputStream.open(Native Method)
    5:45:24 PM [WARNING]    at java.io.FileInputStream.<init>(Unknown Source)
    5:45:24 PM [WARNING]    at me.theminebench.testplugin.TestPlugin.copyFile(TestPlugin.java:184)
    5:45:24 PM [WARNING]    at me.theminebench.testplugin.TestPlugin.copyWorldIntoServer(TestPlugin.java:164)
    5:45:24 PM [WARNING]    at me.theminebench.testplugin.TestPlugin.onCommand(TestPlugin.java:63)
    5:45:24 PM [WARNING]    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    5:45:24 PM [WARNING]    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180)
    5:45:24 PM [WARNING]    at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:701)
    5:45:24 PM [WARNING]    at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerConnection.java:956)
    5:45:24 PM [WARNING]    at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:817)
    5:45:24 PM [WARNING]    at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28)
    5:45:24 PM [WARNING]    at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:47)
    5:45:24 PM [WARNING]    at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157)
    5:45:24 PM [WARNING]    at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134)
    5:45:24 PM [WARNING]    at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667)
    5:45:24 PM [WARNING]    at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260)
    5:45:24 PM [WARNING]    at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558)
    5:45:24 PM [WARNING]    at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469)
    5:45:24 PM [WARNING]    at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628)
    5:45:24 PM [INFO] Preparing start region for level 3 (Seed: -2290555886087778549)
    5:45:25 PM [INFO] Preparing spawn area for SpleefMap, 12%
    5:45:26 PM [INFO] Preparing spawn area for SpleefMap, 36%
    5:45:27 PM [INFO] Preparing spawn area for SpleefMap, 60%
    5:45:28 PM [INFO] Preparing spawn area for SpleefMap, 89%
     
  4. Offline

    fireblast709

    Theminebench well yea it throws an error before that :p (thus doesn't copy the files). It seems that you don't have permission to read the directory.
     
  5. Offline

    Funergy

Thread Status:
Not open for further replies.

Share This Page