diff --git a/src/main/java/com/simibubi/create/compat/computercraft/AttachedComputerPacket.java b/src/main/java/com/simibubi/create/compat/computercraft/AttachedComputerPacket.java new file mode 100644 index 000000000..bd19f2cd9 --- /dev/null +++ b/src/main/java/com/simibubi/create/compat/computercraft/AttachedComputerPacket.java @@ -0,0 +1,35 @@ +package com.simibubi.create.compat.computercraft; + +import com.simibubi.create.foundation.networking.TileEntityDataPacket; +import com.simibubi.create.foundation.tileEntity.SyncedTileEntity; + +import net.minecraft.core.BlockPos; +import net.minecraft.network.FriendlyByteBuf; + +public class AttachedComputerPacket extends TileEntityDataPacket { + + private final boolean hasAttachedComputer; + + public AttachedComputerPacket(BlockPos tilePos, boolean hasAttachedComputer) { + super(tilePos); + this.hasAttachedComputer = hasAttachedComputer; + } + + public AttachedComputerPacket(FriendlyByteBuf buffer) { + super(buffer); + this.hasAttachedComputer = buffer.readBoolean(); + } + + @Override + protected void writeData(FriendlyByteBuf buffer) { + buffer.writeBoolean(hasAttachedComputer); + } + + @Override + protected void handlePacket(SyncedTileEntity tile) { + if (tile instanceof SyncedComputerControllable computerControllable) { + computerControllable.setHasAttachedComputer(hasAttachedComputer); + } + } + +} 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 552bb17e3..39b99efff 100644 --- a/src/main/java/com/simibubi/create/compat/computercraft/ComputerControllable.java +++ b/src/main/java/com/simibubi/create/compat/computercraft/ComputerControllable.java @@ -2,14 +2,7 @@ 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; @@ -24,7 +17,6 @@ public interface ComputerControllable { void setPeripheral(LazyOptional peripheral); LazyOptional getPeripheral(); - default LazyOptional getPeripheralCapability(@NotNull Capability cap) { if (cap == PERIPHERAL_CAPABILITY) { if (getPeripheral() == null || !getPeripheral().isPresent()) @@ -42,22 +34,4 @@ public interface ComputerControllable { } } - 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/compat/computercraft/SequencedGearshiftPeripheral.java b/src/main/java/com/simibubi/create/compat/computercraft/SequencedGearshiftPeripheral.java index 165b9afaa..eb810b870 100644 --- a/src/main/java/com/simibubi/create/compat/computercraft/SequencedGearshiftPeripheral.java +++ b/src/main/java/com/simibubi/create/compat/computercraft/SequencedGearshiftPeripheral.java @@ -11,7 +11,7 @@ import dan200.computercraft.api.lua.IArguments; import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.lua.LuaFunction; -public class SequencedGearshiftPeripheral extends PeripheralBase { +public class SequencedGearshiftPeripheral extends SyncedPeripheralBase { public SequencedGearshiftPeripheral(SequencedGearshiftTileEntity tile) { super(tile); diff --git a/src/main/java/com/simibubi/create/compat/computercraft/SyncedComputerControllable.java b/src/main/java/com/simibubi/create/compat/computercraft/SyncedComputerControllable.java new file mode 100644 index 000000000..5df108082 --- /dev/null +++ b/src/main/java/com/simibubi/create/compat/computercraft/SyncedComputerControllable.java @@ -0,0 +1,7 @@ +package com.simibubi.create.compat.computercraft; + +public interface SyncedComputerControllable extends ComputerControllable { + + void setHasAttachedComputer(boolean hasAttachedComputer); + +} diff --git a/src/main/java/com/simibubi/create/compat/computercraft/SyncedPeripheralBase.java b/src/main/java/com/simibubi/create/compat/computercraft/SyncedPeripheralBase.java new file mode 100644 index 000000000..c2d6b46a0 --- /dev/null +++ b/src/main/java/com/simibubi/create/compat/computercraft/SyncedPeripheralBase.java @@ -0,0 +1,40 @@ +package com.simibubi.create.compat.computercraft; + +import java.util.concurrent.atomic.AtomicInteger; + +import org.jetbrains.annotations.NotNull; + +import com.simibubi.create.foundation.networking.AllPackets; + +import dan200.computercraft.api.peripheral.IComputerAccess; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraftforge.network.PacketDistributor; + +public abstract class SyncedPeripheralBase extends PeripheralBase { + + private final AtomicInteger computers = new AtomicInteger(); + + public SyncedPeripheralBase(T tile) { + super(tile); + } + + @Override + public void attach(@NotNull IComputerAccess computer) { + computers.incrementAndGet(); + updateTile(); + } + + @Override + public void detach(@NotNull IComputerAccess computer) { + computers.decrementAndGet(); + updateTile(); + } + + private void updateTile() { + boolean hasAttachedComputer = computers.get() > 0; + + tile.setHasAttachedComputer(hasAttachedComputer); + AllPackets.channel.send(PacketDistributor.ALL.noArg(), new AttachedComputerPacket(tile.getBlockPos(), hasAttachedComputer)); + } + +} 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 42a38554d..3fd7cd98b 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,7 +35,7 @@ public class ConfigureSequencedGearshiftPacket extends TileEntityConfigurationPa @Override protected void applySettings(SequencedGearshiftTileEntity te) { - if (te.isComputerControlled(te)) + if (te.hasAttachedComputer) return; te.run(-1); 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 281ce24cd..83a62ffcb 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,7 +29,7 @@ public class SequencedGearshiftScreen extends AbstractSimiScreen { private ListTag compareTag; private Vector instructions; private BlockPos pos; - private final boolean isComputerControlled; + private final boolean hasAttachedComputer; private Vector> inputs; @@ -37,7 +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); + this.hasAttachedComputer = te.hasAttachedComputer; compareTag = Instruction.serializeAll(instructions); } @@ -51,7 +51,7 @@ public class SequencedGearshiftScreen extends AbstractSimiScreen { int y = guiTop; inputs = new Vector<>(5); - if (!isComputerControlled) { + if (!hasAttachedComputer) { for (int row = 0; row < inputs.capacity(); row++) inputs.add(new Vector<>(3)); @@ -138,7 +138,7 @@ public class SequencedGearshiftScreen extends AbstractSimiScreen { background.render(ms, x, y, this); - if (isComputerControlled) { + if (hasAttachedComputer) { for (int row = 0; row < instructions.capacity(); row++) { AllGuiTextures toDraw = AllGuiTextures.SEQUENCER_EMPTY; int yOffset = toDraw.height * row; @@ -183,7 +183,7 @@ public class SequencedGearshiftScreen extends AbstractSimiScreen { } public void sendPacket() { - if (isComputerControlled) + if (hasAttachedComputer) return; ListTag serialized = Instruction.serializeAll(instructions); 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 c2cf943e1..c6c9a44a0 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 @@ -5,8 +5,8 @@ import java.util.Vector; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import com.simibubi.create.compat.computercraft.ComputerControllable; import com.simibubi.create.compat.computercraft.SequencedGearshiftPeripheral; +import com.simibubi.create.compat.computercraft.SyncedComputerControllable; import com.simibubi.create.content.contraptions.relays.encased.SplitShaftTileEntity; import dan200.computercraft.api.peripheral.IPeripheral; @@ -19,7 +19,7 @@ import net.minecraft.world.level.block.state.BlockState; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.util.LazyOptional; -public class SequencedGearshiftTileEntity extends SplitShaftTileEntity implements ComputerControllable { +public class SequencedGearshiftTileEntity extends SplitShaftTileEntity implements SyncedComputerControllable { Vector instructions; int currentInstruction; @@ -29,6 +29,7 @@ public class SequencedGearshiftTileEntity extends SplitShaftTileEntity implement boolean poweredPreviously; LazyOptional peripheral; + boolean hasAttachedComputer; public SequencedGearshiftTileEntity(BlockEntityType type, BlockPos pos, BlockState state) { super(type, pos, state); @@ -38,6 +39,7 @@ public class SequencedGearshiftTileEntity extends SplitShaftTileEntity implement currentInstructionProgress = 0; timer = 0; poweredPreviously = false; + hasAttachedComputer = false; } @Override @@ -82,7 +84,7 @@ public class SequencedGearshiftTileEntity extends SplitShaftTileEntity implement } public void onRedstoneUpdate(boolean isPowered, boolean isRunning) { - if (isComputerControlled(this)) + if (hasAttachedComputer) return; if (!poweredPreviously && isPowered) risingFlank(); @@ -197,6 +199,11 @@ public class SequencedGearshiftTileEntity extends SplitShaftTileEntity implement return this.peripheral; } + @Override + public void setHasAttachedComputer(boolean hasAttachedComputer) { + this.hasAttachedComputer = hasAttachedComputer; + } + @Override public float getRotationSpeedModifier(Direction face) { if (isVirtual()) diff --git a/src/main/java/com/simibubi/create/foundation/networking/AllPackets.java b/src/main/java/com/simibubi/create/foundation/networking/AllPackets.java index 10c6a635f..634fd8ec0 100644 --- a/src/main/java/com/simibubi/create/foundation/networking/AllPackets.java +++ b/src/main/java/com/simibubi/create/foundation/networking/AllPackets.java @@ -8,6 +8,7 @@ import java.util.function.Function; import java.util.function.Supplier; import com.simibubi.create.Create; +import com.simibubi.create.compat.computercraft.AttachedComputerPacket; import com.simibubi.create.content.contraptions.components.structureMovement.ContraptionBlockChangedPacket; import com.simibubi.create.content.contraptions.components.structureMovement.ContraptionDisassemblyPacket; import com.simibubi.create.content.contraptions.components.structureMovement.ContraptionRelocationPacket; @@ -184,7 +185,7 @@ public enum AllPackets { S_TRAIN_PROMPT(TrainPromptPacket.class, TrainPromptPacket::new, PLAY_TO_CLIENT), CONTRAPTION_RELOCATION(ContraptionRelocationPacket.class, ContraptionRelocationPacket::new, PLAY_TO_CLIENT), TRACK_GRAPH_ROLL_CALL(TrackGraphRollCallPacket.class, TrackGraphRollCallPacket::new, PLAY_TO_CLIENT), - + ATTACHED_COMPUTER(AttachedComputerPacket.class, AttachedComputerPacket::new, PLAY_TO_CLIENT), ; public static final ResourceLocation CHANNEL_NAME = Create.asResource("main");