From e74b9809f3ff342e293ffd712a34b9b0decd4668 Mon Sep 17 00:00:00 2001 From: Pablete1234 Date: Mon, 29 Aug 2022 16:46:27 +0200 Subject: [PATCH] Fix world border filter NPE Signed-off-by: Pablete1234 --- .../features/FeatureDefinitionContext.java | 19 +++++++++++++++++++ .../tc/oc/pgm/filters/FilterMatchModule.java | 6 +++--- .../java/tc/oc/pgm/filters/FilterModule.java | 5 +++-- .../filters/parse/FeatureFilterParser.java | 4 ++-- .../tc/oc/pgm/filters/parse/FilterParser.java | 6 ++---- .../pgm/filters/parse/LegacyFilterParser.java | 3 +-- .../oc/pgm/worldborder/WorldBorderModule.java | 1 + .../oc/pgm/util/collection/ContextStore.java | 15 +-------------- 8 files changed, 32 insertions(+), 27 deletions(-) diff --git a/core/src/main/java/tc/oc/pgm/features/FeatureDefinitionContext.java b/core/src/main/java/tc/oc/pgm/features/FeatureDefinitionContext.java index fcdf867fcf..ff784259fd 100644 --- a/core/src/main/java/tc/oc/pgm/features/FeatureDefinitionContext.java +++ b/core/src/main/java/tc/oc/pgm/features/FeatureDefinitionContext.java @@ -1,6 +1,7 @@ package tc.oc.pgm.features; import com.google.common.collect.HashMultimap; +import com.google.common.collect.Iterables; import com.google.common.collect.SetMultimap; import java.util.ArrayList; import java.util.Collection; @@ -35,6 +36,19 @@ public Element getNode(FeatureDefinition definition) { return definitionNodes.get(definition); } + @Override + public String add(FeatureDefinition obj) { + String id = super.add(obj); + definitions.add(obj); + return id; + } + + @Override + public void add(String name, FeatureDefinition obj) { + super.add(name, obj); + this.definitions.add(obj); + } + /** * Add the given feature to the context with an optional ID. If no ID is given, there is no way to * retrieve the feature from the context. However, it can still be passed to {@link #getNode} to @@ -151,4 +165,9 @@ private void resolveReference(XMLFeatureReference< public Collection getAll() { return this.definitions; } + + @Override + public Iterable getAll(Class type) { + return Iterables.filter(definitions, type); + } } diff --git a/core/src/main/java/tc/oc/pgm/filters/FilterMatchModule.java b/core/src/main/java/tc/oc/pgm/filters/FilterMatchModule.java index 96e01cf41a..32a3a88f52 100644 --- a/core/src/main/java/tc/oc/pgm/filters/FilterMatchModule.java +++ b/core/src/main/java/tc/oc/pgm/filters/FilterMatchModule.java @@ -70,7 +70,7 @@ public class FilterMatchModule implements MatchModule, FilterDispatcher, Tickable, Listener { private final Match match; - private final ContextStore filterContext; + private final ContextStore filterContext; private final List> listeningFor = new LinkedList<>(); private final Map, ReactorFactory.Reactor> activeReactors = new HashMap<>(); @@ -84,7 +84,7 @@ public class FilterMatchModule implements MatchModule, FilterDispatcher, Tickabl * @param filterContext the context where all {@link Filters} for the relevant match can be found. * Important to find {@link ReactorFactory}s */ - public FilterMatchModule(Match match, ContextStore filterContext) { + public FilterMatchModule(Match match, ContextStore filterContext) { this.match = match; this.filterContext = filterContext; } @@ -110,7 +110,7 @@ public void onMatchLoad(MatchLoadEvent event) { // for dynamic listening. This could potentially result in a need to register a specific event // listener. this.filterContext // Check for any factories hidden in filter trees - .getAllUnchecked(Filter.class) + .getAll(Filter.class) .forEach(this::findAndCreateReactorFactories); // Then register all event listeners diff --git a/core/src/main/java/tc/oc/pgm/filters/FilterModule.java b/core/src/main/java/tc/oc/pgm/filters/FilterModule.java index 0b0aa14f3a..5476c87dd0 100644 --- a/core/src/main/java/tc/oc/pgm/filters/FilterModule.java +++ b/core/src/main/java/tc/oc/pgm/filters/FilterModule.java @@ -5,6 +5,7 @@ import java.util.logging.Logger; import org.jdom2.Document; import org.jdom2.Element; +import tc.oc.pgm.api.filter.Filter; import tc.oc.pgm.api.filter.ReactorFactory; import tc.oc.pgm.api.map.MapModule; import tc.oc.pgm.api.map.MapProtos; @@ -22,7 +23,7 @@ public class FilterModule implements MapModule { - private final ContextStore filterContext; + private final ContextStore filterContext; /** * Create the FilterModule. @@ -30,7 +31,7 @@ public class FilterModule implements MapModule { * @param filterContext the context where all {@link Filters} for the relevant match can be found. * Important to find {@link ReactorFactory}s */ - private FilterModule(ContextStore filterContext) { + private FilterModule(ContextStore filterContext) { this.filterContext = filterContext; } diff --git a/core/src/main/java/tc/oc/pgm/filters/parse/FeatureFilterParser.java b/core/src/main/java/tc/oc/pgm/filters/parse/FeatureFilterParser.java index 2fb2e075e8..69a64d2e76 100644 --- a/core/src/main/java/tc/oc/pgm/filters/parse/FeatureFilterParser.java +++ b/core/src/main/java/tc/oc/pgm/filters/parse/FeatureFilterParser.java @@ -5,12 +5,12 @@ import tc.oc.pgm.api.filter.Filter; import tc.oc.pgm.api.filter.FilterDefinition; import tc.oc.pgm.api.map.factory.MapFactory; +import tc.oc.pgm.features.FeatureDefinitionContext; import tc.oc.pgm.filters.XMLFilterReference; import tc.oc.pgm.filters.operator.AllowFilter; import tc.oc.pgm.filters.operator.DenyFilter; import tc.oc.pgm.filters.operator.InverseFilter; import tc.oc.pgm.util.MethodParser; -import tc.oc.pgm.util.collection.ContextStore; import tc.oc.pgm.util.xml.InvalidXMLException; import tc.oc.pgm.util.xml.Node; @@ -21,7 +21,7 @@ public FeatureFilterParser(MapFactory factory) { } @Override - public ContextStore getUsedContext() { + public FeatureDefinitionContext getUsedContext() { return factory.getFeatures(); } diff --git a/core/src/main/java/tc/oc/pgm/filters/parse/FilterParser.java b/core/src/main/java/tc/oc/pgm/filters/parse/FilterParser.java index b226dfa3f9..85043de143 100644 --- a/core/src/main/java/tc/oc/pgm/filters/parse/FilterParser.java +++ b/core/src/main/java/tc/oc/pgm/filters/parse/FilterParser.java @@ -96,13 +96,11 @@ public FilterParser(MapFactory factory) { } /** - * Gets the context used by this parser to store filters/filter references. Must use {@code - * ?}(wildcard) since {@link Filter} does not extend {@link FeatureDefinition}. - * (ContextStore<Filter> vs ContextStore<FeatureDefinition>) + * Gets the context used by this parser to store filters/filter references. * * @return the context where this parser puts its parsed filters */ - public abstract ContextStore getUsedContext(); + public abstract ContextStore getUsedContext(); /** * The top-level method for parsing an individual filter element. This method should call {@link diff --git a/core/src/main/java/tc/oc/pgm/filters/parse/LegacyFilterParser.java b/core/src/main/java/tc/oc/pgm/filters/parse/LegacyFilterParser.java index ca23fa12cb..86bcd55b0b 100644 --- a/core/src/main/java/tc/oc/pgm/filters/parse/LegacyFilterParser.java +++ b/core/src/main/java/tc/oc/pgm/filters/parse/LegacyFilterParser.java @@ -10,7 +10,6 @@ import tc.oc.pgm.filters.matcher.block.BlockFilter; import tc.oc.pgm.filters.operator.FilterNode; import tc.oc.pgm.util.MethodParser; -import tc.oc.pgm.util.collection.ContextStore; import tc.oc.pgm.util.material.matcher.SingleMaterialMatcher; import tc.oc.pgm.util.xml.InvalidXMLException; import tc.oc.pgm.util.xml.Node; @@ -26,7 +25,7 @@ public LegacyFilterParser(MapFactory factory) { } @Override - public ContextStore getUsedContext() { + public FilterContext getUsedContext() { return this.filterContext; } diff --git a/core/src/main/java/tc/oc/pgm/worldborder/WorldBorderModule.java b/core/src/main/java/tc/oc/pgm/worldborder/WorldBorderModule.java index 735c0aaf75..d19fad93b5 100644 --- a/core/src/main/java/tc/oc/pgm/worldborder/WorldBorderModule.java +++ b/core/src/main/java/tc/oc/pgm/worldborder/WorldBorderModule.java @@ -56,6 +56,7 @@ public WorldBorderModule parse(MapFactory factory, Logger logger, Document doc) "Cannot combine a filter and an explicit time for a world border", el); } filter = MonostableFilter.afterMatchStart(after); + factory.getFilters().getUsedContext().add(filter); } WorldBorder border = diff --git a/util/src/main/java/tc/oc/pgm/util/collection/ContextStore.java b/util/src/main/java/tc/oc/pgm/util/collection/ContextStore.java index 1b872588a7..fe0ce4ca21 100644 --- a/util/src/main/java/tc/oc/pgm/util/collection/ContextStore.java +++ b/util/src/main/java/tc/oc/pgm/util/collection/ContextStore.java @@ -100,20 +100,7 @@ public Collection getAll() { } @SuppressWarnings("unchecked") - public Collection getAll(Class clazz) { - Set results = new HashSet<>(); - - for (T t : this.getAll()) { - if (clazz.isAssignableFrom(t.getClass())) { - results.add((V) t); - } - } - - return results; - } - - @SuppressWarnings("unchecked") - public Collection getAllUnchecked(Class clazz) { + public Iterable getAll(Class clazz) { Set results = new HashSet<>(); for (T t : this.getAll()) {