Skip to content

Commit

Permalink
Fixes mob spawning in water.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Aug 26, 2020
1 parent 9d85e37 commit 26ef3d3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private void growPlants(Greenhouse gh) {
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 ));
}
Expand Down Expand Up @@ -190,9 +190,10 @@ List<Block> getAvailableBlocks(Greenhouse gh, boolean ignoreLiquid) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,20 +61,28 @@ public void setUp() throws Exception {
// 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);
}

Expand Down Expand Up @@ -143,7 +143,8 @@ public void testGetAvailableBlocksAllPlant() {
when(plant.getRelative(eq(BlockFace.UP))).thenReturn(plant);
when(world.getBlockAt(anyInt(), anyInt(), anyInt())).thenReturn(plant);
List<Block> result = eco.getAvailableBlocks(gh, false);
assertEquals(0, result.size());
assertEquals(16, result.size());
assertEquals(plant, result.get(0));
}

/**
Expand All @@ -161,9 +162,35 @@ public void testGetAvailableBlocksLiquidAboveBlockIgnoreLiquids() {
* 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<Block> 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<Block> result = eco.getAvailableBlocks(gh, true);
assertEquals(16, result.size());
for (int i = 0; i< result.size(); i++) {
assertEquals(liquid, result.get(i));
}
}
}

0 comments on commit 26ef3d3

Please sign in to comment.