Search in sources :

Example 1 with ChanceOutput

use of mekanism.api.recipes.SawmillRecipe.ChanceOutput in project Mekanism by mekanism.

the class TileEntitySawingFactory method findRecipe.

@Override
protected SawmillRecipe findRecipe(int process, @Nonnull ItemStack fallbackInput, @Nonnull IInventorySlot outputSlot, @Nullable IInventorySlot secondaryOutputSlot) {
    ItemStack output = outputSlot.getStack();
    ItemStack extra = secondaryOutputSlot == null ? ItemStack.EMPTY : secondaryOutputSlot.getStack();
    return getRecipeType().getInputCache().findTypeBasedRecipe(level, fallbackInput, recipe -> {
        ChanceOutput chanceOutput = recipe.getOutput(fallbackInput);
        if (InventoryUtils.areItemsStackable(chanceOutput.getMainOutput(), output)) {
            // output of this recipe will stack with what is currently in the secondary slot
            if (extra.isEmpty()) {
                return true;
            }
            ItemStack secondaryOutput = chanceOutput.getMaxSecondaryOutput();
            return secondaryOutput.isEmpty() || ItemHandlerHelper.canItemStacksStack(secondaryOutput, extra);
        }
        return false;
    });
}
Also used : ChanceOutput(mekanism.api.recipes.SawmillRecipe.ChanceOutput) ItemStack(net.minecraft.item.ItemStack)

Example 2 with ChanceOutput

use of mekanism.api.recipes.SawmillRecipe.ChanceOutput in project Mekanism by mekanism.

the class OutputHelper method getOutputHandler.

/**
 * Wraps two inventory slots, a "main" slot, and a "secondary" slot into an {@link IOutputHandler} for handling {@link ChanceOutput}s.
 *
 * @param mainSlot      Main slot to wrap.
 * @param secondarySlot Secondary slot to wrap.
 */
public static IOutputHandler<@NonNull ChanceOutput> getOutputHandler(IInventorySlot mainSlot, IInventorySlot secondarySlot) {
    Objects.requireNonNull(mainSlot, "Main slot cannot be null.");
    Objects.requireNonNull(secondarySlot, "Secondary/Extra slot cannot be null.");
    return new IOutputHandler<@NonNull ChanceOutput>() {

        @Override
        public void handleOutput(@Nonnull ChanceOutput toOutput, int operations) {
            OutputHelper.handleOutput(mainSlot, toOutput.getMainOutput(), operations);
            // TODO: Batch this into a single addition call, by looping over and calculating things?
            ItemStack secondaryOutput = toOutput.getSecondaryOutput();
            for (int i = 0; i < operations; i++) {
                OutputHelper.handleOutput(secondarySlot, secondaryOutput, operations);
                if (i < operations - 1) {
                    secondaryOutput = toOutput.nextSecondaryOutput();
                }
            }
        }

        @Override
        public int operationsRoomFor(@Nonnull ChanceOutput toOutput, int currentMax) {
            currentMax = OutputHelper.operationsRoomFor(mainSlot, toOutput.getMainOutput(), currentMax);
            return OutputHelper.operationsRoomFor(secondarySlot, toOutput.getMaxSecondaryOutput(), currentMax);
        }
    };
}
Also used : Nonnull(javax.annotation.Nonnull) NonNull(mekanism.api.annotations.NonNull) ChanceOutput(mekanism.api.recipes.SawmillRecipe.ChanceOutput) ItemStack(net.minecraft.item.ItemStack)

Example 3 with ChanceOutput

use of mekanism.api.recipes.SawmillRecipe.ChanceOutput 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)

Aggregations

ChanceOutput (mekanism.api.recipes.SawmillRecipe.ChanceOutput)3 ItemStack (net.minecraft.item.ItemStack)3 Nonnull (javax.annotation.Nonnull)1 NonNull (mekanism.api.annotations.NonNull)1 SawmillRecipe (mekanism.api.recipes.SawmillRecipe)1 ItemStackIngredient (mekanism.api.recipes.inputs.ItemStackIngredient)1 IngredientHelper (mekanism.common.integration.projecte.IngredientHelper)1 NormalizedSimpleStack (moze_intel.projecte.api.nss.NormalizedSimpleStack)1 Fraction (org.apache.commons.lang3.math.Fraction)1