Item not getting remove.

Discussion in 'Plugin Development' started by BlueBearPvP, May 14, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    BlueBearPvP

    Hey Guys,

    So I created a custom item that will give you speed and regen when you right-click it. The problem is that I want to remove the item when the player right-click it. Here is what I tryed:

    Code:
    public int playerSkull() {
           
            ItemStack playerSkull = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
            SkullMeta pMeta = (SkullMeta) playerSkull.getItemMeta();
            pMeta.setLore(Arrays.asList(ChatColor.GRAY + "  ", ChatColor.GRAY + "Right-Click to eat the head!"));
            playerSkull.setItemMeta(pMeta);
            return 0;
       
        }
    
    @EventHandler
         public void onEatHead(PlayerInteractEvent e) {
             Player p = e.getPlayer();
            
             if (p.getItemInHand() != null && p.getItemInHand().hasItemMeta() && p.getItemInHand().getType() == Material.SKULL_ITEM  && p.getItemInHand().getItemMeta().hasLore()
                     && p.getItemInHand().getItemMeta().getLore().equals(Arrays.asList(ChatColor.GRAY + "  ", ChatColor.GRAY + "Right-Click to eat the head!"))) {
                
                 if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                     p.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 50, 1));
                     p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 500, 9));
                     p.getInventory().remove(playerSkull());
                 }
             }
         }
     
  2. Offline

    BioBG

    Hi :)
    It is removed, but u did not update the player inventory :)
    add last p.updateInventory(); and will work :)

    BioBG
     
  3. Offline

    I Al Istannen

    @BlueBearPvP
    "p.getInventory().remove(playerSkull());"
    and
    "return 0;"
    You remove the item at the index 0 every time, no matter where the skull is. Is that the desired outcome? I would return the created ItemStack in the playerSkull() method and see if it works then. Or try removing the Material SKULL_ITEM if you want to clear every skull.
     
  4. Offline

    Gonmarte

    Try out this
    Code:
    player.getInventory.removeItem(item);
     
  5. Offline

    BioBG

    I use other simple method, just checking the item if is not null and is type, name and meta/lore i look for, i just set the item in hand to null.

    p.getInventory().setItemInHand(null);
    p.updateInventory();

    BioBG
     
Thread Status:
Not open for further replies.

Share This Page