Added chunk load listener.

This commit is contained in:
Harrison Deng 2020-04-26 18:23:20 -05:00
parent b63c39b380
commit 16618ef439
6 changed files with 33 additions and 10 deletions

View File

@ -11,7 +11,7 @@ public class IslandSurvivalCraft extends JavaPlugin {
@Override @Override
public void onEnable() { public void onEnable() {
generator = new IslandSurvivalCraftChunkGenerator(new BiomePerIslandGenerator()); generator = new IslandSurvivalCraftChunkGenerator(this, new BiomePerIslandGenerator());
super.onEnable(); super.onEnable();
} }

View File

@ -4,6 +4,7 @@ import java.util.Random;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.generator.ChunkGenerator; import org.bukkit.generator.ChunkGenerator;
import org.bukkit.plugin.java.JavaPlugin;
import ca.recrown.islandsurvivalcraft.world.generation.IslandBiomeGenerator; import ca.recrown.islandsurvivalcraft.world.generation.IslandBiomeGenerator;
import ca.recrown.islandsurvivalcraft.world.generation.IslandWorldGenerator; import ca.recrown.islandsurvivalcraft.world.generation.IslandWorldGenerator;
@ -11,8 +12,8 @@ import ca.recrown.islandsurvivalcraft.world.generation.IslandWorldGenerator;
public class IslandSurvivalCraftChunkGenerator extends ChunkGenerator { public class IslandSurvivalCraftChunkGenerator extends ChunkGenerator {
private final IslandWorldGeneratorAlternator alternator; private final IslandWorldGeneratorAlternator alternator;
public IslandSurvivalCraftChunkGenerator(IslandBiomeGenerator biomeGenerator) { public IslandSurvivalCraftChunkGenerator(JavaPlugin plugin, IslandBiomeGenerator biomeGenerator) {
alternator = new IslandWorldGeneratorAlternator(biomeGenerator); alternator = new IslandWorldGeneratorAlternator(plugin, biomeGenerator);
} }
@Override @Override
@ -25,6 +26,7 @@ public class IslandSurvivalCraftChunkGenerator extends ChunkGenerator {
worldGenerator.GenerateChunk(chunkX, chunkZ, localX, localZ, chunk, biome); worldGenerator.GenerateChunk(chunkX, chunkZ, localX, localZ, chunk, biome);
} }
} }
return chunk; return chunk;
} }
} }

View File

