Search in sources :

Example 1 with ISidedInventoryProvider

use of net.minecraft.inventory.ISidedInventoryProvider 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)

Aggregations

BlockState (net.minecraft.block.BlockState)1 ISidedInventory (net.minecraft.inventory.ISidedInventory)1 ISidedInventoryProvider (net.minecraft.inventory.ISidedInventoryProvider)1 TileEntity (net.minecraft.tileentity.TileEntity)1 IItemHandler (net.minecraftforge.items.IItemHandler)1 SidedInvWrapper (net.minecraftforge.items.wrapper.SidedInvWrapper)1