Files
purpur-fantasy-stick/src/main/java/games/dmg/fantasystick/ability/OutgoingDamageBonus.java
T
dmg c80c088945
Release / release (push) Successful in 7m59s
feat(ability): add rallying cry
2026-09-20 17:49:12 -04:00

115 lines
6.2 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.BooleanSupplier;
import java.util.function.Consumer;
import org.bukkit.entity.Entity;
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;
/** Strongest deterministic Fantasy Stick outgoing combat bonus stage. */
public final class OutgoingDamageBonus 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 Player recipient;
final BigDecimal percent; final BooleanSupplier current; boolean closed;
Grant(AbilityExecution execution, Player recipient, BigDecimal percent, BooleanSupplier current) {
sequence = ++sequences; this.execution = execution; this.recipient = recipient;
this.percent = percent; this.current = current;
}
boolean valid() {
try { return !closed && execution.stillCurrent() && current.getAsBoolean(); }
catch (RuntimeException invalid) { return false; }
}
@Override public void close() {
if (closed) return; closed = true;
var owned = grants.get(recipient.getUniqueId());
if (owned != null) { owned.remove(this); if (owned.isEmpty()) grants.remove(recipient.getUniqueId()); }
}
}
private record Used(Grant grant, Entity target, 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, Used> pending = new IdentityHashMap<>();
private long sequences;
private boolean closed;
public OutgoingDamageBonus(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, Player recipient, BigDecimal percent, BooleanSupplier current) {
Objects.requireNonNull(execution); Objects.requireNonNull(recipient);
Objects.requireNonNull(percent); Objects.requireNonNull(current);
if (closed || percent.signum() < 0 || percent.compareTo(BigDecimal.valueOf(1000)) > 0) {
throw new IllegalArgumentException("Invalid outgoing damage bonus");
}
var grant = new Grant(execution, recipient, percent, current);
grants.computeIfAbsent(recipient.getUniqueId(), ignored -> new ArrayList<>()).add(grant); return grant;
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void increase(EntityDamageByEntityEvent event) {
if (closed || GuardianBond.redirectedDamage()) return;
Entity attacker = attacker(event); if (!(attacker instanceof Player player)) return;
var owned = grants.get(player.getUniqueId()); if (owned == null) return;
Grant strongest = null;
for (var grant : List.copyOf(owned)) {
if (grant.recipient != player || !grant.valid()) 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;
BigDecimal added = BigDecimal.valueOf(original).multiply(strongest.percent.movePointLeft(2));
double increased = BigDecimal.valueOf(original).add(added).doubleValue();
if (!Double.isFinite(increased) || increased < 0 || added.signum() <= 0) return;
event.setDamage(increased); pending.put(event, new Used(strongest, event.getEntity(), added));
}
@EventHandler(priority = EventPriority.MONITOR)
public void accepted(EntityDamageEvent event) {
var used = pending.remove(event);
if (used == null || event.isCancelled() || event.getFinalDamage() <= 0 || !used.grant.valid()) return;
used.grant.execution.report(AbilityReport.Type.BOOST_USED, used.target, used.amount);
var aided = sessions.active(used.grant.recipient.getUniqueId()).filter(sessions::current).orElse(null);
if (aided == null || aided.character().equals(used.grant.execution.input().session().character())) return;
var sourceParty = parties.partyOf(used.grant.execution.input().player().getUniqueId()).orElse(null);
var aidedParty = parties.partyOf(used.grant.recipient.getUniqueId()).orElse(null);
if (sourceParty == null || aidedParty == null || !sourceParty.id().equals(aidedParty.id())) return;
supports.accept(new Support(used.target.getUniqueId(), used.grant.execution.input().session().character(), aided, used.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();
}
}