@ -8,6 +8,7 @@ import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.plugin.java.JavaPlugin;
import ca.recrown.islandsurvivalcraft.world.generation.IslandBiomeGenerator; import ca.recrown.islandsurvivalcraft.world.generation.IslandBiomeGenerator;
import ca.recrown.islandsurvivalcraft.world.generation.IslandWorldGenerator; import ca.recrown.islandsurvivalcraft.world.generation.IslandWorldGenerator;
@ -24,10 +25,12 @@ public class IslandWorldGeneratorAlternator {
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true); private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
private final ReadLock readLock = lock.readLock(); private final ReadLock readLock = lock.readLock();
private final WriteLock writeLock = lock.writeLock(); 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.chunkGenerators = new HashMap<>();
this.islandBiomeGenerator = biomeGenerator; this.islandBiomeGenerator = biomeGenerator;
this.plugin = plugin;
} }
public IslandWorldGenerator getIslandChunkGeneratorSystem(World world, Random random) { public IslandWorldGenerator getIslandChunkGeneratorSystem(World world, Random random) {
@ -41,7 +44,7 @@ public class IslandWorldGeneratorAlternator {
readLock.unlock(); readLock.unlock();
} }
if (lastGenerator == null) { if (lastGenerator == null) {
lastGenerator = new IslandWorldGenerator(world, islandBiomeGenerator, random); lastGenerator = new IslandWorldGenerator(plugin, world, islandBiomeGenerator, random);
writeLock.lock(); writeLock.lock();
try { try {
chunkGenerators.put(lastUUID, lastGenerator); chunkGenerators.put(lastUUID, lastGenerator);

View File

@ -1,7 +1,11 @@
package ca.recrown.islandsurvivalcraft.world.generation; package ca.recrown.islandsurvivalcraft.world.generation;
import org.bukkit.Chunk;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Biome; 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.Utilities;
import ca.recrown.islandsurvivalcraft.Types.Point2; import ca.recrown.islandsurvivalcraft.Types.Point2;
@ -29,7 +33,7 @@ public class BiomePerIslandGenerator implements IslandBiomeGenerator {
public BiomePerIslandGenerator() { public BiomePerIslandGenerator() {
this.temperatureMapGenerator = new TemperatureMapGenerator(); this.temperatureMapGenerator = new TemperatureMapGenerator();
chunkBiomesCache = new Cache<>(65536); chunkBiomesCache = new Cache<>(65536);
chunkGenStatusCache = new Cache<>(4096); chunkGenStatusCache = new Cache<>(16384);
localChunkCache = new Biome[16][16]; localChunkCache = new Biome[16][16];
} }
@ -43,6 +47,16 @@ public class BiomePerIslandGenerator implements IslandBiomeGenerator {
this.temperatureMapGenerator.setSeed(world.getSeed()); 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 @Override
public IslandBiomeGenerator getInstance() { public IslandBiomeGenerator getInstance() {
return new BiomePerIslandGenerator(); return new BiomePerIslandGenerator();
@ -116,7 +130,7 @@ public class BiomePerIslandGenerator implements IslandBiomeGenerator {
int localZ = Math.abs(worldCoords.y % 16); int localZ = Math.abs(worldCoords.y % 16);
Point2 chunkCoords = new Point2(worldCoords.x / 16, 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; if (chunkCoords.fastEquals(this.currChunkCoords)) localChunkCache[localX][localZ] = biome;
//Set it cache wide. //Set it cache wide.
@ -192,4 +206,6 @@ public class BiomePerIslandGenerator implements IslandBiomeGenerator {
return shore != null && main != null && shallow != null; return shore != null && main != null && shallow != null;
} }
} }
} }

View File

@ -2,11 +2,12 @@ package ca.recrown.islandsurvivalcraft.world.generation;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Biome; import org.bukkit.block.Biome;
import org.bukkit.event.Listener;
import ca.recrown.islandsurvivalcraft.world.BiomeSelector; import ca.recrown.islandsurvivalcraft.world.BiomeSelector;
import ca.recrown.islandsurvivalcraft.world.IslandWorldMapper; import ca.recrown.islandsurvivalcraft.world.IslandWorldMapper;
public interface IslandBiomeGenerator { public interface IslandBiomeGenerator extends Listener {
public void initialize(World world, IslandWorldMapper mapGenerator, BiomeSelector biomeSelector); public void initialize(World world, IslandWorldMapper mapGenerator, BiomeSelector biomeSelector);
public Biome GenerateBiome(int chunkX, int chunkZ, int localX, int localZ); public Biome GenerateBiome(int chunkX, int chunkZ, int localX, int localZ);
public IslandBiomeGenerator getInstance(); public IslandBiomeGenerator getInstance();

View File

@ -8,6 +8,7 @@ import org.bukkit.World;
import org.bukkit.block.Biome; import org.bukkit.block.Biome;
import org.bukkit.generator.ChunkGenerator.BiomeGrid; import org.bukkit.generator.ChunkGenerator.BiomeGrid;
import org.bukkit.generator.ChunkGenerator.ChunkData; import org.bukkit.generator.ChunkGenerator.ChunkData;
import org.bukkit.plugin.java.JavaPlugin;
import ca.recrown.islandsurvivalcraft.Utilities; import ca.recrown.islandsurvivalcraft.Utilities;
import ca.recrown.islandsurvivalcraft.world.BiomeSelector; import ca.recrown.islandsurvivalcraft.world.BiomeSelector;
@ -27,14 +28,13 @@ public class IslandWorldGenerator {
public final IslandWorldMapper islandMapGenerator; public final IslandWorldMapper islandMapGenerator;
public final Random random; 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.world = world;
this.maxHeight = world.getMaxHeight(); this.maxHeight = world.getMaxHeight();
this.random = random; this.random = random;
this.biomeSelector = new BiomeSelector(random); this.biomeSelector = new BiomeSelector(random);
this.islandMapGenerator = new IslandWorldMapper(random); this.islandMapGenerator = new IslandWorldMapper(random);
this.biomeGenerator = islandBiomeGenerator; this.biomeGenerator = islandBiomeGenerator;
int bedrockMaxHeight = 5; int bedrockMaxHeight = 5;
int bedrockMinHeight = 1; int bedrockMinHeight = 1;
this.bedrockGenerator = new BedrockGenerator(random, bedrockMaxHeight, bedrockMinHeight); this.bedrockGenerator = new BedrockGenerator(random, bedrockMaxHeight, bedrockMinHeight);
@ -42,6 +42,7 @@ public class IslandWorldGenerator {
bedrockMaxHeight + 2); bedrockMaxHeight + 2);
biomeSelector.initialize(); biomeSelector.initialize();
islandBiomeGenerator.initialize(world, islandMapGenerator, biomeSelector); 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) { public void GenerateChunk(int chunkX, int chunkZ, int localX, int localZ, ChunkData chunk, BiomeGrid biomeGrid) {