Loading, deleting worlds and spawnlocation.

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

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

    Dreeass

    So I'm developing a plugin that copies a folder and uses it as a world on command. So far I have done the part where it can copy it and use it to teleport to. But deleting it doesn't seem to be working and I don't know what's wrong. I probably forgot to do something or didn't check right.

    Here's the code of deleting and copying the world:

    Code:java
    1. package me.Dreeass.Play;
    2.  
    3. import java.io.File;
    4. import java.io.FileInputStream;
    5. import java.io.FileOutputStream;
    6. import java.io.IOException;
    7. import java.io.InputStream;
    8. import java.io.OutputStream;
    9. import java.util.List;
    10.  
    11. import org.bukkit.Bukkit;
    12. import org.bukkit.Location;
    13. import org.bukkit.World;
    14. import org.bukkit.entity.Player;
    15.  
    16. public class CopyWorld {
    17.  
    18. public static Main plugin;
    19.  
    20. public CopyWorld(Main instance) {
    21. plugin = instance;
    22. }
    23.  
    24. public void main(Player player)
    25. {
    26. String name = player.getName();
    27. //File srcFolder = new File( new File(plugin.getConfig().getString("Config.CopyFromWorld")).getAbsolutePath());
    28. //File destFolder = new File( new File(plugin.getConfig().getString("Config.CopyToWorld")).getAbsolutePath());
    29.  
    30. File srcFolder = new File( plugin.getConfig().getString("Config.CopyFromWorld") + "/");
    31. File destFolder = new File( plugin.getConfig().getString("Config.CopyToWorld") + "/");
    32.  
    33. //make sure source exists
    34. if(!srcFolder.exists()){
    35. plugin.MessageConvert(player, "Messages.NewWorld.CopyFromWorld_DoesNotExist");
    36. plugin.ServerMessageConvert(name, "Messages.NewWorld.CopyFromWorld_DoesNotExist");
    37.  
    38. }
    39. if(destFolder.exists()) {
    40. deleteFolder(destFolder, player);
    41. if(deleteFolder(destFolder, player)) {
    42. plugin.MessageConvert(player, "Messages.NewWorld.Deleted");
    43. try{
    44. copyFolder(srcFolder,destFolder,player);
    45. }catch(IOException e){
    46. e.printStackTrace();
    47. plugin.MessageConvert(player, "Messages.NewWorld.Error");
    48. plugin.ServerMessageConvert(name, "Messages.NewWorld.Error");
    49. }
    50. }
    51. }
    52. else{
    53.  
    54. try{
    55. copyFolder(srcFolder,destFolder,player);
    56. }catch(IOException e){
    57. e.printStackTrace();
    58. plugin.MessageConvert(player, "Messages.NewWorld.Error");
    59. plugin.ServerMessageConvert(name, "Messages.NewWorld.Error");
    60. }
    61. }
    62. }
    63. public boolean deleteFolder(File dest, Player player) {
    64. String worldName = plugin.getConfig().getString("Config.CopyFromWorld");
    65. if(Bukkit.getWorld(worldName) != null) {
    66. World world = Bukkit.getWorld(worldName);
    67. List<Player> players = world.getPlayers();
    68. for(Player p : players) {
    69. World dWorld = plugin.getServer().getWorlds().get(0);
    70. Location dLoc = dWorld.getSpawnLocation();
    71. p.teleport(dLoc);
    72. plugin.MessageConvert(player, "Messages.NewWorld.Teleported");
    73. }
    74. Bukkit.unloadWorld(worldName, true);
    75. }
    76. if (dest.isDirectory()) {
    77. String[] children = dest.list();
    78. for (int i=0; i<children.length; i++) {
    79. boolean success = deleteFolder(new File(dest, children[i]), player);
    80. if (!success) {
    81. return false;
    82. }
    83. }
    84. }
    85.  
    86. // The directory is now empty so delete it
    87. return dest.delete();
    88. }
    89.  
    90. public void copyFolder(File src, File dest, Player player)
    91. throws IOException{
    92.  
    93. if(src.isDirectory()){
    94.  
    95. // If directory not exists, create it
    96. if(!dest.exists()){
    97. dest.mkdir();
    98. plugin.log.info("Directory copied from "
    99. + src + " to " + dest);
    100. }
    101.  
    102. // List all the directory contents
    103. String files[] = src.list();
    104.  
    105. for (String file : files) {
    106. // Construct the src and dest file structure
    107. File srcFile = new File(src, file);
    108. File destFile = new File(dest, file);
    109. // Recursive copy
    110. copyFolder(srcFile,destFile, player);
    111. }
    112.  
    113. }
    114. else{
    115. // If file, then copy it
    116. // Use bytes stream to support all file types
    117. OutputStream out = new FileOutputStream(dest);
    118.  
    119. byte[] buffer = new byte[1024];
    120.  
    121. int length;
    122. // Copy the file content in bytes
    123. while ((length = in.read(buffer)) > 0){
    124. out.write(buffer, 0, length);
    125. }
    126.  
    127. in.close();
    128. out.close();
    129. plugin.log.info("File copied from " + src + " to " + dest);
    130. }
    131. plugin.loading = true;
    132. }
    133. }
    134. [/i]
     
  2. Offline

    Craftiii4

    I think you have to completely unload a world before it can be deleted.
     
  3. Offline

    Dreeass

    Well I added a check if the world is null and if not it will teleport the players out and unload the world. Just don't know why it doesn't work.
     
  4. Offline

    Njol

    You can't simply check for null as it would only be null if you set the variable to null, which you likely don't. You should simply always teleport the players out of the world before unloading, not only if "world is not null".

    BTW: some other people had problems with worlds blocking files even after they were completely unloaded, thus it might not even work if you do everything correctly.
     
  5. Offline

    Dreeass

    I don't really get you because if I have to teleport them out when the world is null then I'll get an error.
     
  6. Offline

    Njol

    My point is that 'world' will never be null.
     
  7. Offline

    Dreeass

    Then how do I check if the world exists?
     
  8. Offline

    Njol

    So you're not even sure whether the world you're trying to unload exists?
    And if you're talking about this null check:

    Code:
    String worldName = plugin.getConfig().getString("Config.CopyFromWorld");
    if(Bukkit.getWorld(worldName) != null) {
    if the world doesn't exist you can't copy anything from it, thus you should send an error message and return. Maybe you should change "Config.CopyFromWorld" to "Config.CopyToWorld"?

    I didn't even see this part of the code before because the indentation is messed up completely. In eclipse you can simply select the code an press ctrl+I and the indentation will be auto-corrected.
     
  9. Offline

    Dreeass

    Lol what a mistake that I did not see! And I have perfect indentation but apparently it got removed in the syntax, maybe because I only use tabs? And I'll let you know if the code works then, thanks!
     
Thread Status:
Not open for further replies.

Share This Page