How to check if a player is holding a custom item?

Discussion in 'Spigot Plugin Development' started by pondamo, Jun 15, 2015.

  1. This may sound like a noob question but im unsure of how to do it. Im assuming its something like
    Code (Text):
    if (p.getItemInHand().getData().equals().Material.ITEM_NAME) {

    }
    However, the custom itemstack I made isnt actually listed as an item or variable for me to use. Heres the the code I have so far:
    Code (Text):
    public void onEnable() {
            blackrecipe();
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            Bukkit.getServer().getLogger().info("Paintbrush by pondamo, has been enabled");
            Bukkit.getServer().getLogger().info("This plugin was inspired by TXCAPTMINECRAFT");
        }
        public void blackrecipe() {
            @SuppressWarnings("deprecation")
            ItemStack blackbrush = new ItemStack(Material.INK_SACK, 1, (short) DyeColor.BLACK.getData());
            ItemMeta meta = blackbrush.getItemMeta();
            meta.setDisplayName(ChatColor.BLACK + "Black Paintbrush");
            meta.setLore(Arrays.asList("Paint the world", "in black!"));
            blackbrush.setItemMeta(meta);
         
            ShapelessRecipe blackrecipe = new ShapelessRecipe(blackbrush);
            blackrecipe.addIngredient(1, Material.INK_SACK);
            blackrecipe.addIngredient(1, Material.STICK);
            Bukkit.getServer().addRecipe(blackrecipe);
        }
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            Player p = (Player) e.getPlayer();
            if (p.getItemInHand()) {
             
            }
        }
     
  2. konsolas

    Supporter

    Check if the lore/display name is the same as the one you set when creating it?
     
  3. Code (Text):
            ItemStack item = /*What ever item your checking */
            ItemMeta meta = item.getItemMeta();
            if(meta.hasDisplayName() && meta.getDisplayName().equals("CustomItemName")){
               
            }
    You can check for item type and durability if you want to make sure
     
  4. Thats a good point! I never even thought of that xD