Blocks only convert based on blocks inside the greenhouse.

Fixes https://github.com/BentoBoxWorld/Greenhouses/issues/63
This commit is contained in:
tastybento 2020-11-15 14:38:19 -08:00
parent 2260e2176d
commit 0bbc25cfd1
3 changed files with 34 additions and 10 deletions

View File

@ -214,14 +214,18 @@ public class BiomeRecipe implements Comparable<BiomeRecipe> {
/**
* Check if block should be converted
* @param gh - greenhouse
* @param b - block to check
*/
public void convertBlock(Block b) {
public void convertBlock(Greenhouse gh, Block b) {
conversionBlocks.get(b.getType()).stream().filter(Objects::nonNull)
.filter(bc -> random.nextDouble() < bc.getProbability())
.forEach(bc -> {
// Check if the block is in the right area, up, down, n,s,e,w
if (ADJ_BLOCKS.stream().map(b::getRelative).map(Block::getType).anyMatch(m -> bc.getLocalMaterial() == null || m == bc.getLocalMaterial())) {
if (ADJ_BLOCKS.stream().map(b::getRelative)
.filter(r -> gh.contains(r.getLocation()))
.map(Block::getType)
.anyMatch(m -> bc.getLocalMaterial() == null || m == bc.getLocalMaterial())) {
// Convert!
b.setType(bc.getNewMaterial());
}

View File

@ -89,7 +89,7 @@ public class EcoSystemManager {
for (int z = (int)gh.getBoundingBox().getMinZ() + 1; z < (int)gh.getBoundingBox().getMaxZ(); z++) {
for (int y = (int)gh.getBoundingBox().getMaxY() - 2; y >= (int)gh.getBoundingBox().getMinY() && y > 0; y--) {
Block b = gh.getWorld().getBlockAt(x, y, z).getRelative(BlockFace.DOWN);
if (!b.isEmpty()) gh.getBiomeRecipe().convertBlock(b);
if (!b.isEmpty()) gh.getBiomeRecipe().convertBlock(gh, b);
}
}
}
@ -191,9 +191,9 @@ public class EcoSystemManager {
for (int y = (int)gh.getBoundingBox().getMaxY() - 2; y >= (int)gh.getBoundingBox().getMinY(); y--) {
Block b = gh.getWorld().getBlockAt(x, y, z);
if (!(b.isEmpty() || (ignoreLiquid && b.isLiquid()))
&& (b.getRelative(BlockFace.UP).isEmpty()
|| (b.getRelative(BlockFace.UP).isPassable() && !b.isLiquid())
|| (ignoreLiquid && b.isLiquid() && b.getRelative(BlockFace.UP).isPassable()))) {
&& (b.getRelative(BlockFace.UP).isEmpty()
|| (b.getRelative(BlockFace.UP).isPassable() && !b.isLiquid())
|| (ignoreLiquid && b.isLiquid() && b.getRelative(BlockFace.UP).isPassable()))) {
result.add(b.getRelative(BlockFace.UP));
break;
}

View File

@ -99,6 +99,7 @@ public class BiomeRecipeTest {
bb = new BoundingBox(10, 100, 10, 20, 120, 20);
when(gh.getBoundingBox()).thenReturn(bb);
when(gh.getWorld()).thenReturn(world);
when(gh.contains(any())).thenReturn(true);
when(world.getBlockAt(anyInt(), anyInt(), anyInt())).thenReturn(block);
// Block
when(block.getType()).thenReturn(Material.AIR,
@ -246,10 +247,29 @@ public class BiomeRecipeTest {
Block ab = mock(Block.class);
when(ab.getType()).thenReturn(Material.WATER);
when(b.getRelative(any())).thenReturn(ab);
br.convertBlock(b);
br.convertBlock(gh, b);
verify(b).setType(Material.CLAY);
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#convertBlock(org.bukkit.block.Block)}.
*/
@Test
public void testConvertBlockNotInGreenhouse() {
// Setup
this.testAddConvBlocks();
// Mock
Block b = mock(Block.class);
when(b.getType()).thenReturn(Material.SAND);
Block ab = mock(Block.class);
when(ab.getType()).thenReturn(Material.WATER);
when(b.getRelative(any())).thenReturn(ab);
when(ab.getLocation()).thenReturn(location);
when(gh.contains(any())).thenReturn(false);
br.convertBlock(gh, b);
verify(b, never()).setType(any());
}
/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.BiomeRecipe#convertBlock(org.bukkit.block.Block)}.
*/
@ -263,7 +283,7 @@ public class BiomeRecipeTest {
Block ab = mock(Block.class);
when(ab.getType()).thenReturn(Material.SAND);
when(b.getRelative(any())).thenReturn(ab);
br.convertBlock(b);
br.convertBlock(gh, b);
verify(b, never()).setType(Material.CLAY);
}
@ -275,7 +295,7 @@ public class BiomeRecipeTest {
// Mock
Block b = mock(Block.class);
when(b.getType()).thenReturn(Material.SAND);
br.convertBlock(b);
br.convertBlock(gh, b);
verify(b, never()).setType(Material.CLAY);
}
@ -297,7 +317,7 @@ public class BiomeRecipeTest {
Block ab = mock(Block.class);
when(ab.getType()).thenReturn(Material.WATER);
when(b.getRelative(any())).thenReturn(ab);
br.convertBlock(b);
br.convertBlock(gh, b);
verify(b, never()).setType(Material.CLAY);
}