From 5eaf5a697a1ef3a161aabe0eccec50ebb328dadf Mon Sep 17 00:00:00 2001 From: KingSimon <19822231+KingOfSquares@users.noreply.github.com> Date: Sun, 1 Nov 2020 21:31:52 +0100 Subject: [PATCH 1/6] Add a match state filter Signed-off-by: KingSimon <19822231+KingOfSquares@users.noreply.github.com> --- .../java/tc/oc/pgm/filters/FilterParser.java | 28 +++++++++++++++++++ .../tc/oc/pgm/filters/MatchStateFilter.java | 24 ++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 core/src/main/java/tc/oc/pgm/filters/MatchStateFilter.java diff --git a/core/src/main/java/tc/oc/pgm/filters/FilterParser.java b/core/src/main/java/tc/oc/pgm/filters/FilterParser.java index 95fd8701cd..44a64626ba 100644 --- a/core/src/main/java/tc/oc/pgm/filters/FilterParser.java +++ b/core/src/main/java/tc/oc/pgm/filters/FilterParser.java @@ -6,6 +6,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.function.Predicate; import javax.annotation.Nullable; import org.bukkit.entity.EntityType; import org.bukkit.entity.LivingEntity; @@ -15,6 +16,7 @@ import org.jdom2.Element; import tc.oc.pgm.api.filter.Filter; import tc.oc.pgm.api.map.factory.MapFactory; +import tc.oc.pgm.api.match.Match; import tc.oc.pgm.api.player.PlayerRelation; import tc.oc.pgm.classes.ClassModule; import tc.oc.pgm.classes.PlayerClass; @@ -458,6 +460,32 @@ public ScoreFilter parseScoreFilter(Element el) throws InvalidXMLException { return new ScoreFilter(XMLUtils.parseNumericRange(new Node(el), Integer.class)); } + @MethodParser("match-state") + public MatchStateFilter parseMatchStateFilter(Element el) throws InvalidXMLException { + String state = el.getValue(); + + Predicate matchPredicate = null; + + switch (state) { + case "running": + case "started": + matchPredicate = Match::isRunning; + break; + case "finished": + matchPredicate = Match::isFinished; + break; + case "loaded": + matchPredicate = Match::isLoaded; + break; + case "before": + matchPredicate = match -> !match.isRunning() && !match.isFinished(); + } + if (matchPredicate == null) + throw new InvalidXMLException("Invalid or no match state found", el); + + return new MatchStateFilter(matchPredicate); + } + // Methods for parsing QueryModifiers @MethodParser("offset") diff --git a/core/src/main/java/tc/oc/pgm/filters/MatchStateFilter.java b/core/src/main/java/tc/oc/pgm/filters/MatchStateFilter.java new file mode 100644 index 0000000000..3719158d39 --- /dev/null +++ b/core/src/main/java/tc/oc/pgm/filters/MatchStateFilter.java @@ -0,0 +1,24 @@ +package tc.oc.pgm.filters; + +import java.util.function.Predicate; +import tc.oc.pgm.api.filter.query.MatchQuery; +import tc.oc.pgm.api.match.Match; + +public class MatchStateFilter extends TypedFilter { + + private final Predicate matchPredicate; + + public MatchStateFilter(Predicate matchPredicate) { + this.matchPredicate = matchPredicate; + } + + @Override + public Class getQueryType() { + return MatchQuery.class; + } + + @Override + protected QueryResponse queryTyped(MatchQuery query) { + return QueryResponse.fromBoolean(matchPredicate.test(query.getMatch())); + } +} From 8db6400361b2dd33d1b18c05f0807a4b8fbd0ce6 Mon Sep 17 00:00:00 2001 From: KingSimon <19822231+KingOfSquares@users.noreply.github.com> Date: Tue, 3 Nov 2020 20:03:34 +0100 Subject: [PATCH 2/6] Add old 1.4.2 support Signed-off-by: KingSimon <19822231+KingOfSquares@users.noreply.github.com> --- .../java/tc/oc/pgm/filters/FilterParser.java | 41 +++++++++++++++---- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/filters/FilterParser.java b/core/src/main/java/tc/oc/pgm/filters/FilterParser.java index 44a64626ba..b1bf006422 100644 --- a/core/src/main/java/tc/oc/pgm/filters/FilterParser.java +++ b/core/src/main/java/tc/oc/pgm/filters/FilterParser.java @@ -17,6 +17,7 @@ import tc.oc.pgm.api.filter.Filter; import tc.oc.pgm.api.map.factory.MapFactory; import tc.oc.pgm.api.match.Match; +import tc.oc.pgm.api.match.MatchPhase; import tc.oc.pgm.api.player.PlayerRelation; import tc.oc.pgm.classes.ClassModule; import tc.oc.pgm.classes.PlayerClass; @@ -461,24 +462,46 @@ public ScoreFilter parseScoreFilter(Element el) throws InvalidXMLException { } @MethodParser("match-state") - public MatchStateFilter parseMatchStateFilter(Element el) throws InvalidXMLException { - String state = el.getValue(); + public MatchStateFilter parseMatchState(Element el) throws InvalidXMLException { + return parseMatchStateFilter(el.getValue(), el); + } + + @MethodParser("match-started") + public MatchStateFilter parseMatchStarted(Element el) throws InvalidXMLException { + return parseMatchStateFilter("started", el); + } + + @MethodParser("match-running") + public MatchStateFilter parseMatchRunning(Element el) throws InvalidXMLException { + return parseMatchStateFilter("running", el); + } + + @MethodParser("match-finished") + public MatchStateFilter parseMatchFinished(Element el) throws InvalidXMLException { + return parseMatchStateFilter("finished", el); + } + + public MatchStateFilter parseMatchStateFilter(String matchState, Element el) + throws InvalidXMLException { Predicate matchPredicate = null; - switch (state) { + switch (matchState) { case "running": - case "started": - matchPredicate = Match::isRunning; + matchPredicate = m -> m.getPhase() == MatchPhase.RUNNING; break; case "finished": - matchPredicate = Match::isFinished; + matchPredicate = m -> m.getPhase() == MatchPhase.FINISHED; break; - case "loaded": - matchPredicate = Match::isLoaded; + case "starting": + matchPredicate = m -> m.getPhase() == MatchPhase.STARTING; break; case "before": - matchPredicate = match -> !match.isRunning() && !match.isFinished(); + matchPredicate = m -> m.getPhase() == MatchPhase.IDLE; + break; + case "started": + matchPredicate = + m -> m.getPhase() == MatchPhase.RUNNING || m.getPhase() == MatchPhase.FINISHED; } if (matchPredicate == null) throw new InvalidXMLException("Invalid or no match state found", el); From d6053c3eaf60370908456ac520019bd3f8ba2929 Mon Sep 17 00:00:00 2001 From: KingSimon <19822231+KingOfSquares@users.noreply.github.com> Date: Sat, 7 Nov 2020 10:21:52 +0100 Subject: [PATCH 3/6] MatchStateFilter -> MatchPhaseFilter - Rework logic Signed-off-by: KingSimon <19822231+KingOfSquares@users.noreply.github.com> --- .../java/tc/oc/pgm/filters/FilterParser.java | 42 +++++++++---------- .../tc/oc/pgm/filters/MatchPhaseFilter.java | 25 +++++++++++ .../tc/oc/pgm/filters/MatchStateFilter.java | 24 ----------- 3 files changed, 44 insertions(+), 47 deletions(-) create mode 100644 core/src/main/java/tc/oc/pgm/filters/MatchPhaseFilter.java delete mode 100644 core/src/main/java/tc/oc/pgm/filters/MatchStateFilter.java diff --git a/core/src/main/java/tc/oc/pgm/filters/FilterParser.java b/core/src/main/java/tc/oc/pgm/filters/FilterParser.java index b1bf006422..f77a28b8ce 100644 --- a/core/src/main/java/tc/oc/pgm/filters/FilterParser.java +++ b/core/src/main/java/tc/oc/pgm/filters/FilterParser.java @@ -3,10 +3,7 @@ import com.google.common.collect.Lists; import com.google.common.collect.Range; import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.function.Predicate; +import java.util.*; import javax.annotation.Nullable; import org.bukkit.entity.EntityType; import org.bukkit.entity.LivingEntity; @@ -16,7 +13,6 @@ import org.jdom2.Element; import tc.oc.pgm.api.filter.Filter; import tc.oc.pgm.api.map.factory.MapFactory; -import tc.oc.pgm.api.match.Match; import tc.oc.pgm.api.match.MatchPhase; import tc.oc.pgm.api.player.PlayerRelation; import tc.oc.pgm.classes.ClassModule; @@ -467,46 +463,46 @@ public MatchStateFilter parseMatchState(Element el) throws InvalidXMLException { } @MethodParser("match-started") - public MatchStateFilter parseMatchStarted(Element el) throws InvalidXMLException { - return parseMatchStateFilter("started", el); + public MatchPhaseFilter parseMatchStarted(Element el) throws InvalidXMLException { + return parseMatchPhaseFilter("started", el); } @MethodParser("match-running") - public MatchStateFilter parseMatchRunning(Element el) throws InvalidXMLException { - return parseMatchStateFilter("running", el); + public MatchPhaseFilter parseMatchRunning(Element el) throws InvalidXMLException { + return parseMatchPhaseFilter("running", el); } @MethodParser("match-finished") - public MatchStateFilter parseMatchFinished(Element el) throws InvalidXMLException { - return parseMatchStateFilter("finished", el); + public MatchPhaseFilter parseMatchFinished(Element el) throws InvalidXMLException { + return parseMatchPhaseFilter("finished", el); } - public MatchStateFilter parseMatchStateFilter(String matchState, Element el) + private MatchPhaseFilter parseMatchPhaseFilter(String matchState, Element el) throws InvalidXMLException { - Predicate matchPredicate = null; + Set matchPhases = new HashSet<>(); switch (matchState) { case "running": - matchPredicate = m -> m.getPhase() == MatchPhase.RUNNING; + matchPhases.add(MatchPhase.RUNNING); break; case "finished": - matchPredicate = m -> m.getPhase() == MatchPhase.FINISHED; + matchPhases.add(MatchPhase.FINISHED); break; case "starting": - matchPredicate = m -> m.getPhase() == MatchPhase.STARTING; + matchPhases.add(MatchPhase.STARTING); break; - case "before": - matchPredicate = m -> m.getPhase() == MatchPhase.IDLE; + case "idle": + matchPhases.add(MatchPhase.IDLE); break; case "started": - matchPredicate = - m -> m.getPhase() == MatchPhase.RUNNING || m.getPhase() == MatchPhase.FINISHED; + matchPhases.add(MatchPhase.RUNNING); + matchPhases.add(MatchPhase.FINISHED); + break; } - if (matchPredicate == null) - throw new InvalidXMLException("Invalid or no match state found", el); + if (matchPhases.isEmpty()) throw new InvalidXMLException("Invalid or no match state found", el); - return new MatchStateFilter(matchPredicate); + return new MatchPhaseFilter(matchPhases); } // Methods for parsing QueryModifiers diff --git a/core/src/main/java/tc/oc/pgm/filters/MatchPhaseFilter.java b/core/src/main/java/tc/oc/pgm/filters/MatchPhaseFilter.java new file mode 100644 index 0000000000..b5d5d6781c --- /dev/null +++ b/core/src/main/java/tc/oc/pgm/filters/MatchPhaseFilter.java @@ -0,0 +1,25 @@ +package tc.oc.pgm.filters; + +import java.util.Set; +import tc.oc.pgm.api.filter.query.MatchQuery; +import tc.oc.pgm.api.match.MatchPhase; + +public class MatchPhaseFilter extends TypedFilter { + + private final Set matchPhase; + + public MatchPhaseFilter(Set matchPhase) { + this.matchPhase = matchPhase; + } + + @Override + public Class getQueryType() { + return MatchQuery.class; + } + + @Override + protected QueryResponse queryTyped(MatchQuery query) { + MatchPhase current = query.getMatch().getPhase(); + return QueryResponse.fromBoolean(matchPhase.stream().anyMatch(mp -> mp == current)); + } +} diff --git a/core/src/main/java/tc/oc/pgm/filters/MatchStateFilter.java b/core/src/main/java/tc/oc/pgm/filters/MatchStateFilter.java deleted file mode 100644 index 3719158d39..0000000000 --- a/core/src/main/java/tc/oc/pgm/filters/MatchStateFilter.java +++ /dev/null @@ -1,24 +0,0 @@ -package tc.oc.pgm.filters; - -import java.util.function.Predicate; -import tc.oc.pgm.api.filter.query.MatchQuery; -import tc.oc.pgm.api.match.Match; - -public class MatchStateFilter extends TypedFilter { - - private final Predicate matchPredicate; - - public MatchStateFilter(Predicate matchPredicate) { - this.matchPredicate = matchPredicate; - } - - @Override - public Class getQueryType() { - return MatchQuery.class; - } - - @Override - protected QueryResponse queryTyped(MatchQuery query) { - return QueryResponse.fromBoolean(matchPredicate.test(query.getMatch())); - } -} From 3235df6a5ce9b24101910708a35266510f809ce7 Mon Sep 17 00:00:00 2001 From: KingSimon <19822231+KingOfSquares@users.noreply.github.com> Date: Sat, 14 Nov 2020 18:12:56 +0100 Subject: [PATCH 4/6] Change parsing logic Signed-off-by: KingSimon <19822231+KingOfSquares@users.noreply.github.com> --- .../java/tc/oc/pgm/filters/FilterParser.java | 28 +++++++++---------- .../tc/oc/pgm/filters/MatchPhaseFilter.java | 7 ++--- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/filters/FilterParser.java b/core/src/main/java/tc/oc/pgm/filters/FilterParser.java index f77a28b8ce..67b710e5b0 100644 --- a/core/src/main/java/tc/oc/pgm/filters/FilterParser.java +++ b/core/src/main/java/tc/oc/pgm/filters/FilterParser.java @@ -463,46 +463,44 @@ public MatchStateFilter parseMatchState(Element el) throws InvalidXMLException { } @MethodParser("match-started") - public MatchPhaseFilter parseMatchStarted(Element el) throws InvalidXMLException { + public Filter parseMatchStarted(Element el) throws InvalidXMLException { return parseMatchPhaseFilter("started", el); } @MethodParser("match-running") - public MatchPhaseFilter parseMatchRunning(Element el) throws InvalidXMLException { + public Filter parseMatchRunning(Element el) throws InvalidXMLException { return parseMatchPhaseFilter("running", el); } @MethodParser("match-finished") - public MatchPhaseFilter parseMatchFinished(Element el) throws InvalidXMLException { + public Filter parseMatchFinished(Element el) throws InvalidXMLException { return parseMatchPhaseFilter("finished", el); } - private MatchPhaseFilter parseMatchPhaseFilter(String matchState, Element el) - throws InvalidXMLException { + private Filter parseMatchPhaseFilter(String matchState, Element el) throws InvalidXMLException { - Set matchPhases = new HashSet<>(); + MatchPhase matchPhase = null; switch (matchState) { case "running": - matchPhases.add(MatchPhase.RUNNING); + matchPhase = MatchPhase.RUNNING; break; case "finished": - matchPhases.add(MatchPhase.FINISHED); + matchPhase = MatchPhase.FINISHED; break; case "starting": - matchPhases.add(MatchPhase.STARTING); + matchPhase = MatchPhase.STARTING; break; case "idle": - matchPhases.add(MatchPhase.IDLE); + matchPhase = MatchPhase.IDLE; break; case "started": - matchPhases.add(MatchPhase.RUNNING); - matchPhases.add(MatchPhase.FINISHED); - break; + return AnyFilter.of( + new MatchPhaseFilter(MatchPhase.RUNNING), new MatchPhaseFilter(MatchPhase.FINISHED)); } - if (matchPhases.isEmpty()) throw new InvalidXMLException("Invalid or no match state found", el); + if (matchPhase == null) throw new InvalidXMLException("Invalid or no match state found", el); - return new MatchPhaseFilter(matchPhases); + return new MatchPhaseFilter(matchPhase); } // Methods for parsing QueryModifiers diff --git a/core/src/main/java/tc/oc/pgm/filters/MatchPhaseFilter.java b/core/src/main/java/tc/oc/pgm/filters/MatchPhaseFilter.java index b5d5d6781c..b85bc53ad6 100644 --- a/core/src/main/java/tc/oc/pgm/filters/MatchPhaseFilter.java +++ b/core/src/main/java/tc/oc/pgm/filters/MatchPhaseFilter.java @@ -1,14 +1,13 @@ package tc.oc.pgm.filters; -import java.util.Set; import tc.oc.pgm.api.filter.query.MatchQuery; import tc.oc.pgm.api.match.MatchPhase; public class MatchPhaseFilter extends TypedFilter { - private final Set matchPhase; + private final MatchPhase matchPhase; - public MatchPhaseFilter(Set matchPhase) { + public MatchPhaseFilter(MatchPhase matchPhase) { this.matchPhase = matchPhase; } @@ -20,6 +19,6 @@ public Class getQueryType() { @Override protected QueryResponse queryTyped(MatchQuery query) { MatchPhase current = query.getMatch().getPhase(); - return QueryResponse.fromBoolean(matchPhase.stream().anyMatch(mp -> mp == current)); + return QueryResponse.fromBoolean(matchPhase == current); } } From d7b1e82b3cd8e4142f284815bf803408dcf63e91 Mon Sep 17 00:00:00 2001 From: KingSimon <19822231+KingOfSquares@users.noreply.github.com> Date: Thu, 3 Dec 2020 22:55:39 +0100 Subject: [PATCH 5/6] refactor Signed-off-by: KingSimon <19822231+KingOfSquares@users.noreply.github.com> --- core/src/main/java/tc/oc/pgm/filters/FilterParser.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/filters/FilterParser.java b/core/src/main/java/tc/oc/pgm/filters/FilterParser.java index 67b710e5b0..6703e2348f 100644 --- a/core/src/main/java/tc/oc/pgm/filters/FilterParser.java +++ b/core/src/main/java/tc/oc/pgm/filters/FilterParser.java @@ -3,7 +3,9 @@ import com.google.common.collect.Lists; import com.google.common.collect.Range; import java.lang.reflect.Method; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; import javax.annotation.Nullable; import org.bukkit.entity.EntityType; import org.bukkit.entity.LivingEntity; @@ -457,9 +459,9 @@ public ScoreFilter parseScoreFilter(Element el) throws InvalidXMLException { return new ScoreFilter(XMLUtils.parseNumericRange(new Node(el), Integer.class)); } - @MethodParser("match-state") - public MatchStateFilter parseMatchState(Element el) throws InvalidXMLException { - return parseMatchStateFilter(el.getValue(), el); + @MethodParser("match-phase") + public Filter parseMatchPhase(Element el) throws InvalidXMLException { + return parseMatchPhaseFilter(el.getValue(), el); } @MethodParser("match-started") From edc9769b93b4f5422cf05dae80821dece29e2c9e Mon Sep 17 00:00:00 2001 From: KingSimon <19822231+KingOfSquares@users.noreply.github.com> Date: Sun, 20 Dec 2020 22:21:17 +0100 Subject: [PATCH 6/6] pre-initialize match state filters Signed-off-by: KingSimon <19822231+KingOfSquares@users.noreply.github.com> --- .../java/tc/oc/pgm/filters/FilterParser.java | 21 ++++++------------- .../tc/oc/pgm/filters/MatchPhaseFilter.java | 9 ++++++++ 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/filters/FilterParser.java b/core/src/main/java/tc/oc/pgm/filters/FilterParser.java index 6703e2348f..138caf2e6a 100644 --- a/core/src/main/java/tc/oc/pgm/filters/FilterParser.java +++ b/core/src/main/java/tc/oc/pgm/filters/FilterParser.java @@ -15,7 +15,6 @@ import org.jdom2.Element; import tc.oc.pgm.api.filter.Filter; import tc.oc.pgm.api.map.factory.MapFactory; -import tc.oc.pgm.api.match.MatchPhase; import tc.oc.pgm.api.player.PlayerRelation; import tc.oc.pgm.classes.ClassModule; import tc.oc.pgm.classes.PlayerClass; @@ -481,28 +480,20 @@ public Filter parseMatchFinished(Element el) throws InvalidXMLException { private Filter parseMatchPhaseFilter(String matchState, Element el) throws InvalidXMLException { - MatchPhase matchPhase = null; - switch (matchState) { case "running": - matchPhase = MatchPhase.RUNNING; - break; + return MatchPhaseFilter.RUNNING; case "finished": - matchPhase = MatchPhase.FINISHED; - break; + return MatchPhaseFilter.FINISHED; case "starting": - matchPhase = MatchPhase.STARTING; - break; + return MatchPhaseFilter.STARTING; case "idle": - matchPhase = MatchPhase.IDLE; - break; + return MatchPhaseFilter.IDLE; case "started": - return AnyFilter.of( - new MatchPhaseFilter(MatchPhase.RUNNING), new MatchPhaseFilter(MatchPhase.FINISHED)); + return MatchPhaseFilter.STARTED; } - if (matchPhase == null) throw new InvalidXMLException("Invalid or no match state found", el); - return new MatchPhaseFilter(matchPhase); + throw new InvalidXMLException("Invalid or no match state found", el); } // Methods for parsing QueryModifiers diff --git a/core/src/main/java/tc/oc/pgm/filters/MatchPhaseFilter.java b/core/src/main/java/tc/oc/pgm/filters/MatchPhaseFilter.java index b85bc53ad6..c482a25eec 100644 --- a/core/src/main/java/tc/oc/pgm/filters/MatchPhaseFilter.java +++ b/core/src/main/java/tc/oc/pgm/filters/MatchPhaseFilter.java @@ -1,10 +1,19 @@ package tc.oc.pgm.filters; +import tc.oc.pgm.api.filter.Filter; import tc.oc.pgm.api.filter.query.MatchQuery; import tc.oc.pgm.api.match.MatchPhase; public class MatchPhaseFilter extends TypedFilter { + public static final MatchPhaseFilter RUNNING = new MatchPhaseFilter(MatchPhase.RUNNING); + public static final MatchPhaseFilter FINISHED = new MatchPhaseFilter(MatchPhase.FINISHED); + public static final MatchPhaseFilter STARTING = new MatchPhaseFilter(MatchPhase.STARTING); + public static final MatchPhaseFilter IDLE = new MatchPhaseFilter(MatchPhase.IDLE); + public static final Filter STARTED = + AnyFilter.of( + new MatchPhaseFilter(MatchPhase.RUNNING), new MatchPhaseFilter(MatchPhase.FINISHED)); + private final MatchPhase matchPhase; public MatchPhaseFilter(MatchPhase matchPhase) {