diff --git a/core/src/main/java/tc/oc/pgm/damagehistory/DamageEntry.java b/core/src/main/java/tc/oc/pgm/damagehistory/DamageEntry.java index f60e089cbd..1aee6126ec 100644 --- a/core/src/main/java/tc/oc/pgm/damagehistory/DamageEntry.java +++ b/core/src/main/java/tc/oc/pgm/damagehistory/DamageEntry.java @@ -1,11 +1,15 @@ package tc.oc.pgm.damagehistory; +import java.util.UUID; import org.jetbrains.annotations.Nullable; +import tc.oc.pgm.api.player.MatchPlayer; import tc.oc.pgm.api.player.ParticipantState; public class DamageEntry { - @Nullable private ParticipantState damager; + @Nullable + private ParticipantState damager; + private double damage; public DamageEntry(@Nullable ParticipantState damager, double damage) { @@ -30,4 +34,11 @@ public void addDamage(@Nullable ParticipantState damager, double damage) { public void removeDamage(double damage) { this.damage -= damage; } + + public boolean canAssist(MatchPlayer victim, ParticipantState killer) { + // Filter out damage without players, or damage from self or killer + if (damager == null) return false; + UUID dmg = damager.getId(); + return damager != null && !dmg.equals(victim.getId()) && !dmg.equals(killer.getId()); + } } diff --git a/core/src/main/java/tc/oc/pgm/damagehistory/DamageHistoryMatchModule.java b/core/src/main/java/tc/oc/pgm/damagehistory/DamageHistoryMatchModule.java index 40daaf854d..969763ab0d 100644 --- a/core/src/main/java/tc/oc/pgm/damagehistory/DamageHistoryMatchModule.java +++ b/core/src/main/java/tc/oc/pgm/damagehistory/DamageHistoryMatchModule.java @@ -2,11 +2,9 @@ import static tc.oc.pgm.util.nms.PlayerUtils.PLAYER_UTILS; -import java.util.Collections; import java.util.Deque; -import java.util.List; +import java.util.LinkedHashMap; import java.util.Map; -import java.util.Set; import java.util.stream.Collectors; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; @@ -51,42 +49,30 @@ public Deque getDamageHistory(MatchPlayer player) { } public @Nullable ParticipantState getAssister(MatchPlayer player) { - Deque damageHistory = getDamageHistory(player); - if (damageHistory == null || damageHistory.size() <= 1) return null; + Deque history = getDamageHistory(player); + if (history == null || history.size() <= 1) return null; - ParticipantState killer = damageHistory.getLast().getDamager(); + ParticipantState killer = history.getLast().getDamager(); if (killer == null) return null; - double damageReceived = damageHistory.stream().mapToDouble(DamageEntry::getDamage).sum(); - - Collections.reverse((List) damageHistory); - - Set> entries = - damageHistory.stream() - // Filter out damage without players, or damage from self or killer - .filter( - historicDamage -> { - ParticipantState damager = historicDamage.getDamager(); - return !(damager == null - || damager.getId().equals(player.getId()) - || damager.getId().equals(killer.getId())); - }) - .collect( - Collectors.groupingBy( - DamageHistoryKey::from, - Collectors.mapping( - DamageEntry::getDamage, Collectors.reducing(0d, Double::sum)))) - .entrySet(); - - Map.Entry highestDamager = - entries.stream().max(Map.Entry.comparingByValue()).orElse(null); - - if (highestDamager == null - || highestDamager.getValue() - < (damageReceived * PGM.get().getConfiguration().getAssistPercent()) - || highestDamager.getKey().getParty().equals(player.getParty())) return null; - - return highestDamager.getKey().getState(); + double total = history.stream().mapToDouble(DamageEntry::getDamage).sum(); + + var highest = history.reversed().stream() + .filter(dmg -> dmg.canAssist(player, killer)) + .collect(Collectors.groupingBy( + DamageHistoryKey::from, + LinkedHashMap::new, + Collectors.mapping(DamageEntry::getDamage, Collectors.reducing(0d, Double::sum)))) + .entrySet() + .stream() + .max(Map.Entry.comparingByValue()) + .orElse(null); + + if (highest == null + || highest.getValue() < (total * PGM.get().getConfiguration().getAssistPercent()) + || highest.getKey().getParty().equals(player.getParty())) return null; + + return highest.getKey().getState(); } @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)