From ab7e472a0fd4231bd4d326962e6ef975c242b375 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 15 Apr 2020 18:35:00 +0200 Subject: [PATCH] remove CONNECTED_INVENTORY --- .../blocks/pipe/ConnectionType.java | 3 +- .../prettypipes/blocks/pipe/PipeBlock.java | 35 +++++++++---------- .../blocks/pipe/PipeTileEntity.java | 9 +++++ .../ellpeck/prettypipes/items/WrenchItem.java | 4 +-- .../assets/prettypipes/blockstates/pipe.json | 12 +++---- 5 files changed, 34 insertions(+), 29 deletions(-) diff --git a/src/main/java/de/ellpeck/prettypipes/blocks/pipe/ConnectionType.java b/src/main/java/de/ellpeck/prettypipes/blocks/pipe/ConnectionType.java index fe3ca6b..2d1e9b6 100644 --- a/src/main/java/de/ellpeck/prettypipes/blocks/pipe/ConnectionType.java +++ b/src/main/java/de/ellpeck/prettypipes/blocks/pipe/ConnectionType.java @@ -5,8 +5,7 @@ import net.minecraft.util.IStringSerializable; import java.util.Locale; public enum ConnectionType implements IStringSerializable { - CONNECTED_PIPE(true), - CONNECTED_INVENTORY(true), + CONNECTED(true), DISCONNECTED(false), BLOCKED(false); diff --git a/src/main/java/de/ellpeck/prettypipes/blocks/pipe/PipeBlock.java b/src/main/java/de/ellpeck/prettypipes/blocks/pipe/PipeBlock.java index 64514b7..9381b55 100644 --- a/src/main/java/de/ellpeck/prettypipes/blocks/pipe/PipeBlock.java +++ b/src/main/java/de/ellpeck/prettypipes/blocks/pipe/PipeBlock.java @@ -67,11 +67,11 @@ public class PipeBlock extends ContainerBlock { public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult p_225533_6_) { if (!player.getHeldItem(handIn).isEmpty()) return ActionResultType.PASS; - if (DIRECTIONS.values().stream().noneMatch(d -> state.get(d) == ConnectionType.CONNECTED_INVENTORY)) - return ActionResultType.PASS; PipeTileEntity tile = Utility.getTileEntity(PipeTileEntity.class, worldIn, pos); if (tile == null) return ActionResultType.PASS; + if (!tile.isConnectedInventory()) + return ActionResultType.PASS; if (!worldIn.isRemote) NetworkHooks.openGui((ServerPlayerEntity) player, tile, pos); return ActionResultType.SUCCESS; @@ -133,7 +133,7 @@ public class PipeBlock extends ContainerBlock { return state; } - public static ConnectionType getConnectionType(World world, BlockPos pos, Direction direction) { + private static ConnectionType getConnectionType(World world, BlockPos pos, Direction direction) { BlockPos offset = pos.offset(direction); if (!world.isBlockLoaded(offset)) return ConnectionType.DISCONNECTED; @@ -141,32 +141,31 @@ public class PipeBlock extends ContainerBlock { if (state.getBlock() instanceof PipeBlock) { if (state.get(DIRECTIONS.get(direction.getOpposite())) == ConnectionType.BLOCKED) return ConnectionType.BLOCKED; - return ConnectionType.CONNECTED_PIPE; - } else { - TileEntity tile = world.getTileEntity(offset); - if (tile == null) - return ConnectionType.DISCONNECTED; - IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, direction.getOpposite()).orElse(null); - return handler == null ? ConnectionType.DISCONNECTED : ConnectionType.CONNECTED_INVENTORY; + return ConnectionType.CONNECTED; } + TileEntity tile = world.getTileEntity(offset); + if (tile != null) { + IItemHandler handler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, direction.getOpposite()).orElse(null); + if (handler != null) + return ConnectionType.CONNECTED; + } + return ConnectionType.DISCONNECTED; } public static void onStateChanged(World world, BlockPos pos, BlockState newState) { - if (DIRECTIONS.values().stream().noneMatch(d -> newState.get(d) == ConnectionType.CONNECTED_INVENTORY)) { - PipeTileEntity tile = Utility.getTileEntity(PipeTileEntity.class, world, pos); - if (tile != null) - Utility.dropInventory(tile, tile.upgrades); - } + PipeTileEntity tile = Utility.getTileEntity(PipeTileEntity.class, world, pos); + if (tile != null && !tile.isConnectedInventory()) + Utility.dropInventory(tile, tile.upgrades); PipeNetwork network = PipeNetwork.get(world); int connections = 0; boolean inventory = false; - for (EnumProperty prop : DIRECTIONS.values()) { - ConnectionType value = newState.get(prop); + for (Direction dir : Direction.values()) { + ConnectionType value = newState.get(DIRECTIONS.get(dir)); if (!value.isConnected()) continue; connections++; - if (value == ConnectionType.CONNECTED_INVENTORY) { + if (tile.isConnectedInventory(dir)) { inventory = true; break; } diff --git a/src/main/java/de/ellpeck/prettypipes/blocks/pipe/PipeTileEntity.java b/src/main/java/de/ellpeck/prettypipes/blocks/pipe/PipeTileEntity.java index 47cdba0..3a26ee2 100644 --- a/src/main/java/de/ellpeck/prettypipes/blocks/pipe/PipeTileEntity.java +++ b/src/main/java/de/ellpeck/prettypipes/blocks/pipe/PipeTileEntity.java @@ -29,6 +29,7 @@ import net.minecraftforge.items.ItemStackHandler; import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; public class PipeTileEntity extends TileEntity implements INamedContainerProvider, ITickableTileEntity { @@ -144,4 +145,12 @@ public class PipeTileEntity extends TileEntity implements INamedContainerProvide return null; return tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, dir.getOpposite()).orElse(null); } + + public boolean isConnectedInventory(Direction dir) { + return this.getItemHandler(dir) != null; + } + + public boolean isConnectedInventory() { + return Arrays.stream(Direction.values()).anyMatch(this::isConnectedInventory); + } } diff --git a/src/main/java/de/ellpeck/prettypipes/items/WrenchItem.java b/src/main/java/de/ellpeck/prettypipes/items/WrenchItem.java index 2857d3b..6bc3141 100644 --- a/src/main/java/de/ellpeck/prettypipes/items/WrenchItem.java +++ b/src/main/java/de/ellpeck/prettypipes/items/WrenchItem.java @@ -53,15 +53,13 @@ public class WrenchItem extends Item { continue; if (!world.isRemote) { - ConnectionType newType = curr == ConnectionType.BLOCKED ? ConnectionType.CONNECTED_PIPE : ConnectionType.BLOCKED; + ConnectionType newType = curr == ConnectionType.BLOCKED ? ConnectionType.CONNECTED : ConnectionType.BLOCKED; BlockPos otherPos = pos.offset(entry.getKey()); BlockState otherState = world.getBlockState(otherPos); if (otherState.getBlock() instanceof PipeBlock) { otherState = otherState.with(PipeBlock.DIRECTIONS.get(entry.getKey().getOpposite()), newType); world.setBlockState(otherPos, otherState); PipeBlock.onStateChanged(world, otherPos, otherState); - } else if (newType == ConnectionType.CONNECTED_PIPE) { - newType = ConnectionType.CONNECTED_INVENTORY; } BlockState newState = state.with(prop, newType); world.setBlockState(pos, newState); diff --git a/src/main/resources/assets/prettypipes/blockstates/pipe.json b/src/main/resources/assets/prettypipes/blockstates/pipe.json index 76d558c..e0433a5 100644 --- a/src/main/resources/assets/prettypipes/blockstates/pipe.json +++ b/src/main/resources/assets/prettypipes/blockstates/pipe.json @@ -7,7 +7,7 @@ }, { "when": { - "north": "connected_pipe|connected_inventory" + "north": "connected" }, "apply": { "model": "prettypipes:block/pipe_end" @@ -15,7 +15,7 @@ }, { "when": { - "south": "connected_pipe|connected_inventory" + "south": "connected" }, "apply": { "model": "prettypipes:block/pipe_end", @@ -24,7 +24,7 @@ }, { "when": { - "east": "connected_pipe|connected_inventory" + "east": "connected" }, "apply": { "model": "prettypipes:block/pipe_end", @@ -33,7 +33,7 @@ }, { "when": { - "west": "connected_pipe|connected_inventory" + "west": "connected" }, "apply": { "model": "prettypipes:block/pipe_end", @@ -42,7 +42,7 @@ }, { "when": { - "down": "connected_pipe|connected_inventory" + "down": "connected" }, "apply": { "model": "prettypipes:block/pipe_end", @@ -51,7 +51,7 @@ }, { "when": { - "up": "connected_pipe|connected_inventory" + "up": "connected" }, "apply": { "model": "prettypipes:block/pipe_end",