use of net.minecraft.world.level.block.entity.ChestBlockEntity 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();
}
use of net.minecraft.world.level.block.entity.ChestBlockEntity 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));
}
}
Aggregations