From 051e0081f754b8d95940df76f5f9dadbaf9d6f45 Mon Sep 17 00:00:00 2001 From: OhPointFive Date: Sat, 1 Feb 2025 05:03:14 -0500 Subject: [PATCH 1/3] Add score causes Signed-off-by: OhPointFive --- .../event/CompetitorScoreChangeEvent.java | 15 +++++++++- .../tc/oc/pgm/controlpoint/ControlPoint.java | 8 +++-- .../java/tc/oc/pgm/flag/state/Carried.java | 12 ++++++-- .../java/tc/oc/pgm/flag/state/Returned.java | 4 ++- .../oc/pgm/score/MatchPlayerScoreEvent.java | 8 ++++- .../main/java/tc/oc/pgm/score/ScoreCause.java | 11 +++++++ .../tc/oc/pgm/score/ScoreMatchModule.java | 30 +++++++++++-------- .../oc/pgm/variables/types/ScoreVariable.java | 5 +++- 8 files changed, 71 insertions(+), 22 deletions(-) create mode 100644 core/src/main/java/tc/oc/pgm/score/ScoreCause.java diff --git a/core/src/main/java/tc/oc/pgm/api/party/event/CompetitorScoreChangeEvent.java b/core/src/main/java/tc/oc/pgm/api/party/event/CompetitorScoreChangeEvent.java index ee8321ddac..8b85bd2d63 100644 --- a/core/src/main/java/tc/oc/pgm/api/party/event/CompetitorScoreChangeEvent.java +++ b/core/src/main/java/tc/oc/pgm/api/party/event/CompetitorScoreChangeEvent.java @@ -2,6 +2,7 @@ import org.bukkit.event.HandlerList; import tc.oc.pgm.api.party.Competitor; +import tc.oc.pgm.score.ScoreCause; /** * Called when the score of a {@link Competitor} changes. @@ -12,11 +13,14 @@ public class CompetitorScoreChangeEvent extends PartyEvent { private final double oldScore; private final double newScore; + private final ScoreCause cause; - public CompetitorScoreChangeEvent(Competitor competitor, double oldScore, double newScore) { + public CompetitorScoreChangeEvent( + Competitor competitor, double oldScore, double newScore, ScoreCause cause) { super(competitor); this.oldScore = oldScore; this.newScore = newScore; + this.cause = cause; } /** @@ -46,6 +50,15 @@ public double getNewScore() { return this.newScore; } + /** + * Get the {@link ScoreCause} of the change in score + * + * @return The cause of the score change. + */ + public ScoreCause getCause() { + return cause; + } + private static final HandlerList handlers = new HandlerList(); @Override diff --git a/core/src/main/java/tc/oc/pgm/controlpoint/ControlPoint.java b/core/src/main/java/tc/oc/pgm/controlpoint/ControlPoint.java index 9203198f88..921f3adf47 100644 --- a/core/src/main/java/tc/oc/pgm/controlpoint/ControlPoint.java +++ b/core/src/main/java/tc/oc/pgm/controlpoint/ControlPoint.java @@ -25,6 +25,7 @@ import tc.oc.pgm.goals.SimpleGoal; import tc.oc.pgm.goals.events.GoalCompleteEvent; import tc.oc.pgm.goals.events.GoalStatusChangeEvent; +import tc.oc.pgm.score.ScoreCause; import tc.oc.pgm.score.ScoreMatchModule; import tc.oc.pgm.teams.Team; import tc.oc.pgm.teams.TeamMatchModule; @@ -275,7 +276,7 @@ protected void tickScore(Duration duration) { float growth = this.getDefinition().getPointsGrowth(); float rate = (float) (initial * Math.pow(2, seconds / growth)); scoreMatchModule.incrementScore( - this.getControllingTeam(), rate * duration.toMillis() / 1000); + this.getControllingTeam(), rate * duration.toMillis() / 1000, ScoreCause.CONTROL_POINT); } } } @@ -373,10 +374,11 @@ private void dominateAndFireEvents( if (scoreMatchModule != null) { if (oldControllingTeam != null) { scoreMatchModule.incrementScore( - oldControllingTeam, getDefinition().getPointsOwner() * -1); + oldControllingTeam, getDefinition().getPointsOwner() * -1, ScoreCause.CONTROL_POINT); } if (this.controllingTeam != null) { - scoreMatchModule.incrementScore(this.controllingTeam, getDefinition().getPointsOwner()); + scoreMatchModule.incrementScore( + this.controllingTeam, getDefinition().getPointsOwner(), ScoreCause.CONTROL_POINT); } } if (this.controllingTeam == null) { diff --git a/core/src/main/java/tc/oc/pgm/flag/state/Carried.java b/core/src/main/java/tc/oc/pgm/flag/state/Carried.java index ca7716c6d3..617b310c7b 100644 --- a/core/src/main/java/tc/oc/pgm/flag/state/Carried.java +++ b/core/src/main/java/tc/oc/pgm/flag/state/Carried.java @@ -44,6 +44,7 @@ import tc.oc.pgm.flag.event.FlagStateChangeEvent; import tc.oc.pgm.goals.events.GoalEvent; import tc.oc.pgm.kits.Kit; +import tc.oc.pgm.score.ScoreCause; import tc.oc.pgm.score.ScoreMatchModule; import tc.oc.pgm.scoreboard.SidebarMatchModule; import tc.oc.pgm.spawns.events.ParticipantDespawnEvent; @@ -204,7 +205,8 @@ public void tickRunning() { if (smm != null && this.flag.getDefinition().getPointsPerSecond() != 0) { smm.incrementScore( this.getBeneficiary(this.flag.getDefinition().getOwner()), - this.flag.getDefinition().getPointsPerSecond() / 20D); + this.flag.getDefinition().getPointsPerSecond() / 20D, + ScoreCause.FLAG_TICK); } } @@ -251,13 +253,17 @@ protected void captureFlag(NetDefinition net) { ScoreMatchModule smm = this.flag.getMatch().getModule(ScoreMatchModule.class); if (smm != null) { if (net.getPointsPerCapture() != 0) { - smm.incrementScore(this.getBeneficiary(net.getOwner()), net.getPointsPerCapture()); + smm.incrementScore( + this.getBeneficiary(net.getOwner()), + net.getPointsPerCapture(), + ScoreCause.FLAG_CAPTURE); } if (this.flag.getDefinition().getPointsPerCapture() != 0) { smm.incrementScore( this.getBeneficiary(this.flag.getDefinition().getOwner()), - this.flag.getDefinition().getPointsPerCapture()); + this.flag.getDefinition().getPointsPerCapture(), + ScoreCause.FLAG_CAPTURE); } } diff --git a/core/src/main/java/tc/oc/pgm/flag/state/Returned.java b/core/src/main/java/tc/oc/pgm/flag/state/Returned.java index 05d2a8c74e..6a452439af 100644 --- a/core/src/main/java/tc/oc/pgm/flag/state/Returned.java +++ b/core/src/main/java/tc/oc/pgm/flag/state/Returned.java @@ -10,6 +10,7 @@ import tc.oc.pgm.api.player.ParticipantState; import tc.oc.pgm.flag.Flag; import tc.oc.pgm.flag.Post; +import tc.oc.pgm.score.ScoreCause; import tc.oc.pgm.score.ScoreMatchModule; import tc.oc.pgm.teams.TeamMatchModule; @@ -45,7 +46,8 @@ public void tickRunning() { if (smm != null && this.post.getOwner() != null && this.post.getPointsPerSecond() != 0) { smm.incrementScore( this.flag.getMatch().needModule(TeamMatchModule.class).getTeam(this.post.getOwner()), - this.post.getPointsPerSecond() / 20D); + this.post.getPointsPerSecond() / 20D, + ScoreCause.FLAG_TICK); } } diff --git a/core/src/main/java/tc/oc/pgm/score/MatchPlayerScoreEvent.java b/core/src/main/java/tc/oc/pgm/score/MatchPlayerScoreEvent.java index 78f59a125d..7ec3687e54 100644 --- a/core/src/main/java/tc/oc/pgm/score/MatchPlayerScoreEvent.java +++ b/core/src/main/java/tc/oc/pgm/score/MatchPlayerScoreEvent.java @@ -9,16 +9,22 @@ public class MatchPlayerScoreEvent extends MatchPlayerEvent { private static final HandlerList handlers = new HandlerList(); private final double score; + private final ScoreCause cause; - public MatchPlayerScoreEvent(MatchPlayer player, double score) { + public MatchPlayerScoreEvent(MatchPlayer player, double score, ScoreCause cause) { super(player); this.score = score; + this.cause = cause; } public double getScore() { return score; } + public ScoreCause getCause() { + return cause; + } + @Override public HandlerList getHandlers() { return handlers; diff --git a/core/src/main/java/tc/oc/pgm/score/ScoreCause.java b/core/src/main/java/tc/oc/pgm/score/ScoreCause.java new file mode 100644 index 0000000000..944e930434 --- /dev/null +++ b/core/src/main/java/tc/oc/pgm/score/ScoreCause.java @@ -0,0 +1,11 @@ +package tc.oc.pgm.score; + +public enum ScoreCause { + CONTROL_POINT, + DEATH, + FLAG_CAPTURE, + FLAG_TICK, + KILL, + SCOREBOX, + VARIABLE +} diff --git a/core/src/main/java/tc/oc/pgm/score/ScoreMatchModule.java b/core/src/main/java/tc/oc/pgm/score/ScoreMatchModule.java index 0fec2f6310..e577c39685 100644 --- a/core/src/main/java/tc/oc/pgm/score/ScoreMatchModule.java +++ b/core/src/main/java/tc/oc/pgm/score/ScoreMatchModule.java @@ -163,10 +163,16 @@ public void incrementDeath(MatchPlayerDeathEvent event) { // add +1 to killer's team if it was a kill, otherwise -1 to victim's team if (event.isChallengeKill()) { this.incrementScore( - event.getKiller().getId(), event.getKiller().getParty(), this.config.killScore()); + event.getKiller().getId(), + event.getKiller().getParty(), + this.config.killScore(), + ScoreCause.KILL); } else { this.incrementScore( - event.getVictim().getId(), event.getVictim().getCompetitor(), -this.config.deathScore()); + event.getVictim().getId(), + event.getVictim().getCompetitor(), + -this.config.deathScore(), + ScoreCause.DEATH); } } @@ -261,7 +267,7 @@ private void playerScore(ScoreBox box, MatchPlayer player, double points) { if (points == 0) return; - this.incrementScore(player.getId(), player.getCompetitor(), points); + this.incrementScore(player.getId(), player.getCompetitor(), points, ScoreCause.SCOREBOX); box.setLastScoreTime(player, Instant.now()); int wholePoints = (int) points; @@ -277,15 +283,15 @@ private void playerScore(ScoreBox box, MatchPlayer player, double points) { player.playSound(Sounds.SCORE); } - public void incrementScore(UUID player, Competitor competitor, double amount) { + public void incrementScore(UUID player, Competitor competitor, double amount, ScoreCause cause) { double contribution = contributions.get(player) + amount; contributions.put(player, contribution); - incrementScore(competitor, amount); + incrementScore(competitor, amount, cause); MatchPlayer mp = match.getPlayer(player); if (mp == null) return; - match.callEvent(new MatchPlayerScoreEvent(mp, amount)); + match.callEvent(new MatchPlayerScoreEvent(mp, amount, cause)); if (contribution <= PGM.get().getConfiguration().getGriefScore()) { // wait until the next tick to do this so stat recording and other stuff works @@ -298,24 +304,24 @@ public void incrementScore(UUID player, Competitor competitor, double amount) { } } - public void setScore(@NotNull Competitor competitor, double value) { + public void setScore(@NotNull Competitor competitor, double value, ScoreCause cause) { double curr = getScore(competitor); - if (curr != value) setScore(competitor, curr, value); + if (curr != value) setScore(competitor, curr, value, cause); } - public void incrementScore(Competitor competitor, double amount) { + public void incrementScore(Competitor competitor, double amount, ScoreCause cause) { double oldScore = this.scores.get(competitor); double newScore = oldScore + amount; - setScore(competitor, oldScore, newScore); + setScore(competitor, oldScore, newScore, cause); } - private void setScore(Competitor competitor, double oldScore, double newScore) { + private void setScore(Competitor competitor, double oldScore, double newScore, ScoreCause cause) { if (this.config.scoreLimit() > 0 && newScore > this.config.scoreLimit()) { newScore = this.config.scoreLimit(); } CompetitorScoreChangeEvent event = - new CompetitorScoreChangeEvent(competitor, oldScore, newScore); + new CompetitorScoreChangeEvent(competitor, oldScore, newScore, cause); this.match.callEvent(event); this.scores.put(competitor, event.getNewScore()); diff --git a/core/src/main/java/tc/oc/pgm/variables/types/ScoreVariable.java b/core/src/main/java/tc/oc/pgm/variables/types/ScoreVariable.java index 69f0ad5123..7a64feaf13 100644 --- a/core/src/main/java/tc/oc/pgm/variables/types/ScoreVariable.java +++ b/core/src/main/java/tc/oc/pgm/variables/types/ScoreVariable.java @@ -2,6 +2,7 @@ import tc.oc.pgm.api.party.Competitor; import tc.oc.pgm.api.party.Party; +import tc.oc.pgm.score.ScoreCause; import tc.oc.pgm.score.ScoreMatchModule; public class ScoreVariable extends AbstractVariable { @@ -24,6 +25,8 @@ protected double getValueImpl(Party party) { @Override protected void setValueImpl(Party party, double value) { if (party instanceof Competitor c) - party.moduleOptional(ScoreMatchModule.class).ifPresent(smm -> smm.setScore(c, value)); + party + .moduleOptional(ScoreMatchModule.class) + .ifPresent(smm -> smm.setScore(c, value, ScoreCause.VARIABLE)); } } From 710b8cf776863ad09215c0c3121eecd954b528af Mon Sep 17 00:00:00 2001 From: OhPointFive Date: Sun, 2 Feb 2025 19:45:00 -0500 Subject: [PATCH 2/3] Split up control point score causes Signed-off-by: OhPointFive --- .../java/tc/oc/pgm/controlpoint/ControlPoint.java | 12 +++++++++--- core/src/main/java/tc/oc/pgm/score/ScoreCause.java | 4 +++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/controlpoint/ControlPoint.java b/core/src/main/java/tc/oc/pgm/controlpoint/ControlPoint.java index 921f3adf47..47f3e8fc25 100644 --- a/core/src/main/java/tc/oc/pgm/controlpoint/ControlPoint.java +++ b/core/src/main/java/tc/oc/pgm/controlpoint/ControlPoint.java @@ -276,7 +276,9 @@ protected void tickScore(Duration duration) { float growth = this.getDefinition().getPointsGrowth(); float rate = (float) (initial * Math.pow(2, seconds / growth)); scoreMatchModule.incrementScore( - this.getControllingTeam(), rate * duration.toMillis() / 1000, ScoreCause.CONTROL_POINT); + this.getControllingTeam(), + rate * duration.toMillis() / 1000, + ScoreCause.CONTROL_POINT_TICK); } } } @@ -374,11 +376,15 @@ private void dominateAndFireEvents( if (scoreMatchModule != null) { if (oldControllingTeam != null) { scoreMatchModule.incrementScore( - oldControllingTeam, getDefinition().getPointsOwner() * -1, ScoreCause.CONTROL_POINT); + oldControllingTeam, + getDefinition().getPointsOwner() * -1, + ScoreCause.CONTROL_POINT_OWNED); } if (this.controllingTeam != null) { scoreMatchModule.incrementScore( - this.controllingTeam, getDefinition().getPointsOwner(), ScoreCause.CONTROL_POINT); + this.controllingTeam, + getDefinition().getPointsOwner(), + ScoreCause.CONTROL_POINT_LOST); } } if (this.controllingTeam == null) { diff --git a/core/src/main/java/tc/oc/pgm/score/ScoreCause.java b/core/src/main/java/tc/oc/pgm/score/ScoreCause.java index 944e930434..73f6457c4f 100644 --- a/core/src/main/java/tc/oc/pgm/score/ScoreCause.java +++ b/core/src/main/java/tc/oc/pgm/score/ScoreCause.java @@ -1,7 +1,9 @@ package tc.oc.pgm.score; public enum ScoreCause { - CONTROL_POINT, + CONTROL_POINT_TICK, + CONTROL_POINT_OWNED, + CONTROL_POINT_LOST, DEATH, FLAG_CAPTURE, FLAG_TICK, From a59aaf939dd8129c6da2dab1b602188c9682df31 Mon Sep 17 00:00:00 2001 From: OhPointFive Date: Mon, 3 Feb 2025 17:30:06 -0500 Subject: [PATCH 3/3] Split flag score causes Signed-off-by: OhPointFive --- core/src/main/java/tc/oc/pgm/flag/state/Carried.java | 2 +- core/src/main/java/tc/oc/pgm/flag/state/Returned.java | 2 +- core/src/main/java/tc/oc/pgm/score/ScoreCause.java | 7 ++++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/flag/state/Carried.java b/core/src/main/java/tc/oc/pgm/flag/state/Carried.java index 617b310c7b..18595382cd 100644 --- a/core/src/main/java/tc/oc/pgm/flag/state/Carried.java +++ b/core/src/main/java/tc/oc/pgm/flag/state/Carried.java @@ -206,7 +206,7 @@ public void tickRunning() { smm.incrementScore( this.getBeneficiary(this.flag.getDefinition().getOwner()), this.flag.getDefinition().getPointsPerSecond() / 20D, - ScoreCause.FLAG_TICK); + ScoreCause.FLAG_CARRIED_TICK); } } diff --git a/core/src/main/java/tc/oc/pgm/flag/state/Returned.java b/core/src/main/java/tc/oc/pgm/flag/state/Returned.java index 6a452439af..05966d48ad 100644 --- a/core/src/main/java/tc/oc/pgm/flag/state/Returned.java +++ b/core/src/main/java/tc/oc/pgm/flag/state/Returned.java @@ -47,7 +47,7 @@ public void tickRunning() { smm.incrementScore( this.flag.getMatch().needModule(TeamMatchModule.class).getTeam(this.post.getOwner()), this.post.getPointsPerSecond() / 20D, - ScoreCause.FLAG_TICK); + ScoreCause.FLAG_RETURNED_TICK); } } diff --git a/core/src/main/java/tc/oc/pgm/score/ScoreCause.java b/core/src/main/java/tc/oc/pgm/score/ScoreCause.java index 73f6457c4f..d4b8e251ef 100644 --- a/core/src/main/java/tc/oc/pgm/score/ScoreCause.java +++ b/core/src/main/java/tc/oc/pgm/score/ScoreCause.java @@ -1,12 +1,13 @@ package tc.oc.pgm.score; public enum ScoreCause { - CONTROL_POINT_TICK, - CONTROL_POINT_OWNED, CONTROL_POINT_LOST, + CONTROL_POINT_OWNED, + CONTROL_POINT_TICK, DEATH, FLAG_CAPTURE, - FLAG_TICK, + FLAG_CARRIED_TICK, + FLAG_RETURNED_TICK, KILL, SCOREBOX, VARIABLE