Search in sources :

Example 6 with PlayerInteractEvent

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;
}
Also used : GlowPlayer(net.glowstone.entity.GlowPlayer) Player(org.bukkit.entity.Player) GlowBlock(net.glowstone.block.GlowBlock) DiggingMessage(net.glowstone.net.message.play.player.DiggingMessage) Message(com.flowpowered.networking.Message) DiggingMessage(net.glowstone.net.message.play.player.DiggingMessage) GlowPlayer(net.glowstone.entity.GlowPlayer) PlayerInteractEvent(org.bukkit.event.player.PlayerInteractEvent) GlowWorld(net.glowstone.GlowWorld)

Example 7 with PlayerInteractEvent

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());
        }
    }
}
Also used : PlayerInteractEvent(org.bukkit.event.player.PlayerInteractEvent) ItemType(net.glowstone.block.itemtype.ItemType)

Example 8 with PlayerInteractEvent

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));
}
Also used : GlowBoat(net.glowstone.entity.objects.GlowBoat) PlayerInteractEvent(org.bukkit.event.player.PlayerInteractEvent) UseItemMessage(net.glowstone.net.message.play.player.UseItemMessage) Material(org.bukkit.Material) ItemStack(org.bukkit.inventory.ItemStack) Location(org.bukkit.Location) GlowPlayerInventory(net.glowstone.inventory.GlowPlayerInventory) Test(org.junit.Test)

Aggregations

PlayerInteractEvent (org.bukkit.event.player.PlayerInteractEvent)8 ItemType (net.glowstone.block.itemtype.ItemType)3 Location (org.bukkit.Location)3 GlowWorld (net.glowstone.GlowWorld)2 GlowBlock (net.glowstone.block.GlowBlock)2 BlockType (net.glowstone.block.blocktype.BlockType)2 GlowPlayer (net.glowstone.entity.GlowPlayer)2 Material (org.bukkit.Material)2 Entity (org.bukkit.entity.Entity)2 Player (org.bukkit.entity.Player)2 BlockBreakEvent (org.bukkit.event.block.BlockBreakEvent)2 AsyncPlayerChatEvent (org.bukkit.event.player.AsyncPlayerChatEvent)2 PlayerInteractEntityEvent (org.bukkit.event.player.PlayerInteractEntityEvent)2 ItemStack (org.bukkit.inventory.ItemStack)2 FixedMetadataValue (org.bukkit.metadata.FixedMetadataValue)2 Test (org.junit.Test)2 Message (com.flowpowered.networking.Message)1 EventFactory (net.glowstone.EventFactory)1 GlowBlockState (net.glowstone.block.GlowBlockState)1 BlockContainer (net.glowstone.block.blocktype.BlockContainer)1