remove CONNECTED_INVENTORY

This commit is contained in:
Ellpeck 2020-04-15 18:35:00 +02:00
parent 4c764023f4
commit ab7e472a0f
5 changed files with 34 additions and 29 deletions

View file

@ -5,8 +5,7 @@ import net.minecraft.util.IStringSerializable;
import java.util.Locale; import java.util.Locale;
public enum ConnectionType implements IStringSerializable { public enum ConnectionType implements IStringSerializable {
CONNECTED_PIPE(true), CONNECTED(true),
CONNECTED_INVENTORY(true),
DISCONNECTED(false), DISCONNECTED(false),
BLOCKED(false); BLOCKED(false);

View file

@ -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_) { public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult p_225533_6_) {
if (!player.getHeldItem(handIn).isEmpty()) if (!player.getHeldItem(handIn).isEmpty())
return ActionResultType.PASS; 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); PipeTileEntity tile = Utility.getTileEntity(PipeTileEntity.class, worldIn, pos);
if (tile == null) if (tile == null)
return ActionResultType.PASS; return ActionResultType.PASS;
if (!tile.isConnectedInventory())
return ActionResultType.PASS;
if (!worldIn.isRemote) if (!worldIn.isRemote)
NetworkHooks.openGui((ServerPlayerEntity) player, tile, pos); NetworkHooks.openGui((ServerPlayerEntity) player, tile, pos);
return ActionResultType.SUCCESS; return ActionResultType.SUCCESS;
@ -133,7 +133,7 @@ public class PipeBlock extends ContainerBlock {
return state; 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); BlockPos offset = pos.offset(direction);
if (!world.isBlockLoaded(offset)) if (!world.isBlockLoaded(offset))
return ConnectionType.DISCONNECTED; return ConnectionType.DISCONNECTED;
@ -141,32 +141,31 @@ public class PipeBlock extends ContainerBlock {
if (state.getBlock() instanceof PipeBlock) { if (state.getBlock() instanceof PipeBlock) {
if (state.get(DIRECTIONS.get(direction.getOpposite())) == ConnectionType.BLOCKED) if (state.get(DIRECTIONS.get(direction.getOpposite())) == ConnectionType.BLOCKED)
return ConnectionType.BLOCKED; return ConnectionType.BLOCKED;
return ConnectionType.CONNECTED_PIPE; return ConnectionType.CONNECTED;
} 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;
} }
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) { 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); PipeTileEntity tile = Utility.getTileEntity(PipeTileEntity.class, world, pos);
if (tile != null) if (tile != null && !tile.isConnectedInventory())
Utility.dropInventory(tile, tile.upgrades); Utility.dropInventory(tile, tile.upgrades);
}
PipeNetwork network = PipeNetwork.get(world); PipeNetwork network = PipeNetwork.get(world);
int connections = 0; int connections = 0;
boolean inventory = false; boolean inventory = false;
for (EnumProperty<ConnectionType> prop : DIRECTIONS.values()) { for (Direction dir : Direction.values()) {
ConnectionType value = newState.get(prop); ConnectionType value = newState.get(DIRECTIONS.get(dir));
if (!value.isConnected()) if (!value.isConnected())
continue; continue;
connections++; connections++;
if (value == ConnectionType.CONNECTED_INVENTORY) { if (tile.isConnectedInventory(dir)) {
inventory = true; inventory = true;
break; break;
} }

View file

@ -29,6 +29,7 @@ import net.minecraftforge.items.ItemStackHandler;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
public class PipeTileEntity extends TileEntity implements INamedContainerProvider, ITickableTileEntity { public class PipeTileEntity extends TileEntity implements INamedContainerProvider, ITickableTileEntity {
@ -144,4 +145,12 @@ public class PipeTileEntity extends TileEntity implements INamedContainerProvide
return null; return null;
return tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, dir.getOpposite()).orElse(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);
}
} }

View file

@ -53,15 +53,13 @@ public class WrenchItem extends Item {
continue; continue;
if (!world.isRemote) { 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()); BlockPos otherPos = pos.offset(entry.getKey());
BlockState otherState = world.getBlockState(otherPos); BlockState otherState = world.getBlockState(otherPos);
if (otherState.getBlock() instanceof PipeBlock) { if (otherState.getBlock() instanceof PipeBlock) {
otherState = otherState.with(PipeBlock.DIRECTIONS.get(entry.getKey().getOpposite()), newType); otherState = otherState.with(PipeBlock.DIRECTIONS.get(entry.getKey().getOpposite()), newType);
world.setBlockState(otherPos, otherState); world.setBlockState(otherPos, otherState);
PipeBlock.onStateChanged(world, otherPos, otherState); PipeBlock.onStateChanged(world, otherPos, otherState);
} else if (newType == ConnectionType.CONNECTED_PIPE) {
newType = ConnectionType.CONNECTED_INVENTORY;
} }
BlockState newState = state.with(prop, newType); BlockState newState = state.with(prop, newType);
world.setBlockState(pos, newState); world.setBlockState(pos, newState);

View file

@ -7,7 +7,7 @@
}, },
{ {
"when": { "when": {
"north": "connected_pipe|connected_inventory" "north": "connected"
}, },
"apply": { "apply": {
"model": "prettypipes:block/pipe_end" "model": "prettypipes:block/pipe_end"
@ -15,7 +15,7 @@
}, },
{ {
"when": { "when": {
"south": "connected_pipe|connected_inventory" "south": "connected"
}, },
"apply": { "apply": {
"model": "prettypipes:block/pipe_end", "model": "prettypipes:block/pipe_end",
@ -24,7 +24,7 @@
}, },
{ {
"when": { "when": {
"east": "connected_pipe|connected_inventory" "east": "connected"
}, },
"apply": { "apply": {
"model": "prettypipes:block/pipe_end", "model": "prettypipes:block/pipe_end",
@ -33,7 +33,7 @@
}, },
{ {
"when": { "when": {
"west": "connected_pipe|connected_inventory" "west": "connected"
}, },
"apply": { "apply": {
"model": "prettypipes:block/pipe_end", "model": "prettypipes:block/pipe_end",
@ -42,7 +42,7 @@
}, },
{ {
"when": { "when": {
"down": "connected_pipe|connected_inventory" "down": "connected"
}, },
"apply": { "apply": {
"model": "prettypipes:block/pipe_end", "model": "prettypipes:block/pipe_end",
@ -51,7 +51,7 @@
}, },
{ {
"when": { "when": {
"up": "connected_pipe|connected_inventory" "up": "connected"
}, },
"apply": { "apply": {
"model": "prettypipes:block/pipe_end", "model": "prettypipes:block/pipe_end",