Search in sources :

Example 6 with ChemicalType

use of mekanism.api.chemical.ChemicalType in project Mekanism by mekanism.

the class SerializerHelper method getBoxedChemicalStack.

/**
 * Helper to get and deserialize a Chemical Stack from a specific sub-element in a Json Object.
 *
 * @param json Parent Json Object
 * @param key  Key in the Json Object that contains a Chemical Stack.
 *
 * @return Chemical Stack.
 */
public static ChemicalStack<?> getBoxedChemicalStack(@Nonnull JsonObject json, @Nonnull String key) {
    validateKey(json, key);
    JsonObject jsonObject = JSONUtils.getAsJsonObject(json, key);
    ChemicalType chemicalType = getChemicalType(jsonObject);
    if (chemicalType == ChemicalType.GAS) {
        return deserializeGas(jsonObject);
    } else if (chemicalType == ChemicalType.INFUSION) {
        return deserializeInfuseType(jsonObject);
    } else if (chemicalType == ChemicalType.PIGMENT) {
        return deserializePigment(jsonObject);
    } else if (chemicalType == ChemicalType.SLURRY) {
        return deserializeSlurry(jsonObject);
    } else {
        throw new IllegalStateException("Unknown chemical type");
    }
}
Also used : ChemicalType(mekanism.api.chemical.ChemicalType) JsonObject(com.google.gson.JsonObject)

Example 7 with ChemicalType

use of mekanism.api.chemical.ChemicalType in project Mekanism by mekanism.

the class BoxedChemicalNetwork method tickEmit.

private <CHEMICAL extends Chemical<CHEMICAL>, STACK extends ChemicalStack<CHEMICAL>> long tickEmit(@Nonnull STACK stack) {
    ChemicalType chemicalType = ChemicalType.getTypeFor(stack);
    Collection<Map<Direction, LazyOptional<BoxedChemicalHandler>>> acceptorValues = acceptorCache.getAcceptorValues();
    ChemicalHandlerTarget<CHEMICAL, STACK, IChemicalHandler<CHEMICAL, STACK>> target = new ChemicalHandlerTarget<>(stack, acceptorValues.size() * 2);
    for (Map<Direction, LazyOptional<BoxedChemicalHandler>> acceptors : acceptorValues) {
        for (LazyOptional<BoxedChemicalHandler> lazyAcceptor : acceptors.values()) {
            lazyAcceptor.ifPresent(acceptor -> {
                IChemicalHandler<CHEMICAL, STACK> handler = acceptor.getHandlerFor(chemicalType);
                if (handler != null && ChemicalUtil.canInsert(handler, stack)) {
                    target.addHandler(handler);
                }
            });
        }
    }
    return EmitUtils.sendToAcceptors(target, stack.getAmount(), stack);
}
Also used : ChemicalType(mekanism.api.chemical.ChemicalType) ChemicalHandlerTarget(mekanism.common.content.network.distribution.ChemicalHandlerTarget) Direction(net.minecraft.util.Direction) BoxedChemicalHandler(mekanism.common.capabilities.chemical.BoxedChemicalHandler) LazyOptional(net.minecraftforge.common.util.LazyOptional) IChemicalHandler(mekanism.api.chemical.IChemicalHandler) Map(java.util.Map)

Example 8 with ChemicalType

use of mekanism.api.chemical.ChemicalType in project Mekanism by mekanism.

the class ChemicalCrystallizerInputRecipeCache method findFirstRecipe.

@Nullable
private <CHEMICAL extends Chemical<CHEMICAL>, STACK extends ChemicalStack<CHEMICAL>> ChemicalCrystallizerRecipe findFirstRecipe(ChemicalType type, STACK stack) {
    Predicate<ChemicalCrystallizerRecipe> matchPredicate = recipe -> ((IChemicalStackIngredient<CHEMICAL, STACK>) recipe.getInput()).test(stack);
    ChemicalInputCache<CHEMICAL, STACK, ChemicalCrystallizerRecipe> cache = (ChemicalInputCache<CHEMICAL, STACK, ChemicalCrystallizerRecipe>) typeBasedCache.get(type);
    ChemicalCrystallizerRecipe recipe = cache.findFirstRecipe(stack, matchPredicate);
    return recipe == null ? findFirstRecipe(typeBasedComplexRecipes.get(type), matchPredicate) : recipe;
}
Also used : EnumUtils(mekanism.common.util.EnumUtils) MekanismRecipeType(mekanism.common.recipe.MekanismRecipeType) EnumMap(java.util.EnumMap) ChemicalStack(mekanism.api.chemical.ChemicalStack) Predicate(java.util.function.Predicate) World(net.minecraft.world.World) Set(java.util.Set) ChemicalCrystallizerRecipe(mekanism.api.recipes.ChemicalCrystallizerRecipe) ChemicalInputCache(mekanism.common.recipe.lookup.cache.type.ChemicalInputCache) ChemicalType(mekanism.api.chemical.ChemicalType) HashSet(java.util.HashSet) List(java.util.List) Chemical(mekanism.api.chemical.Chemical) Map(java.util.Map) Nullable(javax.annotation.Nullable) IChemicalStackIngredient(mekanism.api.recipes.inputs.chemical.IChemicalStackIngredient) BoxedChemicalStack(mekanism.api.chemical.merged.BoxedChemicalStack) IChemicalStackIngredient(mekanism.api.recipes.inputs.chemical.IChemicalStackIngredient) ChemicalInputCache(mekanism.common.recipe.lookup.cache.type.ChemicalInputCache) ChemicalCrystallizerRecipe(mekanism.api.recipes.ChemicalCrystallizerRecipe) Nullable(javax.annotation.Nullable)

