Merge branch 'update_chunk_gen' into develop

Conflicts:
	src/main/java/world/bentobox/acidisland/AcidIsland.java
This commit is contained in:
tastybento 2023-02-05 14:14:38 -08:00
commit 177f171dfc
8 changed files with 125 additions and 181 deletions

View File

@ -59,7 +59,7 @@
<powermock.version>2.0.9</powermock.version>
<!-- More visible way how to change dependency versions -->
<spigot.version>1.19.3-R0.1-SNAPSHOT</spigot.version>
<bentobox.version>1.21.0</bentobox.version>
<bentobox.version>1.22.0-SNAPSHOT</bentobox.version>
<!-- Revision variable removes warning about dynamic version -->
<revision>${build.version}-SNAPSHOT</revision>
<!-- Do not change unless you want different name for local builds. -->

View File

@ -8,6 +8,7 @@ import org.bukkit.World.Environment;
import org.bukkit.WorldCreator;
import org.bukkit.WorldType;
import org.bukkit.entity.SpawnCategory;
import org.bukkit.generator.BiomeProvider;
import org.bukkit.generator.ChunkGenerator;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
@ -15,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;
@ -35,6 +37,7 @@ public class AcidIsland extends GameModeAddon {
private @Nullable AcidTask acidTask;
private @Nullable ChunkGenerator chunkGenerator;
private final Config<AISettings> config = new Config<>(this, AISettings.class);
private BiomeProvider biomeProvider;
private static final String NETHER = "_nether";
private static final String THE_END = "_the_end";
@ -45,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)
{
@ -73,6 +77,7 @@ public class AcidIsland extends GameModeAddon {
}
return false;
}
return true;
}
@ -154,10 +159,10 @@ public class AcidIsland extends GameModeAddon {
w.setSpawnLimit(SpawnCategory.AMBIENT, getSettings().getSpawnLimitAmbient());
}
if (getSettings().getSpawnLimitAnimals() > 0) {
w.setSpawnLimit(SpawnCategory.ANIMAL, getSettings().getSpawnLimitAnimals());
w.setSpawnLimit(SpawnCategory.ANIMAL, getSettings().getSpawnLimitAnimals());
}
if (getSettings().getSpawnLimitWaterAnimals() > 0) {
w.setSpawnLimit(SpawnCategory.WATER_ANIMAL, getSettings().getSpawnLimitWaterAnimals());
w.setSpawnLimit(SpawnCategory.WATER_ANIMAL, getSettings().getSpawnLimitWaterAnimals());
}
if (getSettings().getTicksPerAnimalSpawns() > 0) {
w.setTicksPerSpawns(SpawnCategory.ANIMAL, getSettings().getTicksPerAnimalSpawns());
@ -202,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;
}
}

View File

@ -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;

View File

@ -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<Biome> 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());
};
}
}

View File

@ -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<Environment, WorldConfig> seaHeight = new EnumMap<>(Environment.class);
private final Map<Vector, Material> 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);
}
}

View File

@ -1,7 +1,7 @@
name: AcidIsland
main: world.bentobox.acidisland.AcidIsland
version: ${version}${build.number}
api-version: 1.19
api-version: 1.22
metrics: true
repository: "BentoBoxWorld/AcidIsland"
icon: "OAK_BOAT"

View File

@ -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

View File

@ -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)}.
*/