display an error when connecting a terminal to multiple pipes

Closes #95
This commit is contained in:
Ell 2021-09-26 17:05:02 +02:00
parent f8b618a1d2
commit b2c69bc67b
3 changed files with 26 additions and 2 deletions

View file

@ -21,6 +21,8 @@ import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import net.minecraftforge.fml.network.NetworkHooks;
@ -29,8 +31,10 @@ import net.minecraftforge.items.ItemHandlerHelper;
import javax.annotation.Nullable;
import java.util.List;
import java.util.UUID;
public class ItemTerminalBlock extends ContainerBlock {
public ItemTerminalBlock() {
super(Properties.create(Material.ROCK).hardnessAndResistance(3).sound(SoundType.STONE));
}
@ -38,8 +42,14 @@ public class ItemTerminalBlock extends ContainerBlock {
@Override
public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult result) {
ItemTerminalTileEntity tile = Utility.getTileEntity(ItemTerminalTileEntity.class, worldIn, pos);
if (tile == null || tile.getConnectedPipe() == null)
if (tile == null)
return ActionResultType.PASS;
String reason = tile.getInvalidTerminalReason();
if (reason != null) {
if (!worldIn.isRemote)
player.sendMessage(new TranslationTextComponent(reason).mergeStyle(TextFormatting.RED), UUID.randomUUID());
return ActionResultType.SUCCESS;
}
if (!worldIn.isRemote) {
NetworkHooks.openGui((ServerPlayerEntity) player, tile, pos);
tile.updateItems(player);

View file

@ -112,6 +112,18 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
this.lazyThis.invalidate();
}
public String getInvalidTerminalReason() {
PipeNetwork network = PipeNetwork.get(this.world);
long pipes = Arrays.stream(Direction.values())
.map(d -> network.getPipe(this.pos.offset(d)))
.filter(Objects::nonNull).count();
if (pipes <= 0)
return "info." + PrettyPipes.ID + ".no_pipe_connected";
if (pipes > 1)
return "info." + PrettyPipes.ID + ".too_many_pipes_connected";
return null;
}
public PipeTileEntity getConnectedPipe() {
PipeNetwork network = PipeNetwork.get(this.world);
for (Direction dir : Direction.values()) {

View file

@ -85,5 +85,7 @@
"info.prettypipes.energy": "%s / %s FE",
"info.prettypipes.crafting": "Awaiting",
"info.prettypipes.cancel_all": "Cancel",
"info.prettypipes.cancel_all.desc": "Stops waiting for current crafting outputs\nDoesn't remove inputs from blocks"
"info.prettypipes.cancel_all.desc": "Stops waiting for current crafting outputs\nDoesn't remove inputs from blocks",
"info.prettypipes.no_pipe_connected": "The terminal needs to be connected to a pipe network",
"info.prettypipes.too_many_pipes_connected": "The terminal can only be connected to a single pipe at a time"
}