Search in sources :

Example 21 with ItemStackIngredient

use of mekanism.api.recipes.inputs.ItemStackIngredient in project Mekanism by mekanism.

the class ItemStackToEnergyRecipeSerializer method fromJson.

@Nonnull
@Override
public RECIPE fromJson(@Nonnull ResourceLocation recipeId, @Nonnull JsonObject json) {
    JsonElement input = JSONUtils.isArrayNode(json, JsonConstants.INPUT) ? JSONUtils.getAsJsonArray(json, JsonConstants.INPUT) : JSONUtils.getAsJsonObject(json, JsonConstants.INPUT);
    ItemStackIngredient inputIngredient = ItemStackIngredient.deserialize(input);
    FloatingLong output = SerializerHelper.getFloatingLong(json, JsonConstants.OUTPUT);
    if (output.isZero()) {
        throw new JsonSyntaxException("Expected output to be greater than zero.");
    }
    return this.factory.create(recipeId, inputIngredient, output);
}
Also used : ItemStackIngredient(mekanism.api.recipes.inputs.ItemStackIngredient) FloatingLong(mekanism.api.math.FloatingLong) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) Nonnull(javax.annotation.Nonnull)

Example 22 with ItemStackIngredient

use of mekanism.api.recipes.inputs.ItemStackIngredient in project Mekanism by mekanism.

the class ItemStackToItemStackRecipeSerializer method fromNetwork.

@Override
public RECIPE fromNetwork(@Nonnull ResourceLocation recipeId, @Nonnull PacketBuffer buffer) {
    try {
        ItemStackIngredient inputIngredient = ItemStackIngredient.read(buffer);
        ItemStack output = buffer.readItem();
        return this.factory.create(recipeId, inputIngredient, output);
    } catch (Exception e) {
        Mekanism.logger.error("Error reading itemstack to itemstack recipe from packet.", e);
        throw e;
    }
}
Also used : ItemStackIngredient(mekanism.api.recipes.inputs.ItemStackIngredient) ItemStack(net.minecraft.item.ItemStack) JsonSyntaxException(com.google.gson.JsonSyntaxException)

Example 23 with ItemStackIngredient

use of mekanism.api.recipes.inputs.ItemStackIngredient in project Mekanism by mekanism.

the class ItemStackToItemStackRecipeSerializer method fromJson.

@Nonnull
@Override
public RECIPE fromJson(@Nonnull ResourceLocation recipeId, @Nonnull JsonObject json) {
    JsonElement input = JSONUtils.isArrayNode(json, JsonConstants.INPUT) ? JSONUtils.getAsJsonArray(json, JsonConstants.INPUT) : JSONUtils.getAsJsonObject(json, JsonConstants.INPUT);
    ItemStackIngredient inputIngredient = ItemStackIngredient.deserialize(input);
    ItemStack output = SerializerHelper.getItemStack(json, JsonConstants.OUTPUT);
    if (output.isEmpty()) {
        throw new JsonSyntaxException("Recipe output must not be empty.");
    }
    return this.factory.create(recipeId, inputIngredient, output);
}
Also used : ItemStackIngredient(mekanism.api.recipes.inputs.ItemStackIngredient) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) ItemStack(net.minecraft.item.ItemStack) Nonnull(javax.annotation.Nonnull)

Example 24 with ItemStackIngredient

use of mekanism.api.recipes.inputs.ItemStackIngredient in project Mekanism by mekanism.

the class SawmillRecipeMapper method handleRecipe.

