When the player clicks on an item, he removes it, but he also removes the displayname, how can I fix this? (srry for my english) this is my code: Code (Java): @EventHandler public void replaceItem(PlayerInteractEvent e){ Player p = e.getPlayer(); if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR) { if (p.getItemInHand().getType() == Material.INK_SACK && p.getItemInHand().getItemMeta().hasDisplayName() && p.getItemInHand().getItemMeta().getDisplayName() == "§7Flash Bang") { if (e.getPlayer().getItemInHand().getAmount() > 1) { ItemStack old = new ItemStack(e.getPlayer().getItemInHand().getTypeId(), e.getPlayer().getItemInHand().getAmount() - 1); e.getPlayer().setItemInHand(old); // p.getItemInHand().hasNext(); } else { e.getPlayer().setItemInHand(new ItemStack(Material.AIR)); } } } }
You are creating a new ItemStack, without the ItemMeta of the old item. Why not place the clicked item itself in the player's hand? EDIT: Ah, I see that you are trying to remove one of the items the player is holding. Use the method ItemStack.setAmount().
I think that’s a typical xy Problem, where you’re looking for a solution fo a different problem rather than your actual problem. As far as I can see, you replace the item with a different item stack (that by the way doesn’t have an DisplayName bc you just created the ltemStack and assigned no Meta for it) but why not just the original item that’s in the player’s hand minus 1 ? (So that you „used“ the flashbang item and therefore -1) Like this Code (Java): player.getItemInHand().setAmount(player.getItemInHand().getAmount()-1)