Search in sources :

Example 6 with IInventorySlot

use of mekanism.api.inventory.IInventorySlot in project Mekanism by mekanism.

the class TileEntityFactory method sortInventory.

// End methods IComputerTile
private void sortInventory() {
    Map<HashedItem, RecipeProcessInfo> processes = new HashMap<>();
    List<ProcessInfo> emptyProcesses = new ArrayList<>();
    for (ProcessInfo processInfo : processInfoSlots) {
        IInventorySlot inputSlot = processInfo.getInputSlot();
        if (inputSlot.isEmpty()) {
            emptyProcesses.add(processInfo);
        } else {
            ItemStack inputStack = inputSlot.getStack();
            HashedItem item = HashedItem.raw(inputStack);
            RecipeProcessInfo recipeProcessInfo = processes.computeIfAbsent(item, i -> new RecipeProcessInfo());
            recipeProcessInfo.processes.add(processInfo);
            recipeProcessInfo.totalCount += inputStack.getCount();
            if (recipeProcessInfo.lazyMinPerSlot == null && !CommonWorldTickHandler.flushTagAndRecipeCaches) {
                // If we don't have a lazily initialized min per slot calculation set for it yet
                // and our cache is not invalid/out of date due to a reload
                CachedRecipe<RECIPE> cachedRecipe = getCachedRecipe(processInfo.getProcess());
                if (isCachedRecipeValid(cachedRecipe, inputStack)) {
                    // And our current process has a cached recipe then set the lazily initialized per slot value
                    // Note: If something goes wrong, and we end up with zero as how much we need as an input
                    // we just bump the value up to one to make sure we properly handle it
                    recipeProcessInfo.lazyMinPerSlot = () -> Math.max(1, getNeededInput(cachedRecipe.getRecipe(), inputStack));
                }
            }
        }
    }
    if (processes.isEmpty()) {
        // If all input slots are empty, just exit
        return;
    }
    for (Entry<HashedItem, RecipeProcessInfo> entry : processes.entrySet()) {
        RecipeProcessInfo recipeProcessInfo = entry.getValue();
        if (recipeProcessInfo.lazyMinPerSlot == null) {
            // If we don't have a lazy initializer for our minPerSlot setup, that means that there is
            // no valid cached recipe for any of the slots of this type currently, so we want to try and
            // get the recipe we will have for the first slot, once we end up with more items in the stack
            recipeProcessInfo.lazyMinPerSlot = () -> {
                // Note: We put all of this logic in the lazy init, so that we don't actually call any of this
                // until it is needed. That way if we have no empty slots and all our input slots are filled
                // we don't do any extra processing here, and can properly short circuit
                HashedItem item = entry.getKey();
                ItemStack largerInput = item.createStack(Math.min(item.getStack().getMaxStackSize(), recipeProcessInfo.totalCount));
                ProcessInfo processInfo = recipeProcessInfo.processes.get(0);
                // Try getting a recipe for our input with a larger size, and update the cache if we find one
                RECIPE recipe = getRecipeForInput(processInfo.getProcess(), largerInput, processInfo.getOutputSlot(), processInfo.getSecondaryOutputSlot(), true);
                if (recipe != null) {
                    return Math.max(1, getNeededInput(recipe, largerInput));
                }
                return 1;
            };
        }
    }
    if (!emptyProcesses.isEmpty()) {
        // If we have any empty slots, we need to factor them in as valid slots for items to transferred to
        addEmptySlotsAsTargets(processes, emptyProcesses);
    // Note: Any remaining empty slots are "ignored" as we don't have any
    // spare items to distribute to them
    }
    // Distribute items among the slots
    distributeItems(processes);
}
Also used : IInventorySlot(mekanism.api.inventory.IInventorySlot) HashedItem(mekanism.common.lib.inventory.HashedItem) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProcessInfo(mekanism.common.base.ProcessInfo) ItemStack(net.minecraft.item.ItemStack)