Example 9 with ChemicalType

use of mekanism.api.chemical.ChemicalType in project Mekanism by mekanism.

the class ChemicalCrystallizerInputRecipeCache method containsInput.

/**
 * Checks if there is a matching recipe that has the given input.
 *
 * @param world World.
 * @param input Recipe input.
 *
 * @return {@code true} if there is a match, {@code false} if there isn't.
 */
public boolean containsInput(@Nullable World world, BoxedChemicalStack input) {
    if (input.isEmpty()) {
        // Don't allow empty inputs
        return false;
    }
    initCacheIfNeeded(world);
    ChemicalType type = input.getChemicalType();
    return containsInput(type, input.getChemicalStack()) || typeBasedComplexRecipes.get(type).stream().anyMatch(recipe -> recipe.testType(input));
}
Also used : EnumUtils(mekanism.common.util.EnumUtils) MekanismRecipeType(mekanism.common.recipe.MekanismRecipeType) EnumMap(java.util.EnumMap) ChemicalStack(mekanism.api.chemical.ChemicalStack) Predicate(java.util.function.Predicate) World(net.minecraft.world.World) Set(java.util.Set) ChemicalCrystallizerRecipe(mekanism.api.recipes.ChemicalCrystallizerRecipe) ChemicalInputCache(mekanism.common.recipe.lookup.cache.type.ChemicalInputCache) ChemicalType(mekanism.api.chemical.ChemicalType) HashSet(java.util.HashSet) List(java.util.List) Chemical(mekanism.api.chemical.Chemical) Map(java.util.Map) Nullable(javax.annotation.Nullable) IChemicalStackIngredient(mekanism.api.recipes.inputs.chemical.IChemicalStackIngredient) BoxedChemicalStack(mekanism.api.chemical.merged.BoxedChemicalStack) ChemicalType(mekanism.api.chemical.ChemicalType)

Example 10 with ChemicalType

use of mekanism.api.chemical.ChemicalType in project Mekanism by mekanism.

the class ChemicalCrystallizerInputRecipeCache method containsInput.

/**
 * Checks if there is a matching recipe that has the given input.
 *
 * @param world World.
 * @param input Recipe input.
 *
 * @return {@code true} if there is a match, {@code false} if there isn't.
 */
public <CHEMICAL extends Chemical<CHEMICAL>, STACK extends ChemicalStack<CHEMICAL>> boolean containsInput(@Nullable World world, CHEMICAL input) {
    if (input.isEmptyType()) {
        // Don't allow empty inputs
        return false;
    }
    initCacheIfNeeded(world);
    ChemicalType type = ChemicalType.getTypeFor(input);
    STACK stack = (STACK) input.getStack(1);
    return containsInput(type, stack) || typeBasedComplexRecipes.get(type).stream().anyMatch(recipe -> recipe.testType(stack));
}
Also used : EnumUtils(mekanism.common.util.EnumUtils) MekanismRecipeType(mekanism.common.recipe.MekanismRecipeType) EnumMap(java.util.EnumMap) ChemicalStack(mekanism.api.chemical.ChemicalStack) Predicate(java.util.function.Predicate) World(net.minecraft.world.World) Set(java.util.Set) ChemicalCrystallizerRecipe(mekanism.api.recipes.ChemicalCrystallizerRecipe) ChemicalInputCache(mekanism.common.recipe.lookup.cache.type.ChemicalInputCache) ChemicalType(mekanism.api.chemical.ChemicalType) HashSet(java.util.HashSet) List(java.util.List) Chemical(mekanism.api.chemical.Chemical) Map(java.util.Map) Nullable(javax.annotation.Nullable) IChemicalStackIngredient(mekanism.api.recipes.inputs.chemical.IChemicalStackIngredient) BoxedChemicalStack(mekanism.api.chemical.merged.BoxedChemicalStack) ChemicalType(mekanism.api.chemical.ChemicalType)

Aggregations

ChemicalType (mekanism.api.chemical.ChemicalType)16 List (java.util.List)5 ChemicalStack (mekanism.api.chemical.ChemicalStack)5 BoxedChemicalStack (mekanism.api.chemical.merged.BoxedChemicalStack)5 Map (java.util.Map)4 ChemicalCrystallizerRecipe (mekanism.api.recipes.ChemicalCrystallizerRecipe)4 JsonSyntaxException (com.google.gson.JsonSyntaxException)3 EnumMap (java.util.EnumMap)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 Predicate (java.util.function.Predicate)3 Nullable (javax.annotation.Nullable)3 Chemical (mekanism.api.chemical.Chemical)3 GasStack (mekanism.api.chemical.gas.GasStack)3 PigmentStack (mekanism.api.chemical.pigment.PigmentStack)3 IChemicalStackIngredient (mekanism.api.recipes.inputs.chemical.IChemicalStackIngredient)3 MekanismRecipeType (mekanism.common.recipe.MekanismRecipeType)3 ChemicalInputCache (mekanism.common.recipe.lookup.cache.type.ChemicalInputCache)3 EnumUtils (mekanism.common.util.EnumUtils)3 World (net.minecraft.world.World)3