Class DamageContexts
DamageRequest from the plugin that deals a hit to the plugin that calculates it.
The dealer opens a scope around its call to damage():
try (DamageScope scope = DamageContexts.open(target, request)) {
target.damage(request.getTotal(), caster);
}
A listener of the resulting damage event asks for the request with claim(EntityDamageEvent).
The request belongs to the first damage event of the target that claims it. Every listener of that same event gets it; any other event gets nothing, whether it is a second hit on the same target from inside the first one, or a hit on a different entity. If nothing claims the request, nothing happens: the hit is ordinary damage.
This class does not listen to anything and is not thread safe. Use it from the server thread only.
-
Method Summary
Modifier and TypeMethodDescriptionstatic DamageRequestclaim(org.bukkit.event.entity.EntityDamageEvent event) Returns the request that describes this damage event, if there is one.static booleanisAnnounced(org.bukkit.entity.Entity target) Whether a request is open for the entity and has not been claimed yet.static DamageScopeopen(org.bukkit.entity.Entity target, DamageRequest request) Announces a hit on the target.static DamageRequestpeek(org.bukkit.event.entity.EntityDamageEvent event) Returns the request thatclaim(EntityDamageEvent)would return for this event, without claiming it.
-
Method Details
-
open
@NotNull public static DamageScope open(@NotNull org.bukkit.entity.Entity target, @NotNull DamageRequest request) Announces a hit on the target. Close the returned scope when the call todamage()returns.- Parameters:
target- the entity about to be damagedrequest- the description of the hit- Returns:
- the scope to close
-
claim
@Nullable public static DamageRequest claim(@NotNull org.bukkit.event.entity.EntityDamageEvent event) Returns the request that describes this damage event, if there is one.The first event to ask takes the most recently opened request for its entity that has not been taken yet. Asking again with the same event returns the same request, so several listeners can read it.
Do not call this for an event that is not a real hit, such as one a plugin fires only to ask whether an attack would be allowed. It would take the request away from the real hit.
- Parameters:
event- the damage event being handled- Returns:
- the request for this event, or null if the event was not announced
-
peek
@Nullable public static DamageRequest peek(@NotNull org.bukkit.event.entity.EntityDamageEvent event) Returns the request thatclaim(EntityDamageEvent)would return for this event, without claiming it.Use this to decide something about a hit before the plugin that calculates it has run, for example whether the hit asks for
WeaponStatsMode.RAW. A laterclaimwith the same event still gets the same request, unless another event claims it first.- Parameters:
event- the damage event being handled- Returns:
- the request this event holds or would take, or null if the event was not announced
-
isAnnounced
public static boolean isAnnounced(@NotNull org.bukkit.entity.Entity target) Whether a request is open for the entity and has not been claimed yet.
-