Example 7 with IInventorySlot

use of mekanism.api.inventory.IInventorySlot in project Mekanism by mekanism.

the class TileEntitySawingFactory method getSecondaryOutput.

// Methods relating to IComputerTile
@ComputerMethod
private ItemStack getSecondaryOutput(int process) throws ComputerException {
    validateValidProcess(process);
    IInventorySlot secondaryOutputSlot = processInfoSlots[process].getSecondaryOutputSlot();
    // This should never be null, but in case it is, handle it
    return secondaryOutputSlot == null ? ItemStack.EMPTY : secondaryOutputSlot.getStack();
}
Also used : IInventorySlot(mekanism.api.inventory.IInventorySlot) ComputerMethod(mekanism.common.integration.computer.annotation.ComputerMethod)

Example 8 with IInventorySlot

use of mekanism.api.inventory.IInventorySlot in project Mekanism by mekanism.

the class ISideConfiguration method getActiveDataType.

@Nullable
default DataType getActiveDataType(Object container) {
    ConfigInfo info = null;
    TileComponentConfig config = getConfig();
    if (container instanceof IGasTank && config.supports(TransmissionType.GAS)) {
        info = config.getConfig(TransmissionType.GAS);
    } else if (container instanceof IInfusionTank && config.supports(TransmissionType.INFUSION)) {
        info = config.getConfig(TransmissionType.INFUSION);
    } else if (container instanceof IPigmentTank && config.supports(TransmissionType.PIGMENT)) {
        info = config.getConfig(TransmissionType.PIGMENT);
    } else if (container instanceof ISlurryTank && config.supports(TransmissionType.SLURRY)) {
        info = config.getConfig(TransmissionType.SLURRY);
    } else if (container instanceof IExtendedFluidTank && config.supports(TransmissionType.FLUID)) {
        info = config.getConfig(TransmissionType.FLUID);
    } else if (container instanceof IInventorySlot && config.supports(TransmissionType.ITEM)) {
        info = config.getConfig(TransmissionType.ITEM);
    }
    if (info != null) {
        List<DataType> types = info.getDataTypeForContainer(container);
        int count = types.size();
        // of <= size - 1 to cut down slightly on the calculations
        if (count > 0 && count < info.getSupportedDataTypes().size()) {
            return types.get(0);
        }
    }
    return null;
}
Also used : IInventorySlot(mekanism.api.inventory.IInventorySlot) IPigmentTank(mekanism.api.chemical.pigment.IPigmentTank) IInfusionTank(mekanism.api.chemical.infuse.IInfusionTank) IGasTank(mekanism.api.chemical.gas.IGasTank) DataType(mekanism.common.tile.component.config.DataType) ConfigInfo(mekanism.common.tile.component.config.ConfigInfo) TileComponentConfig(mekanism.common.tile.component.TileComponentConfig) IExtendedFluidTank(mekanism.api.fluid.IExtendedFluidTank) ISlurryTank(mekanism.api.chemical.slurry.ISlurryTank) Nullable(javax.annotation.Nullable)

Example 9 with IInventorySlot

use of mekanism.api.inventory.IInventorySlot in project Mekanism by mekanism.

the class TileComponentConfig method setupItemIOConfig.

public ConfigInfo setupItemIOConfig(List<IInventorySlot> inputSlots, List<IInventorySlot> outputSlots, IInventorySlot energySlot, boolean alwaysAllow) {
    ConfigInfo itemConfig = getConfig(TransmissionType.ITEM);
    if (itemConfig != null) {
        itemConfig.addSlotInfo(DataType.INPUT, new InventorySlotInfo(true, alwaysAllow, inputSlots));
        itemConfig.addSlotInfo(DataType.OUTPUT, new InventorySlotInfo(alwaysAllow, true, outputSlots));
        List<IInventorySlot> ioSlots = new ArrayList<>(inputSlots);
        ioSlots.addAll(outputSlots);
        itemConfig.addSlotInfo(DataType.INPUT_OUTPUT, new InventorySlotInfo(true, true, ioSlots));
        itemConfig.addSlotInfo(DataType.ENERGY, new InventorySlotInfo(true, true, energySlot));
        // Set default config directions
        itemConfig.setDefaults();
    }
    return itemConfig;
}
Also used : IInventorySlot(mekanism.api.inventory.IInventorySlot) ArrayList(java.util.ArrayList) ConfigInfo(mekanism.common.tile.component.config.ConfigInfo) InventorySlotInfo(mekanism.common.tile.component.config.slot.InventorySlotInfo)

