Removing an item from a players inventory

Discussion in 'Spigot Plugin Development' started by CreeperFace0777, May 31, 2016.

  1. Hi guys, I'm making a kitpvp plugin and when a player gets killed a golden apple is dropped which they can pick up and immediately receive effects and the apple is removed. However, the item isn't being removed from the players inventory! Please help?
    Code (Text):

    @EventHandler

    publicvoid onPickup(PlayerPickupItemEvent e) {

    if (e.getItem().getItemStack().getType() == Material.GOLDEN_APPLE

    && e.getItem().getItemStack().getItemMeta().getDisplayName().contains("Death Apple")) {



    Player player = e.getPlayer();

    player.getInventory().removeItem(e.getItem().getItemStack());

    player.updateInventory();

    String[] temp = e.getItem().getItemStack().getItemMeta().getDisplayName().split(" ", 2);

    String playerDeathName = temp[0];

    player.getInventory().remove(e.getItem().getItemStack());

    player.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 2400, 0), true);

    if (!(player.getHealth() >= 8)) {

    player.setHealth(player.getHealth() + 4);

    } else {

    player.setHealth(player.getMaxHealth());

    }

    player.sendMessage(prefix + ChatColor.RESET + "" + ChatColor.GRAY + " You picked up " + playerDeathName

    + "'s Golden Apple");

    player.getInventory().remove(e.getItem().getItemStack());

    player.updateInventory();

    return;

    }

    }

     
     
  2. It ist very simple:
    Code (Text):

    player.getInventory().removeItem(new ItemStack(Material.GOLDEN_APPLE));
     
     
  3. You can do what you're trying to pretty easily with the PlayerPickupItemEvent

    Code (Text):
        @EventHandler
        public void onPickup(PlayerPickupItemEvent e){
            if (e.getItem().getItemStack().equals(yourItem)){
                e.getItem().remove();
                e.setCancelled(true);
                // do your stuff
            }
        }
     
    • Like Like x 1
  4. So, if it's always the same Item.. I would make it static.
    Like:

    Code (Text):
    private static ItemStack deathApple; //then initialize it

    //drop deathApple @ PlayerDeath

    @EventHandler
    publicvoid onPickup(PlayerPickupItemEvent e) {

    if (e.getItem().getItemStack().equals(deathApple)){
    Player player = e.getPlayer();
    player.getInventory().removeItem(deathApple);

    //and so on
    It should make it a littlebit easier.
     
  5. Don't use the == operator. It doesn't really work. Use the isSimilar() function in the item stack class
     
  6. My way is the best way, the item is never added to their inventory so there are no potential bugs.
     
    • Agree Agree x 2