Java Code Examples for org.bukkit.inventory.PlayerInventory#getItemInHand()

The following examples show how to use org.bukkit.inventory.PlayerInventory#getItemInHand() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GraphListener.java    From LagMonitor with MIT License 6 votes vote down vote up
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH)
public void onInteract(PlayerInteractEvent clickEvent) {
    Player player = clickEvent.getPlayer();
    PlayerInventory inventory = player.getInventory();

    ItemStack mainHandItem;
    if (mainHandSupported) {
        mainHandItem = inventory.getItemInMainHand();
    } else {
        mainHandItem = inventory.getItemInHand();
    }

    if (isOurGraph(mainHandItem)) {
        inventory.setItemInMainHand(new ItemStack(Material.AIR));
    }
}
 
Example 2
Source File: GraphListener.java    From LagMonitor with MIT License 6 votes vote down vote up
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH)
public void onInteract(PlayerInteractEvent clickEvent) {
    Player player = clickEvent.getPlayer();
    PlayerInventory inventory = player.getInventory();

    ItemStack mainHandItem;
    if (mainHandSupported) {
        mainHandItem = inventory.getItemInMainHand();
    } else {
        mainHandItem = inventory.getItemInHand();
    }

    if (isOurGraph(mainHandItem)) {
        inventory.setItemInMainHand(new ItemStack(Material.AIR));
    }
}
 
Example 3
Source File: DWorldListener.java    From DungeonsXL with GNU General Public License v3.0 5 votes vote down vote up
private ItemStack getItemInHand(PlayerInteractEvent event) {
    PlayerInventory inventory = event.getPlayer().getInventory();
    if (Version.isAtLeast(Version.MC1_9)) {
        return event.getHand() == EquipmentSlot.HAND ? inventory.getItemInMainHand() : inventory.getItemInOffHand();
    } else {
        return inventory.getItemInHand();
    }
}
 
Example 4
Source File: DungeonItemCommand.java    From DungeonsXL with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onExecute(String[] args, CommandSender sender) {
    Player player = (Player) sender;
    PlayerInventory inv = player.getInventory();

    ItemStack bukkitStack = inv.getItemInHand();
    if (bukkitStack == null) {
        MessageUtil.sendTitleMessage(player, DMessage.ERROR_NO_ITEM_IN_MAIN_HAND.getMessage());
        return;
    }
    Object tag = NBTUtil.getTag(bukkitStack);

    String action = args.length >= 2 ? args[1] : "info";
    if (action.equalsIgnoreCase("true")) {
        if (tag == null) {
            tag = NBTUtil.createTag();
        }
        NBTUtil.addBoolean(tag, NBTUtil.DUNGEON_ITEM_KEY, true);
        inv.setItemInHand(NBTUtil.setTag(bukkitStack, tag));
        MessageUtil.sendMessage(sender, DMessage.CMD_DUNGEON_ITEM_SET_DUNGEON.getMessage());
        MessageUtil.sendMessage(sender, DMessage.CMD_DUNGEON_ITEM_DUNGEON_ITEM_HELP.getMessage());

    } else if (action.equalsIgnoreCase("false")) {
        if (tag != null) {
            NBTUtil.removeKey(tag, NBTUtil.DUNGEON_ITEM_KEY);
            inv.setItemInHand(NBTUtil.setTag(bukkitStack, tag));
        }
        MessageUtil.sendMessage(sender, DMessage.CMD_DUNGEON_ITEM_SET_GLOBAL.getMessage());
        MessageUtil.sendMessage(sender, DMessage.CMD_DUNGEON_ITEM_GLOBAL_ITEM_HELP.getMessage());

    } else {
        if (tag != null && NBTUtil.hasKey(tag, NBTUtil.DUNGEON_ITEM_KEY)) {
            MessageUtil.sendMessage(sender, DMessage.CMD_DUNGEON_ITEM_INFO_DUNGEON.getMessage());
            MessageUtil.sendMessage(sender, DMessage.CMD_DUNGEON_ITEM_DUNGEON_ITEM_HELP.getMessage());
        } else {
            MessageUtil.sendMessage(sender, DMessage.CMD_DUNGEON_ITEM_INFO_GLOBAL.getMessage());
            MessageUtil.sendMessage(sender, DMessage.CMD_DUNGEON_ITEM_GLOBAL_ITEM_HELP.getMessage());
        }
    }
}