Skip to content

Commit

Permalink
Implement score and team variables
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Herrera <[email protected]>
  • Loading branch information
Pablete1234 committed Aug 19, 2023
1 parent 62fb585 commit a931d3c
Show file tree
Hide file tree
Showing 25 changed files with 359 additions and 162 deletions.
3 changes: 1 addition & 2 deletions core/src/main/java/tc/oc/pgm/broadcast/BroadcastModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public BroadcastModule parse(MapFactory factory, Logger logger, Document doc)
for (Element elBroadcast : elBroadcasts.getChildren()) {
final Node nodeBroadcast = new Node(elBroadcast);
Broadcast.Type type =
XMLUtils.parseEnum(
nodeBroadcast, elBroadcast.getName(), Broadcast.Type.class, "broadcast type");
XMLUtils.parseEnum(nodeBroadcast, elBroadcast.getName(), Broadcast.Type.class);

Component message = XMLUtils.parseFormattedText(nodeBroadcast);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ public Collection<Class<? extends MapModule<?>>> getWeakDependencies() {
actionParser.parseReference(actionNode, MatchPlayer.class);

ConsumeCause cause =
XMLUtils.parseEnum(
Node.fromRequiredAttr(consumableElement, "on"),
ConsumeCause.class,
"consume cause");
XMLUtils.parseEnum(Node.fromRequiredAttr(consumableElement, "on"), ConsumeCause.class);

ConsumableDefinition consumableDefinition =
new ConsumableDefinition(id, action, cause, override);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ public static ControlPointDefinition parseControlPoint(
XMLUtils.parseEnum(
Node.fromAttr(el, "capture-rule"),
ControlPointDefinition.CaptureCondition.class,
"capture rule",
ControlPointDefinition.CaptureCondition.EXCLUSIVE);

if (pd) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public EnderChestModule parse(MapFactory factory, Logger logger, Document doc)
XMLUtils.parseEnum(
Node.fromAttr(enderchestEl, "fallback"),
DropoffFallback.class,
"fallback",
DropoffFallback.AUTO);
enabled = true;
}
Expand Down
10 changes: 4 additions & 6 deletions core/src/main/java/tc/oc/pgm/filters/parse/FilterParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public VoidFilter parseVoid(Element el) throws InvalidXMLException {

@MethodParser("entity")
public EntityTypeFilter parseEntity(Element el) throws InvalidXMLException {
return new EntityTypeFilter(XMLUtils.parseEnum(el, EntityType.class, "entity type"));
return new EntityTypeFilter(XMLUtils.parseEnum(el, EntityType.class));
}

@MethodParser("mob")
Expand All @@ -309,8 +309,7 @@ public EntityTypeFilter parseMob(Element el) throws InvalidXMLException {

@MethodParser("spawn")
public SpawnReasonFilter parseSpawnReason(Element el) throws InvalidXMLException {
return new SpawnReasonFilter(
XMLUtils.parseEnum(new Node(el), SpawnReason.class, "spawn reason"));
return new SpawnReasonFilter(XMLUtils.parseEnum(new Node(el), SpawnReason.class));
}

@MethodParser("kill-streak")
Expand Down Expand Up @@ -465,13 +464,12 @@ public CarryingFlagFilter parseCarryingFlag(Element el) throws InvalidXMLExcepti

@MethodParser("cause")
public CauseFilter parseCause(Element el) throws InvalidXMLException {
return new CauseFilter(XMLUtils.parseEnum(el, CauseFilter.Cause.class, "cause filter"));
return new CauseFilter(XMLUtils.parseEnum(el, CauseFilter.Cause.class));
}

@MethodParser("relation")
public RelationFilter parseRelation(Element el) throws InvalidXMLException {
return new RelationFilter(
XMLUtils.parseEnum(el, PlayerRelation.class, "player relation filter"));
return new RelationFilter(XMLUtils.parseEnum(el, PlayerRelation.class));
}

@MethodParser("carrying")
Expand Down
5 changes: 1 addition & 4 deletions core/src/main/java/tc/oc/pgm/goals/ProximityMetric.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ public int hashCode() {

ProximityMetric.Type type =
XMLUtils.parseEnum(
Node.fromAttr(el, prefix + "proximity-metric"),
ProximityMetric.Type.class,
"proximity metric",
def.type);
Node.fromAttr(el, prefix + "proximity-metric"), ProximityMetric.Type.class, def.type);

// If proximity metric is none, use null proximity so that it doesn't try to get tracked nor
// shows in the scoreboard
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/tc/oc/pgm/kits/KitParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,7 @@ public ItemStack parseFirework(Element el) throws InvalidXMLException {
meta.setPower(power);

for (Element explosionEl : el.getChildren("explosion")) {
Type type =
XMLUtils.parseEnum(Node.fromAttr(explosionEl, "type"), Type.class, null, Type.BURST);
Type type = XMLUtils.parseEnum(Node.fromAttr(explosionEl, "type"), Type.class, Type.BURST);
boolean flicker = XMLUtils.parseBoolean(Node.fromAttr(explosionEl, "flicker"), false);
boolean trail = XMLUtils.parseBoolean(Node.fromAttr(explosionEl, "trail"), false);

Expand Down
8 changes: 2 additions & 6 deletions core/src/main/java/tc/oc/pgm/map/MapInfoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,13 @@ public MapInfoImpl(MapSource source, Element root) throws InvalidXMLException {
this.rules = parseRules(root);
this.difficulty =
XMLUtils.parseEnum(
Node.fromLastChildOrAttr(root, "difficulty"),
Difficulty.class,
"difficulty",
Difficulty.NORMAL)
Node.fromLastChildOrAttr(root, "difficulty"), Difficulty.class, Difficulty.NORMAL)
.ordinal();
this.world = parseWorld(root);
this.gamemode = XMLUtils.parseFormattedText(root, "game");
this.gamemodes = parseGamemodes(root);
this.phase =
XMLUtils.parseEnum(
Node.fromLastChildOrAttr(root, "phase"), Phase.class, "phase", Phase.PRODUCTION);
XMLUtils.parseEnum(Node.fromLastChildOrAttr(root, "phase"), Phase.class, Phase.PRODUCTION);
this.friendlyFire =
XMLUtils.parseBoolean(
Node.fromLastChildOrAttr(root, "friendlyfire", "friendly-fire"), false);
Expand Down
1 change: 0 additions & 1 deletion core/src/main/java/tc/oc/pgm/map/WorldInfoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public WorldInfoImpl(Element element) throws InvalidXMLException {
XMLUtils.parseEnum(
Node.fromLastChildOrAttr(element, "environment"),
World.Environment.class,
"environment",
World.Environment.NORMAL)
.ordinal());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ public ProjectileModule parse(MapFactory factory, Logger logger, Document doc)
Node.fromChildOrAttr(projectileElement, "velocity"), Double.class, 1.0);
ClickAction clickAction =
XMLUtils.parseEnum(
Node.fromAttr(projectileElement, "click"),
ClickAction.class,
"click action",
ClickAction.BOTH);
Node.fromAttr(projectileElement, "click"), ClickAction.class, ClickAction.BOTH);
Class<? extends Entity> entity =
XMLUtils.parseEntityTypeAttribute(projectileElement, "projectile", Arrow.class);
List<PotionEffect> potionKit = kitParser.parsePotions(projectileElement);
Expand Down
19 changes: 10 additions & 9 deletions core/src/main/java/tc/oc/pgm/score/MercyRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Map;
import tc.oc.pgm.api.party.Competitor;
import tc.oc.pgm.api.party.event.CompetitorScoreChangeEvent;
import tc.oc.pgm.util.Pair;

public class MercyRule {

Expand All @@ -12,8 +13,8 @@ public class MercyRule {
private final int mercyLimit;
private final int mercyLimitMin;

private Map.Entry<Competitor, Double> leader;
private Map.Entry<Competitor, Double> trailer;
private Pair<Competitor, Double> leader;
private Pair<Competitor, Double> trailer;

public MercyRule(
ScoreMatchModule scoreMatchModule, int scoreLimit, int mercyLimit, int mercyLimitMin) {
Expand All @@ -26,27 +27,27 @@ public MercyRule(
}

private double getLeaderScore() {
return leader.getValue();
return leader.getRight();
}

private double getTrailerScore() {
return trailer.getValue();
return trailer.getRight();
}

private void setLeader(Competitor competitor, Double score) {
leader = new AbstractMap.SimpleEntry<>(competitor, score);
leader = Pair.of(competitor, score);
}

private void setTrailer(Competitor competitor, Double score) {
trailer = new AbstractMap.SimpleEntry<>(competitor, score);
trailer = Pair.of(competitor, score);
}

private boolean isLeader(Competitor competitor) {
return competitor.equals(leader.getKey());
return competitor.equals(leader.getLeft());
}

private boolean isTrailer(Competitor competitor) {
return competitor.equals(trailer.getKey());
return competitor.equals(trailer.getLeft());
}

public int getScoreLimit() {
Expand Down Expand Up @@ -83,7 +84,7 @@ public void handleEvent(CompetitorScoreChangeEvent event) {
if (event.getOldScore() > event.getNewScore()) {
if (isLeader(event.getCompetitor())
|| isTrailer(event.getCompetitor())
|| trailer.getKey() == null) {
|| trailer.getLeft() == null) {
calculateLeaders();
}
}
Expand Down
11 changes: 10 additions & 1 deletion core/src/main/java/tc/oc/pgm/score/ScoreMatchModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
import tc.oc.pgm.api.PGM;
import tc.oc.pgm.api.filter.Filter;
import tc.oc.pgm.api.match.Match;
Expand Down Expand Up @@ -99,7 +100,7 @@ public Map<Competitor, Double> getScores() {
return this.scores;
}

public double getScore(Competitor competitor) {
public double getScore(@NotNull Competitor competitor) {
return this.scores.get(competitor);
}

Expand Down Expand Up @@ -319,10 +320,18 @@ public void incrementScore(UUID player, Competitor competitor, double amount) {
}
}

public void setScore(@NotNull Competitor competitor, double value) {
double curr = getScore(competitor);
if (curr != value) setScore(competitor, curr, value);
}

public void incrementScore(Competitor competitor, double amount) {
double oldScore = this.scores.get(competitor);
double newScore = oldScore + amount;
setScore(competitor, oldScore, newScore);
}

private void setScore(Competitor competitor, double oldScore, double newScore) {
if (this.config.scoreLimit > 0 && newScore > this.config.scoreLimit) {
newScore = this.config.scoreLimit;
}
Expand Down
33 changes: 0 additions & 33 deletions core/src/main/java/tc/oc/pgm/variables/BlitzVariable.java

This file was deleted.

48 changes: 0 additions & 48 deletions core/src/main/java/tc/oc/pgm/variables/DummyVariable.java

This file was deleted.

3 changes: 3 additions & 0 deletions core/src/main/java/tc/oc/pgm/variables/Variable.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tc.oc.pgm.variables;

import tc.oc.pgm.api.feature.Feature;
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.filters.Filterable;

public interface Variable<T extends Filterable<?>> extends Feature<VariableDefinition<T>> {
Expand All @@ -13,4 +14,6 @@ default String getId() {
double getValue(Filterable<?> context);

void setValue(Filterable<?> context, double value);

default void postLoad(Match match) {}
}
17 changes: 15 additions & 2 deletions core/src/main/java/tc/oc/pgm/variables/VariableDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public class VariableDefinition<T extends Filterable<?>> extends SelfIdentifying
private final double def;
private final VariableType variableType;

public VariableDefinition(String id, Class<T> scope, double def, VariableType variableType) {
public VariableDefinition(String id, Class<T> scope, double def, VariableType type) {
super(id);
this.scope = scope;
this.def = def;
this.variableType = variableType;
this.variableType = type;
}

public Class<T> getScope() {
Expand All @@ -37,4 +37,17 @@ public VariableType getVariableType() {
public Variable<T> getVariable(Match match) {
return (Variable<T>) match.getFeatureContext().get(this.getId());
}

public static class Context<T extends Filterable<?>, C> extends VariableDefinition<T> {
private final C context;

public Context(String id, Class<T> scope, double def, VariableType type, C context) {
super(id, scope, def, type);
this.context = context;
}

public C getContext() {
return context;
}
}
}
Loading

0 comments on commit a931d3c

Please sign in to comment.