From 8f0fc67ce054edbe5be5786abbc9f77f0bcc683c Mon Sep 17 00:00:00 2001 From: tastybento Date: Mon, 29 Apr 2019 16:38:24 -0700 Subject: [PATCH] Performs chunk regeneration. --- pom.xml | 2 +- .../world/bentobox/bskyblock/BSkyBlock.java | 7 + .../generators/ChunkGeneratorWorld.java | 272 ++++++++++-------- 3 files changed, 153 insertions(+), 128 deletions(-) diff --git a/pom.xml b/pom.xml index cfb6aee..971e451 100644 --- a/pom.xml +++ b/pom.xml @@ -91,7 +91,7 @@ world.bentobox bentobox - 1.4.0 + 1.5.0-SNAPSHOT provided diff --git a/src/main/java/world/bentobox/bskyblock/BSkyBlock.java b/src/main/java/world/bentobox/bskyblock/BSkyBlock.java index d28b428..eb5cadb 100644 --- a/src/main/java/world/bentobox/bskyblock/BSkyBlock.java +++ b/src/main/java/world/bentobox/bskyblock/BSkyBlock.java @@ -1,5 +1,6 @@ package world.bentobox.bskyblock; +import org.bukkit.Chunk; import org.bukkit.World; import org.bukkit.World.Environment; import org.bukkit.WorldCreator; @@ -130,4 +131,10 @@ public class BSkyBlock extends GameModeAddon { } } + + @Override + public void regerateChunk(Chunk chunk) { + if (chunkGenerator != null) chunkGenerator.regenerateChunk(chunk); + + } } diff --git a/src/main/java/world/bentobox/bskyblock/generators/ChunkGeneratorWorld.java b/src/main/java/world/bentobox/bskyblock/generators/ChunkGeneratorWorld.java index 7230f16..1fb7784 100644 --- a/src/main/java/world/bentobox/bskyblock/generators/ChunkGeneratorWorld.java +++ b/src/main/java/world/bentobox/bskyblock/generators/ChunkGeneratorWorld.java @@ -4,6 +4,7 @@ import java.util.Collections; import java.util.List; import java.util.Random; +import org.bukkit.Chunk; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.World.Environment; @@ -20,141 +21,158 @@ import world.bentobox.bskyblock.BSkyBlock; */ public class ChunkGeneratorWorld extends ChunkGenerator { - BSkyBlock addon; - Random rand; - PerlinOctaveGenerator gen; + BSkyBlock addon; + Random rand; + PerlinOctaveGenerator gen; - /** - * @param addon - BSkyBlock object - */ - public ChunkGeneratorWorld(BSkyBlock addon) { - super(); - this.addon = addon; - } + /** + * @param addon - BSkyBlock object + */ + public ChunkGeneratorWorld(BSkyBlock addon) { + super(); + this.addon = addon; + } - @Override - public ChunkData generateChunkData(World world, Random random, int chunkX, int chunkZ, ChunkGenerator.BiomeGrid biomeGrid) { - this.rand = random; - if (world.getEnvironment().equals(World.Environment.NETHER) && addon.getSettings().isNetherRoof()) { - return generateNetherRoofChunks(world, random); - } - ChunkData result = createChunkData(world); - if (!world.getEnvironment().equals(Environment.NORMAL)) { - return result; - } - Biome bio = addon.getSettings().getDefaultBiome(); - int seaHeight = addon.getSettings().getSeaHeight(); - for (int x = 0; x < 16; x++) { - for (int z = 0; z < 16; z++) { - biomeGrid.setBiome(x, z, bio); - if (seaHeight != 0) { - for (int y = 0; y <= seaHeight; y++) { - result.setBlock(x, y, z, Material.WATER); - } - } - } - } - return result; - } + @Override + public ChunkData generateChunkData(World world, Random random, int chunkX, int chunkZ, ChunkGenerator.BiomeGrid biomeGrid) { + this.rand = random; + if (world.getEnvironment().equals(World.Environment.NETHER) && addon.getSettings().isNetherRoof()) { + return generateNetherRoofChunks(world, random); + } + ChunkData result = createChunkData(world); + if (!world.getEnvironment().equals(Environment.NORMAL)) { + return result; + } + Biome bio = addon.getSettings().getDefaultBiome(); + int seaHeight = addon.getSettings().getSeaHeight(); + for (int x = 0; x < 16; x++) { + for (int z = 0; z < 16; z++) { + if (biomeGrid != null) biomeGrid.setBiome(x, z, bio); + if (seaHeight != 0) { + for (int y = 0; y <= seaHeight; y++) { + result.setBlock(x, y, z, Material.WATER); + } + } + } + } + return result; + } - @Override - public boolean canSpawn(World world, int x, int z) { - return true; - } + @Override + public boolean canSpawn(World world, int x, int z) { + return true; + } - @Override - public List getDefaultPopulators(final World world) { - return Collections.emptyList(); - } + @Override + public List getDefaultPopulators(final World world) { + return Collections.emptyList(); + } - /* - * Nether Section - */ - private ChunkData generateNetherRoofChunks(World world, Random random) { - ChunkData result = createChunkData(world); - rand.setSeed(world.getSeed()); - gen = new PerlinOctaveGenerator((long) (random.nextLong() * random.nextGaussian()), 8); - // Make the roof - common across the world - for (int x = 0; x < 16; x++) { - for (int z = 0; z < 16; z++) { - // Do the ceiling - makeCeiling(result, x, z, world.getMaxHeight()); - } - } - return result; + /* + * Nether Section + */ + private ChunkData generateNetherRoofChunks(World world, Random random) { + ChunkData result = createChunkData(world); + rand.setSeed(world.getSeed()); + gen = new PerlinOctaveGenerator((long) (random.nextLong() * random.nextGaussian()), 8); + // Make the roof - common across the world + for (int x = 0; x < 16; x++) { + for (int z = 0; z < 16; z++) { + // Do the ceiling + makeCeiling(result, x, z, world.getMaxHeight()); + } + } + return result; - } + } - private void makeCeiling(ChunkData result, int x, int z, int maxHeight) { - result.setBlock(x, (maxHeight - 1), z, Material.BEDROCK); - // Next three layers are a mix of bedrock and netherrack - for (int y = 2; y < 5; y++) { - double r = gen.noise(x, (maxHeight - y), z, 0.5, 0.5); - if (r > 0D) { - result.setBlock(x, (maxHeight - y), z, Material.BEDROCK); - } - } - // Next three layers are a mix of netherrack and air - for (int y = 5; y < 8; y++) { - double r = gen.noise(x, (double)maxHeight - y, z, 0.5, 0.5); - if (r > 0D) { - result.setBlock(x, (maxHeight - y), z, Material.NETHERRACK); - } else { - result.setBlock(x, (maxHeight - y), z, Material.AIR); - } - } - // Layer 8 may be glowstone - doGlowStone(result, maxHeight, x, z); - } + private void makeCeiling(ChunkData result, int x, int z, int maxHeight) { + result.setBlock(x, (maxHeight - 1), z, Material.BEDROCK); + // Next three layers are a mix of bedrock and netherrack + for (int y = 2; y < 5; y++) { + double r = gen.noise(x, (maxHeight - y), z, 0.5, 0.5); + if (r > 0D) { + result.setBlock(x, (maxHeight - y), z, Material.BEDROCK); + } + } + // Next three layers are a mix of netherrack and air + for (int y = 5; y < 8; y++) { + double r = gen.noise(x, (double)maxHeight - y, z, 0.5, 0.5); + if (r > 0D) { + result.setBlock(x, (maxHeight - y), z, Material.NETHERRACK); + } else { + result.setBlock(x, (maxHeight - y), z, Material.AIR); + } + } + // Layer 8 may be glowstone + doGlowStone(result, maxHeight, x, z); + } - private void doGlowStone(ChunkData result, int maxHeight, int x, int z) { - double r = gen.noise(x, (double)maxHeight - 8, z, rand.nextFloat(), rand.nextFloat()); - if (r < 0.5D) { - return; - } - // Have blobs of glowstone - switch (rand.nextInt(4)) { - case 1: - // Blob type 1 - setBlob1(result, x, maxHeight - 8, z); - break; - case 2: - // Stalactite - setStalactite(result, x, maxHeight - 8, z); - break; - case 3: - setBlob2(result, x, maxHeight - 8, z); - break; - default: - result.setBlock(x, (maxHeight - 8), z, Material.GLOWSTONE); - } - result.setBlock(x, (maxHeight - 8), z, Material.GLOWSTONE); - } + private void doGlowStone(ChunkData result, int maxHeight, int x, int z) { + double r = gen.noise(x, (double)maxHeight - 8, z, rand.nextFloat(), rand.nextFloat()); + if (r < 0.5D) { + return; + } + // Have blobs of glowstone + switch (rand.nextInt(4)) { + case 1: + // Blob type 1 + setBlob1(result, x, maxHeight - 8, z); + break; + case 2: + // Stalactite + setStalactite(result, x, maxHeight - 8, z); + break; + case 3: + setBlob2(result, x, maxHeight - 8, z); + break; + default: + result.setBlock(x, (maxHeight - 8), z, Material.GLOWSTONE); + } + result.setBlock(x, (maxHeight - 8), z, Material.GLOWSTONE); + } - private void setBlob2(ChunkData result, int x, int y, int z) { - result.setBlock(x, y, z, Material.GLOWSTONE); - if (x > 3 && z > 3) { - for (int xx = 0; xx < 3; xx++) { - for (int zz = 0; zz < 3; zz++) { - result.setBlock(x - xx, y - rand.nextInt(2), z - xx, Material.GLOWSTONE); - } - } - } - } + private void setBlob2(ChunkData result, int x, int y, int z) { + result.setBlock(x, y, z, Material.GLOWSTONE); + if (x > 3 && z > 3) { + for (int xx = 0; xx < 3; xx++) { + for (int zz = 0; zz < 3; zz++) { + result.setBlock(x - xx, y - rand.nextInt(2), z - xx, Material.GLOWSTONE); + } + } + } + } - private void setStalactite(ChunkData result, int x, int y, int z) { - for (int i = 0; i < rand.nextInt(10); i++) { - result.setBlock(x, y - i, z, Material.GLOWSTONE); - } - } + private void setStalactite(ChunkData result, int x, int y, int z) { + for (int i = 0; i < rand.nextInt(10); i++) { + result.setBlock(x, y - i, z, Material.GLOWSTONE); + } + } - private void setBlob1(ChunkData result, int x, int y, int z) { - result.setBlock(x, y, z, Material.GLOWSTONE); - if (x < 14 && z < 14) { - result.setBlock(x + 1, y, z + 1, Material.GLOWSTONE); - result.setBlock(x + 2, y, z + 2, Material.GLOWSTONE); - result.setBlock(x + 1, y, z + 2, Material.GLOWSTONE); - result.setBlock(x + 1, y, z + 2, Material.GLOWSTONE); - } - } -} \ No newline at end of file + private void setBlob1(ChunkData result, int x, int y, int z) { + result.setBlock(x, y, z, Material.GLOWSTONE); + if (x < 14 && z < 14) { + result.setBlock(x + 1, y, z + 1, Material.GLOWSTONE); + result.setBlock(x + 2, y, z + 2, Material.GLOWSTONE); + result.setBlock(x + 1, y, z + 2, Material.GLOWSTONE); + result.setBlock(x + 1, y, z + 2, Material.GLOWSTONE); + } + } + + /** + * Regenerate a chunk + * @param chunk - chunk to regenerate + */ + public void regenerateChunk(Chunk chunk) { + ChunkData cd = generateChunkData(chunk.getWorld(), new Random(), chunk.getX(), chunk.getZ(), null); + for (int x = 0; x < 16; x++) { + for (int z = 0; z < 16; z++) { + chunk.getBlock(x, 0, z).setBiome(addon.getSettings().getDefaultBiome()); + for (int y = 0; y < chunk.getWorld().getMaxHeight(); y++) { + chunk.getBlock(x, y, z).setBlockData(cd.getBlockData(x, y, z)); + } + } + } + } + +}