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;
}
Aggregations