how can i get the next item in hand?

Discussion in 'Spigot Plugin Development' started by ShonaaDev, Feb 20, 2020.

Thread Status:
Not open for further replies.
  1. 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));
                         }
                     }
                 }
            }
     
  2. 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().
     
    • Like Like x 1
  3. 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)
     
    #3 Rawora, Feb 20, 2020
    Last edited: Feb 20, 2020
    • Like Like x 1
  4. thanks i will check it
     
  5. Thank you the plugin is working
     
Thread Status:
Not open for further replies.