Search in sources :

Example 41 with ISidedInventory

use of net.minecraft.inventory.ISidedInventory in project PneumaticCraft by MineMaarten.

the class IOHelper method extract.

public static ItemStack extract(IInventory inventory, ForgeDirection direction, boolean simulate) {
    if (inventory instanceof ISidedInventory) {
        ISidedInventory isidedinventory = (ISidedInventory) inventory;
        int[] accessibleSlotsFromSide = isidedinventory.getAccessibleSlotsFromSide(direction.ordinal());
        for (int anAccessibleSlotsFromSide : accessibleSlotsFromSide) {
            ItemStack stack = extract(inventory, direction, anAccessibleSlotsFromSide, simulate);
            if (stack != null)
                return stack;
        }
    } else {
        int j = inventory.getSizeInventory();
        for (int k = 0; k < j; ++k) {
            ItemStack stack = extract(inventory, direction, k, simulate);
            if (stack != null)
                return stack;
        }
    }
    return null;
}
Also used : ISidedInventory(net.minecraft.inventory.ISidedInventory) ItemStack(net.minecraft.item.ItemStack)

Example 42 with ISidedInventory

use of net.minecraft.inventory.ISidedInventory in project Engine by VoltzEngine-Project.

the class InternalInventoryHandler method tryPlaceInPosition.

/**
 * Tries to place an itemStack in a specific position if it is an inventory.
 *
 * @return The ItemStack remained after place attempt
 */
public ItemStack tryPlaceInPosition(ItemStack itemStack, Pos position, ForgeDirection dir) {
    TileEntity tileEntity = position.getTileEntity(world);
    ForgeDirection direction = dir.getOpposite();
    if (tileEntity != null && itemStack != null) {
        if (tileEntity instanceof TileEntityChest) {
            TileEntityChest[] chests = { (TileEntityChest) tileEntity, null };
            /**
             * Try to find a double chest.
             */
            for (int i = 2; i < 6; i++) {
                ForgeDirection searchDirection = ForgeDirection.getOrientation(i);
                Pos searchPosition = position.clone();
                searchPosition.add(searchDirection);
                if (searchPosition.getTileEntity(world) != null) {
                    if (searchPosition.getTileEntity(world).getClass() == chests[0].getClass()) {
                        chests[1] = (TileEntityChest) searchPosition.getTileEntity(world);
                        break;
                    }
                }
            }
            for (TileEntityChest chest : chests) {
                if (chest != null) {
                    for (int i = 0; i < chest.getSizeInventory(); i++) {
                        itemStack = this.addStackToInventory(i, chest, itemStack);
                        if (itemStack == null) {
                            return null;
                        }
                    }
                }
            }
        } else if (tileEntity instanceof IExtendedStorage) {
            return ((IExtendedStorage) tileEntity).addStackToStorage(itemStack);
        } else if (tileEntity instanceof ISidedInventory) {
            ISidedInventory inventory = (ISidedInventory) tileEntity;
            int[] slots = inventory.getAccessibleSlotsFromSide(direction.ordinal());
            for (int i = 0; i < slots.length; i++) {
                if (inventory.canInsertItem(slots[i], itemStack, direction.ordinal())) {
                    itemStack = this.addStackToInventory(slots[i], inventory, itemStack);
                }
                if (itemStack == null) {
                    return null;
                }
            }
        } else if (tileEntity instanceof IInventory) {
            IInventory inventory = (IInventory) tileEntity;
            for (int i = 0; i < inventory.getSizeInventory(); i++) {
                itemStack = this.addStackToInventory(i, inventory, itemStack);
                if (itemStack == null) {
                    return null;
                }
            }
        }
    }
    if (itemStack == null || itemStack.stackSize <= 0) {
        return null;
    }
    return itemStack;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) ISidedInventory(net.minecraft.inventory.ISidedInventory) IInventory(net.minecraft.inventory.IInventory) TileEntityChest(net.minecraft.tileentity.TileEntityChest) IExtendedStorage(com.builtbroken.mc.api.tile.IExtendedStorage) Pos(com.builtbroken.mc.lib.transform.vector.Pos) ForgeDirection(net.minecraftforge.common.util.ForgeDirection)

Example 43 with ISidedInventory

use of net.minecraft.inventory.ISidedInventory in project Engine by VoltzEngine-Project.

the class InventoryUtility method putStackInInventory.

/**
 * Tries to place an item into the inventory. If the inventory is not an instance of {@link ISidedInventory} it
 * will ignore the side param.
 *
 * @param inventory - inventory to insert the item into
 * @param itemStack - stack to insert
 * @param side      - side to inser the item into (0-5)
 * @param force-    overrides {@link IInventory#isItemValidForSlot(int, ItemStack)} check
 * @return what is left of the toInsert stack
 */
