Skip to content

Commit

Permalink
Fix block filter trait parsing and list generation
Browse files Browse the repository at this point in the history
  • Loading branch information
14mRh4X0r authored and me4502 committed May 21, 2018
1 parent 294a338 commit 8dd32bb
Showing 1 changed file with 15 additions and 38 deletions.
Expand Up @@ -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> {

Expand All @@ -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);
Expand All @@ -75,42 +74,20 @@ public List<BlockState> getApplicableBlocks() {

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;
Expand Down

0 comments on commit 8dd32bb

Please sign in to comment.