Example 10 with IInventorySlot

use of mekanism.api.inventory.IInventorySlot in project Mekanism by mekanism.

the class TileEntityFormulaicAssemblicator method doSingleCraft.

private boolean doSingleCraft() {
    recalculateRecipe();
    ItemStack output = lastOutputStack;
    if (!output.isEmpty() && tryMoveToOutput(output, Action.SIMULATE) && (lastRemainingItems.isEmpty() || lastRemainingItems.stream().allMatch(it -> it.isEmpty() || tryMoveToOutput(it, Action.SIMULATE)))) {
        tryMoveToOutput(output, Action.EXECUTE);
        // For example if there are multiple stacks of dirt in remaining and we have room for one stack, but given we only check one stack at a time...)
        for (ItemStack remainingItem : lastRemainingItems) {
            if (!remainingItem.isEmpty()) {
                // TODO: Check if it matters that we are not actually updating the list of remaining items?
                // The better solution would be to not allow continuing until we moved output AND all remaining items
                // instead of trying to move all at once??
                tryMoveToOutput(remainingItem, Action.EXECUTE);
            }
        }
        for (IInventorySlot craftingSlot : craftingGridSlots) {
            if (!craftingSlot.isEmpty()) {
                MekanismUtils.logMismatchedStackSize(craftingSlot.shrinkStack(1, Action.EXECUTE), 1);
            }
        }
        if (formula != null) {
            moveItemsToGrid();
        }
        markDirty(false);
        return true;
    }
    return false;
}
Also used : SyncableInt(mekanism.common.inventory.container.sync.SyncableInt) AutomationType(mekanism.api.inventory.AutomationType) IEnergyContainerHolder(mekanism.common.capabilities.holder.energy.IEnergyContainerHolder) ComputerIInventorySlotWrapper(mekanism.common.integration.computer.SpecialComputerMethodWrapper.ComputerIInventorySlotWrapper) TileComponentConfig(mekanism.common.tile.component.TileComponentConfig) CompoundNBT(net.minecraft.nbt.CompoundNBT) WrappingComputerMethod(mekanism.common.integration.computer.annotation.WrappingComputerMethod) InventorySlotHelper(mekanism.common.capabilities.holder.slot.InventorySlotHelper) ICraftingRecipe(net.minecraft.item.crafting.ICraftingRecipe) TileComponentEjector(mekanism.common.tile.component.TileComponentEjector) CraftingInventory(net.minecraft.inventory.CraftingInventory) TileEntityConfigurableMachine(mekanism.common.tile.prefab.TileEntityConfigurableMachine) NonNullList(net.minecraft.util.NonNullList) Object2IntOpenHashMap(it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap) BlockState(net.minecraft.block.BlockState) MekanismBlocks(mekanism.common.registries.MekanismBlocks) HashedItem(mekanism.common.lib.inventory.HashedItem) MachineEnergyContainer(mekanism.common.capabilities.energy.MachineEnergyContainer) EnergyInventorySlot(mekanism.common.inventory.slot.EnergyInventorySlot) StackUtils(mekanism.common.util.StackUtils) MekanismUtils(mekanism.common.util.MekanismUtils) FormulaicCraftingSlot(mekanism.common.inventory.slot.FormulaicCraftingSlot) List(java.util.List) ComputerException(mekanism.common.integration.computer.ComputerException) NBTConstants(mekanism.api.NBTConstants) IHasMode(mekanism.common.tile.interfaces.IHasMode) MekanismContainer(mekanism.common.inventory.container.MekanismContainer) Upgrade(mekanism.api.Upgrade) EnergyContainerHelper(mekanism.common.capabilities.holder.energy.EnergyContainerHelper) SyncableBoolean(mekanism.common.inventory.container.sync.SyncableBoolean) RecipeFormula(mekanism.common.content.assemblicator.RecipeFormula) ArrayList(java.util.ArrayList) IRecipeType(net.minecraft.item.crafting.IRecipeType) ITextComponent(net.minecraft.util.text.ITextComponent) ItemStack(net.minecraft.item.ItemStack) ItemHandlerHelper(net.minecraftforge.items.ItemHandlerHelper) TransmissionType(mekanism.common.lib.transmitter.TransmissionType) FloatingLong(mekanism.api.math.FloatingLong) ComputerMethod(mekanism.common.integration.computer.annotation.ComputerMethod) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Mekanism(mekanism.common.Mekanism) FormulaInventorySlot(mekanism.common.inventory.slot.FormulaInventorySlot) SyntheticComputerMethod(mekanism.common.integration.computer.annotation.SyntheticComputerMethod) SyncableItemStack(mekanism.common.inventory.container.sync.SyncableItemStack) IntList(it.unimi.dsi.fastutil.ints.IntList) InputInventorySlot(mekanism.common.inventory.slot.InputInventorySlot) Object2IntMap(it.unimi.dsi.fastutil.objects.Object2IntMap) IntOpenHashSet(it.unimi.dsi.fastutil.ints.IntOpenHashSet) IInventorySlot(mekanism.api.inventory.IInventorySlot) CommonWorldTickHandler(mekanism.common.CommonWorldTickHandler) ItemCraftingFormula(mekanism.common.item.ItemCraftingFormula) IntSet(it.unimi.dsi.fastutil.ints.IntSet) OutputInventorySlot(mekanism.common.inventory.slot.OutputInventorySlot) IInventorySlotHolder(mekanism.common.capabilities.holder.slot.IInventorySlotHolder) Action(mekanism.api.Action) UpgradeUtils(mekanism.common.util.UpgradeUtils) IInventorySlot(mekanism.api.inventory.IInventorySlot) ItemStack(net.minecraft.item.ItemStack) SyncableItemStack(mekanism.common.inventory.container.sync.SyncableItemStack)