public static ItemStack putStackInInventory(IInventory inventory, ItemStack itemStack, int side, boolean force) {
    ItemStack toInsert = itemStack != null ? itemStack.copy() : null;
    if (toInsert != null) {
        if (!(inventory instanceof ISidedInventory)) {
            return putStackInInventory(inventory, toInsert, force);
        } else {
            ISidedInventory sidedInventory = (ISidedInventory) inventory;
            int[] slots = sidedInventory.getAccessibleSlotsFromSide(side);
            if (slots != null && slots.length != 0) {
                for (int get = 0; get < slots.length; get++) {
                    int slotID = slots[get];
                    if (force || sidedInventory.isItemValidForSlot(slotID, toInsert) && sidedInventory.canInsertItem(slotID, toInsert, side)) {
                        ItemStack inSlot = inventory.getStackInSlot(slotID);
                        if (inSlot == null) {
                            inventory.setInventorySlotContents(slotID, toInsert);
                            return null;
                        } else if (inSlot.isItemEqual(toInsert) && inSlot.stackSize < inSlot.getMaxStackSize()) {
                            if (inSlot.stackSize + toInsert.stackSize <= inSlot.getMaxStackSize()) {
                                ItemStack toSet = toInsert.copy();
                                toSet.stackSize += inSlot.stackSize;
                                inventory.setInventorySlotContents(slotID, toSet);
                                return null;
                            } else {
                                int rejects = (inSlot.stackSize + toInsert.stackSize) - inSlot.getMaxStackSize();
                                ItemStack toSet = toInsert.copy();
                                toSet.stackSize = inSlot.getMaxStackSize();
                                ItemStack remains = toInsert.copy();
                                remains.stackSize = rejects;
                                inventory.setInventorySlotContents(slotID, toSet);
                                toInsert = remains;
                            }
                        }
                    }
                }
            }
        }
    }
    return toInsert;
}
Also used : ISidedInventory(net.minecraft.inventory.ISidedInventory)

Example 44 with ISidedInventory

use of net.minecraft.inventory.ISidedInventory in project Engine by VoltzEngine-Project.

the class InventoryUtility method findFirstItemInInventory.

/**
 * Gets the first slot containing items
 * <p>
 * Does not actually consume the items
 *
 * @param inventory - inventory to search for items
 * @param side      - side to access, used for {@link ISidedInventory}
 *                  If this value is not between 0-5 it will not use
 *                  ISideInventory and instead bypass the sided checks
 * @param stackSize - amount to remove
 * @return pair containing the removed stack, and item
 */
public static Pair<ItemStack, Integer> findFirstItemInInventory(IInventory inventory, int side, int stackSize, IInventoryFilter filter) {
    if (!(inventory instanceof ISidedInventory) || side == -1 || side > 5) {
        for (int i = inventory.getSizeInventory() - 1; i >= 0; i--) {
            final ItemStack slotStack = inventory.getStackInSlot(i);
            if (slotStack != null && (filter == null || filter.isStackInFilter(slotStack))) {
                int amountToTake = stackSize <= 0 ? slotStack.getMaxStackSize() : Math.min(stackSize, slotStack.getMaxStackSize());
                amountToTake = Math.min(amountToTake, slotStack.stackSize);
                ItemStack toSend = slotStack.copy();
                toSend.stackSize = amountToTake;
                // inventory.decrStackSize(i, amountToTake);
                return new Pair(toSend, i);
            }
        }
    } else {
        ISidedInventory sidedInventory = (ISidedInventory) inventory;
        int[] slots = sidedInventory.getAccessibleSlotsFromSide(side);
        if (slots != null) {
            for (int get = slots.length - 1; get >= 0; get--) {
                int slotID = slots[get];
                final ItemStack slotStack = sidedInventory.getStackInSlot(slotID);
                if (slotStack != null && (filter == null || filter.isStackInFilter(slotStack))) {
                    int amountToTake = stackSize <= 0 ? slotStack.getMaxStackSize() : Math.min(stackSize, slotStack.getMaxStackSize());
                    amountToTake = Math.min(amountToTake, slotStack.stackSize);
                    ItemStack toSend = slotStack.copy();
                    toSend.stackSize = amountToTake;
                    if (sidedInventory.canExtractItem(slotID, toSend, side)) {
                        // sidedInventory.decrStackSize(slotID, amountToTake);
                        return new Pair(toSend, slotID);
                    }
                }
            }
        }
    }
    return null;
}
Also used : ISidedInventory(net.minecraft.inventory.ISidedInventory) Pair(com.builtbroken.jlib.type.Pair)

