Class DamageContexts

java.lang.Object
studio.magemonkey.codex.api.damage.DamageContexts

public final class DamageContexts extends Object
Hands a 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 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 to damage() returns.
      Parameters:
      target - the entity about to be damaged
      request - 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
    • 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.