From 0bbc25cfd14ec261f7c803e21478ca8204c47dfe Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 15 Nov 2020 14:38:19 -0800 Subject: [PATCH] Blocks only convert based on blocks inside the greenhouse. Fixes https://github.com/BentoBoxWorld/Greenhouses/issues/63 --- .../greenhouses/greenhouse/BiomeRecipe.java | 8 ++++-- .../managers/EcoSystemManager.java | 8 +++--- .../greenhouse/BiomeRecipeTest.java | 28 ++++++++++++++++--- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/main/java/world/bentobox/greenhouses/greenhouse/BiomeRecipe.java b/src/main/java/world/bentobox/greenhouses/greenhouse/BiomeRecipe.java index 588c453..4465e5b 100644 --- a/src/main/java/world/bentobox/greenhouses/greenhouse/BiomeRecipe.java +++ b/src/main/java/world/bentobox/greenhouses/greenhouse/BiomeRecipe.java @@ -214,14 +214,18 @@ public class BiomeRecipe implements Comparable { /** * 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()); } diff --git a/src/main/java/world/bentobox/greenhouses/managers/EcoSystemManager.java b/src/main/java/world/bentobox/greenhouses/managers/EcoSystemManager.java index 48f516e..ab430e9 100644 --- a/src/main/java/world/bentobox/greenhouses/managers/EcoSystemManager.java +++ b/src/main/java/world/bentobox/greenhouses/managers/EcoSystemManager.java @@ -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; } diff --git a/src/test/java/world/bentobox/greenhouses/greenhouse/BiomeRecipeTest.java b/src/test/java/world/bentobox/greenhouses/greenhouse/BiomeRecipeTest.java index 240698d..22524f0 100644 --- a/src/test/java/world/bentobox/greenhouses/greenhouse/BiomeRecipeTest.java +++ b/src/test/java/world/bentobox/greenhouses/greenhouse/BiomeRecipeTest.java @@ -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); }