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? Spoiler 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; } }
It ist very simple: Code (Text): player.getInventory().removeItem(new ItemStack(Material.GOLDEN_APPLE));
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 } }
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.
Don't use the == operator. It doesn't really work. Use the isSimilar() function in the item stack class