Skip to content

Commit

Permalink
Fix world border filter NPE (#1052)
Browse files Browse the repository at this point in the history
Signed-off-by: Pablete1234 <[email protected]>
  • Loading branch information
Pablete1234 authored Sep 2, 2022
1 parent 080e659 commit 1081794
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -151,4 +165,9 @@ private <T extends FeatureDefinition> void resolveReference(XMLFeatureReference<
public Collection<FeatureDefinition> getAll() {
return this.definitions;
}

@Override
public <T extends FeatureDefinition> Iterable<T> getAll(Class<T> type) {
return Iterables.filter(definitions, type);
}
}
6 changes: 3 additions & 3 deletions core/src/main/java/tc/oc/pgm/filters/FilterMatchModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
public class FilterMatchModule implements MatchModule, FilterDispatcher, Tickable, Listener {

private final Match match;
private final ContextStore<?> filterContext;
private final ContextStore<? super Filter> filterContext;
private final List<Class<? extends Event>> listeningFor = new LinkedList<>();

private final Map<ReactorFactory<?>, ReactorFactory.Reactor> activeReactors = new HashMap<>();
Expand All @@ -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<? super Filter> filterContext) {
this.match = match;
this.filterContext = filterContext;
}
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/tc/oc/pgm/filters/FilterModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,15 +23,15 @@

public class FilterModule implements MapModule<FilterMatchModule> {

private final ContextStore<?> filterContext;
private final ContextStore<? super Filter> filterContext;

/**
* Create the FilterModule.
*
* @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<? super Filter> filterContext) {
this.filterContext = filterContext;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -21,7 +21,7 @@ public FeatureFilterParser(MapFactory factory) {
}

@Override
public ContextStore<?> getUsedContext() {
public FeatureDefinitionContext getUsedContext() {
return factory.getFeatures();
}

Expand Down
6 changes: 2 additions & 4 deletions core/src/main/java/tc/oc/pgm/filters/parse/FilterParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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&lt;Filter&gt; vs ContextStore&lt;FeatureDefinition&gt;)
* 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<? super Filter> getUsedContext();

/**
* The top-level method for parsing an individual filter element. This method should call {@link
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,7 +25,7 @@ public LegacyFilterParser(MapFactory factory) {
}

@Override
public ContextStore<?> getUsedContext() {
public FilterContext getUsedContext() {
return this.filterContext;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
15 changes: 1 addition & 14 deletions util/src/main/java/tc/oc/pgm/util/collection/ContextStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,7 @@ public Collection<T> getAll() {
}

@SuppressWarnings("unchecked")
public <V extends T> Collection<V> getAll(Class<V> clazz) {
Set<V> results = new HashSet<>();

for (T t : this.getAll()) {
if (clazz.isAssignableFrom(t.getClass())) {
results.add((V) t);
}
}

return results;
}

@SuppressWarnings("unchecked")
public <V> Collection<V> getAllUnchecked(Class<V> clazz) {
public <V extends T> Iterable<V> getAll(Class<V> clazz) {
Set<V> results = new HashSet<>();

for (T t : this.getAll()) {
Expand Down

0 comments on commit 1081794

Please sign in to comment.