Solved Cancel playerdropitemevent?

Discussion in 'Plugin Development' started by shohouku, May 11, 2014.

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

    shohouku

    So this code doesn't work right now, I have all my events registered and I have a listener.

    No errors too.

    Code:java
    1. @EventHandler
    2. public void onItemDrop(PlayerDropItemEvent e){
    3. Player p = e.getPlayer();
    4. ItemStack item = e.getItemDrop().getItemStack().clone();
    5. if(p.getItemInHand().hasItemMeta()) {
    6. if(p.getItemInHand().getItemMeta().hasDisplayName()) {
    7. String name = e.getItemDrop().getItemStack().getItemMeta().getDisplayName();
    8.  
    9. if(name.equalsIgnoreCase(ChatColor.GRAY + "MainMenuWindnow")) {
    10. p.getInventory().addItem(item);
    11. e.setCancelled(true);
    12. }
    13. }
    14. }
    15.  
    16. }
     
  2. Offline

    MineStein

    Where is the error, specify line #('s)
     
  3. Offline

    shohouku


    There are no errors.

    Just isn't working.

    Glitchy while cancelling I guess?
     
  4. Offline

    Bobit

    From Adamqpzm:
     
  5. Offline

    shohouku


    I'v seen some servers when they actually cancel out the drop...

    And how would PlayerMoveEvent affect the inventory?
     
  6. Offline

    Bobit

    Tell me how you end up solving it; I need to cancel this event soon.

    PlayerMoveEvent was just the context, it has nothing to do with the inventory.
    Anyhow, try testing it with an updateInventory().

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  7. Offline

    mazentheamazin

    Bobit shohouku
    You could always just removed the dropped entity, then add the item back in their inventory. At least for me, cancelling the event works fine o_0

    EDIT: Or, you can use ItemSpawnEvent
     
  8. Offline

    shohouku

    I'v tried updating the player inventory doesn't work, tried cancelling the drop event (I'm using craft bukkit 1.7.9 and BUkkit 1.7.9) and I've tried putting the item back in the players inventory and that doesn't work..

    I have all my events registered and I have listeners.
     
  9. Bobit PlayerDropItem is actually fine for cancellation from what I've seen of it. It's also not technically an inventory event, so maybe that's why ;)

    shohouku I noticed you've marked this as solved but haven't posted a solution; did you solve it or has it been mismarked? Either way, I would've recommended checking against item rather than getItemInHand :)
     
  10. Offline

    shohouku


    My new code, it works but very buggy.


    Code:java
    1.  
    2. @EventHandler
    3. public void onItemDrop(PlayerDropItemEvent e){
    4. Player p = e.getPlayer();
    5. String name = e.getItemDrop().getItemStack().getItemMeta().getDisplayName();
    6. if(e.getItemDrop().getItemStack().hasItemMeta()) {
    7. if(e.getItemDrop().getItemStack().getItemMeta().hasDisplayName()) {
    8.  
    9.  
    10. if(name.equalsIgnoreCase(ChatColor.GRAY + "MainMenuWindow")) {
    11.  
    12. e.setCancelled(true);
    13. p.sendMessage("test");
    14.  
    15. }
    16. }
    17. }
    18.  
    19.  
    20.  
    21.  
    22.  
    23.  
    24.  
    25. }
    26.  
    27.  
    28. @EventHandler
    29. public void onInventoryDrag(InventoryDragEvent event) {
    30.  
    31. event.setCancelled(true);
    32. System.out.println(10);
    33. }
    34.  
    35.  
    36. @EventHandler
    37. public void onInventoryDrag(InventoryInteractEvent event) {
    38. for(Player p : Bukkit.getOnlinePlayers()){
    39. String name = p.getItemInHand().getItemMeta().getDisplayName();
    40. if(p.getItemInHand().getItemMeta().hasDisplayName()) {
    41. if(p.getItemInHand().hasItemMeta()) {
    42. if(name.equalsIgnoreCase(ChatColor.GRAY + "MainMenuWindow")) {
    43. event.setCancelled(true);
    44. System.out.println(1);
    45.  
    46. }
    47. }
    48. }
    49. }
    50.  
    51.  
    52. }
    53.  
    54. @EventHandler
    55. public void onClickInventory(InventoryClickEvent event){
    56.  
    57. for(Player p : Bukkit.getOnlinePlayers()){
    58. String name = p.getItemInHand().getItemMeta().getDisplayName();
    59. System.out.println(name);
    60. if(p.getItemInHand().getItemMeta().hasDisplayName()) {
    61. if(p.getItemInHand().hasItemMeta()) {
    62. if(name.equalsIgnoreCase(ChatColor.GRAY + "MainMenuWindow")) {
    63. event.setCancelled(true);
    64. System.out.println(1);
    65. }
    66. }
    67. }
    68. }
    69. }
    70.  
     
  11. shohouku You get the display name before checking if it has item meta :)
     
  12. Offline

    shohouku


    Would it matter? :confused:

    I'm pretty sure it wouldn't make a difference..but I'll just double check.

    EDIT:

    Btw I'm cancelling the PlayerDropItemEvent

    And it's going to the first slot? I'm assuming that shouldn't be happening?

    I'm guessing I'm going to have to set the slot when the player tries to drop it.
     
  13. shohouku Yes. It'll throw an NPE if the dropped item doesn't have any item meta.
     
Thread Status:
Not open for further replies.

Share This Page