Search in sources :

Example 16 with IInventorySlot

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

the class ItemRecipeData method applyToStack.

@Override
public boolean applyToStack(ItemStack stack) {
    if (slots.isEmpty()) {
        return true;
    }
    Item item = stack.getItem();
    boolean isBin = item instanceof ItemBlockBin;
    Optional<IItemHandler> capability = stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).resolve();
    List<IInventorySlot> slots = new ArrayList<>();
    if (capability.isPresent()) {
        IItemHandler itemHandler = capability.get();
        for (int i = 0; i < itemHandler.getSlots(); i++) {
            int slot = i;
            slots.add(new DummyInventorySlot(itemHandler.getSlotLimit(slot), itemStack -> itemHandler.isItemValid(slot, itemStack), isBin));
        }
    } else if (item instanceof BlockItem) {
        TileEntityMekanism tile = getTileFromBlock(((BlockItem) item).getBlock());
        if (tile == null || !tile.persistInventory()) {
            // Something went wrong
            return false;
        }
        for (int i = 0; i < tile.getSlots(); i++) {
            int slot = i;
            slots.add(new DummyInventorySlot(tile.getSlotLimit(slot), itemStack -> tile.isItemValid(slot, itemStack), isBin));
        }
    } else if (item instanceof ItemRobit) {
        // Inventory slots
        for (int slotY = 0; slotY < 3; slotY++) {
            for (int slotX = 0; slotX < 9; slotX++) {
                slots.add(new DummyInventorySlot(BasicInventorySlot.DEFAULT_LIMIT, BasicInventorySlot.alwaysTrue, false));
            }
        }
        // Energy slot
        slots.add(new DummyInventorySlot(BasicInventorySlot.DEFAULT_LIMIT, itemStack -> {
            if (EnergyCompatUtils.hasStrictEnergyHandler(itemStack)) {
                return true;
            }
            ItemStackToEnergyRecipe foundRecipe = MekanismRecipeType.ENERGY_CONVERSION.getInputCache().findTypeBasedRecipe(null, itemStack);
            return foundRecipe != null && !foundRecipe.getOutput(itemStack).isZero();
        }, false));
        // Smelting input slot
        slots.add(new DummyInventorySlot(BasicInventorySlot.DEFAULT_LIMIT, itemStack -> MekanismRecipeType.SMELTING.getInputCache().containsInput(null, itemStack), false));
        // Smelting output slot
        slots.add(new DummyInventorySlot(BasicInventorySlot.DEFAULT_LIMIT, BasicInventorySlot.alwaysTrue, false));
    } else if (item instanceof ISustainedInventory) {
        // Fallback just save it all
        for (IInventorySlot slot : this.slots) {
            if (!slot.isEmpty()) {
                // We have no information about what our item supports, but we have at least some stacks we want to transfer
                ((ISustainedInventory) stack.getItem()).setInventory(DataHandlerUtils.writeContainers(this.slots), stack);
                return true;
            }
        }
        return true;
    } else {
        return false;
    }
    if (slots.isEmpty()) {
        // We don't actually have any tanks in the output
        return true;
    }
    // TODO: Improve the logic so that it maybe tries multiple different slot combinations
    IMekanismInventory outputHandler = new IMekanismInventory() {

        @Nonnull
        @Override
        public List<IInventorySlot> getInventorySlots(@Nullable Direction side) {
            return slots;
        }

        @Override
        public void onContentsChanged() {
        }
    };
    boolean hasData = false;
    for (IInventorySlot slot : this.slots) {
        if (!slot.isEmpty()) {
            if (!ItemHandlerHelper.insertItemStacked(outputHandler, slot.getStack(), false).isEmpty()) {
                // If we have a remainder something failed so bail
                return false;
            }
            hasData = true;
        }
    }
    if (hasData) {
        // We managed to transfer it all into valid slots, so save it to the stack
        ((ISustainedInventory) stack.getItem()).setInventory(DataHandlerUtils.writeContainers(slots), stack);
    }
    return true;
}
Also used : IInventorySlot(mekanism.api.inventory.IInventorySlot) IItemHandler(net.minecraftforge.items.IItemHandler) EnergyCompatUtils(mekanism.common.integration.energy.EnergyCompatUtils) IMekanismInventory(mekanism.api.inventory.IMekanismInventory) Item(net.minecraft.item.Item) BasicInventorySlot(mekanism.common.inventory.slot.BasicInventorySlot) Direction(net.minecraft.util.Direction) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack) ItemHandlerHelper(net.minecraftforge.items.ItemHandlerHelper) Nonnull(javax.annotation.Nonnull) NonNull(mekanism.api.annotations.NonNull) ItemBlockBin(mekanism.common.item.block.ItemBlockBin) Nullable(javax.annotation.Nullable) TileEntityMekanism(mekanism.common.tile.base.TileEntityMekanism) ISustainedInventory(mekanism.common.tile.interfaces.ISustainedInventory) ListNBT(net.minecraft.nbt.ListNBT) MekanismRecipeType(mekanism.common.recipe.MekanismRecipeType) ItemRobit(mekanism.common.item.ItemRobit) Predicate(java.util.function.Predicate) DataHandlerUtils(mekanism.api.DataHandlerUtils) List(java.util.List) FieldsAreNonnullByDefault(mekanism.api.annotations.FieldsAreNonnullByDefault) IInventorySlot(mekanism.api.inventory.IInventorySlot) BlockItem(net.minecraft.item.BlockItem) NBTConstants(mekanism.api.NBTConstants) CapabilityItemHandler(net.minecraftforge.items.CapabilityItemHandler) ItemStackToEnergyRecipe(mekanism.api.recipes.ItemStackToEnergyRecipe) Optional(java.util.Optional) TileEntityMekanism(mekanism.common.tile.base.TileEntityMekanism) ISustainedInventory(mekanism.common.tile.interfaces.ISustainedInventory) IItemHandler(net.minecraftforge.items.IItemHandler) ItemRobit(mekanism.common.item.ItemRobit) ArrayList(java.util.ArrayList) BlockItem(net.minecraft.item.BlockItem) Direction(net.minecraft.util.Direction) ItemBlockBin(mekanism.common.item.block.ItemBlockBin) Item(net.minecraft.item.Item) BlockItem(net.minecraft.item.BlockItem) ItemStackToEnergyRecipe(mekanism.api.recipes.ItemStackToEnergyRecipe) IMekanismInventory(mekanism.api.inventory.IMekanismInventory) Nullable(javax.annotation.Nullable)