Aggregations

IInventorySlot (mekanism.api.inventory.IInventorySlot)22 ItemStack (net.minecraft.item.ItemStack)12 ArrayList (java.util.ArrayList)7 Nonnull (javax.annotation.Nonnull)5 SyncableItemStack (mekanism.common.inventory.container.sync.SyncableItemStack)5 HashedItem (mekanism.common.lib.inventory.HashedItem)5 ConfigInfo (mekanism.common.tile.component.config.ConfigInfo)4 List (java.util.List)3 Nullable (javax.annotation.Nullable)3 IntList (it.unimi.dsi.fastutil.ints.IntList)2 IntOpenHashSet (it.unimi.dsi.fastutil.ints.IntOpenHashSet)2 IntSet (it.unimi.dsi.fastutil.ints.IntSet)2 Object2IntMap (it.unimi.dsi.fastutil.objects.Object2IntMap)2 Object2IntOpenHashMap (it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap)2 HashMap (java.util.HashMap)2 NBTConstants (mekanism.api.NBTConstants)2 IMekanismInventory (mekanism.api.inventory.IMekanismInventory)2 FloatingLong (mekanism.api.math.FloatingLong)2 ComputerMethod (mekanism.common.integration.computer.annotation.ComputerMethod)2 TileEntityMekanism (mekanism.common.tile.base.TileEntityMekanism)2