Search in sources :

Example 1 with InventoryProvider

use of net.minecraft.block.InventoryProvider in project lithium-fabric by CaffeineMC.

the class HopperHelper method vanillaGetBlockInventory.

/**
 * Gets the block inventory at the given position, exactly like vanilla gets it.
 * Needed because we don't want to search for entity inventories like the vanilla method does.
 *
 * @param world    world we are searching in
 * @param blockPos position of the block inventory
 * @return the block inventory at the given position
 */
@Nullable
public static Inventory vanillaGetBlockInventory(World world, BlockPos blockPos) {
    // [VanillaCopy]
    Inventory inventory = null;
    BlockState blockState = world.getBlockState(blockPos);
    Block block = blockState.getBlock();
    if (block instanceof InventoryProvider) {
        inventory = ((InventoryProvider) block).getInventory(blockState, world, blockPos);
    } else if (blockState.hasBlockEntity()) {
        BlockEntity blockEntity = world.getBlockEntity(blockPos);
        if (blockEntity instanceof Inventory) {
            inventory = (Inventory) blockEntity;
            if (inventory instanceof ChestBlockEntity && block instanceof ChestBlock) {
                inventory = ChestBlock.getInventory((ChestBlock) block, blockState, world, blockPos, true);
            }
        }
    }
    return inventory;
}
Also used : BlockState(net.minecraft.block.BlockState) ChestBlockEntity(net.minecraft.block.entity.ChestBlockEntity) Block(net.minecraft.block.Block) ChestBlock(net.minecraft.block.ChestBlock) InventoryProvider(net.minecraft.block.InventoryProvider) ChestBlock(net.minecraft.block.ChestBlock) SidedInventory(net.minecraft.inventory.SidedInventory) Inventory(net.minecraft.inventory.Inventory) BlockEntity(net.minecraft.block.entity.BlockEntity) LootableContainerBlockEntity(net.minecraft.block.entity.LootableContainerBlockEntity) HopperBlockEntity(net.minecraft.block.entity.HopperBlockEntity) ChestBlockEntity(net.minecraft.block.entity.ChestBlockEntity) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Block (net.minecraft.block.Block)1 BlockState (net.minecraft.block.BlockState)1 ChestBlock (net.minecraft.block.ChestBlock)1 InventoryProvider (net.minecraft.block.InventoryProvider)1 BlockEntity (net.minecraft.block.entity.BlockEntity)1 ChestBlockEntity (net.minecraft.block.entity.ChestBlockEntity)1 HopperBlockEntity (net.minecraft.block.entity.HopperBlockEntity)1 LootableContainerBlockEntity (net.minecraft.block.entity.LootableContainerBlockEntity)1 Inventory (net.minecraft.inventory.Inventory)1 SidedInventory (net.minecraft.inventory.SidedInventory)1 Nullable (org.jetbrains.annotations.Nullable)1