Skip to content

Commit

Permalink
Blocks only convert based on blocks inside the greenhouse.
Browse files Browse the repository at this point in the history
Fixes #63
  • Loading branch information
tastybento committed Nov 15, 2020
1 parent 2260e21 commit 0bbc25c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,18 @@ public Set<GreenhouseResult> checkRecipe(Greenhouse gh) {

/**
* 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private void convertBlocks(Greenhouse gh) {
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);
}
}
}
Expand Down Expand Up @@ -191,9 +191,9 @@ List<Block> getAvailableBlocks(Greenhouse gh, boolean ignoreLiquid) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public void setUp() throws Exception {
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,
Expand Down Expand Up @@ -246,10 +247,29 @@ public void testConvertBlock() {
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)}.
*/
Expand All @@ -263,7 +283,7 @@ public void testConvertBlockNoWater() {
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);
}

Expand All @@ -275,7 +295,7 @@ public void testConvertBlockNoConverts() {
// 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);
}

Expand All @@ -297,7 +317,7 @@ public void testConvertBlockNoProbability() {
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);
}

Expand Down

0 comments on commit 0bbc25c

Please sign in to comment.