79 lines
5.5 KiB
Java
79 lines
5.5 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; boolean closed;
|
|
Grant(AbilityExecution execution, LivingEntity target, BigDecimal percent) {
|
|
this.sequence=++sequences;this.execution=execution;this.target=target;this.percent=percent;
|
|
}
|
|
@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());}}
|
|
}
|
|
private record Prevention(Grant grant, Entity recipient, BigDecimal amount) { }
|
|
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){
|
|
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);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(),prevented));
|
|
}
|
|
|
|
@EventHandler(priority=EventPriority.MONITOR)
|
|
public void accepted(EntityDamageEvent event){
|
|
var prevention=pending.remove(event);if(prevention==null||event.isCancelled()||!prevention.grant.execution.stillCurrent())return;
|
|
prevention.grant.execution.report(AbilityReport.Type.PREVENTION,prevention.grant.target,prevention.amount);
|
|
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,prevention.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();}
|
|
}
|