1.15.2 give-command gives one item too much

Discussion in 'Spigot Plugin Development' started by Freeforever, Mar 30, 2020.

  1. Hey, whenever someone gets an item via the give-command the PlayerDropItemEvent gets called and if I cancel it the player gets one more of the item then specified. Not twice, just one more.

    I'm using Spigot 1.15.2. The event only gets called once. As written in the javadoc the event should only be fired if a player drops an item, but not when getting one. The event does not get called when the player picks up an item lying on the ground.

    Does anyone has a solution to this?

    Best regards
    Freeforever


    Code (Text):
        @EventHandler
        public void onPlayerDropItem(PlayerDropItemEvent e) { // TODO
            System.out.println("Item: " + e.getItemDrop().getName());
            System.out.println("Player: " + e.getPlayer().getName());

            e.setCancelled(true);
        }
     
    Console:
    give @a dirt
    [14:21:33] [Server thread/INFO]: Item: Dirt
    [14:21:33] [Server thread/INFO]: Player: Freeforever24
    [14:21:33] [Server thread/INFO]: Gave 1 [Dirt] to Freeforever24

    --> got 2x dirt
     
    #1 Freeforever, Mar 30, 2020
    Last edited: May 26, 2020
  2. Well if you tried to drop 1 dirt and you cancel the event you're going to end up with 2 dirt since you stopped them dropping the first one.

    But i would suggest not using the console and just giving them the ItemStack you want.
     
  3. I don't think this is the desired functionality for cancelling PlayerDropItemEvent :unsure:
    Although due to user input I'm relying on the give command.
     
  4. I updatet my server file with BuildTools to the latest version but still the same error.
     
  5. Sorry for bumping again. But over a month has passed, I'm using the latest spigot-1.15.2 version and the problem still exists. I don't think behavior like this is intentional. @md_5



    Code (Java):
        @EventHandler
        public void onPlayerDropItem(PlayerDropItemEvent e) { // TODO
            System.out.println("Item: " + e.getItemDrop().getName());
            System.out.println("Player: " + e.getPlayer().getName());
            e.setCancelled(true);
        }
    Code (YAML):
    [13:54:03] [Server thread/INFO]: Item: Coal Ore
    [13:54:03] [Server thread/INFO]
    : Player: Freeforever
    [13:54:03] [Server thread/INFO]
    : Item: Coal Ore
    [13:54:03] [Server thread/INFO]
    : Player: Freeforever
    [13:54:04] [Server thread/INFO]
    : Item: Coal Ore
    [13:54:04] [Server thread/INFO]
    : Player: Freeforever
    [13:54:04] [Server thread/INFO]
    : Item: Coal Ore
    [13:54:04] [Server thread/INFO]
    : Player: Freeforever
    [13:54:05] [Server thread/INFO]
    : Item: Coal Ore
    [13:54:05] [Server thread/INFO]
    : Player: Freeforever
    [13:54:05] [Server thread/INFO]
    : Item: Coal Ore
    [13:54:05] [Server thread/INFO]
    : Player: Freeforever
    [13:54:05] [Server thread/INFO]
    : Item: Coal Ore
    [13:54:05] [Server thread/INFO]
    : Player: Freeforever
    [13:54:10] [Server thread/INFO]
    : Freeforever issued server command: /give Freeforever stone
    [13:54:10] [Server thread/INFO]
    : Item: Stone
    [13:54:10] [Server thread/INFO]
    : Player: Freeforever
    [13:54:11] [Server thread/INFO]
    : Freeforever issued server command: /give Freeforever stone
    [13:54:11] [Server thread/INFO]
    : Item: Stone
    [13:54:11] [Server thread/INFO]
    : Player: Freeforever
    [13:54:11] [Server thread/INFO]
    : Freeforever issued server command: /give Freeforever stone
    [13:54:11] [Server thread/INFO]
    : Item: Stone
    [13:54:11] [Server thread/INFO]
    : Player: Freeforever
    [13:54:14] [Server thread/INFO]
    : Freeforever issued server command: /give Freeforever dirt 5
    [13:54:14] [Server thread/INFO]
    : Item: Dirt
    [13:54:14] [Server thread/INFO]
    : Player: Freeforever
    [13:54:16] [Server thread/INFO]
    : Freeforever issued server command: /give Freeforever dirt 5
    [13:54:16] [Server thread/INFO]
    : Item: Dirt
    [13:54:16] [Server thread/INFO]
    : Player: Freeforever
    [13:57:47] [Server thread/INFO]
    : This server is running CraftBukkit version git-Spigot-2f5d615-cfeef75 (MC: 1.15.2) (Implementing API version 1.15.2-R0.1-SNAPSHOT)[m
    [13:57:47] [Server thread/INFO]
    : Checking version, please wait...[m
    [13:57:48] [Thread-10/INFO]
    : You are running the latest version[m
     
     
  6. Have you reported on Jira?
     
  7. I just did
     
    #8 Freeforever, May 26, 2020
    Last edited: May 26, 2020
    • Informative Informative x 1
  8. I believe by the time PlayerDropItemEvent is called, it's too late to modify the command.

    This has been an ongoing problem, and it's really annoying to deal with because the PlayerDropItemEvent is pretty unstable. It's basically a workaround. This is in EntityHuman#a(ItemStack, boolean, boolean)
    [​IMG]
    By the time the event is called, the Item entity has already been spawned, and the ItemStack has been removed from the player's inventory. There's also no way to get which slot the ItemStack came from. That's important because it would allow a very easy workaround for this mess. You could simply set canceled to false, retrieve the ItemStack from the dropped Item entity, kill the Item entity, then add the ItemStack back to the slot which it came from. If they dropped just one or part of the stack, just add to the amount of the stack.
    Or in some cases, don't add it back. You should try experimenting with not canceling the event and removing the item.
     
    • Agree Agree x 1
  9. That’s not a bug, thats a feature ;)

    But in all reality, maybe just listen for a packet when a player sends the command and if the command was the give command, cancel the packet and add the item directly to the inventory
     
    • Useful Useful x 1
  10. Yo tengo el mismo problema y aun no lo soluciono, pero en su lugar uso el comando /replaceitem, a mi me funciona ya que tengo que darle objetos a los jugadores, no es igual que /give pero a mi me sirve, espero que pueda servirte.