From 62839b4b8bb8ecd1bfc53dd5dac226cf4e5d9228 Mon Sep 17 00:00:00 2001 From: Christopher White <18whitechristop@gmail.com> Date: Sat, 21 Oct 2023 23:52:53 -0700 Subject: [PATCH] Add timelimit action Signed-off-by: Christopher White <18whitechristop@gmail.com> --- .../java/tc/oc/pgm/action/ActionParser.java | 7 ++++++ .../oc/pgm/action/ActionScopeValidation.java | 2 +- .../pgm/action/actions/TimeLimitAction.java | 22 ++++++++++++++++++ .../tc/oc/pgm/timelimit/TimeLimitModule.java | 23 +++++++++++++++---- 4 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 core/src/main/java/tc/oc/pgm/action/actions/TimeLimitAction.java diff --git a/core/src/main/java/tc/oc/pgm/action/ActionParser.java b/core/src/main/java/tc/oc/pgm/action/ActionParser.java index c64b9aa7f3..bc2b4562f9 100644 --- a/core/src/main/java/tc/oc/pgm/action/ActionParser.java +++ b/core/src/main/java/tc/oc/pgm/action/ActionParser.java @@ -23,6 +23,7 @@ import tc.oc.pgm.action.actions.ScopeSwitchAction; import tc.oc.pgm.action.actions.SetVariableAction; import tc.oc.pgm.action.actions.SoundAction; +import tc.oc.pgm.action.actions.TimeLimitAction; import tc.oc.pgm.api.feature.FeatureValidation; import tc.oc.pgm.api.filter.Filter; import tc.oc.pgm.api.filter.Filterables; @@ -37,6 +38,7 @@ import tc.oc.pgm.kits.Kit; import tc.oc.pgm.regions.BlockBoundedValidation; import tc.oc.pgm.regions.RegionParser; +import tc.oc.pgm.timelimit.TimeLimitModule; import tc.oc.pgm.util.MethodParser; import tc.oc.pgm.util.MethodParsers; import tc.oc.pgm.util.inventory.ItemMatcher; @@ -309,6 +311,11 @@ public ReplaceItemAction parseReplaceItem(Element el, Class scope) throws Inv return new ReplaceItemAction(matcher, item, keepAmount, keepEnchants); } + @MethodParser("timelimit") + public TimeLimitAction parseTimeLimit(Element el, Class scope) throws InvalidXMLException { + return new TimeLimitAction(TimeLimitModule.Factory.parseTimeLimit(factory, el)); + } + @MethodParser("fill") public FillAction parseFill(Element el, Class scope) throws InvalidXMLException { return new FillAction( diff --git a/core/src/main/java/tc/oc/pgm/action/ActionScopeValidation.java b/core/src/main/java/tc/oc/pgm/action/ActionScopeValidation.java index f855b11c04..39fe783b5c 100644 --- a/core/src/main/java/tc/oc/pgm/action/ActionScopeValidation.java +++ b/core/src/main/java/tc/oc/pgm/action/ActionScopeValidation.java @@ -28,7 +28,7 @@ public void validate(ActionDefinition definition, Node node) throws InvalidXM "Wrong action scope, got " + scope.getSimpleName() + " but expected " - + scope.getSimpleName(), + + this.scope.getSimpleName(), node); } } diff --git a/core/src/main/java/tc/oc/pgm/action/actions/TimeLimitAction.java b/core/src/main/java/tc/oc/pgm/action/actions/TimeLimitAction.java new file mode 100644 index 0000000000..81cc251846 --- /dev/null +++ b/core/src/main/java/tc/oc/pgm/action/actions/TimeLimitAction.java @@ -0,0 +1,22 @@ +package tc.oc.pgm.action.actions; + +import tc.oc.pgm.api.match.Match; +import tc.oc.pgm.timelimit.TimeLimit; +import tc.oc.pgm.timelimit.TimeLimitMatchModule; + +public class TimeLimitAction extends AbstractAction { + private final TimeLimit timeLimit; + + public TimeLimitAction(TimeLimit timeLimit) { + super(Match.class); + this.timeLimit = timeLimit; + } + + @Override + public void trigger(Match match) { + TimeLimitMatchModule time = match.needModule(TimeLimitMatchModule.class); + time.cancel(); + time.setTimeLimit(timeLimit); + time.start(); + } +} diff --git a/core/src/main/java/tc/oc/pgm/timelimit/TimeLimitModule.java b/core/src/main/java/tc/oc/pgm/timelimit/TimeLimitModule.java index 3dc15e2544..7d008d64b4 100644 --- a/core/src/main/java/tc/oc/pgm/timelimit/TimeLimitModule.java +++ b/core/src/main/java/tc/oc/pgm/timelimit/TimeLimitModule.java @@ -1,6 +1,7 @@ package tc.oc.pgm.timelimit; import com.google.common.collect.ImmutableList; +import java.time.Duration; import java.util.Collection; import java.util.Collections; import java.util.logging.Logger; @@ -52,7 +53,8 @@ public Collection>> getWeakDependencies() { @Override public TimeLimitModule parse(MapFactory factory, Logger logger, Document doc) throws InvalidXMLException { - TimeLimit timeLimit = parseTimeLimit(factory, doc.getRootElement()); + Element el = doc.getRootElement(); + TimeLimit timeLimit = parseTimeLimit(factory, el, "time"); timeLimit = parseLegacyTimeLimit(factory, doc.getRootElement(), "score", timeLimit); timeLimit = parseLegacyTimeLimit(factory, doc.getRootElement(), "blitz", timeLimit); @@ -65,7 +67,7 @@ public TimeLimitModule parse(MapFactory factory, Logger logger, Document doc) throws InvalidXMLException { el = el.getChild(legacyTag); if (el != null) { - TimeLimit newTimeLimit = parseTimeLimit(factory, el); + TimeLimit newTimeLimit = parseTimeLimit(factory, el, "time"); if (newTimeLimit != null) { if (factory.getProto().isNoOlderThan(MapProtos.REMOVE_SCORE_TIME_LIMIT)) { throw new InvalidXMLException( @@ -83,14 +85,25 @@ public TimeLimitModule parse(MapFactory factory, Logger logger, Document doc) return oldTimeLimit; } - private static @Nullable TimeLimit parseTimeLimit(MapFactory factory, Element el) + private static @Nullable TimeLimit parseTimeLimit(MapFactory factory, Element el, String child) throws InvalidXMLException { - el = el.getChild("time"); + el = el.getChild(child); if (el == null) return null; + return parseTimeLimit(factory, el); + } + + public static TimeLimit parseTimeLimit(MapFactory factory, Element el) + throws InvalidXMLException { + String durationText = el.getTextNormalize(); + Duration duration = + durationText == null || durationText.isEmpty() + ? XMLUtils.parseDuration(el.getAttribute("duration")) + : TextParser.parseDuration(durationText); + return new TimeLimit( el.getAttributeValue("id"), - TextParser.parseDuration(el.getTextNormalize()), + duration, XMLUtils.parseDuration(el.getAttribute("overtime")), XMLUtils.parseDuration(el.getAttribute("max-overtime")), XMLUtils.parseDuration(el.getAttribute("end-overtime")),