Handle null itemInHand for BlockPlaceEvent

Officially, null is invalid as an in-hand item. It should be AIR.

Fixes https://github.com/BentoBoxWorld/BentoBox/issues/1172
This commit is contained in:
tastybento 2020-02-07 11:52:01 -08:00
parent 559578002b
commit 898e310e4f
2 changed files with 17 additions and 0 deletions

View File

@ -27,6 +27,7 @@ public class PlaceBlocksListener extends FlagListener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onBlockPlace(final BlockPlaceEvent e) {
if (e.getBlock().getType().equals(Material.FIRE)
|| e.getItemInHand() == null // Note that this should never happen officially, but it's possible for other plugins to cause it to happen
|| e.getItemInHand().getType().equals(Material.WRITABLE_BOOK)
|| e.getItemInHand().getType().equals(Material.WRITTEN_BOOK)) {
// Books can only be placed on lecterns and as such are protected by the LECTERN flag.

View File

@ -224,6 +224,22 @@ public class PlaceBlocksListenerTest {
pbl.onBlockPlace(e);
assertFalse(e.isCancelled());
}
/**
* Test method for {@link PlaceBlocksListener#onBlockPlace(org.bukkit.event.block.BlockPlaceEvent)}.
*/
@Test
public void testOnBlockPlaceNullItemInHand() {
Block placedBlock = mock(Block.class);
when(placedBlock.getType()).thenReturn(Material.STONE);
when(placedBlock.getLocation()).thenReturn(location);
BlockState replacedBlockState = mock(BlockState.class);
Block placedAgainst = mock(Block.class);
EquipmentSlot hand = EquipmentSlot.HAND;
BlockPlaceEvent e = new BlockPlaceEvent(placedBlock, replacedBlockState, placedAgainst, null, player, true, hand);
pbl.onBlockPlace(e);
assertFalse(e.isCancelled());
}
/**
* Test method for {@link PlaceBlocksListener#onBlockPlace(org.bukkit.event.block.BlockPlaceEvent)}.