Solved Checking if an item is clicked on another item

Discussion in 'Spigot Plugin Development' started by MaxKrissigo, May 22, 2016.

  1. I'm currently trying to figure out how I would check whether a player clicks an item using another item already selected in their inventory. If you don't quite understand what I mean, here is a photo:

    [​IMG]
    This is my code so far, but it isn't work.
    Code (Text):
            @EventHandler
            public void onInventoryClick(InventoryClickEvent event) {
                Player p = (Player) event.getWhoClicked();
                p.sendMessage("2");
                Inventory inventory = event.getInventory();
                ItemStack clicked = event.getCurrentItem();
                if (clicked.getItemMeta().getDisplayName().equals(ChatColor.GREEN + "" + ChatColor.BOLD + "Respiration I")) {
                    p.sendMessage("1");
                    if (p.getItemOnCursor().equals(Material.DIAMOND_HELMET)) {
                        p.sendMessage("3");
                    }
                }
            }
     
  2. I think InventoryAction.SWAP_WITH_CURSOR is what you're looking for.
    Code (Text):

    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        if (event.getAction() == InventoryAction.SWAP_WITH_CURSOR) {
            // ...
        }
    }
     
  3. How would I check what is being swapped and what the cursor item is?
     
  4. Check the documentation I provided :) there are two methods near the top that do exactly what you want.
     
  5. InventoryClickEvent#getCursor is the item being held
    InventoryClickEvent#getCurrentItem is the item being clicked on

    The inventory action is PLACE_ALL.