use of org.bukkit.event.player.PlayerInteractEvent in project Dragonet-Legacy by DragonetMC.
the class RemoveBlockPacketTranslator method handleSpecific.
@Override
public Message[] handleSpecific(RemoveBlockPacket packet) {
if (!(this.getSession().getPlayer() instanceof Player)) {
return null;
}
if (getSession().getPlayer().getGameMode().equals(GameMode.CREATIVE)) {
final GlowPlayer player = getSession().getPlayer();
GlowWorld world = player.getWorld();
GlowBlock block = world.getBlockAt(packet.x, packet.y, packet.z);
PlayerInteractEvent interactEvent = EventFactory.onPlayerInteract(player, Action.LEFT_CLICK_BLOCK, block, BlockFace.UP);
if (interactEvent.isCancelled()) {
player.sendBlockChange(block.getLocation(), block.getType(), block.getData());
return null;
}
block.setType(Material.AIR);
} else {
// Not Creative
DiggingMessage msgFinishBreak = new DiggingMessage(DiggingMessage.FINISH_DIGGING, packet.x, packet.y, packet.z, 1);
return new Message[] { msgFinishBreak };
}
return null;
}
use of org.bukkit.event.player.PlayerInteractEvent in project Glowstone by GlowstoneMC.
the class UseItemHandler method handleRightClickAir.
static void handleRightClickAir(GlowPlayer player, ItemStack holding, EquipmentSlot slot) {
PlayerInteractEvent event = EventFactory.getInstance().onPlayerInteract(player, Action.RIGHT_CLICK_AIR, slot);
event.useItemInHand();
if (event.useItemInHand() == Event.Result.DENY) {
return;
}
if (!InventoryUtil.isEmpty(holding)) {
ItemType type = ItemTable.instance().getItem(holding.getType());
if (type != null) {
if (type.getContext().isAirApplicable()) {
type.rightClickAir(player, holding);
}
}
// Empties the user's inventory when the item is used up
if (holding.getAmount() <= 0) {
player.getInventory().setItem(slot, InventoryUtil.createEmptyStack());
}
}
}
use of org.bukkit.event.player.PlayerInteractEvent in project Glowstone by GlowstoneMC.
the class UseItemHandlerTest method testRightClickAir.
@Test
public void testRightClickAir() {
// prepare objects under test
Location location = new Location(world, 1.0, 1.0, 1.0);
Location playerLocation = new Location(world, 2.0, 2.0, 2.0);
GlowPlayerInventory inventory = new GlowPlayerInventory(player);
ItemStack stack = new ItemStack(Material.JUNGLE_BOAT);
GlowBoat boat = new GlowBoat(location);
inventory.setItemInMainHand(stack);
UseItemMessage message = new UseItemMessage(EquipmentSlot.HAND.ordinal());
PlayerInteractEvent event = new PlayerInteractEvent(player, Action.RIGHT_CLICK_AIR, stack, null, null);
// prepare mock behaviours
Mockito.when(session.getPlayer()).thenReturn(player);
Mockito.when(player.getInventory()).thenReturn(inventory);
Mockito.when(player.getLocation()).thenReturn(playerLocation);
Mockito.when(player.getTargetBlock((Set<Material>) null, 5)).thenReturn(targetBlock);
Mockito.when(player.getGameMode()).thenReturn(GameMode.SURVIVAL);
Mockito.when(eventFactory.onPlayerInteract(player, Action.RIGHT_CLICK_AIR, EquipmentSlot.HAND)).thenReturn(event);
Mockito.when(emptyBlock.isEmpty()).thenReturn(Boolean.TRUE);
Mockito.when(emptyBlock.getLocation()).thenReturn(location);
Mockito.when(targetBlock.isEmpty()).thenReturn(Boolean.FALSE);
Mockito.when(targetBlock.getRelative(BlockFace.UP)).thenReturn(emptyBlock);
Mockito.when(targetBlock.getWorld()).thenReturn(world);
Mockito.when(world.spawn(location, Boat.class)).thenReturn(boat);
// run test
Assert.assertTrue(inventory.contains(Material.JUNGLE_BOAT, 1));
UseItemHandler handler = new UseItemHandler();
handler.handle(session, message);
// after calling use item and creating a boat, the inventory no longer contains a boat
Assert.assertFalse(inventory.contains(Material.JUNGLE_BOAT, 1));
}
Aggregations