From f51ff4165ba56b397977109e9ddd8ed9a229585d Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 1 Jan 2023 15:06:09 -0800 Subject: [PATCH 1/3] Update to latest BentoBox API --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index a9e8377..2df3688 100644 --- a/pom.xml +++ b/pom.xml @@ -59,7 +59,7 @@ 2.0.9 1.19.3-R0.1-SNAPSHOT - 1.21.0 + 1.22.0-SNAPSHOT ${build.version}-SNAPSHOT From ea16a45114eda2cd3caed83fe3e1994475e3df33 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 1 Jan 2023 18:54:57 -0800 Subject: [PATCH 2/3] Version that has an ocen floor and uses new generator methods --- .../world/bentobox/acidisland/AcidIsland.java | 24 +++-- .../acidisland/listeners/AcidEffect.java | 4 +- .../acidisland/world/AcidBiomeProvider.java | 45 +++++++++ .../acidisland/world/ChunkGeneratorWorld.java | 91 +++++++++++++------ 4 files changed, 126 insertions(+), 38 deletions(-) create mode 100644 src/main/java/world/bentobox/acidisland/world/AcidBiomeProvider.java diff --git a/src/main/java/world/bentobox/acidisland/AcidIsland.java b/src/main/java/world/bentobox/acidisland/AcidIsland.java index 6b87884..8d75a15 100644 --- a/src/main/java/world/bentobox/acidisland/AcidIsland.java +++ b/src/main/java/world/bentobox/acidisland/AcidIsland.java @@ -5,8 +5,10 @@ import java.util.Objects; import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.World.Environment; +import org.bukkit.entity.SpawnCategory; import org.bukkit.WorldCreator; import org.bukkit.WorldType; +import org.bukkit.generator.BiomeProvider; import org.bukkit.generator.ChunkGenerator; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; @@ -14,6 +16,7 @@ import org.eclipse.jdt.annotation.Nullable; import world.bentobox.acidisland.commands.IslandAboutCommand; import world.bentobox.acidisland.listeners.AcidEffect; import world.bentobox.acidisland.listeners.LavaCheck; +import world.bentobox.acidisland.world.AcidBiomeProvider; import world.bentobox.acidisland.world.AcidTask; import world.bentobox.acidisland.world.ChunkGeneratorWorld; import world.bentobox.bentobox.api.addons.GameModeAddon; @@ -34,6 +37,7 @@ public class AcidIsland extends GameModeAddon { private @Nullable AcidTask acidTask; private @Nullable ChunkGenerator chunkGenerator; private final Config config = new Config<>(this, AISettings.class); + private BiomeProvider biomeProvider; private static final String NETHER = "_nether"; private static final String THE_END = "_the_end"; @@ -44,10 +48,11 @@ public class AcidIsland extends GameModeAddon { saveDefaultConfig(); // Load settings from config.yml. This will check if there are any issues with it too. loadSettings(); + // Make the biome provider + this.biomeProvider = new AcidBiomeProvider(this); // Chunk generator chunkGenerator = settings.isUseOwnGenerator() ? null : new ChunkGeneratorWorld(this); // Register commands - // Register commands playerCommand = new DefaultPlayerCommand(this) { @@ -72,6 +77,7 @@ public class AcidIsland extends GameModeAddon { } return false; } + return true; } @@ -147,22 +153,22 @@ public class AcidIsland extends GameModeAddon { // Set spawn rates if (w != null && getSettings() != null) { if (getSettings().getSpawnLimitMonsters() > 0) { - w.setMonsterSpawnLimit(getSettings().getSpawnLimitMonsters()); + w.setSpawnLimit(SpawnCategory.MONSTER, getSettings().getSpawnLimitMonsters()); } if (getSettings().getSpawnLimitAmbient() > 0) { - w.setAmbientSpawnLimit(getSettings().getSpawnLimitAmbient()); + w.setSpawnLimit(SpawnCategory.AMBIENT, getSettings().getSpawnLimitAmbient()); } if (getSettings().getSpawnLimitAnimals() > 0) { - w.setAnimalSpawnLimit(getSettings().getSpawnLimitAnimals()); + w.setSpawnLimit(SpawnCategory.ANIMAL, getSettings().getSpawnLimitAnimals()); } if (getSettings().getSpawnLimitWaterAnimals() > 0) { - w.setWaterAnimalSpawnLimit(getSettings().getSpawnLimitWaterAnimals()); + w.setSpawnLimit(SpawnCategory.WATER_ANIMAL, getSettings().getSpawnLimitWaterAnimals()); } if (getSettings().getTicksPerAnimalSpawns() > 0) { - w.setTicksPerAnimalSpawns(getSettings().getTicksPerAnimalSpawns()); + w.setTicksPerSpawns(SpawnCategory.ANIMAL, getSettings().getTicksPerAnimalSpawns()); } if (getSettings().getTicksPerMonsterSpawns() > 0) { - w.setTicksPerMonsterSpawns(getSettings().getTicksPerMonsterSpawns()); + w.setTicksPerSpawns(SpawnCategory.MONSTER, getSettings().getTicksPerMonsterSpawns()); } } return w; @@ -201,4 +207,8 @@ public class AcidIsland extends GameModeAddon { // Save settings. This will occur after all addons have loaded this.saveWorldSettings(); } + + public BiomeProvider getBiomeProvider() { + return this.biomeProvider; + } } diff --git a/src/main/java/world/bentobox/acidisland/listeners/AcidEffect.java b/src/main/java/world/bentobox/acidisland/listeners/AcidEffect.java index 7bdd39a..4d61814 100644 --- a/src/main/java/world/bentobox/acidisland/listeners/AcidEffect.java +++ b/src/main/java/world/bentobox/acidisland/listeners/AcidEffect.java @@ -93,7 +93,7 @@ public class AcidEffect implements Listener { public void onSeaBounce(PlayerMoveEvent e) { Player player = e.getPlayer(); if (!player.getGameMode().equals(GameMode.CREATIVE) && !player.getGameMode().equals(GameMode.SPECTATOR) - && player.getWorld().equals(addon.getOverWorld()) && player.getLocation().getBlockY() < 1) { + && player.getWorld().equals(addon.getOverWorld()) && player.getLocation().getBlockY() < player.getWorld().getMinHeight()) { player.setVelocity(new Vector(player.getVelocity().getX(), 1D, player.getVelocity().getZ())); } } @@ -326,7 +326,7 @@ public class AcidEffect implements Listener { if (im instanceof Damageable d) { d.setDamage(d.getDamage() + 1); - item.setItemMeta((ItemMeta) d); + item.setItemMeta(d); return d.getDamage() >= item.getType().getMaxDurability(); } return false; diff --git a/src/main/java/world/bentobox/acidisland/world/AcidBiomeProvider.java b/src/main/java/world/bentobox/acidisland/world/AcidBiomeProvider.java new file mode 100644 index 0000000..2e25176 --- /dev/null +++ b/src/main/java/world/bentobox/acidisland/world/AcidBiomeProvider.java @@ -0,0 +1,45 @@ +package world.bentobox.acidisland.world; + +import java.util.List; + +import org.bukkit.block.Biome; +import org.bukkit.generator.BiomeProvider; +import org.bukkit.generator.WorldInfo; + +import world.bentobox.acidisland.AcidIsland; + +/** + * Biome provider for AcidIsland + * @author tastybento + * + */ +public class AcidBiomeProvider extends BiomeProvider { + + private final AcidIsland addon; + + /** + * @param addon Addon + */ + public AcidBiomeProvider(AcidIsland addon) { + this.addon = addon; + } + + @Override + public Biome getBiome(WorldInfo worldInfo, int x, int y, int z) { + return switch(worldInfo.getEnvironment()) { + default -> addon.getSettings().getDefaultBiome(); + case NETHER -> addon.getSettings().getDefaultNetherBiome(); + case THE_END -> addon.getSettings().getDefaultEndBiome(); + }; + } + + @Override + public List getBiomes(WorldInfo worldInfo) { + return switch(worldInfo.getEnvironment()) { + default -> List.of(addon.getSettings().getDefaultBiome()); + case NETHER -> List.of(addon.getSettings().getDefaultNetherBiome()); + case THE_END -> List.of(addon.getSettings().getDefaultEndBiome()); + }; + } + +} diff --git a/src/main/java/world/bentobox/acidisland/world/ChunkGeneratorWorld.java b/src/main/java/world/bentobox/acidisland/world/ChunkGeneratorWorld.java index 620b991..8f6fb0f 100644 --- a/src/main/java/world/bentobox/acidisland/world/ChunkGeneratorWorld.java +++ b/src/main/java/world/bentobox/acidisland/world/ChunkGeneratorWorld.java @@ -10,11 +10,13 @@ import java.util.Random; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.World.Environment; -import org.bukkit.block.Biome; +import org.bukkit.generator.BiomeProvider; import org.bukkit.generator.BlockPopulator; import org.bukkit.generator.ChunkGenerator; +import org.bukkit.generator.WorldInfo; import org.bukkit.util.Vector; import org.bukkit.util.noise.PerlinOctaveGenerator; +import org.eclipse.jdt.annotation.NonNull; import world.bentobox.acidisland.AcidIsland; @@ -29,7 +31,8 @@ public class ChunkGeneratorWorld extends ChunkGenerator { private final Random rand = new Random(); private final Map seaHeight = new EnumMap<>(Environment.class); private final Map roofChunk = new HashMap<>(); - + private PerlinOctaveGenerator gen; + private record WorldConfig(int seaHeight, Material waterBlock) {} /** @@ -41,38 +44,69 @@ public class ChunkGeneratorWorld extends ChunkGenerator { seaHeight.put(Environment.NORMAL, new WorldConfig(addon.getSettings().getSeaHeight(), addon.getSettings().getWaterBlock())); seaHeight.put(Environment.NETHER, new WorldConfig(addon.getSettings().getNetherSeaHeight(), addon.getSettings().getNetherWaterBlock())); seaHeight.put(Environment.THE_END, new WorldConfig(addon.getSettings().getEndSeaHeight(), addon.getSettings().getEndWaterBlock())); + rand.setSeed(System.currentTimeMillis()); + gen = new PerlinOctaveGenerator((long) (rand.nextLong() * rand.nextGaussian()), 8); + gen.setScale(1.0/30.0); makeNetherRoof(); } - public ChunkData generateChunks(World world) { - ChunkData result = createChunkData(world); - WorldConfig wc = seaHeight.get(world.getEnvironment()); - int sh = wc.seaHeight(); - if (sh > world.getMinHeight()) { - result.setRegion(0, world.getMinHeight(), 0, 16, sh + 1, 16, wc.waterBlock()); - } - if (world.getEnvironment().equals(Environment.NETHER) && addon.getSettings().isNetherRoof()) { - roofChunk.forEach((k,v) -> result.setBlock(k.getBlockX(), world.getMaxHeight() + k.getBlockY(), k.getBlockZ(), v)); - } - return result; - } - @Override - public ChunkData generateChunkData(World world, Random random, int chunkX, int chunkZ, BiomeGrid biomeGrid) { - setBiome(world, biomeGrid); - return generateChunks(world); - } - - private void setBiome(World world, BiomeGrid biomeGrid) { - Biome biome = world.getEnvironment() == Environment.NORMAL ? addon.getSettings().getDefaultBiome() : - world.getEnvironment() == Environment.NETHER ? addon.getSettings().getDefaultNetherBiome() : addon.getSettings().getDefaultEndBiome(); - for (int x = 0; x < 16; x+=4) { - for (int z = 0; z < 16; z+=4) { - for (int y = world.getMinHeight(); y < world.getMaxHeight(); y+=4) { - biomeGrid.setBiome(x, y, z, biome); + public void generateNoise(@NonNull WorldInfo worldInfo, @NonNull Random random, int chunkX, int chunkZ, @NonNull ChunkData chunkData) { + WorldConfig wc = seaHeight.get(worldInfo.getEnvironment()); + int sh = wc.seaHeight(); + if (sh > worldInfo.getMinHeight()) { + chunkData.setRegion(0, worldInfo.getMinHeight(), 0, 16, worldInfo.getMinHeight() + 1, 16, Material.BEDROCK); + chunkData.setRegion(0, worldInfo.getMinHeight() + 1, 0, 16, sh + 1, 16, wc.waterBlock()); + // Add some noise + for (int x = 0; x < 16; x++) { + for (int z = 0; z < 16; z++) { + int n = (int)(25 * gen.noise((chunkX << 4) + x, (chunkZ << 4) + z, 0.5, 0.5, true)); + for (int y = worldInfo.getMinHeight(); y < 25 + n; y++) { + chunkData.setBlock(x, y, z, rand.nextBoolean() ? Material.SAND : Material.SANDSTONE); } } } + + + } + if (worldInfo.getEnvironment().equals(Environment.NETHER) && addon.getSettings().isNetherRoof()) { + roofChunk.forEach((k,v) -> chunkData.setBlock(k.getBlockX(), worldInfo.getMaxHeight() + k.getBlockY(), k.getBlockZ(), v)); + } + } + + @Override + public boolean shouldGenerateBedrock() { + return true; + } + + @Override + public boolean shouldGenerateNoise() { + return false; + } + @Override + public boolean shouldGenerateSurface() { + return true; + } + @Override + public boolean shouldGenerateCaves() { + return true; + } + @Override + public boolean shouldGenerateDecorations() { + return true; + } + @Override + public boolean shouldGenerateMobs() { + return true; + } + @Override + public boolean shouldGenerateStructures() { + return true; + } + + @Override + public BiomeProvider getDefaultBiomeProvider(WorldInfo worldInfo) { + return addon.getBiomeProvider(); } // This needs to be set to return true to override minecraft's default @@ -91,8 +125,6 @@ public class ChunkGeneratorWorld extends ChunkGenerator { * Nether Section */ private void makeNetherRoof() { - rand.setSeed(System.currentTimeMillis()); - PerlinOctaveGenerator gen = new PerlinOctaveGenerator((long) (rand.nextLong() * rand.nextGaussian()), 8); // Make the roof - common across the world for (int x = 0; x < 16; x++) { @@ -161,4 +193,5 @@ public class ChunkGeneratorWorld extends ChunkGenerator { private void setBlock(int x, int y, int z, Material m) { roofChunk.put(new Vector(x, y, z), m); } + } From b703e911297dbe0cadadbf0c0b553a3aada17f4b Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 5 Jan 2023 21:46:21 -0800 Subject: [PATCH 3/3] Fix tests --- .../acidisland/listeners/AcidEffectTest.java | 3 +- .../world/ChunkGeneratorWorldTest.java | 144 ------------------ 2 files changed, 2 insertions(+), 145 deletions(-) diff --git a/src/test/java/world/bentobox/acidisland/listeners/AcidEffectTest.java b/src/test/java/world/bentobox/acidisland/listeners/AcidEffectTest.java index 99aab9d..c5cd966 100644 --- a/src/test/java/world/bentobox/acidisland/listeners/AcidEffectTest.java +++ b/src/test/java/world/bentobox/acidisland/listeners/AcidEffectTest.java @@ -138,7 +138,7 @@ public class AcidEffectTest { when(inv.getArmorContents()).thenReturn(armor); // Location - when(location.getBlockY()).thenReturn(0); + when(location.getBlockY()).thenReturn(-66); when(location.getWorld()).thenReturn(world); when(location.getBlock()).thenReturn(block); @@ -172,6 +172,7 @@ public class AcidEffectTest { when(world.hasStorm()).thenReturn(true); when(world.getBlockAt(anyInt(), anyInt(), anyInt())).thenReturn(airBlock); when(world.getMaxHeight()).thenReturn(5); + when(world.getMinHeight()).thenReturn(-65); when(world.getEnvironment()).thenReturn(Environment.NORMAL); // Plugin diff --git a/src/test/java/world/bentobox/acidisland/world/ChunkGeneratorWorldTest.java b/src/test/java/world/bentobox/acidisland/world/ChunkGeneratorWorldTest.java index d3c5d2c..fc7239f 100644 --- a/src/test/java/world/bentobox/acidisland/world/ChunkGeneratorWorldTest.java +++ b/src/test/java/world/bentobox/acidisland/world/ChunkGeneratorWorldTest.java @@ -1,26 +1,14 @@ package world.bentobox.acidisland.world; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyInt; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.atLeast; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import java.util.Random; - import org.bukkit.Bukkit; -import org.bukkit.Material; import org.bukkit.Server; import org.bukkit.World; -import org.bukkit.generator.ChunkGenerator.BiomeGrid; import org.bukkit.generator.ChunkGenerator.ChunkData; -import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -45,9 +33,6 @@ public class ChunkGeneratorWorldTest { private ChunkGeneratorWorld cg; @Mock private World world; - private final Random random = new Random(); - @Mock - private BiomeGrid biomeGrid; private AISettings settings; @Mock private ChunkData data; @@ -69,135 +54,6 @@ public class ChunkGeneratorWorldTest { when(addon.getSettings()).thenReturn(settings); } - - /** - * Test method for {@link world.bentobox.bskyblock.generators.ChunkGeneratorWorld#generateChunkData(org.bukkit.World, java.util.Random, int, int, org.bukkit.generator.ChunkGenerator.BiomeGrid)}. - */ - @SuppressWarnings("deprecation") - @Test - public void testGenerateChunkDataWorldRandomIntIntBiomeGridOverworldNormal() { - // Instance - cg = new ChunkGeneratorWorld(addon); - ChunkData cd = cg.generateChunkData(world, random, 0 , 0 , biomeGrid); - assertEquals(data, cd); - // Verifications - // Default biome - verify(biomeGrid, times(1024)).setBiome(anyInt(), anyInt(), anyInt(), any()); - // Void - verify(cd).setRegion(0, 0, 0, 16, 55, 16, Material.WATER); - } - - /** - * Test method for {@link world.bentobox.bskyblock.generators.ChunkGeneratorWorld#generateChunkData(org.bukkit.World, java.util.Random, int, int, org.bukkit.generator.ChunkGenerator.BiomeGrid)}. - */ - @SuppressWarnings("deprecation") - @Test - public void testGenerateChunkDataWorldRandomIntIntBiomeGridOverworldNormalVoid() { - settings.setSeaHeight(0); - // Instance - cg = new ChunkGeneratorWorld(addon); - ChunkData cd = cg.generateChunkData(world, random, 0 , 0 , biomeGrid); - assertEquals(data, cd); - // Verifications - // Default biome - verify(biomeGrid, times(1024)).setBiome(anyInt(), anyInt(), anyInt(), any()); - // Void - verify(cd, never()).setRegion(anyInt(), anyInt(),anyInt(),anyInt(),anyInt(),anyInt(), any(Material.class)); - } - - /** - * Test method for {@link world.bentobox.bskyblock.generators.ChunkGeneratorWorld#generateChunkData(org.bukkit.World, java.util.Random, int, int, org.bukkit.generator.ChunkGenerator.BiomeGrid)}. - */ - @SuppressWarnings("deprecation") - @Test - public void testGenerateChunkDataWorldRandomIntIntBiomeGridOverworldVoidLava() { - settings.setSeaHeight(54); - settings.setWaterBlock(Material.LAVA); - cg = new ChunkGeneratorWorld(addon); - ChunkData cd = cg.generateChunkData(world, random, 0 , 0 , biomeGrid); - assertEquals(data, cd); - // Verifications - // Default biome - verify(biomeGrid, times(1024)).setBiome(anyInt(), anyInt(), anyInt(), any()); - // Void - verify(cd).setRegion(0, 0, 0, 16, 55, 16, Material.LAVA); - } - - /** - * Test method for {@link world.bentobox.bskyblock.generators.ChunkGeneratorWorld#generateChunkData(org.bukkit.World, java.util.Random, int, int, org.bukkit.generator.ChunkGenerator.BiomeGrid)}. - */ - @SuppressWarnings("deprecation") - @Test - public void testGenerateChunkDataWorldRandomIntIntBiomeGridOverworldSea() { - // Set sea height - settings.setSeaHeight(10); - // new instance - cg = new ChunkGeneratorWorld(addon); - ChunkData cd = cg.generateChunkData(world, random, 0 , 0 , biomeGrid); - assertEquals(data, cd); - // Verifications - // Default biome - verify(biomeGrid, times(1024)).setBiome(anyInt(), anyInt(), anyInt(), any()); - // Water. Blocks = 16 x 16 x 11 because block 0 - verify(cd).setRegion(0, 0, 0, 16, 11, 16, Material.WATER); - } - - /** - * Test method for {@link world.bentobox.bskyblock.generators.ChunkGeneratorWorld#generateChunkData(org.bukkit.World, java.util.Random, int, int, org.bukkit.generator.ChunkGenerator.BiomeGrid)}. - */ - @SuppressWarnings("deprecation") - @Test - public void testGenerateChunkDataWorldRandomIntIntBiomeGridEnd() { - settings.setEndSeaHeight(0); - // Instance - cg = new ChunkGeneratorWorld(addon); - when(world.getEnvironment()).thenReturn(World.Environment.THE_END); - ChunkData cd = cg.generateChunkData(world, random, 0 , 0 , biomeGrid); - assertEquals(data, cd); - // Verifications - // Set biome in end - verify(biomeGrid, times(1024)).setBiome(anyInt(), anyInt(), anyInt(), any()); - // Void - verify(cd, never()).setRegion(anyInt(), anyInt(),anyInt(),anyInt(),anyInt(),anyInt(), any(Material.class)); - } - - /** - * Test method for {@link world.bentobox.bskyblock.generators.ChunkGeneratorWorld#generateChunkData(org.bukkit.World, java.util.Random, int, int, org.bukkit.generator.ChunkGenerator.BiomeGrid)}. - */ - @SuppressWarnings("deprecation") - @Test - public void testGenerateChunkDataWorldRandomIntIntBiomeGridNetherWithRoof() { - // Instance - cg = new ChunkGeneratorWorld(addon); - when(world.getEnvironment()).thenReturn(World.Environment.NETHER); - ChunkData cd = cg.generateChunkData(world, random, 0 , 0 , biomeGrid); - assertEquals(data, cd); - // Verifications - // Never set biome in nether - verify(biomeGrid, times(1024)).setBiome(anyInt(), anyInt(), anyInt(), any()); - // Nether roof - at least bedrock layer - verify(cd, atLeast(16 * 16)).setBlock(anyInt(), anyInt(), anyInt(), eq(Material.BEDROCK)); - } - - /** - * Test method for {@link world.bentobox.bskyblock.generators.ChunkGeneratorWorld#generateChunkData(org.bukkit.World, java.util.Random, int, int, org.bukkit.generator.ChunkGenerator.BiomeGrid)}. - */ - @SuppressWarnings("deprecation") - @Test - public void testGenerateChunkDataWorldRandomIntIntBiomeGridNetherNoRoof() { - settings.setNetherRoof(false); - // Instance - cg = new ChunkGeneratorWorld(addon); - when(world.getEnvironment()).thenReturn(World.Environment.NETHER); - ChunkData cd = cg.generateChunkData(world, random, 0 , 0 , biomeGrid); - assertEquals(data, cd); - // Verifications - // Never set biome in nether - verify(biomeGrid, times(1024)).setBiome(anyInt(), anyInt(), anyInt(), any()); - // Nether roof - at least bedrock layer - verify(cd, never()).setBlock(anyInt(), anyInt(), anyInt(), any(Material.class)); - } - /** * Test method for {@link world.bentobox.bskyblock.generators.ChunkGeneratorWorld#canSpawn(org.bukkit.World, int, int)}. */