From dd894f996e6607b7bc6539ade4b7bc4e0970160f Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sun, 19 Apr 2020 17:38:13 +0200 Subject: [PATCH] allow pipes to connect to other blocks --- .../prettypipes/pipe/IPipeConnectable.java | 12 ++++++++ .../ellpeck/prettypipes/pipe/PipeBlock.java | 28 ++++++++++++------- 2 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 src/main/java/de/ellpeck/prettypipes/pipe/IPipeConnectable.java diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/IPipeConnectable.java b/src/main/java/de/ellpeck/prettypipes/pipe/IPipeConnectable.java new file mode 100644 index 0000000..6fdf34d --- /dev/null +++ b/src/main/java/de/ellpeck/prettypipes/pipe/IPipeConnectable.java @@ -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); + +} diff --git a/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java b/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java index aed122c..9d4591a 100644 --- a/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java +++ b/src/main/java/de/ellpeck/prettypipes/pipe/PipeBlock.java @@ -37,7 +37,7 @@ import javax.annotation.Nullable; import java.util.HashMap; import java.util.Map; -public class PipeBlock extends ContainerBlock { +public class PipeBlock extends ContainerBlock implements IPipeConnectable { public static final Map> DIRECTIONS = new HashMap<>(); private static final Map SHAPE_CACHE = new HashMap<>(); @@ -158,11 +158,9 @@ public class PipeBlock extends ContainerBlock { if (!world.isBlockLoaded(offset)) return ConnectionType.DISCONNECTED; BlockState offState = world.getBlockState(offset); - if (offState.getBlock() instanceof PipeBlock) { - if (offState.get(DIRECTIONS.get(direction.getOpposite())) == ConnectionType.BLOCKED) - return ConnectionType.BLOCKED; - return ConnectionType.CONNECTED; - } + Block block = offState.getBlock(); + if (block instanceof IPipeConnectable) + return ((IPipeConnectable) block).getConnectionType(world, offset, offState, pos, direction.getOpposite()); TileEntity tile = world.getTileEntity(offset); if (tile != 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); int connections = 0; - boolean inventory = false; + boolean force = false; for (Direction dir : Direction.values()) { ConnectionType value = newState.get(DIRECTIONS.get(dir)); if (!value.isConnected()) continue; connections++; - if (tile.isConnectedInventory(dir)) { - inventory = true; + BlockState otherState = world.getBlockState(pos.offset(dir)); + // force a node if we're connecting to a different block (inventory etc.) + if (otherState.getBlock() != newState.getBlock()) { + force = true; break; } } - if (inventory || connections > 2) { + if (force || connections > 2) { network.addNode(pos, newState); + System.out.println("Node at " + pos); } else { network.removeNode(pos); } @@ -236,4 +237,11 @@ public class PipeBlock extends ContainerBlock { public BlockRenderType getRenderType(BlockState state) { 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; + } }