use general capabilities when querying storage blocks, rather than tile-specific ones

This commit is contained in:
Ell 2024-11-30 18:59:07 +01:00
parent 8894d1d8c4
commit 4d0e484c08
3 changed files with 11 additions and 38 deletions

View file

@ -147,17 +147,6 @@ public final class Utility {
e.connection.send(packet);
}
public static IItemHandler getBlockItemHandler(Level world, BlockPos pos, Direction direction) {
var state = world.getBlockState(pos);
var block = state.getBlock();
if (!(block instanceof WorldlyContainerHolder holder))
return null;
var inventory = holder.getContainer(state, world, pos);
if (inventory == null)
return null;
return new SidedInvWrapper(inventory, direction);
}
public static BlockPos readBlockPos(Tag tag) {
if (tag instanceof IntArrayTag i) {
int[] arr = i.getAsIntArray();

View file

@ -199,26 +199,20 @@ public class PipeBlock extends BaseEntityBlock implements SimpleWaterloggedBlock
return state;
}
protected ConnectionType getConnectionType(Level world, BlockPos pos, Direction direction, BlockState state) {
protected ConnectionType getConnectionType(Level world, BlockPos pos, Direction direction, BlockState pipeState) {
var offset = pos.relative(direction);
if (!world.isLoaded(offset))
return ConnectionType.DISCONNECTED;
var opposite = direction.getOpposite();
var tile = world.getBlockEntity(offset);
if (tile != null) {
var connectable = world.getCapability(Registry.pipeConnectableCapability, offset, tile.getBlockState(), tile, opposite);
if (connectable != null)
return connectable.getConnectionType(pos, direction);
var handler = world.getCapability(Capabilities.ItemHandler.BLOCK, offset, tile.getBlockState(), tile, opposite);
if (handler != null)
return ConnectionType.CONNECTED;
}
var blockHandler = Utility.getBlockItemHandler(world, offset, opposite);
if (blockHandler != null)
var connectable = world.getCapability(Registry.pipeConnectableCapability, offset, null, null, opposite);
if (connectable != null)
return connectable.getConnectionType(pos, direction);
var handler = world.getCapability(Capabilities.ItemHandler.BLOCK, offset, null, null, opposite);
if (handler != null)
return ConnectionType.CONNECTED;
var offState = world.getBlockState(offset);
if (PipeBlock.hasLegsTo(world, offState, offset, direction)) {
if (PipeBlock.DIRECTIONS.values().stream().noneMatch(d -> state.getValue(d) == ConnectionType.LEGS))
if (PipeBlock.DIRECTIONS.values().stream().noneMatch(d -> pipeState.getValue(d) == ConnectionType.LEGS))
return ConnectionType.LEGS;
}
return ConnectionType.DISCONNECTED;

View file

@ -307,27 +307,17 @@ public class PipeBlockEntity extends BlockEntity implements MenuProvider, IPipeC
}
public IItemHandler getItemHandler(Direction dir) {
var handler = this.getNeighborCap(dir, Capabilities.ItemHandler.BLOCK);
if (handler != null)
return handler;
return Utility.getBlockItemHandler(this.level, this.worldPosition.relative(dir), dir.getOpposite());
return this.getNeighborCap(dir, Capabilities.ItemHandler.BLOCK);
}
public <T, C> T getNeighborCap(Direction dir, BlockCapability<T, Direction> cap) {
public <T> T getNeighborCap(Direction dir, BlockCapability<T, Direction> cap) {
if (!this.isConnected(dir))
return null;
var pos = this.worldPosition.relative(dir);
var tile = this.level.getBlockEntity(pos);
if (tile != null)
return this.level.getCapability(cap, tile.getBlockPos(), tile.getBlockState(), tile, dir.getOpposite());
return null;
return this.level.getCapability(cap, this.worldPosition.relative(dir), null, null, dir.getOpposite());
}
public IPipeConnectable getPipeConnectable(Direction dir) {
var tile = this.level.getBlockEntity(this.worldPosition.relative(dir));
if (tile != null)
return this.level.getCapability(Registry.pipeConnectableCapability, tile.getBlockPos(), tile.getBlockState(), tile, dir.getOpposite());
return null;
return this.level.getCapability(Registry.pipeConnectableCapability, this.worldPosition.relative(dir), null, null, dir.getOpposite());
}
public boolean canHaveModules() {