PlayerChestToInventoryEvent with invetory.removeItem(****) not working as expected

  • I have this very simple set of java commands to delete any dynamite upon pickup from chests (i.e. to avoid getting dynamite in the server through the shack chests) but it is not really working, I can just pick the dynamite and place it in my inventory without any problems, in spite of that I get the message I have set for it to send "tnt removed!", any thoughts

  • If you want to prevent the player from picking up an item from a chest, you have to cancel the event by calling event.setCancelled(true);. Usually most events are triggered right before the particular action happens, so in this case, the event is called when the item is about to be moved from the chest to the inventory (but the item is still in the chest, not in the inventory). If the event does not get cancelled, the game will proceed with moving the item from the chest to the inventory. So if you want to stop the game from processing a certain action, you have to cancel the particular event :)

  • i tried that but then the item remains in the chest, while I want to make it disappear.

  • Usually the best approach to achieve that would be to cancel the event, and then remove the item from the chest. However, unfortunately the API does not provide any access to chests currently, probably that will be added with the next update. As a workaround (but that's a dirty solution) you could create a timer and remove the item from the inventory with a small delay, e.g.:



    This should work, however, this isn't a good solution. I'd recommend to wait for appropriate methods to access chests ;)

  • hmm yes I was looking for a method to delete the item in the chest but as you said there isn't one yet. haha 2 second timer nice workaround but if they know and manage to move it before the timer hits then we get a null pointer exception and they keep the dynamite :P


    I would like an Item.destroy() method like for the WorldItems if possible to tackle this better :) (as expressed here: Item.destroy() method similar to WorldItem.destroy()) or the chest specific methods when you create them :)


    my current workaround will probably be to cancel the event and if they try to destroy the chest to pick up the item use the PlayerInvetoryAddEvent to remove the item completely.

  • haha 2 second timer nice workaround but if they know and manage to move it before the timer hits then we get a null pointer exception and they keep the dynamite

    That's true... yeah well the timer is indeed a dirty solution. It's better to cancel the event, this way the player isn't able to get the item ;)


    my current workaround will probably be to cancel the event and if they try to destroy the chest to pick up the item use the PlayerInvetoryAddEvent to remove the item completely.

    If you cancel the PlayerChestToInventoryEvent and the PlayerInventoryAddEvent, the player has no chance to obtain the dynamite from a chest. Even if the item remains in the chest after cancelling the PlayerChestToInventoryEvent, the player isn't able to get the item ^^ If he destroy the chest, he still cannot pickup the item (if you cancel the PlayerInventoryAddEvent), and after a given amount of time (or after a restart), the item disappears anyway (like any world items) ;)

Participate now!

Don’t have an account yet? Create a new account now and be part of our community!