Skip to content

Commit

Permalink
Better way to handle filter references
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablete1234 committed Aug 26, 2016
1 parent 0e84ca8 commit 263f936
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/main/java/in/twizmwaz/cardinal/module/filter/FilterModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package in.twizmwaz.cardinal.module.filter;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import in.twizmwaz.cardinal.Cardinal;
import in.twizmwaz.cardinal.event.match.MatchModuleLoadCompleteEvent;
Expand Down Expand Up @@ -53,6 +54,7 @@
import in.twizmwaz.cardinal.module.filter.type.CreatureFilter;
import in.twizmwaz.cardinal.module.filter.type.CrouchingFilter;
import in.twizmwaz.cardinal.module.filter.type.EntityFilter;
import in.twizmwaz.cardinal.module.filter.type.FilterReference;
import in.twizmwaz.cardinal.module.filter.type.FlyingFilter;
import in.twizmwaz.cardinal.module.filter.type.HoldingFilter;
import in.twizmwaz.cardinal.module.filter.type.LayerFilter;
Expand Down Expand Up @@ -84,12 +86,14 @@
import org.jdom2.located.Located;

import java.util.Collection;
import java.util.List;
import java.util.Map;

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

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

//Static filters. Can be shared across matches, because they use no arguments
public static final Filter ALLOW = new StaticFilter(FilterState.ALLOW);
Expand All @@ -114,6 +118,7 @@ public FilterModule() {
@Override
public boolean loadMatch(@NonNull Match match) {
filters.put(match, Maps.newHashMap());
references.put(match, Lists.newArrayList());
for (Element filtersElement : match.getMap().getDocument().getRootElement().getChildren("filters")) {
for (Element filterElement : filtersElement.getChildren()) {
filters.get(match).put("always", ALLOW);
Expand All @@ -125,6 +130,10 @@ public boolean loadMatch(@NonNull Match match) {
}
}
}
for (FilterReference reference : references.get(match)) {
reference.load(this, match);
}
references.remove(match);
return true;
}

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

public Filter getFilterReference(@NonNull Match match, @NonNull String id) {
FilterReference reference = new FilterReference(id);
references.get(match).add(reference);
return reference;
}

/**
* Parses an element for a filter.
*
Expand Down Expand Up @@ -312,7 +327,7 @@ public Filter getFilter(Match match, Element element, String... alternateAttribu
for (String alternateAttribute : alternateAttributes) {
String filterValue = element.getAttributeValue(alternateAttribute);
if (filterValue != null) {
Filter filter = getFilter(match, filterValue);
Filter filter = getFilterReference(match, filterValue);
if (filter != null) {
return checkFilter(match, id, filter);
}
Expand All @@ -321,7 +336,7 @@ public Filter getFilter(Match match, Element element, String... alternateAttribu

String filterValue = element.getAttributeValue("id");
if (filterValue != null) {
Filter filter = getFilter(match, filterValue);
Filter filter = getFilterReference(match, filterValue);
if (filter != null) {
return checkFilter(match, id, filter);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2016, Kevin Phoenix
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package in.twizmwaz.cardinal.module.filter.type;

import in.twizmwaz.cardinal.match.Match;
import in.twizmwaz.cardinal.module.ModuleError;
import in.twizmwaz.cardinal.module.filter.Filter;
import in.twizmwaz.cardinal.module.filter.FilterModule;
import in.twizmwaz.cardinal.module.filter.FilterState;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class FilterReference implements Filter {

private final String id;
private Filter filter;

public void load(FilterModule module, Match match) {
filter = module.getFilter(match, id);
if (filter == null) {
module.getErrors().add(
new ModuleError(module, match.getMap(), new String[]{"Could not find filter for id \'" + id + "\'"}, false));
}
}

@Override
public FilterState evaluate(Object... objects) {
if (filter != null) {
return filter.evaluate(objects);
} else {
return FilterState.ABSTAIN;
}
}

}

0 comments on commit 263f936

Please sign in to comment.