Advertisement
Guest User

Untitled

a guest
Oct 10th, 2015
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. public class WorldDownload {
  2.  
  3. private static final int BUFFER_SIZE = 4096;
  4.  
  5. public static void loadWorld(final String world) {
  6. if (Bukkit.getWorld (world)!=null){
  7. Bukkit.broadcastMessage ("ร‚ยง4Couldn't load world ''" + world + "'' since it all ready exists");
  8. }
  9. Bukkit.createWorld (new WorldCreator (world));
  10. }
  11.  
  12. public static void unloadWorld(String world) {
  13. World w = Bukkit.getWorld (world);
  14. if(w != null) {
  15. for (Player p : w.getPlayers ()){
  16. p.teleport (Bukkit.getWorld ("world").getSpawnLocation ());
  17. }
  18. Bukkit.unloadWorld (w, true);
  19. }
  20. }
  21.  
  22. public static void resetWorld(String world) {
  23. if(Bukkit.getServer().getWorld(world) != null) {
  24. deleteWorld(world);
  25. createWorld (world);
  26. }
  27. }
  28.  
  29. public static void createWorld(String world) {
  30. loadWorld (world);
  31. }
  32.  
  33. public static void deleteWorld(String world) {
  34. World delete = Bukkit.getWorld (world);
  35. if(delete != null) {
  36. unloadWorld (world);
  37.  
  38. File deleteFolder = delete.getWorldFolder();
  39. deleteWorld (deleteFolder);
  40. }
  41. }
  42.  
  43. public static void downloadFile(final String fileURL, final String fileName, final String saveDir) throws IOException {
  44. final URL url = new URL(fileURL);
  45. Bukkit.getScheduler ().runTaskAsynchronously (Core.getInstance (), new Runnable () {
  46. @Override
  47. public void run () {
  48. HttpURLConnection httpConn = null;
  49. try {
  50. httpConn = (HttpURLConnection) url.openConnection ();
  51. int responseCode = httpConn.getResponseCode ();
  52. if (responseCode == 200) {
  53. InputStream inputStream = httpConn.getInputStream ();
  54. String saveFilePath = saveDir + File.separator + fileName;
  55. FileOutputStream outputStream = new FileOutputStream (saveFilePath);
  56. boolean bytesRead = true;
  57. byte[] buffer = new byte[ 4096 ];
  58.  
  59. int bytesRead1;
  60. while ((bytesRead1 = inputStream.read (buffer)) != - 1) {
  61. outputStream.write (buffer, 0, bytesRead1);
  62. }
  63.  
  64. outputStream.close ();
  65. inputStream.close ();
  66. } else {
  67. System.out.println ("Failed to download map, returned ERROR: " + responseCode);
  68. }
  69. } catch (IOException e) {
  70. e.printStackTrace ();
  71. }
  72. httpConn.disconnect();
  73. }
  74. });
  75.  
  76. }
  77.  
  78. public static void downloadMap(String link, String zip_name, String world_name) {
  79.  
  80. if (Bukkit.getWorld (world_name) != null){
  81. deleteWorld (world_name);
  82. }
  83.  
  84. String file_name = zip_name + ".zip";
  85. String file_path = getBaseDirectory().replace(".", "") + file_name;
  86. String world = world_name.replace(".", "");
  87.  
  88. try {
  89. downloadFile(link, file_name, getBaseDirectory());
  90. unzip(file_path, getBaseDirectory().replace(".", "") + world.replace(".", ""));
  91. } catch (IOException var10) {
  92. var10.printStackTrace();
  93. }
  94.  
  95. loadWorld (world);
  96. (new File(file_path)).delete ();
  97. System.out.println ("DOWNLOADED map `" + world + "`.");
  98. Iterator i$ = Bukkit.getWorld(world).getEntities().iterator();
  99.  
  100. while(i$.hasNext()) {
  101. Entity entity = (Entity)i$.next();
  102. entity.remove();
  103. }
  104.  
  105. }
  106.  
  107. public static String getBaseDirectory() {
  108. return Bukkit.getServer().getWorldContainer().getAbsolutePath();
  109. }
  110.  
  111. public static void unzip(String zipFilePath, String destDirectory) throws IOException {
  112. File destDir = new File(destDirectory);
  113. if(!destDir.exists()) {
  114. destDir.mkdir();
  115. }
  116.  
  117. ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
  118.  
  119. for(ZipEntry entry = zipIn.getNextEntry(); entry != null; entry = zipIn.getNextEntry()) {
  120. String filePath = destDirectory + File.separator + entry.getName();
  121. if(!entry.isDirectory()) {
  122. extractFile(zipIn, filePath);
  123. } else {
  124. File dir = new File(filePath);
  125. dir.mkdir();
  126. }
  127.  
  128. zipIn.closeEntry();
  129. }
  130.  
  131. zipIn.close ();
  132. }
  133.  
  134. private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
  135. BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
  136. byte[] bytesIn = new byte[4096];
  137. boolean read = false;
  138.  
  139. int read1;
  140. while((read1 = zipIn.read(bytesIn)) != -1) {
  141. bos.write(bytesIn, 0, read1);
  142. }
  143. bos.close ();
  144. }
  145.  
  146. public static boolean deleteWorld (final File path) {
  147. Bukkit.getScheduler ().runTaskAsynchronously (Core.getInstance (), new Runnable () {
  148. @Override
  149. public void run () {
  150. if(path.exists()) {
  151. File files[] = path.listFiles();
  152. for(int i=0; i<files.length; i++) {
  153. if(files[i].isDirectory()) {
  154. deleteWorld(files[i]);
  155. } else {
  156. files[i].delete();
  157. }
  158. }
  159. }
  160. }
  161. });
  162. return(path.delete());
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement