Permalink
Browse files

Fix block filter trait parsing and list generation

  • Loading branch information...
14mRh4X0r authored and me4502 committed May 20, 2018
1 parent 294a338 commit 8dd32bb4fba05dc24fc3b48754f2ce8cc427e128
Showing with 15 additions and 38 deletions.
  1. +15 −38 craftbook-sponge/src/main/java/com/sk89q/craftbook/sponge/util/SpongeBlockFilter.java
@@ -24,10 +24,11 @@
import org.spongepowered.api.block.BlockType;
import org.spongepowered.api.block.trait.BlockTrait;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public final class SpongeBlockFilter extends BlockFilter<BlockState> {
@@ -52,15 +53,13 @@ public SpongeBlockFilter(String rule) {
public List<BlockState> getApplicableBlocks() {
if(cache == null) {
//Enumerate the cache.
cache = new ArrayList<>();
BlockType blockType;
Map<String, String> traitSpecifics = new HashMap<>();
if(getRule().contains("[") && getRule().endsWith("]")) {
String subRule = getRule().substring(getRule().indexOf('['), getRule().length()-2);
String subRule = getRule().substring(getRule().indexOf('[') + 1, getRule().length() - 1);
String[] parts = RegexUtil.COMMA_PATTERN.split(subRule);
blockType = Sponge.getGame().getRegistry().getType(BlockType.class, getRule().substring(0, getRule().indexOf('['))).orElse(null);
@@ -75,42 +74,20 @@ public SpongeBlockFilter(String rule) {
if(blockType == null) {
CraftBookPlugin.spongeInst().getLogger().warn("Missing type for filter rule: " + getRule());
return cache;
return Collections.emptyList();
}
int[] counter = new int[blockType.getTraits().size()];
if(counter.length != 0) {
while (true) {
BlockState state = blockType.getDefaultState();
List<BlockTrait<?>> blockTraits = new ArrayList<>(state.getTraits());
for (int i = 0; i < counter.length; i++) {
BlockTrait<?> trait = blockTraits.get(i);
if(traitSpecifics.containsKey(trait.getName().toLowerCase()))
state = state.withTrait(trait, traitSpecifics.get(trait.getName().toLowerCase())).orElse(null);
else {
ArrayList<?> possibleValues = new ArrayList<>(trait.getPossibleValues());
if (counter[i] >= possibleValues.size()) {
counter[i] = 0;
if (i + 1 >= counter.length)
return cache;
counter[i + 1]++;
}
state = state.withTrait(trait, possibleValues.get(counter[i])).get();
}
}
if(state != null) {
cache.add(state);
} else {
CraftBookPlugin.spongeInst().getLogger().warn("A state was null when it shouldn't have been. Are you sure '" + getRule() + "' is correct?");
}
counter[0] += 1;
}
} else {
cache.add(blockType.getDefaultState());
}
BlockState.MatcherBuilder matcherBuilder = BlockState.matcher(blockType);
traitSpecifics.forEach((k, v) ->
blockType.getTrait(k).ifPresent(t ->
t.parseValue(v).ifPresent(tv ->
matcherBuilder.trait((BlockTrait) t, (Comparable) tv)
)
)
);
cache = matcherBuilder.build().getCompatibleStates();
}
return cache;

0 comments on commit 8dd32bb

Please sign in to comment.