use of gregtech.api.util.WorldBlockPredicate in project GregTech by GregTechCE.
the class PredicateConfigUtils method createBlockStatePredicate.
public static WorldBlockPredicate createBlockStatePredicate(JsonElement element) {
if (element instanceof JsonPrimitive) {
String stringDeclaration = element.getAsString();
return createSimpleStatePredicate(stringDeclaration);
} else if (element instanceof JsonObject) {
JsonObject object = element.getAsJsonObject();
if (!object.has("block"))
throw new IllegalArgumentException("Block state predicate missing required block key!");
Predicate<IBlockState> predicate = parseBlockStatePropertyPredicate(object);
return (state, world, pos) -> predicate.test(state);
} else if (element instanceof JsonArray) {
JsonArray array = element.getAsJsonArray();
ArrayList<WorldBlockPredicate> allPredicates = new ArrayList<>();
for (JsonElement arrayElement : array) {
allPredicates.add(createBlockStatePredicate(arrayElement));
}
return (state, world, pos) -> allPredicates.stream().anyMatch(p -> p.test(state, world, pos));
} else {
throw new IllegalArgumentException("Unsupported block state variant predicate type: " + element);
}
}
use of gregtech.api.util.WorldBlockPredicate in project GregTech by GregTechCE.
the class FillerConfigUtils method createStateMatchFiller.
private static FillerEntry createStateMatchFiller(JsonObject object) {
JsonArray valuesArray = object.get("values").getAsJsonArray();
JsonElement defaultElement = object.get("default");
ArrayList<Pair<WorldBlockPredicate, FillerEntry>> matchers = new ArrayList<>();
for (JsonElement valueDefinition : valuesArray) {
Preconditions.checkArgument(valueDefinition.isJsonObject(), "Found invalid value definition: %s", valueDefinition.toString());
JsonObject valueObject = valueDefinition.getAsJsonObject();
WorldBlockPredicate predicate = PredicateConfigUtils.createBlockStatePredicate(valueObject.get("predicate"));
FillerEntry filler = createBlockStateFiller(valueObject.get("value"));
matchers.add(Pair.of(predicate, filler));
}
if (!defaultElement.isJsonNull()) {
FillerEntry filler = createBlockStateFiller(defaultElement);
WorldBlockPredicate predicate = (state, world, pos) -> true;
matchers.add(Pair.of(predicate, filler));
} else {
WorldBlockPredicate predicate = (state, world, pos) -> true;
FillerEntry fillerEntry = matchers.iterator().next().getRight();
matchers.add(Pair.of(predicate, fillerEntry));
}
return new BlockStateMatcherEntry(matchers);
}
Aggregations