allow pipes to connect to other blocks

This commit is contained in:
Ellpeck 2020-04-19 17:38:13 +02:00
parent 9d7d0cd048
commit dd894f996e
2 changed files with 30 additions and 10 deletions

View file

@ -0,0 +1,12 @@
package de.ellpeck.prettypipes.pipe;
import net.minecraft.block.BlockState;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
public interface IPipeConnectable {
ConnectionType getConnectionType(World world, BlockPos pos, BlockState state, BlockPos pipePos, Direction direction);
}

View file

@ -37,7 +37,7 @@ import javax.annotation.Nullable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class PipeBlock extends ContainerBlock { public class PipeBlock extends ContainerBlock implements IPipeConnectable {
public static final Map<Direction, EnumProperty<ConnectionType>> DIRECTIONS = new HashMap<>(); public static final Map<Direction, EnumProperty<ConnectionType>> DIRECTIONS = new HashMap<>();
private static final Map<BlockState, VoxelShape> SHAPE_CACHE = new HashMap<>(); private static final Map<BlockState, VoxelShape> SHAPE_CACHE = new HashMap<>();
@ -158,11 +158,9 @@ public class PipeBlock extends ContainerBlock {
if (!world.isBlockLoaded(offset)) if (!world.isBlockLoaded(offset))
return ConnectionType.DISCONNECTED; return ConnectionType.DISCONNECTED;
BlockState offState = world.getBlockState(offset); BlockState offState = world.getBlockState(offset);
if (offState.getBlock() instanceof PipeBlock) { Block block = offState.getBlock();
if (offState.get(DIRECTIONS.get(direction.getOpposite())) == ConnectionType.BLOCKED) if (block instanceof IPipeConnectable)
return ConnectionType.BLOCKED; return ((IPipeConnectable) block).getConnectionType(world, offset, offState, pos, direction.getOpposite());
return ConnectionType.CONNECTED;
}
TileEntity tile = world.getTileEntity(offset); TileEntity tile = world.getTileEntity(offset);
if (tile != null) { if (tile != null) {
IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, direction.getOpposite()).orElse(null); IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, direction.getOpposite()).orElse(null);
@ -191,19 +189,22 @@ public class PipeBlock extends ContainerBlock {
PipeNetwork network = PipeNetwork.get(world); PipeNetwork network = PipeNetwork.get(world);
int connections = 0; int connections = 0;
boolean inventory = false; boolean force = false;
for (Direction dir : Direction.values()) { for (Direction dir : Direction.values()) {
ConnectionType value = newState.get(DIRECTIONS.get(dir)); ConnectionType value = newState.get(DIRECTIONS.get(dir));
if (!value.isConnected()) if (!value.isConnected())
continue; continue;
connections++; connections++;
if (tile.isConnectedInventory(dir)) { BlockState otherState = world.getBlockState(pos.offset(dir));
inventory = true; // force a node if we're connecting to a different block (inventory etc.)
if (otherState.getBlock() != newState.getBlock()) {
force = true;
break; break;
} }
} }
if (inventory || connections > 2) { if (force || connections > 2) {
network.addNode(pos, newState); network.addNode(pos, newState);
System.out.println("Node at " + pos);
} else { } else {
network.removeNode(pos); network.removeNode(pos);
} }
@ -236,4 +237,11 @@ public class PipeBlock extends ContainerBlock {
public BlockRenderType getRenderType(BlockState state) { public BlockRenderType getRenderType(BlockState state) {
return BlockRenderType.MODEL; return BlockRenderType.MODEL;
} }
@Override
public ConnectionType getConnectionType(World world, BlockPos pos, BlockState state, BlockPos pipePos, Direction direction) {
if (state.get(DIRECTIONS.get(direction)) == ConnectionType.BLOCKED)
return ConnectionType.BLOCKED;
return ConnectionType.CONNECTED;
}
} }