102 lines
6.9 KiB
Java
102 lines
6.9 KiB
Java
package games.dmg.fantasystick.ability;
|
|
|
|
import games.dmg.fantasystick.party.PartyRoster;
|
|
import games.dmg.fantasystick.session.EquipmentSessions;
|
|
import java.math.BigDecimal;
|
|
import java.util.ArrayList;
|
|
import java.util.IdentityHashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
import java.util.UUID;
|
|
import java.util.function.Consumer;
|
|
import org.bukkit.entity.Entity;
|
|
import org.bukkit.entity.LivingEntity;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.entity.Projectile;
|
|
import org.bukkit.event.EventHandler;
|
|
import org.bukkit.event.EventPriority;
|
|
import org.bukkit.event.Listener;
|
|
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
|
import org.bukkit.event.entity.EntityDamageEvent;
|
|
import org.bukkit.projectiles.ProjectileSource;
|
|
|
|
/** Internal pre-recipient strongest outgoing-damage reduction stage. */
|
|
public final class OutgoingDamageMitigation implements Listener, AutoCloseable {
|
|
public record Support(UUID target, UUID supportingCharacter, EquipmentSessions.Active aided, BigDecimal amount) { }
|
|
private final class Grant implements AutoCloseable {
|
|
final long sequence; final AbilityExecution execution; final LivingEntity target; final BigDecimal percent;
|
|
final Consumer<Accepted> observation; boolean closed;
|
|
Grant(AbilityExecution execution, LivingEntity target, BigDecimal percent, Consumer<Accepted> observation) {
|
|
this.sequence=++sequences;this.execution=execution;this.target=target;this.percent=percent;
|
|
this.observation = observation;
|
|
}
|
|
@Override public void close(){if(closed)return;closed=true;var owned=grants.get(target.getUniqueId());
|
|
if(owned!=null){owned.remove(this);if(owned.isEmpty())grants.remove(target.getUniqueId());}}
|
|
}
|
|
public record Accepted(LivingEntity debuffedAttacker, Entity recipient,
|
|
double incoming, double remaining, double prevented) { }
|
|
private record Prevention(Grant grant, Entity recipient, double incoming, double reduced) { }
|
|
private final EquipmentSessions sessions; private final PartyRoster parties; private final Consumer<Support> supports;
|
|
private final Map<UUID,List<Grant>> grants=new java.util.HashMap<>();
|
|
private final Map<EntityDamageEvent,Prevention> pending=new IdentityHashMap<>();
|
|
private long sequences; private boolean closed;
|
|
public OutgoingDamageMitigation(EquipmentSessions sessions,PartyRoster parties,Consumer<Support> supports){
|
|
this.sessions=Objects.requireNonNull(sessions);this.parties=Objects.requireNonNull(parties);this.supports=Objects.requireNonNull(supports);}
|
|
public AutoCloseable grant(AbilityExecution execution, LivingEntity target, BigDecimal percent) {
|
|
return grantObserved(execution, target, percent, null);
|
|
}
|
|
public AutoCloseable grantObserved(AbilityExecution execution, LivingEntity target, BigDecimal percent,
|
|
Consumer<Accepted> observation) {
|
|
Objects.requireNonNull(execution); Objects.requireNonNull(target); Objects.requireNonNull(percent);
|
|
if (closed || percent.signum() < 0 || percent.compareTo(BigDecimal.valueOf(100)) > 0)
|
|
throw new IllegalArgumentException("Invalid outgoing mitigation grant");
|
|
var grant = new Grant(execution, target, percent, observation);
|
|
grants.computeIfAbsent(target.getUniqueId(), ignored -> new ArrayList<>()).add(grant);
|
|
return grant;
|
|
}
|
|
|
|
@EventHandler(priority=EventPriority.NORMAL,ignoreCancelled=true)
|
|
public void reduce(EntityDamageByEntityEvent event){
|
|
if(closed||GuardianBond.redirectedDamage())return;Entity attacker=attacker(event);if(attacker==null)return;var owned=grants.get(attacker.getUniqueId());if(owned==null)return;
|
|
Grant strongest=null;for(var grant:List.copyOf(owned)){if(grant.closed||grant.target!=attacker||!grant.execution.stillCurrent())continue;
|
|
if(strongest==null||grant.percent.compareTo(strongest.percent)>0
|
|
||grant.percent.compareTo(strongest.percent)==0&&grant.sequence>strongest.sequence)strongest=grant;}
|
|
double original=event.getDamage();if(strongest==null||!Double.isFinite(original)||original<=0)return;
|
|
double reduced=BigDecimal.valueOf(original).multiply(BigDecimal.ONE.subtract(strongest.percent.movePointLeft(2))).doubleValue();
|
|
if(!Double.isFinite(reduced)||reduced<0)return;event.setDamage(reduced);var prevented=BigDecimal.valueOf(original-reduced);
|
|
if(prevented.signum()>0)pending.put(event,new Prevention(strongest,event.getEntity(),original,reduced));
|
|
}
|
|
|
|
@EventHandler(priority=EventPriority.MONITOR)
|
|
public void accepted(EntityDamageEvent event){
|
|
var prevention = pending.remove(event);
|
|
if (prevention == null || event.isCancelled() || !Double.isFinite(event.getFinalDamage())
|
|
|| event.getFinalDamage() <= 0 || !prevention.grant.execution.stillCurrent()) return;
|
|
double remaining = event.getDamage();
|
|
double actual = Math.min(prevention.incoming - prevention.reduced, prevention.incoming - remaining);
|
|
if (!Double.isFinite(remaining) || !Double.isFinite(actual) || actual <= 0) return;
|
|
var amount = BigDecimal.valueOf(actual);
|
|
try { prevention.grant.execution.report(AbilityReport.Type.PREVENTION, prevention.grant.target, amount); }
|
|
catch (RuntimeException ignored) { /* Feedback cannot interrupt accepted native prevention. */ }
|
|
if (prevention.grant.observation != null) try {
|
|
prevention.grant.observation.accept(new Accepted(prevention.grant.target,
|
|
prevention.recipient, prevention.incoming, remaining, actual));
|
|
} catch (RuntimeException ignored) { /* Audit cannot alter damage or rewards. */ }
|
|
if (!(prevention.recipient instanceof Player player)) return;
|
|
var aided=sessions.active(player.getUniqueId()).orElse(null);if(aided==null||!sessions.current(aided))return;
|
|
var input=prevention.grant.execution.input();if(aided.character().equals(input.session().character()))return;
|
|
var sourceParty=parties.partyOf(input.player().getUniqueId()).orElse(null);
|
|
var aidedParty=parties.partyOf(player.getUniqueId()).orElse(null);
|
|
if(sourceParty==null||aidedParty==null||!sourceParty.id().equals(aidedParty.id()))return;
|
|
supports.accept(new Support(prevention.grant.target.getUniqueId(),input.session().character(),aided,amount));
|
|
}
|
|
|
|
private static Entity attacker(EntityDamageByEntityEvent event){
|
|
Entity causing=event.getDamageSource().getCausingEntity();if(causing!=null)return causing;
|
|
if(event.getDamager() instanceof Projectile projectile){ProjectileSource shooter=projectile.getShooter();if(shooter instanceof Entity entity)return entity;}
|
|
return event.getDamager();
|
|
}
|
|
@Override public void close(){if(closed)return;closed=true;for(var owned:List.copyOf(grants.values()))for(var grant:List.copyOf(owned))grant.close();grants.clear();pending.clear();}
|
|
}
|