Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. package me.leluckyy.worldmanager.utils;
  2.  
  3. import me.leluckyy.worldmanager.main.Config;
  4. import org.apache.commons.io.FileUtils;
  5. import org.bukkit.Bukkit;
  6. import org.bukkit.World;
  7. import org.bukkit.WorldCreator;
  8. import org.bukkit.WorldType;
  9. import org.bukkit.entity.Player;
  10. import org.bukkit.generator.ChunkGenerator;
  11.  
  12. import java.io.File;
  13. import java.io.IOException;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16.  
  17. public class WorldManager {
  18.  
  19. public static boolean worldExists(String worldName) {
  20. World world = Bukkit.getWorld(worldName);
  21. return(world == null ? false : true);
  22. }
  23.  
  24. public void createWorld(String worldName, Long seed, World.Environment world, WorldType type, Boolean structures) {
  25. WorldCreator creator = new WorldCreator(worldName).environment(world).type(type);
  26. if (!worldExists(worldName)) {
  27. if (seed != null) {
  28. creator.seed(seed);
  29. }
  30. if (structures != null) {
  31. if (structures == true) {
  32. creator.generateStructures(true);
  33. } else creator.generateStructures(false);
  34. } else creator.generateStructures(true);
  35. creator.createWorld();
  36. Bukkit.broadcastMessage("Welt '§c" + worldName + "§r' wurde generiert!");
  37. Bukkit.getWorlds().add(Bukkit.getWorld(worldName));
  38. saveWorlds(worldName);
  39. }
  40. }
  41.  
  42. public void generateSkyWorld(String worldName) {
  43. WorldCreator creator = new WorldCreator(worldName).generator(getSkyWorld());
  44. creator.createWorld();
  45. Bukkit.broadcastMessage("SkyWorld '§c" + worldName + "§r' wurde generiert!");
  46. World world = Bukkit.getWorld(worldName);
  47. world.setSpawnLocation(0, 100, 0);
  48. Bukkit.getWorlds().add(world);
  49. saveWorlds(worldName);
  50. }
  51.  
  52. public void deleteWorld(String worldName) {
  53. if (worldExists(worldName)) {
  54. World world = Bukkit.getWorld(worldName);
  55. for (Player player : world.getPlayers()) {
  56. if (worldName != "world") {
  57. World backupWorld = Bukkit.getWorld("world");
  58. player.teleport(backupWorld.getSpawnLocation());
  59. } else player.kickPlayer("Die Welt wird momentan geändert!");
  60. }
  61. Bukkit.unloadWorld(world, false);
  62. try {
  63. FileUtils.deleteDirectory(new File(worldName));
  64. } catch (IOException e) { e.printStackTrace(); }
  65. Bukkit.broadcastMessage("Welt '§c" + worldName + "§r' wurde §cgelöscht!");
  66. }
  67. }
  68.  
  69. private ChunkGenerator getSkyWorld() {
  70. return new GenerateSkyWorld();
  71. }
  72.  
  73. private void saveWorlds(String worldName) {
  74. try {
  75. if (!Config.file.exists()) {
  76. List<String> worlds = new ArrayList<>();
  77. Config.file.createNewFile();
  78. worlds.add(worldName);
  79. Config.cfg.set("Worlds", worlds);
  80. Config.cfg.save(Config.file);
  81. } else {
  82. List<String> worlds = Config.cfg.getStringList("Worlds");
  83. worlds.add(worldName);
  84. Config.cfg.set("Worlds", worlds);
  85. Config.cfg.save(Config.file);
  86. }
  87. } catch (IOException e) { e.printStackTrace(); }
  88. }
  89.  
  90. public void loadCustomWorld(String worldName) {
  91. File file = new File(worldName);
  92. if (file.exists()) {
  93. if (Bukkit.getWorld(worldName) == null) {
  94. try {
  95. if (!Config.file.exists()) {
  96. List<String> worlds = new ArrayList<>();
  97. Config.file.createNewFile();
  98. worlds.add(worldName);
  99. Config.cfg.set("Worlds", worlds);
  100. Config.cfg.save(Config.file);
  101. } else {
  102. List<String> worlds = Config.cfg.getStringList("Worlds");
  103. worlds.add(worldName);
  104. Config.cfg.set("Worlds", worlds);
  105. Config.cfg.save(Config.file);
  106. }
  107. } catch (IOException e) { e.printStackTrace(); }
  108. }
  109. }
  110. }
  111.  
  112. // only for the onEnable method
  113. public void loadWorlds() {
  114. if (Config.file.exists()) {
  115. List<String> worlds = Config.cfg.getStringList("Worlds");
  116. for (String world : worlds) {
  117. WorldCreator creator = new WorldCreator(world);
  118. creator.createWorld();
  119. }
  120. }
  121. }
  122. // only for the onEnable method
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement