Problem when changing item pickup

Discussion in 'Plugin Development' started by nathanthesnooper, Feb 10, 2018.

Thread Status:
Not open for further replies.
  1. Here I have a very simple event:

    @EventHandler (priority = EventPriority.HIGHEST)
    public void playerPickupItemOnServer (PlayerPickupItemEvent event) {
        ItemStack is = event.getItem().getItemStack();
        System.out.println("ItemStack Amount: " + event.getItem().getItemStack().getAmount());
    Whenever I pickup an item, this shows in console: (as expected)

    ItemStack Amount: 16

    However, the item's amount doesn't change.
    Also, changing the type of the item doesn't work.

    I must be missing something, any help is appreciated!
  2. @nathanthesnooper
    is this line needed?


    can't you just do event.getItem().getItemStack().setAmount(16);
    Last edited: Feb 10, 2018
  3. Offline


    that line is needed indeed.
    looking into nms code and you'll find that getItemStack() method return a copy of related item snapshot.
    public ItemStack getItemStack() {
        return CraftItemStack.asCraftMirror(this.item.getItemStack());
    so you can't modify amount directly, need to set again.
    but what's weird is that setItemStack() doesn't work. I've tested it, whatever you set, there always comes out
    ItemStack{AIR x 0}
    well, no further clue...consider to cancel that event and add items to player's inventory directly:(
  4. I really don't know what u wanna do but I tried to do what I think u wanna do and ye..
    Here is the code. Maybe that helps. (?)
    If there are some problems just reply. :D

        public void onPlayerPickupitem(PlayerPickupItemEvent e) {
            Player p = e.getPlayer();
            ItemStack i = e.getItem().getItemStack();
            System.out.println("ItemStack Amount: " + e.getItem().getItemStack().getAmount());
            Bukkit.getWorld("world").playSound(p.getLocation(), Sound.ITEM_PICKUP, 1F, 1F);
Thread Status:
Not open for further replies.

Share This Page