@Override
public boolean handleRecipe(IMappingCollector<NormalizedSimpleStack, Long> mapper, IRecipe<?> iRecipe, INSSFakeGroupManager groupManager) {
    if (!(iRecipe instanceof SawmillRecipe)) {
        // Double check that we have a type of recipe we know how to handle
        return false;
    }
    SawmillRecipe recipe = (SawmillRecipe) iRecipe;
    ItemStackIngredient input = recipe.getInput();
    int primaryMultiplier = 1;
    int secondaryMultiplier = 1;
    if (recipe.getSecondaryChance() > 0 && recipe.getSecondaryChance() < 1) {
        Fraction multiplier;
        try {
            multiplier = Fraction.getFraction(recipe.getSecondaryChance()).invert();
        } catch (ArithmeticException e) {
            // If we couldn't turn it into a fraction, then note we failed to convert the recipe
            return false;
        }
        primaryMultiplier = multiplier.getNumerator();
        secondaryMultiplier = multiplier.getDenominator();
    }
    boolean handled = false;
    for (ItemStack representation : input.getRepresentations()) {
        ChanceOutput output = recipe.getOutput(representation);
        ItemStack mainOutput = output.getMainOutput();
        ItemStack secondaryOutput = output.getMaxSecondaryOutput();
        NormalizedSimpleStack nssInput = NSSItem.createItem(representation);
        IngredientHelper ingredientHelper = new IngredientHelper(mapper);
        if (secondaryOutput.isEmpty()) {
            // We only have a main output
            if (!mainOutput.isEmpty()) {
                ingredientHelper.put(nssInput, representation.getCount());
                if (ingredientHelper.addAsConversion(mainOutput)) {
                    handled = true;
                }
            }
        } else if (mainOutput.isEmpty()) {
            // We only have a secondary output
            ingredientHelper.put(nssInput, representation.getCount() * primaryMultiplier);
            if (ingredientHelper.addAsConversion(NSSItem.createItem(secondaryOutput), secondaryOutput.getCount() * secondaryMultiplier)) {
                handled = true;
            }
        } else {
            NormalizedSimpleStack nssMainOutput = NSSItem.createItem(mainOutput);
            NormalizedSimpleStack nssSecondaryOutput = NSSItem.createItem(secondaryOutput);
            // We have both so do our best guess by trying to subtract them from each other
            // Add trying to calculate the main output (using it as if we needed negative of secondary output)
            ingredientHelper.put(nssInput, representation.getCount() * primaryMultiplier);
            ingredientHelper.put(nssSecondaryOutput, -secondaryOutput.getCount() * secondaryMultiplier);
            if (ingredientHelper.addAsConversion(nssMainOutput, mainOutput.getCount() * primaryMultiplier)) {
                handled = true;
            }
            // Add trying to calculate secondary output (using it as if we needed negative of main output)
            ingredientHelper.resetHelper();
            ingredientHelper.put(nssInput, representation.getCount() * primaryMultiplier);
            ingredientHelper.put(nssMainOutput, -mainOutput.getCount() * primaryMultiplier);
            if (ingredientHelper.addAsConversion(nssSecondaryOutput, secondaryOutput.getCount() * secondaryMultiplier)) {
                handled = true;
            }
        }
    }
    return handled;
}
Also used : ItemStackIngredient(mekanism.api.recipes.inputs.ItemStackIngredient) NormalizedSimpleStack(moze_intel.projecte.api.nss.NormalizedSimpleStack) ChanceOutput(mekanism.api.recipes.SawmillRecipe.ChanceOutput) SawmillRecipe(mekanism.api.recipes.SawmillRecipe) Fraction(org.apache.commons.lang3.math.Fraction) ItemStack(net.minecraft.item.ItemStack) IngredientHelper(mekanism.common.integration.projecte.IngredientHelper)

Example 25 with ItemStackIngredient

use of mekanism.api.recipes.inputs.ItemStackIngredient in project Mekanism by mekanism.

the class MekanismRecipeType method getRecipes.

