Skip to content

Commit

Permalink
Add Score & team-rescoping variables (#1220)
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Herrera <[email protected]>
  • Loading branch information
Pablete1234 authored Aug 31, 2023
1 parent 0e1dcae commit 73afe50
Show file tree
Hide file tree
Showing 29 changed files with 366 additions and 223 deletions.
3 changes: 1 addition & 2 deletions core/src/main/java/tc/oc/pgm/action/ActionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,7 @@ public MessageAction parseChatMessage(Element el, Class<?> scope) throws Invalid
@MethodParser("sound")
public SoundAction parseSoundAction(Element el, Class<?> scope) throws InvalidXMLException {
SoundType soundType =
XMLUtils.parseEnum(
Node.fromAttr(el, "preset"), SoundType.class, "preset", SoundType.CUSTOM);
XMLUtils.parseEnum(Node.fromAttr(el, "preset"), SoundType.class, SoundType.CUSTOM);
Node resourceNode = Node.fromAttr(el, "key");
String resource = resourceNode == null ? soundType.getResource() : resourceNode.getValue();

Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public boolean respondsTo(Class<? extends Query> queryType) {

@Override
public boolean isDynamic() {
// Variables' setValue will always invalidate the filterable directly, no events required
return true;
return variable.isDynamic();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import tc.oc.pgm.util.xml.Node;
import tc.oc.pgm.util.xml.XMLUtils;
import tc.oc.pgm.variables.VariableDefinition;
import tc.oc.pgm.variables.VariableType;
import tc.oc.pgm.variables.VariablesModule;

public class FeatureFilterParser extends FilterParser {
Expand Down Expand Up @@ -100,9 +99,6 @@ public Filter parseNot(Element el) throws InvalidXMLException {
if (varMatch.matches()) {
VariableDefinition<?> variable =
features.resolve(node, varMatch.group(1), VariableDefinition.class);
if (!variable.getVariableType().equals(VariableType.DUMMY)) {
throw new InvalidXMLException("Variable filters only support dummy variables!", node);
}
Range<Double> range = XMLUtils.parseNumericRange(node, varMatch.group(2), Double.class);
return new VariableFilter(variable, range);
}
Expand Down
14 changes: 4 additions & 10 deletions core/src/main/java/tc/oc/pgm/filters/parse/FilterParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
import tc.oc.pgm.util.xml.Node;
import tc.oc.pgm.util.xml.XMLUtils;
import tc.oc.pgm.variables.VariableDefinition;
import tc.oc.pgm.variables.VariableType;

public abstract class FilterParser implements XMLParser<Filter, FilterDefinition> {

Expand Down Expand Up @@ -295,7 +294,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 +308,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 +463,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 Expand Up @@ -662,9 +659,6 @@ public PlayerCountFilter parsePlayerCountFilter(Element el) throws InvalidXMLExc
public Filter parseVariableFilter(Element el) throws InvalidXMLException {
VariableDefinition<?> varDef =
features.resolve(Node.fromRequiredAttr(el, "var"), VariableDefinition.class);
if (!varDef.getVariableType().equals(VariableType.DUMMY)) {
throw new InvalidXMLException("Variable filters only support dummy variables!", el);
}
Range<Double> range = XMLUtils.parseNumericRange(new Node(el), Double.class);

if (varDef.getScope() == Party.class)
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) {}
}
Loading

0 comments on commit 73afe50

Please sign in to comment.