Solved Removing an item from a players inventory

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

Thread Status:
Not open for further replies.
  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?
    Show Spoiler

    Code:
    
    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().remove(Material.GOLDEN_APPLE);
    
    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");
    
    
    
    }
    
    }
    
    
     
  2. Offline

    MrGeneralQ

    I'm not sure, but didn't you forgot the @EventHandler ?
     
  3. Still doesn't work

    Code:
    
    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;
    
    }
    
    }
    
    
    @MrGeneralQ no it's in there and events registered. I get the effects but the item isn't removed

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  4. Offline

    webbhead

    You could just cancel the pickup all together and give them the effects and remove the item from the floor.
     
Thread Status:
Not open for further replies.

Share This Page