Skip to content

Commit

Permalink
Add cause to score-related events (#1487)
Browse files Browse the repository at this point in the history
Signed-off-by: OhPointFive <[email protected]>
  • Loading branch information
OhPointFive authored Feb 3, 2025
1 parent 3d1df1f commit 8e6caaf
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions core/src/main/java/tc/oc/pgm/controlpoint/ControlPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -275,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);
this.getControllingTeam(),
rate * duration.toMillis() / 1000,
ScoreCause.CONTROL_POINT_TICK);
}
}
}
Expand Down Expand Up @@ -373,10 +376,15 @@ private void dominateAndFireEvents(
if (scoreMatchModule != null) {
if (oldControllingTeam != null) {
scoreMatchModule.incrementScore(
oldControllingTeam, getDefinition().getPointsOwner() * -1);
oldControllingTeam,
getDefinition().getPointsOwner() * -1,
ScoreCause.CONTROL_POINT_OWNED);
}
if (this.controllingTeam != null) {
scoreMatchModule.incrementScore(this.controllingTeam, getDefinition().getPointsOwner());
scoreMatchModule.incrementScore(
this.controllingTeam,
getDefinition().getPointsOwner(),
ScoreCause.CONTROL_POINT_LOST);
}
}
if (this.controllingTeam == null) {
Expand Down
12 changes: 9 additions & 3 deletions core/src/main/java/tc/oc/pgm/flag/state/Carried.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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_CARRIED_TICK);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down
4 changes: 3 additions & 1 deletion core/src/main/java/tc/oc/pgm/flag/state/Returned.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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_RETURNED_TICK);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions core/src/main/java/tc/oc/pgm/score/ScoreCause.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package tc.oc.pgm.score;

public enum ScoreCause {
CONTROL_POINT_LOST,
CONTROL_POINT_OWNED,
CONTROL_POINT_TICK,
DEATH,
FLAG_CAPTURE,
FLAG_CARRIED_TICK,
FLAG_RETURNED_TICK,
KILL,
SCOREBOX,
VARIABLE
}
30 changes: 18 additions & 12 deletions core/src/main/java/tc/oc/pgm/score/ScoreMatchModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Party> {
Expand All @@ -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));
}
}

0 comments on commit 8e6caaf

Please sign in to comment.