@Nonnull
public List<RECIPE_TYPE> getRecipes(@Nullable World world) {
    if (world == null) {
        // Try to get a fallback world if we are in a context that may not have one
        // If we are on the client get the client's world, if we are on the server get the current server's world
        world = DistExecutor.safeRunForDist(() -> MekanismClient::tryGetClientWorld, () -> () -> ServerLifecycleHooks.getCurrentServer().overworld());
        if (world == null) {
            // If we failed, then return no recipes
            return Collections.emptyList();
        }
    }
    if (cachedRecipes.isEmpty()) {
        RecipeManager recipeManager = world.getRecipeManager();
        // TODO: Should we use the byType(RecipeType) that we ATd so that our recipes don't have to always return true for matching?
        List<RECIPE_TYPE> recipes = recipeManager.getRecipesFor(this, IgnoredIInventory.INSTANCE, world);
        if (this == SMELTING) {
            Map<ResourceLocation, IRecipe<IInventory>> smeltingRecipes = recipeManager.byType(IRecipeType.SMELTING);
            // Copy recipes our recipes to make sure it is mutable
            recipes = new ArrayList<>(recipes);
            for (Entry<ResourceLocation, IRecipe<IInventory>> entry : smeltingRecipes.entrySet()) {
                IRecipe<IInventory> smeltingRecipe = entry.getValue();
                ItemStack recipeOutput = smeltingRecipe.getResultItem();
                if (!smeltingRecipe.isSpecial() && !recipeOutput.isEmpty()) {
                    // TODO: Can Smelting recipes even be "special", if so can we add some sort of checker to make getOutput return the correct result
                    NonNullList<Ingredient> ingredients = smeltingRecipe.getIngredients();
                    int ingredientCount = ingredients.size();
                    ItemStackIngredient input;
                    if (ingredientCount == 0) {
                        // Something went wrong
                        continue;
                    } else if (ingredientCount == 1) {
                        input = ItemStackIngredient.from(ingredients.get(0));
                    } else {
                        ItemStackIngredient[] itemIngredients = new ItemStackIngredient[ingredientCount];
                        for (int i = 0; i < ingredientCount; i++) {
                            itemIngredients[i] = ItemStackIngredient.from(ingredients.get(i));
                        }
                        input = ItemStackIngredient.createMulti(itemIngredients);
                    }
                    recipes.add((RECIPE_TYPE) new SmeltingIRecipe(entry.getKey(), input, recipeOutput));
                }
            }
        }
        cachedRecipes = recipes;
    }
    return cachedRecipes;
}
Also used : IgnoredIInventory(mekanism.api.inventory.IgnoredIInventory) IInventory(net.minecraft.inventory.IInventory) ItemStackIngredient(mekanism.api.recipes.inputs.ItemStackIngredient) SmeltingIRecipe(mekanism.common.recipe.impl.SmeltingIRecipe) SmeltingIRecipe(mekanism.common.recipe.impl.SmeltingIRecipe) IRecipe(net.minecraft.item.crafting.IRecipe) ItemStackIngredient(mekanism.api.recipes.inputs.ItemStackIngredient) Ingredient(net.minecraft.item.crafting.Ingredient) ResourceLocation(net.minecraft.util.ResourceLocation) RecipeManager(net.minecraft.item.crafting.RecipeManager) ItemStack(net.minecraft.item.ItemStack) Nonnull(javax.annotation.Nonnull)

Aggregations

ItemStackIngredient (mekanism.api.recipes.inputs.ItemStackIngredient)26 ItemStack (net.minecraft.item.ItemStack)19 JsonSyntaxException (com.google.gson.JsonSyntaxException)18 JsonElement (com.google.gson.JsonElement)10 Nonnull (javax.annotation.Nonnull)10 GasStackIngredient (mekanism.api.recipes.inputs.chemical.GasStackIngredient)8 FloatingLong (mekanism.api.math.FloatingLong)5 FluidStackIngredient (mekanism.api.recipes.inputs.FluidStackIngredient)4 Ingredient (net.minecraft.item.crafting.Ingredient)4 IIngredient (com.blamejared.crafttweaker.api.item.IIngredient)3 GasStack (mekanism.api.chemical.gas.GasStack)3 Item (net.minecraft.item.Item)3 JsonObject (com.google.gson.JsonObject)2 ChemicalType (mekanism.api.chemical.ChemicalType)2 ChemicalStackIngredient (mekanism.api.recipes.inputs.chemical.ChemicalStackIngredient)2 IChemicalStackIngredient (mekanism.api.recipes.inputs.chemical.IChemicalStackIngredient)2 InfusionStackIngredient (mekanism.api.recipes.inputs.chemical.InfusionStackIngredient)2 PigmentStackIngredient (mekanism.api.recipes.inputs.chemical.PigmentStackIngredient)2 SlurryStackIngredient (mekanism.api.recipes.inputs.chemical.SlurryStackIngredient)2 ResourceLocation (net.minecraft.util.ResourceLocation)2