Cancelling item dropping

Discussion in 'Plugin Development' started by Limeth, Nov 18, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Limeth

    Hello, I'm having a problem with cancelling the item dropping in the inventory slot #8 (9. in hotbar).
    When I cancel the event, the item gets moved to the first free slot from 0 to 8. I would like to cancel the event and leave the item where it is.

    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerDropItem(PlayerDropItemEvent e) {
    4.  
    5. Player player = e.getPlayer();
    6. PlayerInventory playerInventory = player.getInventory();
    7.  
    8. if(playerInventory.getHeldItemSlot() == 8) {
    9.  
    10. e.setCancelled(true);
    11.  
    12. }
    13.  
    14. }
    15.  
     
  2. Offline

    Limeth

    - Bump -
     
  3. very weird, tried it myself. I don't really have a good answer. Only way around i can see is to get the item, copy, remove and place back. Wouldn't want to do this though
     
  4. Offline

    Limeth

    mollekake
    Alright, tried this:
    Code:java
    1.  
    2.  
    3. @EventHandler
    4. public void onPlayerDropItem(PlayerDropItemEvent event) {
    5.  
    6. Player player = event.getPlayer();
    7. PlayerInventory playerInventory = player.getInventory();
    8.  
    9. if(playerInventory.getHeldItemSlot() == 8) {
    10.  
    11. Item drop = event.getItemDrop();
    12. ItemStack is = drop.getItemStack();
    13. ItemStack isClone = is.clone();
    14. drop.setItemStack(null);
    15.  
    16. playerInventory.setItem(0, isClone);
    17.  
    18. }
    19.  
    20. }
    21.  



    Threw me this:
    Code:text
    1.  
    2. 21:47:31 [WARNING] Failed to handle packet for Limeth/127.0.0.1: java.lang.NullP
    3. ointerException
    4. java.lang.NullPointerException
    5. at net.minecraft.server.Packet21PickupSpawn.<init>(SourceFile:23)
    6. at net.minecraft.server.EntityTrackerEntry.b(EntityTrackerEntry.java:383
    7. )
    8. at net.minecraft.server.EntityTrackerEntry.updatePlayer(EntityTrackerEnt
    9. ry.java:304)
    10. at net.minecraft.server.EntityTrackerEntry.scanPlayers(EntityTrackerEntr
    11. y.java:369)
    12. at net.minecraft.server.EntityTracker.addEntity(EntityTracker.java:103)
    13. at net.minecraft.server.EntityTracker.track(EntityTracker.java:55)
    14. at net.minecraft.server.WorldManager.a(WorldManager.java:18)
    15. at net.minecraft.server.World.a(World.java:938)
    16. at net.minecraft.server.WorldServer.a(WorldServer.java:718)
    17. at net.minecraft.server.World.addEntity(World.java:931)
    18. at net.minecraft.server.World.addEntity(World.java:876)
    19. at net.minecraft.server.EntityHuman.a(EntityHuman.java:454)
    20. at net.minecraft.server.EntityHuman.a(EntityHuman.java:447)
    21. at net.minecraft.server.EntityHuman.bR(EntityHuman.java:397)
    22. at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:501)
    23. at net.minecraft.server.Packet14BlockDig.handle(SourceFile:46)
    24. at net.minecraft.server.NetworkManager.b(NetworkManager.java:290)
    25. at net.minecraft.server.NetServerHandler.d(NetServerHandler.java:113)
    26. at net.minecraft.server.ServerConnection.b(SourceFile:39)
    27. at net.minecraft.server.DedicatedServerConnection.b(SourceFile:30)
    28. at net.minecraft.server.MinecraftServer.r(MinecraftServer.java:595)
    29. at net.minecraft.server.DedicatedServer.r(DedicatedServer.java:222)
    30. at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:493)
    31. at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:426)
    32. at net.minecraft.server.ThreadServerApplication.run(SourceFile:856)
    33.  
     
  5. Offline

    fireblast709

    try that with drop.setItemStack(new ItemStack(Material.AIR, 0));
     
  6. Offline

    Limeth

    Alright, tried that but that did not change anything. Still getting the error.
     
  7. Offline

    fireblast709

    Weird. Could you tell me what
    Code:java
    1. System.out.println(drop.getItemStack().toString();

    prints after you set it to AIR?
     
  8. Offline

    Limeth

    It prints out "ItemStack{AIR x 0}", like expected.
    EDIT: The error appears after the message.
     
  9. Offline

    fireblast709

    try it with amount 1:
    Code:java
    1. new ItemStack(Material.AIR, 1);
     
  10. Offline

    Limeth

    Null Pointer Exception again. =(
     
  11. Offline

    DarkBladee12

    You can try this:
    Code:
    @EventHandler
    public void onPlayerDropItem(PlayerDropItemEvent e) {
     
    Player player = e.getPlayer();
    PlayerInventory playerInventory = player.getInventory();
    ItemStack[] contents = playerInventory.getContents();
     
    if(playerInventory.getHeldItemSlot() == 8) {
     
    e.setCancelled(true);
    playerInventory.setContents(contents);
     
    }
     
    }
     
  12. Offline

    Elrontur

    Try this:

    Code:
    @EventHandler(priority=EventPriority.NORMAL, ignoreCancelled=false)
    public void onDropItem(PlayerDropItemEvent e) {
     
        Player p = e.getPlayer();
       
        if (p.getInventory().getHeldItemSlot() == 8) {
            e.setCancelled(true);
            p.sendMessage("You can't drop something out of the 9. item slot!");
        }
       
    }
     
    maxmar628 likes this.
  13. Offline

    ZachBora

    If just cancelling the event still puts the item in the first slot, submit a bug report on the leaky.
     
  14. Offline

    Elrontur

    No it doesn't do it...
    It's working very well - the only bad is that it's spamming the message. :D

    A picture:
    [​IMG]
     
  15. Offline

    Limeth

    That is really weird, I tried that, but it still puts the item to the first hot-bar slot available. I'm using CB 1.4.5-R2.0.

    https://bukkit.atlassian.net/browse/BUKKIT-3028

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  16. Offline

    TnT

    If this error cannot be confirmed by someone else, do not submit a leaky ticket for the problem please.
     
  17. Offline

    Limeth

    Alright, that also did not help me, TnT .
     
  18. Offline

    Elrontur

  19. Offline

    ZachBora

    Try to remove all plugins except yours?
     
  20. Offline

    Limeth

    There are no other plugins, would you mind trying the code for yourself, too, please?
    EDIT: I can actually upload my server setup with the plugin, if it helps.

    Elrontur ZachBora TnT DarkBladee12 fireblast709 mollekake
    I've found the difference! If you cancel the event when you're holding in the 8th slot 2 or more items, it cancels perfectly, without any weird actions. BUT when you hold only one item in the slot and you press 'Q', then it acts weird! As I said, it puts the item to the first empty hotbar slot!
    Any ideas?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  21. Offline

    chaseoes

    I would do it manually if Bukkit is acting up. Set the item to air, then put it back in their inventory in whatever slot you want, instead of canceling the event.
     
  22. Offline

    Limeth

  23. Offline

    Badeye

    Having the same problem with 1.6.2-R0.1 and the newest build for 1.6.2. Multipile items are working, a single item gets added to the nerarest slot to slot #0.

    Edit: running no other plugins, this is a bukkit issue.
     
  24. Offline

    Badeye

    Bump? Any solutions for this weird bug?
     
  25. Submit a leaky ticket ;)
     
  26. Offline

    macguy8

    Just thought I'd clear this up: In the CraftBukkit event, what happens is the drop item is created. The item is removed from the players inventory. The event is called. If the event is cancelled, a return; is called and the item is added to the players inventory with **.addItem**. This means, if you drop an item in the 9th slot, the next available is the 1st, so it goes to that. If there are 2+ items, the closest stack for it to join with is the 9th one, so the event cancellation works as expected. This can be fixed by either changing this .addItem to a .setItem call in bukkit, or, until bukkit fixes this, calling event.getDrop().remove(); and manually using .setItem to set the item IN HAND to the item you want.
     
  27. Offline

    FadedMystery

    [item].remove(); ? Did you try that.. ?
     
  28. Offline

    Badeye

    FadedMystery Tried it, you can say e.remove(); that automaticly removes the events drop. I think what macguy8 described is totaly possible, it's just a weird bug, since it works when the stack you try to drop an item out of has more than 2 items inside. It just appears when the stackzize == 1.
     
  29. Offline

    macguy8

    Badeye
    That's exactly what's happening, per checking CraftBukkit code
     
  30. use that, that works 100%:
    Code:Java
    1.  
    2. @EventHandler
    3. public void onItemDrop(PlayerDropItemEvent e){
    4. Player p = e.getPlayer();
    5. ItemStack item = e.getItemDrop().getItemStack().clone();
    6. e.getItemDrop().remove();
    7. p.getInventory().addItem(item);
    8.  
    9. }
    10.  

    hope that helped you :D
     
    bentech likes this.
Thread Status:
Not open for further replies.

Share This Page