Skip to content

Commit

Permalink
Add timelimit action
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher White <[email protected]>
  • Loading branch information
cswhite2000 committed Oct 22, 2023
1 parent 0f6b60e commit 62839b4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
7 changes: 7 additions & 0 deletions core/src/main/java/tc/oc/pgm/action/ActionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
22 changes: 22 additions & 0 deletions core/src/main/java/tc/oc/pgm/action/actions/TimeLimitAction.java
Original file line number Diff line number Diff line change
@@ -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<Match> {
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();
}
}
23 changes: 18 additions & 5 deletions core/src/main/java/tc/oc/pgm/timelimit/TimeLimitModule.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -52,7 +53,8 @@ public Collection<Class<? extends MapModule<?>>> 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);

Expand All @@ -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(
Expand All @@ -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")),
Expand Down

0 comments on commit 62839b4

Please sign in to comment.