Search in sources :

Example 1 with ChestType

use of net.minecraft.world.level.block.state.properties.ChestType in project SpongeCommon by SpongePowered.

the class ChestBlockEntityMixin_API method connectedChest.

@Override
public Optional<Chest> connectedChest() {
    // Based off of the logic in ChestBlock.getChestInventory but without a blocked check and returning the TE instead of the inventory.
    ChestBlockEntity chestTileEntity = (ChestBlockEntity) (Object) this;
    BlockState chestState = chestTileEntity.getBlockState();
    ChestType chestType = chestTileEntity.getBlockState().getValue(ChestBlock.TYPE);
    Level world = chestTileEntity.getLevel();
    if (chestType != ChestType.SINGLE) {
        BlockPos connectedPos = chestTileEntity.getBlockPos().relative(ChestBlock.getConnectedDirection(chestState));
        BlockState connectedState = world.getBlockState(connectedPos);
        if (connectedState.getBlock() == chestState.getBlock()) {
            ChestType connectedType = connectedState.getValue(ChestBlock.TYPE);
            if (connectedType != ChestType.SINGLE && chestType != connectedType && chestState.getValue(ChestBlock.FACING) == connectedState.getValue(ChestBlock.FACING)) {
                BlockEntity connectedTileEntity = world.getBlockEntity(connectedPos);
                if (connectedTileEntity instanceof ChestBlockEntity) {
                    return Optional.of((Chest) connectedTileEntity);
                }
            }
        }
    }
    return Optional.empty();
}
Also used : BlockState(net.minecraft.world.level.block.state.BlockState) ChestBlockEntity(net.minecraft.world.level.block.entity.ChestBlockEntity) Level(net.minecraft.world.level.Level) BlockPos(net.minecraft.core.BlockPos) ChestType(net.minecraft.world.level.block.state.properties.ChestType) ChestBlockEntity(net.minecraft.world.level.block.entity.ChestBlockEntity) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Example 2 with ChestType

use of net.minecraft.world.level.block.state.properties.ChestType in project SpongeCommon by SpongePowered.

the class InventoryUtil method getDoubleChestInventory.

public static Optional<Inventory> getDoubleChestInventory(final ChestBlockEntity chest) {
    final Optional<Chest> connectedChestOptional = ((Chest) chest).connectedChest();
    if (!connectedChestOptional.isPresent()) {
        return Optional.empty();
    }
    final ChestType chestType = chest.getBlockState().getValue(ChestBlock.TYPE);
    final ChestBlockEntity connectedChest = (ChestBlockEntity) connectedChestOptional.get();
    // Logic in the instanceof check of ChestBlock.getChestInventory but with exploded ternary operators.
    if (chestType == ChestType.RIGHT) {
        return Optional.of((Inventory) new CompoundContainer(chest, connectedChest));
    } else {
        return Optional.of((Inventory) new CompoundContainer(connectedChest, chest));
    }
}
Also used : Chest(org.spongepowered.api.block.entity.carrier.chest.Chest) ChestBlockEntity(net.minecraft.world.level.block.entity.ChestBlockEntity) CompoundContainer(net.minecraft.world.CompoundContainer) ChestType(net.minecraft.world.level.block.state.properties.ChestType)

Aggregations

ChestBlockEntity (net.minecraft.world.level.block.entity.ChestBlockEntity)2 ChestType (net.minecraft.world.level.block.state.properties.ChestType)2 BlockPos (net.minecraft.core.BlockPos)1 CompoundContainer (net.minecraft.world.CompoundContainer)1 Level (net.minecraft.world.level.Level)1 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)1 BlockState (net.minecraft.world.level.block.state.BlockState)1 Chest (org.spongepowered.api.block.entity.carrier.chest.Chest)1