I am trying to disable people from dropping their items at a certain time.
I tried this code and it still allowed people to drop their items.
@Listener
public void onItemDrop(DropItemEvent.Dispense e, @First Player p) {
e.setCancelled(true);
}
I also tried this:
@Listener
public void onItemDrop(DropItemEvent e, @First Player p) {
e.setCancelled(true);
}
Still the same result.
Any idea how to fix this?
(I did register my listener)
Test if the code is executed by logging something to the console or sending a message to the player.
1 Like
I think the drop event uses EntitySpawnCause
as the drop cause (because it’s a type of spawn event) which means you need to use @First EntitySpawnCause spawnCause
instead of Player
.
Then you can inspect the spawnCause.getEntity()
Thank you, i will try it right now.
It doesn’t seem to work either, and i did add a message, that worked fine.
what spawn cause type are you checking for when you cancel?
I tried exactly what Simon said, it didnt work. Then i tried to listen to ConstructEntityEvent.Post (because pre couldnt get the entiylty), and remove the item, that still didnt work.
Unfortunately, the DroppedItem method will trigger both for dropping items, and breaking blocks resulting in ‘drops’ , and likely other processes that result in spawning an itemstack to be on the ground from something - both with SpawnCauses for DroppedItems… however, the player dropping also specifically releases an "EntitySpawnCause which is a spawncause variant, while the blocks drop with BlockSpawnCause. I’ve tested for a few mins, this can get you started, but the downside is that it will prevent the dropped item from appering, yet not actually prevent it from disappearing from your inventory.
When the inventory API is finished, there will be some methods to catch inventory changes like this to cancel… you might be able to hack one out of some existing methods…
@Listener
public void drop(DropItemEvent event, @Root EntitySpawnCause escause) {
if (escause.getEntity() instanceof Player) {
event.setCancelled(true);
}
}
That code uses filtering to detect only the EntitySpawnCause spawn-causes , ignoring mined-block drops , and checking for instanceof player eliminates the events picked up by pig-death dropping porkchops or skeletons bones, etc. However, there may not be a way to distinguish a player death-dropping items and normal dropping items. Consider trying that code in the .Pre event, since it will still catch a player dropping things, just not the thing that is gunna be dropped - and the event cancelled there might prevent transfer of the item out of inventory to nothingness…
Post is too late entirely to do anything but log it pretty much.
If you need to identify specific things that can be dropped, you wont be able to use .Pre, but for ‘anything’ it should work.
1 Like
Thank your a lot, i will try it right now, and the item disappearing is actually really nice for the plugin that i am making.