Skip to content

Commit

Permalink
Prevent pistons pulling blocks out.
Browse files Browse the repository at this point in the history
Allow piston pushing of blocks above or below the greenhouse because
biomes are 3D now.

#77
  • Loading branch information
tastybento committed Feb 19, 2021
1 parent 999ea07 commit 19cd685
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPistonExtendEvent;
import org.bukkit.event.player.PlayerBucketEmptyEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
Expand Down Expand Up @@ -169,21 +168,5 @@ public void onBlockBreak(final BlockBreakEvent e) {
});
}

private boolean checkBlockHeight(Block block) {
return addon.getManager().getMap().getGreenhouse(block.getLocation())
.filter(g -> g.getCeilingHeight() < block.getY())
.filter(g -> !block.getWorld().getEnvironment().equals(World.Environment.NETHER))
.isPresent();

}

/**
* Check to see if anyone is sneaking a block over a greenhouse by using a piston
* @param e - event
*/
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled=true)
public void onPistonPush(final BlockPistonExtendEvent e) {
e.setCancelled(e.getBlocks().stream().anyMatch(this::checkBlockHeight));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockFromToEvent;
import org.bukkit.event.block.BlockPistonExtendEvent;
import org.bukkit.event.block.BlockPistonRetractEvent;

import world.bentobox.greenhouses.Greenhouses;
import world.bentobox.greenhouses.data.Greenhouse;
Expand Down Expand Up @@ -68,6 +69,19 @@ public void onPistonPush(BlockPistonExtendEvent e) {
.isPresent());
}

/**
* Prevents sticky pistons from pulling greenhouse wall or roof blocks
* @param e - event
*/
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onPistonPull(BlockPistonRetractEvent e) {
e.setCancelled(e.getBlocks().stream()
.map(Block::getLocation)
.filter(this::inGreenhouse)
.findFirst()
.isPresent());
}

private boolean inGreenhouse(Location l) {
return addon.getManager().getMap().getGreenhouse(l).map(g -> g.isRoofOrWallBlock(l)).orElse(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Collections;
import java.util.Optional;

import org.bukkit.Bukkit;
Expand All @@ -24,7 +23,6 @@
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPistonExtendEvent;
import org.bukkit.event.player.PlayerBucketEmptyEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
Expand Down Expand Up @@ -439,34 +437,4 @@ public void testOnBlockBreak() {
verify(gm).removeGreenhouse(any());
}

/**
* Test method for {@link world.bentobox.greenhouses.listeners.GreenhouseEvents#onPistonPush(org.bukkit.event.block.BlockPistonExtendEvent)}.
*/
@Test
public void testOnPistonPush() {
Block block = mock(Block.class);
when(block.getLocation()).thenReturn(location);
when(block.getY()).thenReturn(255);
when(block.getWorld()).thenReturn(world);
when(world.getEnvironment()).thenReturn(Environment.NORMAL);
BlockPistonExtendEvent e = new BlockPistonExtendEvent(block, Collections.singletonList(block), BlockFace.EAST);
ghe.onPistonPush(e);
assertTrue(e.isCancelled());
}

/**
* Test method for {@link world.bentobox.greenhouses.listeners.GreenhouseEvents#onPistonPush(org.bukkit.event.block.BlockPistonExtendEvent)}.
*/
@Test
public void testOnPistonPushUnderGH() {
Block block = mock(Block.class);
when(block.getLocation()).thenReturn(location);
when(block.getY()).thenReturn(0);
when(block.getWorld()).thenReturn(world);
when(world.getEnvironment()).thenReturn(Environment.NORMAL);
BlockPistonExtendEvent e = new BlockPistonExtendEvent(block, Collections.singletonList(block), BlockFace.EAST);
ghe.onPistonPush(e);
assertFalse(e.isCancelled());
}

}

0 comments on commit 19cd685

Please sign in to comment.