Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.corruptmc.oreregen.util;
- import net.corruptmc.oreregen.OreRegen;
- import org.bukkit.Bukkit;
- import org.bukkit.Chunk;
- import org.bukkit.Material;
- import org.bukkit.World;
- import org.bukkit.block.Block;
- import org.bukkit.configuration.ConfigurationSection;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.configuration.file.YamlConfiguration;
- import org.bukkit.plugin.java.JavaPlugin;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- import java.util.Set;
- import java.util.logging.Logger;
- public class RegenOres {
- OreRegen main = JavaPlugin.getPlugin(OreRegen.class);
- FileConfiguration config = main.getConfig();
- Logger log = main.getLogger();
- int blockLimiter = config.getInt("ore-regen.block-limiter");
- Random random = new Random();
- /*
- Generate the ores in a specific chunk
- */
- public void regenChunk(Chunk chunk) {
- World world = chunk.getWorld();
- String worldName = world.getName();
- List<String> ores = config.getStringList("ore-regen.worlds." + worldName + ".ores");
- File chunkFile;
- FileConfiguration chunkConfig = null;
- boolean loadChunks = config.getBoolean("ore-regen.load-chunks");
- //Check whether or not the plugin should load unloaded chunks
- if (loadChunks) {
- int x = chunk.getX();
- int z = chunk.getZ();
- chunkFile = new File(main.getDataFolder() + File.separator + "chunkData" + File.separator + worldName,
- x + ":" + z + ".chunk");
- chunkConfig = YamlConfiguration.loadConfiguration(chunkFile);
- }
- //Loop through possible ores in a world.
- for (String s : ores) {
- Material type;
- try {
- type = Material.matchMaterial(s);
- } catch (Exception e) {
- log.info("Invalid material in world: " + worldName);
- log.info("Invalid material: " + s);
- continue;
- }
- ConfigurationSection section = config.getConfigurationSection("ore-settings." + s);
- if(section == null)
- continue;
- //Whether or not to check for existing ores
- boolean checkExisting = config.getBoolean("ore-regen.check-existing");
- if (checkExisting && loadChunks) {
- Set<String> keys = chunkConfig.getKeys(false);
- if (keys.contains(s)) {
- //Make sure there aren't too many ores in one chunk.
- int qty = chunkConfig.getInt(s);
- int min = section.getInt("threshold");
- if (qty > min)
- continue;
- }
- }
- /*
- This is where the plugin tries to spawn a vein
- I have kept it as close to vanilla Minecraft generation as possible
- It should be self explanatory if you are familiar with world gen
- */
- int tries = section.getInt("tries");
- int chance = section.getInt("chance");
- int minHeight = section.getInt("mix-height");
- int heightRange = section.getInt("height-range");
- int sizeLimiter = section.getInt("size-limiter");
- for (int i = 0; i < tries; i++) {
- if (random.nextInt(100) < chance) {
- int x = random.nextInt(15);
- int z = random.nextInt(15);
- int y = random.nextInt(heightRange) + minHeight;
- boolean isStone;
- if (chunk.getBlock(x, y, z).getType() == Material.STONE) {
- isStone = true;
- while (isStone) {
- Block block = chunk.getBlock(x, y, z);
- if(type == Material.EMERALD_ORE){
- String biome = world.getBiome(x, y, z).toString();
- if (!biome.contains("HILLS"))
- isStone = false;
- }
- Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
- @Override
- public void run() {
- block.setType(type);
- }
- }, blockLimiter);
- //Add additional block to vein
- if (random.nextInt(100) < sizeLimiter) {
- //chooses the direction to expand the vein
- switch (random.nextInt(5)) {
- case 0:
- x++;
- break;
- case 1:
- y++;
- break;
- case 2:
- z++;
- break;
- case 3:
- x--;
- break;
- case 4:
- y--;
- break;
- case 5:
- z--;
- break;
- }
- try {
- isStone = (chunk.getBlock(x, y, z).getType() == Material.STONE) &&
- (chunk.getBlock(z, y, z).getType() != type);
- } catch (IllegalArgumentException e) {
- //TODO Multi-chunk ore gen
- isStone = false;
- }
- } else
- isStone = false;
- }
- }
- }
- }
- }
- }
- /*
- Generate the ores in a specific world
- */
- public void regenChunks(World world) {
- String worldName = world.getName();
- Chunk[] chunks = null;
- List<Chunk> chunksArray = null;
- if (!config.getBoolean("ore-regen.load-chunks")) {
- chunks = world.getLoadedChunks();
- } else {
- chunksArray = new ArrayList<>();
- File path = new File(main.getDataFolder() + File.separator + "chunkData" + File.separator + worldName);
- if (path == null)
- return;
- File[] chunkFiles = path.listFiles();
- if (chunkFiles == null || chunkFiles.length == 0)
- return;
- for (File file : chunkFiles) {
- String[] name = file.getName().replaceAll(".chunk", "").split(":");
- int x;
- int z;
- try{
- x = Integer.parseInt(name[0]);
- z = Integer.parseInt(name[1]);
- } catch (NumberFormatException e){
- return;
- }
- Chunk chunk = world.getChunkAt(x, z);
- chunksArray.add(chunk);
- }
- }
- if (chunks != null) {
- for (Chunk chunk : chunks) {
- regenChunk(chunk);
- }
- } else {
- for (Chunk chunk : chunksArray) {
- regenChunk(chunk);
- }
- }
- }
- /*
- Generate the ores in a all defined worlds
- */
- public void regenAllChunks() {
- Set<String> keys = config.getConfigurationSection("ore-regen.worlds").getKeys(false);
- World world;
- if (keys == null || keys.size() == 0)
- return;
- for (String s : keys) {
- try {
- world = Bukkit.getWorld(s);
- } catch (Exception e) {
- log.info("Invalid world: " + s);
- continue;
- }
- if (world == null) {
- log.info("Invalid world: " + s);
- continue;
- }
- regenChunks(world);
- }
- }
- }
Add Comment
Please, Sign In to add comment