Search in sources :

Example 1 with WorldBlockPredicate

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);
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) WorldBlockPredicate(gregtech.api.util.WorldBlockPredicate) Predicate(java.util.function.Predicate) HashMap(java.util.HashMap) StoneType(gregtech.api.unification.ore.StoneType) ArrayList(java.util.ArrayList) JsonElement(com.google.gson.JsonElement) IBlockState(net.minecraft.block.state.IBlockState) JsonArray(com.google.gson.JsonArray) List(java.util.List) IProperty(net.minecraft.block.properties.IProperty) Block(net.minecraft.block.Block) Optional(com.google.common.base.Optional) Map(java.util.Map) JsonPrimitive(com.google.gson.JsonPrimitive) JsonPrimitive(com.google.gson.JsonPrimitive) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) WorldBlockPredicate(gregtech.api.util.WorldBlockPredicate) Predicate(java.util.function.Predicate) WorldBlockPredicate(gregtech.api.util.WorldBlockPredicate)

Example 2 with WorldBlockPredicate

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);
}
Also used : JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) WorldBlockPredicate(gregtech.api.util.WorldBlockPredicate) java.util(java.util) ImmutableSet(com.google.common.collect.ImmutableSet) Blocks(net.minecraft.init.Blocks) XSTR(gregtech.api.util.XSTR) FillerEntry(gregtech.api.worldgen.filler.FillerEntry) BlockPos(net.minecraft.util.math.BlockPos) FluidRegistry(net.minecraftforge.fluids.FluidRegistry) StoneType(gregtech.api.unification.ore.StoneType) JsonElement(com.google.gson.JsonElement) IBlockState(net.minecraft.block.state.IBlockState) JsonArray(com.google.gson.JsonArray) StoneTypes(gregtech.api.unification.ore.StoneTypes) ImmutableList(com.google.common.collect.ImmutableList) Block(net.minecraft.block.Block) Pair(org.apache.commons.lang3.tuple.Pair) Fluid(net.minecraftforge.fluids.Fluid) Preconditions(com.google.common.base.Preconditions) JsonPrimitive(com.google.gson.JsonPrimitive) GTUtility(gregtech.api.util.GTUtility) IBlockAccess(net.minecraft.world.IBlockAccess) FillerEntry(gregtech.api.worldgen.filler.FillerEntry) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) Pair(org.apache.commons.lang3.tuple.Pair) WorldBlockPredicate(gregtech.api.util.WorldBlockPredicate)

Aggregations

JsonArray (com.google.gson.JsonArray)2 JsonElement (com.google.gson.JsonElement)2 JsonObject (com.google.gson.JsonObject)2 JsonPrimitive (com.google.gson.JsonPrimitive)2 StoneType (gregtech.api.unification.ore.StoneType)2 WorldBlockPredicate (gregtech.api.util.WorldBlockPredicate)2 Block (net.minecraft.block.Block)2 IBlockState (net.minecraft.block.state.IBlockState)2 Optional (com.google.common.base.Optional)1 Preconditions (com.google.common.base.Preconditions)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 StoneTypes (gregtech.api.unification.ore.StoneTypes)1 GTUtility (gregtech.api.util.GTUtility)1 XSTR (gregtech.api.util.XSTR)1 FillerEntry (gregtech.api.worldgen.filler.FillerEntry)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1