Example 45 with ISidedInventory

use of net.minecraft.inventory.ISidedInventory in project Engine by VoltzEngine-Project.

the class InternalInventoryHandler method tryGrabFromPosition.

/**
 * Tries to get an item from a position
 *
 * @param position - location of item
 * @param dir      - direction this item is from the original
 * @param ammount  - amount up to one stack to grab
 * @return the grabbed item stack
 */
public ItemStack tryGrabFromPosition(Pos position, ForgeDirection dir, int ammount) {
    ItemStack returnStack = null;
    TileEntity tileEntity = position.getTileEntity(world);
    ForgeDirection direction = dir.getOpposite();
    if (tileEntity != null) {
        if (tileEntity.getClass() == TileEntityChest.class) {
            TileEntityChest[] chests = { (TileEntityChest) tileEntity, null };
            /**
             * Try to find a double chest.
             */
            for (int i = 2; i < 6; i++) {
                ForgeDirection searchDirection = ForgeDirection.getOrientation(i);
                Pos searchPosition = position.clone();
                searchPosition.add(searchDirection);
                if (searchPosition.getTileEntity(world) != null) {
                    if (searchPosition.getTileEntity(world).getClass() == chests[0].getClass()) {
                        chests[1] = (TileEntityChest) searchPosition.getTileEntity(world);
                        break;
                    }
                }
            }
            chestSearch: for (TileEntityChest chest : chests) {
                if (chest != null) {
                    for (int i = 0; i < chest.getSizeInventory(); i++) {
                        ItemStack itemStack = this.removeStackFromInventory(i, chest, ammount);
                        if (itemStack != null) {
                            returnStack = itemStack;
                            break chestSearch;
                        }
                    }
                }
            }
        } else if (tileEntity instanceof ISidedInventory) {
            ISidedInventory inventory = (ISidedInventory) tileEntity;
            int[] slots = inventory.getAccessibleSlotsFromSide(direction.ordinal());
            for (int i = 0; i < slots.length; i++) {
                int slot = slots[i];
                ItemStack slotStack = inventory.getStackInSlot(slot);
                if (inventory.canExtractItem(slot, slotStack, direction.ordinal())) {
                    ItemStack itemStack = this.removeStackFromInventory(slot, inventory, ammount);
                    if (itemStack != null) {
                        returnStack = itemStack;
                        break;
                    }
                }
            }
        } else if (tileEntity instanceof IInventory) {
            IInventory inventory = (IInventory) tileEntity;
            for (int i = 0; i < inventory.getSizeInventory(); i++) {
                ItemStack itemStack = this.removeStackFromInventory(i, inventory, ammount);
                if (itemStack != null) {
                    returnStack = itemStack;
                    break;
                }
            }
        }
    }
    return returnStack;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) ISidedInventory(net.minecraft.inventory.ISidedInventory) IInventory(net.minecraft.inventory.IInventory) TileEntityChest(net.minecraft.tileentity.TileEntityChest) Pos(com.builtbroken.mc.lib.transform.vector.Pos) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) ItemStack(net.minecraft.item.ItemStack)

Aggregations

ISidedInventory (net.minecraft.inventory.ISidedInventory)53 ItemStack (net.minecraft.item.ItemStack)36 IInventory (net.minecraft.inventory.IInventory)24 TileEntity (net.minecraft.tileentity.TileEntity)9 IItemHandler (net.minecraftforge.items.IItemHandler)7 ForgeDirection (net.minecraftforge.common.util.ForgeDirection)5 ArrayList (java.util.ArrayList)3 World (net.minecraft.world.World)3 Pos (com.builtbroken.mc.lib.transform.vector.Pos)2 IBee (forestry.api.apiculture.IBee)2 IInventoryUtil (logisticspipes.interfaces.IInventoryUtil)2 ISlotUpgradeManager (logisticspipes.interfaces.ISlotUpgradeManager)2 TileEntityChest (net.minecraft.tileentity.TileEntityChest)2 EnumFacing (net.minecraft.util.EnumFacing)2 InvWrapper (net.minecraftforge.items.wrapper.InvWrapper)2 SidedInvWrapper (net.minecraftforge.items.wrapper.SidedInvWrapper)2 Int3 (WayofTime.alchemicalWizardry.api.Int3)1 RoutingFocusParadigm (WayofTime.alchemicalWizardry.api.RoutingFocusParadigm)1 RoutingFocusPosAndFacing (WayofTime.alchemicalWizardry.api.RoutingFocusPosAndFacing)1 IBloodAltar (WayofTime.alchemicalWizardry.api.tile.IBloodAltar)1