Paper
Paper copied to clipboard
Add event to retrieve Entity who caused a PlayerItemDamageEvent
Is your feature request related to a problem?
Currently, AFAIK, there is no way to retrieve which Entity could have caused a PlayerItemDamageEvent to fire, such as when an entity hits a player who is currently using a shield which loses durability for doing so.
In my plugin LevelledMobs, I would like to add a feature where higher-level mobs are able to inflict more durability damage to players' shields so that they are less effective against those mobs.
If you were unfamiliar with the concept: a mob's 'Level' determines how strong they are, i.e. a higher level mob is stronger than lower level mobs.
Describe the solution you'd like.
I think a great way to go about solving this is to add a new event - PlayerItemDamageByEntityEvent. This event extends PlayerItemDamageEvent, and is fired when an entity causes PlayerItemDamageEvent to fire, i.e., during combat.
This event contains a method which references which entity caused the event to fire.
This event could also contain a reference to a linked EntityDamageByEntityEvent which caused the PlayerItemDamageByEntityEvent to fire.
Describe alternatives you've considered.
I have thought about using a bit of hack-ish code to get around it, but I'd rather not since it could cause weird side-effects. For instance, applying a 1-tick-expiry temporary metadata value to the defending player in EntityDamageByEntityEvent with the shield breaker multiplier value, and then referencing that in the PlayerItemDamageEvent by changing the damage value to be multiplied by the multiplier stored in the metadata.
There's also player#getLastDamageCause.getEntity() - although close, that would still be a hack-ish workaround: there is no guarantee that it would return the entity which has caused the particular PlayerItemDamageEvent to fire. Imagine that an entity hit a player 20 minutes ago, and then the player's item was damaged 20 minutes later. That method would return the entity from 20 minutes ago which was completely unrelated.
Other
No response
I agree to this, There would many good uses for being able to get the entity. But not only that, it would be useful maybe to be able to get the damageCause, for things like fire, entity, custom etc..
Until this is implemented, the LastDamageCause should work though.
I'm not sure there's a simple way to do this. First of all, what is considered "caused by an entity"? If I use shears on a mooshroom, it takes durability, is that "caused by an entity"? For that matter, a player using a tool is also "caused by an entity", itself. What about food on sticks be "used" to point pigs or striders in a direction?
Then secondly, it'd be a kinda large refactor to move stuff around to have access to the entity causing (whatever that means) the durability to change.
I'm not sure there's a simple way to do this. First of all, what is considered "caused by an entity"? If I use shears on a mooshroom, it takes durability, is that "caused by an entity"? For that matter, a player using a tool is also "caused by an entity", itself. What about food on sticks be "used" to point pigs or striders in a direction?
Then secondly, it'd be a kinda large refactor to move stuff around to have access to the entity causing (whatever that means) the durability to change.
I think they are more pointing towards something like shields. If a zombie or something attacked the player and they blocked the attack with a shield etc..
Thanks for the input @OmegaWeaponDev and @Machine-Maker,
I don't think using a tool against an entity should fire this event, it should only be one-way: when an entity does something to a player which causes them to damage one of the player's items, e.g. hitting a player wearing armour.
Players should fire this event too, since they are entities.