From 96dc4db6dc6cbf6519725109dbaa69851dcb0dbb Mon Sep 17 00:00:00 2001 From: caelwarner Date: Tue, 4 Oct 2022 21:11:38 -0700 Subject: [PATCH] Sequenced Gearshift screen "greys out" when being controlled by a computer - This is to stop players from trying to using both the builtin sequencing and a computer to control the Sequenced Gearshift at the same time, leading to undefined behaviour - The "greyed out" screen should have a message added explaining why it's greyed out. - Added ComputerControllable#isComputerControlled to check if a tile entity is connected to a modem --- .../java/com/simibubi/create/compat/Mods.java | 7 +++ .../computercraft/ComputerControllable.java | 28 +++++++++- .../ConfigureSequencedGearshiftPacket.java | 3 + .../sequencer/SequencedGearshiftScreen.java | 55 ++++++++++++------- .../SequencedGearshiftTileEntity.java | 2 + .../block/display/AllDisplayBehaviours.java | 1 - 6 files changed, 75 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/simibubi/create/compat/Mods.java b/src/main/java/com/simibubi/create/compat/Mods.java index 35bada843..3de4a181e 100644 --- a/src/main/java/com/simibubi/create/compat/Mods.java +++ b/src/main/java/com/simibubi/create/compat/Mods.java @@ -5,7 +5,10 @@ import java.util.function.Supplier; import com.simibubi.create.foundation.utility.Lang; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.level.block.Block; import net.minecraftforge.fml.ModList; +import net.minecraftforge.registries.ForgeRegistries; /** * For compatibility with and without another mod present, we have to define load conditions of the specific code @@ -51,4 +54,8 @@ public enum Mods { toExecute.get().run(); } } + + public Block getBlock(String id) { + return ForgeRegistries.BLOCKS.getValue(new ResourceLocation(asId(), id)); + } } diff --git a/src/main/java/com/simibubi/create/compat/computercraft/ComputerControllable.java b/src/main/java/com/simibubi/create/compat/computercraft/ComputerControllable.java index 70dfb38f8..552bb17e3 100644 --- a/src/main/java/com/simibubi/create/compat/computercraft/ComputerControllable.java +++ b/src/main/java/com/simibubi/create/compat/computercraft/ComputerControllable.java @@ -2,7 +2,14 @@ package com.simibubi.create.compat.computercraft; import org.jetbrains.annotations.NotNull; +import com.simibubi.create.compat.Mods; +import com.simibubi.create.foundation.utility.Iterate; + import dan200.computercraft.api.peripheral.IPeripheral; +import net.minecraft.core.Direction; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.block.state.properties.BooleanProperty; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.CapabilityManager; import net.minecraftforge.common.capabilities.CapabilityToken; @@ -30,8 +37,27 @@ public interface ComputerControllable { } default void removePeripheral() { - if (getPeripheral() != null) + if (getPeripheral() != null) { getPeripheral().invalidate(); + } + } + + default boolean isComputerControlled(BlockEntity tile) { + if (tile.getLevel() == null || !(tile instanceof ComputerControllable)) + return false; + + for (Direction direction : Iterate.directions) { + BlockState state = tile.getLevel().getBlockState(tile.getBlockPos().relative(direction)); + + // TODO: Add a check for "cable" wired modem. + // The difficulty comes since the "cable" wired modem uses an enum property instead of a boolean property. + // This could possibly be surpassed with reflection. It would be good to find a more solid solution. + if (state.getBlock().equals(Mods.COMPUTERCRAFT.getBlock("wired_modem_full"))) { + return state.getOptionalValue(BooleanProperty.create("peripheral")).orElse(false); + } + } + + return false; } } diff --git a/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/sequencer/ConfigureSequencedGearshiftPacket.java b/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/sequencer/ConfigureSequencedGearshiftPacket.java index 75fb4ca84..42a38554d 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/sequencer/ConfigureSequencedGearshiftPacket.java +++ b/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/sequencer/ConfigureSequencedGearshiftPacket.java @@ -35,6 +35,9 @@ public class ConfigureSequencedGearshiftPacket extends TileEntityConfigurationPa @Override protected void applySettings(SequencedGearshiftTileEntity te) { + if (te.isComputerControlled(te)) + return; + te.run(-1); te.instructions = Instruction.deserializeAll(instructions); te.sendData(); diff --git a/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/sequencer/SequencedGearshiftScreen.java b/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/sequencer/SequencedGearshiftScreen.java index 7f2c86ab4..281ce24cd 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/sequencer/SequencedGearshiftScreen.java +++ b/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/sequencer/SequencedGearshiftScreen.java @@ -29,6 +29,7 @@ public class SequencedGearshiftScreen extends AbstractSimiScreen { private ListTag compareTag; private Vector instructions; private BlockPos pos; + private final boolean isComputerControlled; private Vector> inputs; @@ -36,6 +37,7 @@ public class SequencedGearshiftScreen extends AbstractSimiScreen { super(Lang.translateDirect("gui.sequenced_gearshift.title")); this.instructions = te.instructions; this.pos = te.getBlockPos(); + this.isComputerControlled = te.isComputerControlled(te); compareTag = Instruction.serializeAll(instructions); } @@ -49,11 +51,13 @@ public class SequencedGearshiftScreen extends AbstractSimiScreen { int y = guiTop; inputs = new Vector<>(5); - for (int row = 0; row < inputs.capacity(); row++) - inputs.add(new Vector<>(3)); + if (!isComputerControlled) { + for (int row = 0; row < inputs.capacity(); row++) + inputs.add(new Vector<>(3)); - for (int row = 0; row < instructions.size(); row++) - initInputsOfRow(row, x, y); + for (int row = 0; row < instructions.size(); row++) + initInputsOfRow(row, x, y); + } confirmButton = new IconButton(x + background.width - 33, y + background.height - 24, AllIcons.I_CONFIRM); @@ -134,26 +138,36 @@ public class SequencedGearshiftScreen extends AbstractSimiScreen { background.render(ms, x, y, this); - for (int row = 0; row < instructions.capacity(); row++) { - AllGuiTextures toDraw = AllGuiTextures.SEQUENCER_EMPTY; - int yOffset = toDraw.height * row; - if (row >= instructions.size()) { + if (isComputerControlled) { + for (int row = 0; row < instructions.capacity(); row++) { + AllGuiTextures toDraw = AllGuiTextures.SEQUENCER_EMPTY; + int yOffset = toDraw.height * row; + toDraw.render(ms, x, y + 14 + yOffset, this); - continue; } - Instruction instruction = instructions.get(row); - SequencerInstructions def = instruction.instruction; - def.background.render(ms, x, y + 14 + yOffset, this); + } else { + for (int row = 0; row < instructions.capacity(); row++) { + AllGuiTextures toDraw = AllGuiTextures.SEQUENCER_EMPTY; + int yOffset = toDraw.height * row; + if (row >= instructions.size()) { + toDraw.render(ms, x, y + 14 + yOffset, this); + continue; + } - label(ms, 36, yOffset - 3, Lang.translateDirect(def.translationKey)); - if (def.hasValueParameter) { - String text = def.formatValue(instruction.value); - int stringWidth = font.width(text); - label(ms, 90 + (12 - stringWidth / 2), yOffset - 3, Components.literal(text)); + Instruction instruction = instructions.get(row); + SequencerInstructions def = instruction.instruction; + def.background.render(ms, x, y + 14 + yOffset, this); + + label(ms, 36, yOffset - 3, Lang.translateDirect(def.translationKey)); + if (def.hasValueParameter) { + String text = def.formatValue(instruction.value); + int stringWidth = font.width(text); + label(ms, 90 + (12 - stringWidth / 2), yOffset - 3, Components.literal(text)); + } + if (def.hasSpeedParameter) + label(ms, 127, yOffset - 3, instruction.speedModifier.label); } - if (def.hasSpeedParameter) - label(ms, 127, yOffset - 3, instruction.speedModifier.label); } drawCenteredString(ms, font, title, x + (background.width - 8) / 2, y + 3, 0xFFFFFF); @@ -169,6 +183,9 @@ public class SequencedGearshiftScreen extends AbstractSimiScreen { } public void sendPacket() { + if (isComputerControlled) + return; + ListTag serialized = Instruction.serializeAll(instructions); if (serialized.equals(compareTag)) return; diff --git a/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/sequencer/SequencedGearshiftTileEntity.java b/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/sequencer/SequencedGearshiftTileEntity.java index 83787ab58..c2cf943e1 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/sequencer/SequencedGearshiftTileEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/sequencer/SequencedGearshiftTileEntity.java @@ -82,6 +82,8 @@ public class SequencedGearshiftTileEntity extends SplitShaftTileEntity implement } public void onRedstoneUpdate(boolean isPowered, boolean isRunning) { + if (isComputerControlled(this)) + return; if (!poweredPreviously && isPowered) risingFlank(); poweredPreviously = isPowered; diff --git a/src/main/java/com/simibubi/create/content/logistics/block/display/AllDisplayBehaviours.java b/src/main/java/com/simibubi/create/content/logistics/block/display/AllDisplayBehaviours.java index b97bdd789..2798ca71c 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/display/AllDisplayBehaviours.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/display/AllDisplayBehaviours.java @@ -244,7 +244,6 @@ public class AllDisplayBehaviours { DisplayBehaviour computerDisplaySource = register(Create.asResource("computer_display_source"), new ComputerDisplaySource()); assignTile(computerDisplaySource, new ResourceLocation(Mods.COMPUTERCRAFT.asId(), "wired_modem_full")); - assignTile(computerDisplaySource, new ResourceLocation("computercraft", "wired_modem")); }); } }