Skip to content

Commit 263f936

Browse files
committed
Better way to handle filter references
1 parent 0e84ca8 commit 263f936

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

src/main/java/in/twizmwaz/cardinal/module/filter/FilterModule.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
package in.twizmwaz.cardinal.module.filter;
2727

28+
import com.google.common.collect.Lists;
2829
import com.google.common.collect.Maps;
2930
import in.twizmwaz.cardinal.Cardinal;
3031
import in.twizmwaz.cardinal.event.match.MatchModuleLoadCompleteEvent;
@@ -53,6 +54,7 @@
5354
import in.twizmwaz.cardinal.module.filter.type.CreatureFilter;
5455
import in.twizmwaz.cardinal.module.filter.type.CrouchingFilter;
5556
import in.twizmwaz.cardinal.module.filter.type.EntityFilter;
57+
import in.twizmwaz.cardinal.module.filter.type.FilterReference;
5658
import in.twizmwaz.cardinal.module.filter.type.FlyingFilter;
5759
import in.twizmwaz.cardinal.module.filter.type.HoldingFilter;
5860
import in.twizmwaz.cardinal.module.filter.type.LayerFilter;
@@ -84,12 +86,14 @@
8486
import org.jdom2.located.Located;
8587

8688
import java.util.Collection;
89+
import java.util.List;
8790
import java.util.Map;
8891

8992
@ModuleEntry(depends = {RegionModule.class, TeamModule.class})
9093
public class FilterModule extends AbstractModule implements Listener {
9194

9295
private Map<Match, Map<String, Filter>> filters = Maps.newHashMap();
96+
private Map<Match, List<FilterReference>> references = Maps.newHashMap();
9397

9498
//Static filters. Can be shared across matches, because they use no arguments
9599
public static final Filter ALLOW = new StaticFilter(FilterState.ALLOW);
@@ -114,6 +118,7 @@ public FilterModule() {
114118
@Override
115119
public boolean loadMatch(@NonNull Match match) {
116120
filters.put(match, Maps.newHashMap());
121+
references.put(match, Lists.newArrayList());
117122
for (Element filtersElement : match.getMap().getDocument().getRootElement().getChildren("filters")) {
118123
for (Element filterElement : filtersElement.getChildren()) {
119124
filters.get(match).put("always", ALLOW);
@@ -125,6 +130,10 @@ public boolean loadMatch(@NonNull Match match) {
125130
}
126131
}
127132
}
133+
for (FilterReference reference : references.get(match)) {
134+
reference.load(this, match);
135+
}
136+
references.remove(match);
128137
return true;
129138
}
130139

@@ -158,6 +167,12 @@ public Filter getFilter(@NonNull Match match, @NonNull String id) {
158167
return filters.get(match).get(id);
159168
}
160169

170+
public Filter getFilterReference(@NonNull Match match, @NonNull String id) {
171+
FilterReference reference = new FilterReference(id);
172+
references.get(match).add(reference);
173+
return reference;
174+
}
175+
161176
/**
162177
* Parses an element for a filter.
163178
*
@@ -312,7 +327,7 @@ public Filter getFilter(Match match, Element element, String... alternateAttribu
312327
for (String alternateAttribute : alternateAttributes) {
313328
String filterValue = element.getAttributeValue(alternateAttribute);
314329
if (filterValue != null) {
315-
Filter filter = getFilter(match, filterValue);
330+
Filter filter = getFilterReference(match, filterValue);
316331
if (filter != null) {
317332
return checkFilter(match, id, filter);
318333
}
@@ -321,7 +336,7 @@ public Filter getFilter(Match match, Element element, String... alternateAttribu
321336

322337
String filterValue = element.getAttributeValue("id");
323338
if (filterValue != null) {
324-
Filter filter = getFilter(match, filterValue);
339+
Filter filter = getFilterReference(match, filterValue);
325340
if (filter != null) {
326341
return checkFilter(match, id, filter);
327342
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2016, Kevin Phoenix
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
26+
package in.twizmwaz.cardinal.module.filter.type;
27+
28+
import in.twizmwaz.cardinal.match.Match;
29+
import in.twizmwaz.cardinal.module.ModuleError;
30+
import in.twizmwaz.cardinal.module.filter.Filter;
31+
import in.twizmwaz.cardinal.module.filter.FilterModule;
32+
import in.twizmwaz.cardinal.module.filter.FilterState;
33+
import lombok.RequiredArgsConstructor;
34+
35+
@RequiredArgsConstructor
36+
public class FilterReference implements Filter {
37+
38+
private final String id;
39+
private Filter filter;
40+
41+
public void load(FilterModule module, Match match) {
42+
filter = module.getFilter(match, id);
43+
if (filter == null) {
44+
module.getErrors().add(
45+
new ModuleError(module, match.getMap(), new String[]{"Could not find filter for id \'" + id + "\'"}, false));
46+
}
47+
}
48+
49+
@Override
50+
public FilterState evaluate(Object... objects) {
51+
if (filter != null) {
52+
return filter.evaluate(objects);
53+
} else {
54+
return FilterState.ABSTAIN;
55+
}
56+
}
57+
58+
}

0 commit comments

Comments
 (0)