Guest User

RegenOres.java

a guest
Jun 30th, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.38 KB | None | 0 0
  1. package net.corruptmc.oreregen.util;
  2.  
  3. import net.corruptmc.oreregen.OreRegen;
  4. import org.bukkit.Bukkit;
  5. import org.bukkit.Chunk;
  6. import org.bukkit.Material;
  7. import org.bukkit.World;
  8. import org.bukkit.block.Block;
  9. import org.bukkit.configuration.ConfigurationSection;
  10. import org.bukkit.configuration.file.FileConfiguration;
  11. import org.bukkit.configuration.file.YamlConfiguration;
  12. import org.bukkit.plugin.java.JavaPlugin;
  13.  
  14. import java.io.File;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import java.util.Random;
  18. import java.util.Set;
  19. import java.util.logging.Logger;
  20.  
  21. public class RegenOres {
  22.  
  23. OreRegen main = JavaPlugin.getPlugin(OreRegen.class);
  24. FileConfiguration config = main.getConfig();
  25. Logger log = main.getLogger();
  26. int blockLimiter = config.getInt("ore-regen.block-limiter");
  27.  
  28. Random random = new Random();
  29.  
  30. /*
  31. Generate the ores in a specific chunk
  32. */
  33. public void regenChunk(Chunk chunk) {
  34. World world = chunk.getWorld();
  35. String worldName = world.getName();
  36. List<String> ores = config.getStringList("ore-regen.worlds." + worldName + ".ores");
  37. File chunkFile;
  38. FileConfiguration chunkConfig = null;
  39. boolean loadChunks = config.getBoolean("ore-regen.load-chunks");
  40.  
  41. //Check whether or not the plugin should load unloaded chunks
  42. if (loadChunks) {
  43. int x = chunk.getX();
  44. int z = chunk.getZ();
  45. chunkFile = new File(main.getDataFolder() + File.separator + "chunkData" + File.separator + worldName,
  46. x + ":" + z + ".chunk");
  47. chunkConfig = YamlConfiguration.loadConfiguration(chunkFile);
  48. }
  49.  
  50. //Loop through possible ores in a world.
  51. for (String s : ores) {
  52. Material type;
  53. try {
  54. type = Material.matchMaterial(s);
  55. } catch (Exception e) {
  56. log.info("Invalid material in world: " + worldName);
  57. log.info("Invalid material: " + s);
  58. continue;
  59. }
  60. ConfigurationSection section = config.getConfigurationSection("ore-settings." + s);
  61. if(section == null)
  62. continue;
  63.  
  64.  
  65. //Whether or not to check for existing ores
  66. boolean checkExisting = config.getBoolean("ore-regen.check-existing");
  67. if (checkExisting && loadChunks) {
  68. Set<String> keys = chunkConfig.getKeys(false);
  69. if (keys.contains(s)) {
  70. //Make sure there aren't too many ores in one chunk.
  71. int qty = chunkConfig.getInt(s);
  72. int min = section.getInt("threshold");
  73. if (qty > min)
  74. continue;
  75. }
  76. }
  77.  
  78. /*
  79. This is where the plugin tries to spawn a vein
  80. I have kept it as close to vanilla Minecraft generation as possible
  81. It should be self explanatory if you are familiar with world gen
  82. */
  83. int tries = section.getInt("tries");
  84. int chance = section.getInt("chance");
  85. int minHeight = section.getInt("mix-height");
  86. int heightRange = section.getInt("height-range");
  87. int sizeLimiter = section.getInt("size-limiter");
  88. for (int i = 0; i < tries; i++) {
  89.  
  90. if (random.nextInt(100) < chance) {
  91.  
  92. int x = random.nextInt(15);
  93. int z = random.nextInt(15);
  94.  
  95. int y = random.nextInt(heightRange) + minHeight;
  96.  
  97. boolean isStone;
  98.  
  99. if (chunk.getBlock(x, y, z).getType() == Material.STONE) {
  100. isStone = true;
  101. while (isStone) {
  102. Block block = chunk.getBlock(x, y, z);
  103.  
  104. if(type == Material.EMERALD_ORE){
  105. String biome = world.getBiome(x, y, z).toString();
  106. if (!biome.contains("HILLS"))
  107. isStone = false;
  108. }
  109.  
  110. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
  111. @Override
  112. public void run() {
  113. block.setType(type);
  114. }
  115. }, blockLimiter);
  116.  
  117.  
  118. //Add additional block to vein
  119. if (random.nextInt(100) < sizeLimiter) {
  120. //chooses the direction to expand the vein
  121. switch (random.nextInt(5)) {
  122. case 0:
  123. x++;
  124. break;
  125. case 1:
  126. y++;
  127. break;
  128. case 2:
  129. z++;
  130. break;
  131. case 3:
  132. x--;
  133. break;
  134. case 4:
  135. y--;
  136. break;
  137. case 5:
  138. z--;
  139. break;
  140. }
  141. try {
  142. isStone = (chunk.getBlock(x, y, z).getType() == Material.STONE) &&
  143. (chunk.getBlock(z, y, z).getType() != type);
  144. } catch (IllegalArgumentException e) {
  145.  
  146. //TODO Multi-chunk ore gen
  147.  
  148. isStone = false;
  149. }
  150. } else
  151. isStone = false;
  152. }
  153. }
  154.  
  155. }
  156. }
  157. }
  158. }
  159.  
  160. /*
  161. Generate the ores in a specific world
  162. */
  163.  
  164. public void regenChunks(World world) {
  165.  
  166. String worldName = world.getName();
  167. Chunk[] chunks = null;
  168. List<Chunk> chunksArray = null;
  169. if (!config.getBoolean("ore-regen.load-chunks")) {
  170. chunks = world.getLoadedChunks();
  171. } else {
  172. chunksArray = new ArrayList<>();
  173. File path = new File(main.getDataFolder() + File.separator + "chunkData" + File.separator + worldName);
  174. if (path == null)
  175. return;
  176.  
  177. File[] chunkFiles = path.listFiles();
  178. if (chunkFiles == null || chunkFiles.length == 0)
  179. return;
  180.  
  181. for (File file : chunkFiles) {
  182. String[] name = file.getName().replaceAll(".chunk", "").split(":");
  183. int x;
  184. int z;
  185. try{
  186. x = Integer.parseInt(name[0]);
  187. z = Integer.parseInt(name[1]);
  188. } catch (NumberFormatException e){
  189. return;
  190. }
  191. Chunk chunk = world.getChunkAt(x, z);
  192. chunksArray.add(chunk);
  193. }
  194. }
  195.  
  196. if (chunks != null) {
  197. for (Chunk chunk : chunks) {
  198. regenChunk(chunk);
  199. }
  200. } else {
  201. for (Chunk chunk : chunksArray) {
  202. regenChunk(chunk);
  203. }
  204. }
  205.  
  206. }
  207.  
  208.  
  209. /*
  210. Generate the ores in a all defined worlds
  211. */
  212. public void regenAllChunks() {
  213.  
  214. Set<String> keys = config.getConfigurationSection("ore-regen.worlds").getKeys(false);
  215. World world;
  216.  
  217. if (keys == null || keys.size() == 0)
  218. return;
  219.  
  220. for (String s : keys) {
  221. try {
  222. world = Bukkit.getWorld(s);
  223. } catch (Exception e) {
  224. log.info("Invalid world: " + s);
  225. continue;
  226. }
  227. if (world == null) {
  228. log.info("Invalid world: " + s);
  229. continue;
  230. }
  231. regenChunks(world);
  232. }
  233. }
  234.  
  235. }
Add Comment
Please, Sign In to add comment