From 16618ef4399b4656c2c931481ad2a0e404e6087b Mon Sep 17 00:00:00 2001 From: Harrison Date: Sun, 26 Apr 2020 18:23:20 -0500 Subject: [PATCH] Added chunk load listener. --- .../IslandSurvivalCraft.java | 2 +- .../IslandSurvivalCraftChunkGenerator.java | 6 ++++-- .../world/IslandWorldGeneratorAlternator.java | 7 +++++-- .../generation/BiomePerIslandGenerator.java | 20 +++++++++++++++++-- .../generation/IslandBiomeGenerator.java | 3 ++- .../generation/IslandWorldGenerator.java | 5 +++-- 6 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/main/java/ca/recrown/islandsurvivalcraft/IslandSurvivalCraft.java b/src/main/java/ca/recrown/islandsurvivalcraft/IslandSurvivalCraft.java index 190c4f0..e6d2a1d 100644 --- a/src/main/java/ca/recrown/islandsurvivalcraft/IslandSurvivalCraft.java +++ b/src/main/java/ca/recrown/islandsurvivalcraft/IslandSurvivalCraft.java @@ -11,7 +11,7 @@ public class IslandSurvivalCraft extends JavaPlugin { @Override public void onEnable() { - generator = new IslandSurvivalCraftChunkGenerator(new BiomePerIslandGenerator()); + generator = new IslandSurvivalCraftChunkGenerator(this, new BiomePerIslandGenerator()); super.onEnable(); } diff --git a/src/main/java/ca/recrown/islandsurvivalcraft/world/IslandSurvivalCraftChunkGenerator.java b/src/main/java/ca/recrown/islandsurvivalcraft/world/IslandSurvivalCraftChunkGenerator.java index c9be0dc..4ef9338 100644 --- a/src/main/java/ca/recrown/islandsurvivalcraft/world/IslandSurvivalCraftChunkGenerator.java +++ b/src/main/java/ca/recrown/islandsurvivalcraft/world/IslandSurvivalCraftChunkGenerator.java @@ -4,6 +4,7 @@ import java.util.Random; import org.bukkit.World; import org.bukkit.generator.ChunkGenerator; +import org.bukkit.plugin.java.JavaPlugin; import ca.recrown.islandsurvivalcraft.world.generation.IslandBiomeGenerator; import ca.recrown.islandsurvivalcraft.world.generation.IslandWorldGenerator; @@ -11,8 +12,8 @@ import ca.recrown.islandsurvivalcraft.world.generation.IslandWorldGenerator; public class IslandSurvivalCraftChunkGenerator extends ChunkGenerator { private final IslandWorldGeneratorAlternator alternator; - public IslandSurvivalCraftChunkGenerator(IslandBiomeGenerator biomeGenerator) { - alternator = new IslandWorldGeneratorAlternator(biomeGenerator); + public IslandSurvivalCraftChunkGenerator(JavaPlugin plugin, IslandBiomeGenerator biomeGenerator) { + alternator = new IslandWorldGeneratorAlternator(plugin, biomeGenerator); } @Override @@ -25,6 +26,7 @@ public class IslandSurvivalCraftChunkGenerator extends ChunkGenerator { worldGenerator.GenerateChunk(chunkX, chunkZ, localX, localZ, chunk, biome); } } + return chunk; } } \ No newline at end of file diff --git a/src/main/java/ca/recrown/islandsurvivalcraft/world/IslandWorldGeneratorAlternator.java b/src/main/java/ca/recrown/islandsurvivalcraft/world/IslandWorldGeneratorAlternator.java index c6ce4ca..13d8273 100644 --- a/src/main/java/ca/recrown/islandsurvivalcraft/world/IslandWorldGeneratorAlternator.java +++ b/src/main/java/ca/recrown/islandsurvivalcraft/world/IslandWorldGeneratorAlternator.java @@ -8,6 +8,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock; import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock; import org.bukkit.World; +import org.bukkit.plugin.java.JavaPlugin; import ca.recrown.islandsurvivalcraft.world.generation.IslandBiomeGenerator; import ca.recrown.islandsurvivalcraft.world.generation.IslandWorldGenerator; @@ -24,10 +25,12 @@ public class IslandWorldGeneratorAlternator { private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); private final ReadLock readLock = lock.readLock(); private final WriteLock writeLock = lock.writeLock(); + private final JavaPlugin plugin; - public IslandWorldGeneratorAlternator(IslandBiomeGenerator biomeGenerator) { + public IslandWorldGeneratorAlternator(JavaPlugin plugin, IslandBiomeGenerator biomeGenerator) { this.chunkGenerators = new HashMap<>(); this.islandBiomeGenerator = biomeGenerator; + this.plugin = plugin; } public IslandWorldGenerator getIslandChunkGeneratorSystem(World world, Random random) { @@ -41,7 +44,7 @@ public class IslandWorldGeneratorAlternator { readLock.unlock(); } if (lastGenerator == null) { - lastGenerator = new IslandWorldGenerator(world, islandBiomeGenerator, random); + lastGenerator = new IslandWorldGenerator(plugin, world, islandBiomeGenerator, random); writeLock.lock(); try { chunkGenerators.put(lastUUID, lastGenerator); diff --git a/src/main/java/ca/recrown/islandsurvivalcraft/world/generation/BiomePerIslandGenerator.java b/src/main/java/ca/recrown/islandsurvivalcraft/world/generation/BiomePerIslandGenerator.java index 021c9ff..36a5ccc 100644 --- a/src/main/java/ca/recrown/islandsurvivalcraft/world/generation/BiomePerIslandGenerator.java +++ b/src/main/java/ca/recrown/islandsurvivalcraft/world/generation/BiomePerIslandGenerator.java @@ -1,7 +1,11 @@ package ca.recrown.islandsurvivalcraft.world.generation; +import org.bukkit.Chunk; import org.bukkit.World; import org.bukkit.block.Biome; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.world.ChunkLoadEvent; import ca.recrown.islandsurvivalcraft.Utilities; import ca.recrown.islandsurvivalcraft.Types.Point2; @@ -29,7 +33,7 @@ public class BiomePerIslandGenerator implements IslandBiomeGenerator { public BiomePerIslandGenerator() { this.temperatureMapGenerator = new TemperatureMapGenerator(); chunkBiomesCache = new Cache<>(65536); - chunkGenStatusCache = new Cache<>(4096); + chunkGenStatusCache = new Cache<>(16384); localChunkCache = new Biome[16][16]; } @@ -43,6 +47,16 @@ public class BiomePerIslandGenerator implements IslandBiomeGenerator { this.temperatureMapGenerator.setSeed(world.getSeed()); } + @EventHandler(priority = EventPriority.HIGH) + public void onChunkLoaded(ChunkLoadEvent event) { + if (event.isNewChunk()) { + Chunk chunk = event.getChunk(); + if (chunk.getWorld().getUID().hashCode() == world.getUID().hashCode() && chunk.getWorld().getUID().equals(world.getUID())) { + chunkGenStatusCache.setValue(new Point2(chunk.getX(), chunk.getZ()), true); + } + } + } + @Override public IslandBiomeGenerator getInstance() { return new BiomePerIslandGenerator(); @@ -116,7 +130,7 @@ public class BiomePerIslandGenerator implements IslandBiomeGenerator { int localZ = Math.abs(worldCoords.y % 16); Point2 chunkCoords = new Point2(worldCoords.x / 16, worldCoords.y / 16); - //If local, set it locally + //If local, set it locally. if (chunkCoords.fastEquals(this.currChunkCoords)) localChunkCache[localX][localZ] = biome; //Set it cache wide. @@ -192,4 +206,6 @@ public class BiomePerIslandGenerator implements IslandBiomeGenerator { return shore != null && main != null && shallow != null; } } + + } \ No newline at end of file diff --git a/src/main/java/ca/recrown/islandsurvivalcraft/world/generation/IslandBiomeGenerator.java b/src/main/java/ca/recrown/islandsurvivalcraft/world/generation/IslandBiomeGenerator.java index c11f787..f10e2a4 100644 --- a/src/main/java/ca/recrown/islandsurvivalcraft/world/generation/IslandBiomeGenerator.java +++ b/src/main/java/ca/recrown/islandsurvivalcraft/world/generation/IslandBiomeGenerator.java @@ -2,11 +2,12 @@ package ca.recrown.islandsurvivalcraft.world.generation; import org.bukkit.World; import org.bukkit.block.Biome; +import org.bukkit.event.Listener; import ca.recrown.islandsurvivalcraft.world.BiomeSelector; import ca.recrown.islandsurvivalcraft.world.IslandWorldMapper; -public interface IslandBiomeGenerator { +public interface IslandBiomeGenerator extends Listener { public void initialize(World world, IslandWorldMapper mapGenerator, BiomeSelector biomeSelector); public Biome GenerateBiome(int chunkX, int chunkZ, int localX, int localZ); public IslandBiomeGenerator getInstance(); diff --git a/src/main/java/ca/recrown/islandsurvivalcraft/world/generation/IslandWorldGenerator.java b/src/main/java/ca/recrown/islandsurvivalcraft/world/generation/IslandWorldGenerator.java index 4e36094..ae4a0c8 100644 --- a/src/main/java/ca/recrown/islandsurvivalcraft/world/generation/IslandWorldGenerator.java +++ b/src/main/java/ca/recrown/islandsurvivalcraft/world/generation/IslandWorldGenerator.java @@ -8,6 +8,7 @@ import org.bukkit.World; import org.bukkit.block.Biome; import org.bukkit.generator.ChunkGenerator.BiomeGrid; import org.bukkit.generator.ChunkGenerator.ChunkData; +import org.bukkit.plugin.java.JavaPlugin; import ca.recrown.islandsurvivalcraft.Utilities; import ca.recrown.islandsurvivalcraft.world.BiomeSelector; @@ -27,14 +28,13 @@ public class IslandWorldGenerator { public final IslandWorldMapper islandMapGenerator; public final Random random; - public IslandWorldGenerator(World world, IslandBiomeGenerator islandBiomeGenerator, Random random) { + public IslandWorldGenerator(JavaPlugin plugin, World world, IslandBiomeGenerator islandBiomeGenerator, Random random) { this.world = world; this.maxHeight = world.getMaxHeight(); this.random = random; this.biomeSelector = new BiomeSelector(random); this.islandMapGenerator = new IslandWorldMapper(random); this.biomeGenerator = islandBiomeGenerator; - int bedrockMaxHeight = 5; int bedrockMinHeight = 1; this.bedrockGenerator = new BedrockGenerator(random, bedrockMaxHeight, bedrockMinHeight); @@ -42,6 +42,7 @@ public class IslandWorldGenerator { bedrockMaxHeight + 2); biomeSelector.initialize(); islandBiomeGenerator.initialize(world, islandMapGenerator, biomeSelector); + plugin.getServer().getPluginManager().registerEvents(islandBiomeGenerator, plugin); } public void GenerateChunk(int chunkX, int chunkZ, int localX, int localZ, ChunkData chunk, BiomeGrid biomeGrid) {