From 26ef3d3a249d4187ada4ee6cabfdb103d845b7cb Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 25 Aug 2020 18:04:53 -0700 Subject: [PATCH] Fixes mob spawning in water. https://github.com/BentoBoxWorld/Greenhouses/issues/62 --- .../managers/EcoSystemManager.java | 9 +-- .../managers/EcoSystemManagerTest.java | 55 ++++++++++++++----- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/main/java/world/bentobox/greenhouses/managers/EcoSystemManager.java b/src/main/java/world/bentobox/greenhouses/managers/EcoSystemManager.java index 8cd4c48..48f516e 100644 --- a/src/main/java/world/bentobox/greenhouses/managers/EcoSystemManager.java +++ b/src/main/java/world/bentobox/greenhouses/managers/EcoSystemManager.java @@ -154,7 +154,7 @@ public class EcoSystemManager { int bonemeal = getBoneMeal(gh); if (bonemeal > 0) { // Get a list of all available blocks - int plantsGrown = getAvailableBlocks(gh, false).stream().limit(bonemeal).mapToInt(bl -> gh.getBiomeRecipe().growPlant(bl) ? 1 : 0).sum(); + int plantsGrown = getAvailableBlocks(gh, true).stream().limit(bonemeal).mapToInt(bl -> gh.getBiomeRecipe().growPlant(bl) ? 1 : 0).sum(); if (plantsGrown > 0) { setBoneMeal(gh, bonemeal - (int)Math.ceil((double)plantsGrown / PLANTS_PER_BONEMEAL )); } @@ -190,9 +190,10 @@ 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--) { Block b = gh.getWorld().getBlockAt(x, y, z); - if ((!b.isEmpty() && !b.isPassable()) - && (b.getRelative(BlockFace.UP).isEmpty() || b.getRelative(BlockFace.UP).isPassable() - || (ignoreLiquid && b.getRelative(BlockFace.UP).isLiquid()))) { + 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()))) { result.add(b.getRelative(BlockFace.UP)); break; } diff --git a/src/test/java/world/bentobox/greenhouses/managers/EcoSystemManagerTest.java b/src/test/java/world/bentobox/greenhouses/managers/EcoSystemManagerTest.java index 07f5724..e0a9ada 100644 --- a/src/test/java/world/bentobox/greenhouses/managers/EcoSystemManagerTest.java +++ b/src/test/java/world/bentobox/greenhouses/managers/EcoSystemManagerTest.java @@ -3,17 +3,9 @@ */ package world.bentobox.greenhouses.managers; -import static org.junit.Assert.*; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyDouble; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.util.List; @@ -69,20 +61,28 @@ public class EcoSystemManagerTest { // World when(gh.getWorld()).thenReturn(world); when(world.getBlockAt(anyInt(), anyInt(), anyInt())).thenReturn(block); - // Block + // Blocks // Air + // Liquid false when(air.isEmpty()).thenReturn(true); when(air.isPassable()).thenReturn(true); when(air.getRelative(eq(BlockFace.UP))).thenReturn(air); // Plant + // Empty false + // Liquid false when(plant.isPassable()).thenReturn(true); when(plant.getRelative(eq(BlockFace.UP))).thenReturn(air); // Liquid + // Empty false when(liquid.isLiquid()).thenReturn(true); - when(liquid.getRelative(eq(BlockFace.UP))).thenReturn(air); + when(liquid.isPassable()).thenReturn(true); + when(liquid.getRelative(eq(BlockFace.UP))).thenReturn(air); // Default for block + // Empty false + // Passable false + // Liquid false when(block.getRelative(eq(BlockFace.UP))).thenReturn(air); - + eco = new EcoSystemManager(null, null); } @@ -143,7 +143,8 @@ public class EcoSystemManagerTest { when(plant.getRelative(eq(BlockFace.UP))).thenReturn(plant); when(world.getBlockAt(anyInt(), anyInt(), anyInt())).thenReturn(plant); List result = eco.getAvailableBlocks(gh, false); - assertEquals(0, result.size()); + assertEquals(16, result.size()); + assertEquals(plant, result.get(0)); } /** @@ -161,9 +162,35 @@ public class EcoSystemManagerTest { * Test method for {@link world.bentobox.greenhouses.managers.EcoSystemManager#getAvailableBlocks(world.bentobox.greenhouses.data.Greenhouse)}. */ @Test - public void testGetAvailableBlocksLiquidAboveBlock() { + public void testGetAvailableBlocksAirAboveLiquidNotIgnoreLiquids() { + when(world.getBlockAt(anyInt(), eq(3), anyInt())).thenReturn(air); + when(world.getBlockAt(anyInt(), eq(2), anyInt())).thenReturn(liquid); + when(world.getBlockAt(anyInt(), eq(1), anyInt())).thenReturn(block); + when(liquid.getRelative(eq(BlockFace.UP))).thenReturn(air); when(block.getRelative(eq(BlockFace.UP))).thenReturn(liquid); + List result = eco.getAvailableBlocks(gh, false); - assertEquals(0, result.size()); + assertEquals(16, result.size()); + for (int i = 0; i< result.size(); i++) { + assertEquals(air, result.get(i)); + } + } + + /** + * Test method for {@link world.bentobox.greenhouses.managers.EcoSystemManager#getAvailableBlocks(world.bentobox.greenhouses.data.Greenhouse)}. + */ + @Test + public void testGetAvailableBlocksAirAboveLiquidIgnoreLiquids() { + when(world.getBlockAt(anyInt(), eq(3), anyInt())).thenReturn(air); + when(world.getBlockAt(anyInt(), eq(2), anyInt())).thenReturn(liquid); + when(world.getBlockAt(anyInt(), eq(1), anyInt())).thenReturn(block); + when(liquid.getRelative(eq(BlockFace.UP))).thenReturn(air); + when(block.getRelative(eq(BlockFace.UP))).thenReturn(liquid); + + List result = eco.getAvailableBlocks(gh, true); + assertEquals(16, result.size()); + for (int i = 0; i< result.size(); i++) { + assertEquals(liquid, result.get(i)); + } } }