Problem with canceling events!

Discussion in 'Plugin Development' started by FirecatHD, Feb 25, 2014.

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

    FirecatHD

    So am checking if the player is NOT holding the specified item, and if he isnt i want to cancel the event.
    However it looks like bukkit is ignoring the checking part ;_;

    Code:java
    1. @EventHandler
    2. public void onPlayerDropItemEvent(PlayerDropItemEvent e) {
    3.  
    4. ItemStack LimeDye = new ItemStack(Material.INK_SACK, 1, (short) 1);
    5. if (!(e.getPlayer().getItemInHand().equals(LimeDye))) {
    6.  
    7. e.setCancelled(true);
    8.  
    9. }
    10.  
    11. e.setCancelled(true);
    12.  
    13. }


    -Firecat
     
  2. Offline

    NathanWolf

    This code looks like it's going to cancel the event no matter what. Why is that second "setCancelled" there- just for testing?

    I'm pretty certain this works, canceling the event prevents the player from dropping the item.
     
  3. Offline

    FirecatHD

    NathanWolf

    I forgot to remove the other setchanceled before copying the code, but it wont work.
    I am basically trying to fill the inventory slot that has the dye in it with air, or not touch it at all (Nothing happens when you press Q) But the problem with canceling the event is that the dye gets stacked, i am trying to prevent this, but canceling the event at the same time if that makes sense :)
     
  4. Offline

    NathanWolf

    oh- I think that is, unfortunately, a bukkit bug. If you cancel a drop event, the item gets re-added to the inventory, which may change it's location, or cause it to stack.

    I think you could set the stacksize of the item to 1 before dropping to semi-work-around this, perhaps?
     
  5. Offline

    Traks

    NathanWolf Well, there isn't a .getSlot() method in the PlayerDropItemEvent as far as i know. Maybe because players can also open their inventory, take the item out of a slot and drop it.

    Best thing I can think of, would be to remove the drop and adding an item back to the player's hotbar or wherever you want it to be. So you'd end up with something like this:

    Code:java
    1. @EventHandler
    2. public void onDrop(final PlayerDropItemEvent e) {
    3. e.getItemDrop().remove();
    4.  
    5. // Safer, won't work in some cases otherwise
    6. new BukkitRunnable() {
    7. @Override
    8. public void run() {
    9. // Add an item back to the player's inventory
    10. }
    11. }.runTaskLater(Plugin, 1); // 'Plugin' should be an instance of your main class
    12. }
     
    FirecatHD likes this.
  6. Offline

    NathanWolf

    Oh, I meant modifying the max stack size of the dropped item (you shouldn't need the slot for this)..

    But now that I say that, I guess I'm not sure that's possible, or at least not without NMS.. I feel like I've read some threads around here before about how to prevent items from stacking, which might work around your issue.

    Otherwise your approach may work, I've had to do some fiddly 1-tick-delayed stuff like that before, though it always feels a little... fragile.
     
    FirecatHD likes this.
  7. Offline

    FirecatHD

    Traks
    NathanWolf

    I am trying to replace the dropped item after its remove using the Traks
    Cant belive i didnt think of this :3 I got it to work so now the cancelItemDrop event does what it should; Make nothing happen when you hit "Q"!

    Thanks guys :)
     
Thread Status:
Not open for further replies.

Share This Page