Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. package ru.slimetwitch.bedwars.utils;
  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.ArrayList;
  10. import java.util.Arrays;
  11.  
  12. import org.bukkit.Bukkit;
  13. import org.bukkit.World;
  14. import org.bukkit.WorldCreator;
  15. import org.bukkit.WorldType;
  16.  
  17. public class WorldManager {
  18.  
  19. public static void createWorld(String name) {
  20.  
  21. WorldCreator wc = new WorldCreator(name);
  22.  
  23. wc.type(WorldType.FLAT);
  24. wc.generatorSettings("2;0;1;");
  25.  
  26. wc.createWorld();
  27.  
  28. }
  29. public static void unloadWorld(String name) {
  30. World world = Bukkit.getWorld(name);
  31. if(!world.equals(null)) {
  32. Bukkit.getServer().unloadWorld(world, true);
  33. }
  34. }
  35.  
  36. public static boolean deleteWorld(String name) {
  37. World world = Bukkit.getWorld(name);
  38. File path = world.getWorldFolder();
  39. if(path.exists()) {
  40. File files[] = path.listFiles();
  41. for(int i=0; i<files.length; i++) {
  42. if(files[i].isDirectory()) {
  43. deleteWorld(files[i]);
  44. } else {
  45. files[i].delete();
  46. }
  47. }
  48. }
  49. return(path.delete());
  50. }
  51. public static boolean deleteWorld(File path) {
  52. if(path.exists()) {
  53. File files[] = path.listFiles();
  54. for(int i=0; i<files.length; i++) {
  55. if(files[i].isDirectory()) {
  56. deleteWorld(files[i]);
  57. } else {
  58. files[i].delete();
  59. }
  60. }
  61. }
  62. return(path.delete());
  63. }
  64.  
  65. public static void copyWorld(World originalWorld, String newWorldName) {
  66. copyFileStructure(originalWorld.getWorldFolder(), new File(Bukkit.getWorldContainer(), newWorldName));
  67. new WorldCreator(newWorldName).createWorld();
  68. }
  69.  
  70.  
  71. private static void copyFileStructure(File source, File target){
  72. try {
  73. ArrayList<String> ignore = new ArrayList<>(Arrays.asList("uid.dat", "session.lock"));
  74. if(!ignore.contains(source.getName())) {
  75. if(source.isDirectory()) {
  76. if(!target.exists())
  77. if (!target.mkdirs())
  78. throw new IOException("Couldn't create world directory!");
  79. String files[] = source.list();
  80. for (String file : files) {
  81. File srcFile = new File(source, file);
  82. File destFile = new File(target, file);
  83. copyFileStructure(srcFile, destFile);
  84. }
  85. } else {
  86. InputStream in = new FileInputStream(source);
  87. OutputStream out = new FileOutputStream(target);
  88. byte[] buffer = new byte[1024];
  89. int length;
  90. while ((length = in.read(buffer)) > 0)
  91. out.write(buffer, 0, length);
  92. in.close();
  93. out.close();
  94. }
  95. }
  96. } catch (IOException e) {
  97. throw new RuntimeException(e);
  98. }
  99. }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement