Prevent pistons pulling blocks out.

Allow piston pushing of blocks above or below the greenhouse because
biomes are 3D now.

https://github.com/BentoBoxWorld/Greenhouses/issues/77
This commit is contained in:
tastybento 2021-02-18 17:09:42 -08:00
parent 999ea07ef0
commit 19cd685900
3 changed files with 14 additions and 49 deletions

View File

@ -17,7 +17,6 @@ import org.bukkit.event.EventHandler;
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;
@ -169,21 +168,5 @@ public class GreenhouseEvents implements Listener {
});
}
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));
}
}

View File

@ -9,6 +9,7 @@ import org.bukkit.event.EventPriority;
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;
@ -68,6 +69,19 @@ public class GreenhouseGuard implements Listener {
.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);
}

View File

@ -9,7 +9,6 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Collections;
import java.util.Optional;
import org.bukkit.Bukkit;
@ -24,7 +23,6 @@ import org.bukkit.block.Block;
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;
@ -439,34 +437,4 @@ public class GreenhouseEventsTest {
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());
}
}