Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix damage history order not being respected #1460

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion core/src/main/java/tc/oc/pgm/damagehistory/DamageEntry.java
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,42 +49,30 @@ public Deque<DamageEntry> getDamageHistory(MatchPlayer player) {
}

public @Nullable ParticipantState getAssister(MatchPlayer player) {
Deque<DamageEntry> damageHistory = getDamageHistory(player);
if (damageHistory == null || damageHistory.size() <= 1) return null;
Deque<DamageEntry> 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<Map.Entry<DamageHistoryKey, Double>> 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<DamageHistoryKey, Double> 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)
Expand Down
Loading