Example 17 with IInventorySlot

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

the class MekanismUtils method redstoneLevelFromContents.

/**
 * Calculates the redstone level based on the percentage of amount stored. Like {@link net.minecraftforge.items.ItemHandlerHelper#calcRedstoneFromInventory(IItemHandler)}
 * except without limiting slots to the max stack size of the item to allow for better support for bins
 *
 * @return A redstone level based on the percentage of the amount stored.
 */
public static int redstoneLevelFromContents(List<IInventorySlot> slots) {
    long totalCount = 0;
    long totalLimit = 0;
    for (IInventorySlot slot : slots) {
        if (slot.isEmpty()) {
            totalLimit += slot.getLimit(ItemStack.EMPTY);
        } else {
            totalCount += slot.getCount();
            totalLimit += slot.getLimit(slot.getStack());
        }
    }
    return redstoneLevelFromContents(totalCount, totalLimit);
}
Also used : IInventorySlot(mekanism.api.inventory.IInventorySlot)

Example 18 with IInventorySlot

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

the class StackUtils method merge.

public static void merge(@Nonnull List<IInventorySlot> orig, @Nonnull List<IInventorySlot> toAdd) {
    for (int i = 0; i < toAdd.size(); i++) {
        IInventorySlot toAddSlot = toAdd.get(i);
        if (!toAddSlot.isEmpty()) {
            IInventorySlot origSlot = orig.get(i);
            origSlot.setStack(merge(origSlot.getStack(), toAddSlot.getStack()));
        }
    }
}
Also used : IInventorySlot(mekanism.api.inventory.IInventorySlot)

Example 19 with IInventorySlot

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

the class PortableQIODashboardInventory method getInitialInventory.

@Override
protected List<IInventorySlot> getInitialInventory() {
    List<IInventorySlot> slots = new ArrayList<>();
    craftingWindows = new QIOCraftingWindow[MAX_CRAFTING_WINDOWS];
    for (byte tableIndex = 0; tableIndex < craftingWindows.length; tableIndex++) {
        QIOCraftingWindow craftingWindow = new QIOCraftingWindow(this, tableIndex);
        craftingWindows[tableIndex] = craftingWindow;
        for (int slot = 0; slot < 9; slot++) {
            slots.add(craftingWindow.getInputSlot(slot));
        }
        slots.add(craftingWindow.getOutputSlot());
    }
    return slots;
}
Also used : IInventorySlot(mekanism.api.inventory.IInventorySlot) QIOCraftingWindow(mekanism.common.content.qio.QIOCraftingWindow) ArrayList(java.util.ArrayList)

Example 20 with IInventorySlot

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

the class GuiMekanismTile method getFromSlot.

private DataType getFromSlot(Slot slot) {
    if (slot.index < tile.getSlots() && slot instanceof InventoryContainerSlot) {
        ISideConfiguration config = (ISideConfiguration) tile;
        ConfigInfo info = config.getConfig().getConfig(TransmissionType.ITEM);
        if (info != null) {
            Set<DataType> supportedDataTypes = info.getSupportedDataTypes();
            IInventorySlot inventorySlot = ((InventoryContainerSlot) slot).getInventorySlot();
            for (DataType type : supportedDataTypes) {
                ISlotInfo slotInfo = info.getSlotInfo(type);
                if (slotInfo instanceof InventorySlotInfo && ((InventorySlotInfo) slotInfo).hasSlot(inventorySlot)) {
                    return type;
                }
            }
        }
    }
    return null;
}
Also used : IInventorySlot(mekanism.api.inventory.IInventorySlot) ISlotInfo(mekanism.common.tile.component.config.slot.ISlotInfo) DataType(mekanism.common.tile.component.config.DataType) InventoryContainerSlot(mekanism.common.inventory.container.slot.InventoryContainerSlot) ConfigInfo(mekanism.common.tile.component.config.ConfigInfo) InventorySlotInfo(mekanism.common.tile.component.config.slot.InventorySlotInfo) ISideConfiguration(mekanism.common.tile.interfaces.ISideConfiguration)

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