Search in sources :

Example 36 with ISidedInventory

use of net.minecraft.inventory.ISidedInventory in project Bookshelf by Darkhax-Minecraft.

the class InventoryUtils method getInventory.

/**
 * Gets an inventory from a position within the world. If no tile exists or the tile does
 * not have the inventory capability the empty inventory handler will be returned.
 *
 * @param world The world instance.
 * @param pos The position of the expected tile entity.
 * @param side The side to access the inventory from.
 * @return The inventory handler. Will be empty if none was found.
 */
public static IItemHandler getInventory(World world, BlockPos pos, Direction side) {
    final TileEntity tileEntity = world.getBlockEntity(pos);
    if (tileEntity != null) {
        final LazyOptional<IItemHandler> inventoryCap = tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side);
        return inventoryCap.orElse(EmptyHandler.INSTANCE);
    } else {
        // Some blocks like composters are not tile entities so their inv can not be
        // accessed through the normal capability system.
        final BlockState state = world.getBlockState(pos);
        if (state.getBlock() instanceof ISidedInventoryProvider) {
            final ISidedInventoryProvider inventoryProvider = (ISidedInventoryProvider) state.getBlock();
            final ISidedInventory inventory = inventoryProvider.getContainer(state, world, pos);
            if (inventory != null) {
                return new SidedInvWrapper(inventory, side);
            }
        }
    }
    return EmptyHandler.INSTANCE;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) ISidedInventory(net.minecraft.inventory.ISidedInventory) ISidedInventoryProvider(net.minecraft.inventory.ISidedInventoryProvider) BlockState(net.minecraft.block.BlockState) IItemHandler(net.minecraftforge.items.IItemHandler) SidedInvWrapper(net.minecraftforge.items.wrapper.SidedInvWrapper)

Example 37 with ISidedInventory

use of net.minecraft.inventory.ISidedInventory in project ArsMagica2 by Mithion.

the class InventoryUtilities method getFirstStackStartingFromSlot.

public static GetFirstStackStartingFromSlotResult getFirstStackStartingFromSlot(IInventory inventory, ItemStack itemStack, int slot, int side) {
    if (inventory instanceof ISidedInventory) {
        ISidedInventory sidededInventory = (ISidedInventory) inventory;
        int[] slots = sidededInventory.getAccessibleSlotsFromSide(side);
        for (int i = slot; i < slots.length; i++) {
            itemStack = inventory.getStackInSlot(slots[i]);
            if (itemStack != null && canExtractItemFromInventory(sidededInventory, itemStack, slots[i], side)) {
                return new GetFirstStackStartingFromSlotResult(i, itemStack);
            }
        }
    } else {
        return getFirstStackStartingFromSlot(inventory, itemStack, slot);
    }
    return new GetFirstStackStartingFromSlotResult(-1, null);
}
Also used : ISidedInventory(net.minecraft.inventory.ISidedInventory)

Example 38 with ISidedInventory

use of net.minecraft.inventory.ISidedInventory in project ArsMagica2 by Mithion.

the class InventoryUtilities method inventoryHasItem.

public static boolean inventoryHasItem(IInventory inventory, ItemStack search, int quantity, int side) {
    if (inventory instanceof ISidedInventory) {
        ISidedInventory sidedInventory = (ISidedInventory) inventory;
        int qtyFound = 0;
        int[] slots = sidedInventory.getAccessibleSlotsFromSide(side);
        for (int i = 0; i < slots.length; i++) {
            ItemStack inventoryStack = inventory.getStackInSlot(slots[i]);
            if (inventoryStack == null)
                continue;
            else if (compareItemStacks(inventoryStack, search, true, false, true, true)) {
                qtyFound += inventoryStack.stackSize;
                if (qtyFound >= quantity)
                    return true;
            }
        }
        return false;
    } else {
        return inventoryHasItem(inventory, search, quantity);
    }
}
Also used : ISidedInventory(net.minecraft.inventory.ISidedInventory) ItemStack(net.minecraft.item.ItemStack)

Example 39 with ISidedInventory

use of net.minecraft.inventory.ISidedInventory in project ArsMagica2 by Mithion.

the class InventoryUtilities method getLikeItemCount.

public static int getLikeItemCount(IInventory inventory, ItemStack stack, int side) {
    if (inventory instanceof ISidedInventory) {
        int totalCount = 0;
        ISidedInventory sidedInventory = (ISidedInventory) inventory;
        int[] slots = sidedInventory.getAccessibleSlotsFromSide(side);
        for (int i = 0; i < slots.length; i++) {
            ItemStack invStack = inventory.getStackInSlot(slots[i]);
            if (invStack != null && compareItemStacks(invStack, stack, true, false, true, true))
                totalCount += invStack.stackSize;
        }
        return totalCount;
    } else {
        return getLikeItemCount(inventory, stack);
    }
}
Also used : ISidedInventory(net.minecraft.inventory.ISidedInventory) ItemStack(net.minecraft.item.ItemStack)

Example 40 with ISidedInventory

use of net.minecraft.inventory.ISidedInventory in project ArsMagica2 by Mithion.

the class InventoryUtilities method mergeIntoInventory.

public static boolean mergeIntoInventory(IInventory inventory, ItemStack toMerge, int quantity, int side) {
    if (inventory instanceof ISidedInventory) {
        ItemStack stack = toMerge.splitStack(Math.min(toMerge.stackSize, quantity));
        ISidedInventory sidedInventory = (ISidedInventory) inventory;
        int[] slots = sidedInventory.getAccessibleSlotsFromSide(side);
        boolean flag = false;
        for (int i = 0; i < slots.length && stack != null && stack.stackSize > 0; ++i) {
            // For each slot that can be accessed from this side
            ItemStack prvStack = sidedInventory.getStackInSlot(slots[i]);
            if (InventoryUtilities.canInsertItemToInventory(sidedInventory, stack, slots[i], side)) {
                // if the items can be inserted into the current slot
                if (prvStack == null) {
                    // if the stack in the slot is null then get the max value that can be moved and transfer the stack to the inventory
                    int max = Math.min(stack.getMaxStackSize(), sidedInventory.getInventoryStackLimit());
                    if (max >= stack.stackSize) {
                        sidedInventory.setInventorySlotContents(slots[i], stack.copy());
                        stack.stackSize = 0;
                        flag = true;
                    } else {
                        sidedInventory.setInventorySlotContents(slots[i], stack.splitStack(max));
                        flag = true;
                    }
                } else if (InventoryUtilities.canStacksMerge(prvStack, stack)) {
                    // if the stack in the slot can be merged with the stack we are trying to move get the max items that can exist in the slot
                    // and insert as many as will fit from the stack we are trying to move
                    int max = Math.min(stack.getMaxStackSize(), sidedInventory.getInventoryStackLimit());
                    if (max > prvStack.stackSize) {
                        int qty = Math.min(stack.stackSize, max - prvStack.stackSize);
                        prvStack.stackSize += qty;
                        stack.stackSize -= qty;
                        flag = qty > 0;
                    }
                }
            }
        }
        toMerge.stackSize = toMerge.stackSize + stack.stackSize;
        return flag;
    } else {
        return mergeIntoInventory(inventory, toMerge, quantity);
    }
}
Also used : ISidedInventory(net.minecraft.inventory.ISidedInventory) 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