From 56a1210fff386fd538733a0a0998dd1bc0bd060f Mon Sep 17 00:00:00 2001 From: caelwarner Date: Wed, 19 Oct 2022 22:39:38 -0700 Subject: [PATCH] Created ComputerBehaviour behaviour - ComputerBehaviour replaces ComputerControllable and SyncedComputerControllable --- .../computercraft/AttachedComputerPacket.java | 5 +- .../computercraft/ComputerBehaviour.java | 62 +++++++++++++++++++ .../computercraft/ComputerControllable.java | 37 ----------- .../SyncedComputerControllable.java | 7 --- .../peripherals/PeripheralBase.java | 36 +++++++++-- .../SequencedGearshiftPeripheral.java | 3 +- .../peripherals/SyncedPeripheralBase.java | 43 ------------- .../advanced/SpeedControllerTileEntity.java | 32 +++------- .../ConfigureSequencedGearshiftPacket.java | 2 +- .../sequencer/SequencedGearshiftScreen.java | 2 +- .../SequencedGearshiftTileEntity.java | 46 +++++--------- .../relays/gauge/GaugeTileEntity.java | 36 +---------- .../relays/gauge/SpeedGaugeTileEntity.java | 31 +++++++++- .../relays/gauge/StressGaugeTileEntity.java | 24 ++++++- .../block/display/DisplayLinkTileEntity.java | 41 +++++------- .../edgePoint/station/StationTileEntity.java | 32 +++------- 16 files changed, 197 insertions(+), 242 deletions(-) create mode 100644 src/main/java/com/simibubi/create/compat/computercraft/ComputerBehaviour.java delete mode 100644 src/main/java/com/simibubi/create/compat/computercraft/ComputerControllable.java delete mode 100644 src/main/java/com/simibubi/create/compat/computercraft/SyncedComputerControllable.java delete mode 100644 src/main/java/com/simibubi/create/compat/computercraft/peripherals/SyncedPeripheralBase.java diff --git a/src/main/java/com/simibubi/create/compat/computercraft/AttachedComputerPacket.java b/src/main/java/com/simibubi/create/compat/computercraft/AttachedComputerPacket.java index bd19f2cd9..696d175f7 100644 --- a/src/main/java/com/simibubi/create/compat/computercraft/AttachedComputerPacket.java +++ b/src/main/java/com/simibubi/create/compat/computercraft/AttachedComputerPacket.java @@ -1,6 +1,7 @@ package com.simibubi.create.compat.computercraft; import com.simibubi.create.foundation.networking.TileEntityDataPacket; +import com.simibubi.create.foundation.tileEntity.SmartTileEntity; import com.simibubi.create.foundation.tileEntity.SyncedTileEntity; import net.minecraft.core.BlockPos; @@ -27,8 +28,8 @@ public class AttachedComputerPacket extends TileEntityDataPacket TYPE = new BehaviourType<>(); + protected static final Capability PERIPHERAL_CAPABILITY = CapabilityManager.get(new CapabilityToken<>() {}); + + LazyOptional peripheral; + NonNullSupplier peripheralSupplier; + + boolean hasAttachedComputer; + + public ComputerBehaviour(SmartTileEntity te, NonNullSupplier peripheralSupplier) { + super(te); + this.peripheralSupplier = peripheralSupplier; + this.hasAttachedComputer = false; + } + + public static boolean isPeripheralCap(@NotNull Capability cap) { + return cap == PERIPHERAL_CAPABILITY; + } + + public LazyOptional getPeripheralCapability() { + if (peripheral == null || !peripheral.isPresent()) + peripheral = LazyOptional.of(peripheralSupplier); + + return peripheral.cast(); + } + + public void removePeripheral() { + if (peripheral != null) { + peripheral.invalidate(); + } + } + + public void setHasAttachedComputer(boolean hasAttachedComputer) { + this.hasAttachedComputer = hasAttachedComputer; + } + + public boolean hasAttachedComputer() { + return hasAttachedComputer; + } + + @Override + public BehaviourType getType() { + return TYPE; + } + +} diff --git a/src/main/java/com/simibubi/create/compat/computercraft/ComputerControllable.java b/src/main/java/com/simibubi/create/compat/computercraft/ComputerControllable.java deleted file mode 100644 index 39b99efff..000000000 --- a/src/main/java/com/simibubi/create/compat/computercraft/ComputerControllable.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.simibubi.create.compat.computercraft; - -import org.jetbrains.annotations.NotNull; - -import dan200.computercraft.api.peripheral.IPeripheral; -import net.minecraftforge.common.capabilities.Capability; -import net.minecraftforge.common.capabilities.CapabilityManager; -import net.minecraftforge.common.capabilities.CapabilityToken; -import net.minecraftforge.common.util.LazyOptional; - -public interface ComputerControllable { - - Capability PERIPHERAL_CAPABILITY = CapabilityManager.get(new CapabilityToken<>() {}); - - IPeripheral createPeripheral(); - - void setPeripheral(LazyOptional peripheral); - - LazyOptional getPeripheral(); - default LazyOptional getPeripheralCapability(@NotNull Capability cap) { - if (cap == PERIPHERAL_CAPABILITY) { - if (getPeripheral() == null || !getPeripheral().isPresent()) - setPeripheral(LazyOptional.of(this::createPeripheral)); - - return getPeripheral().cast(); - } - - return LazyOptional.empty(); - } - - default void removePeripheral() { - if (getPeripheral() != null) { - getPeripheral().invalidate(); - } - } - -} diff --git a/src/main/java/com/simibubi/create/compat/computercraft/SyncedComputerControllable.java b/src/main/java/com/simibubi/create/compat/computercraft/SyncedComputerControllable.java deleted file mode 100644 index 5df108082..000000000 --- a/src/main/java/com/simibubi/create/compat/computercraft/SyncedComputerControllable.java +++ /dev/null @@ -1,7 +0,0 @@ -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/peripherals/PeripheralBase.java b/src/main/java/com/simibubi/create/compat/computercraft/peripherals/PeripheralBase.java index 157e28af4..fcd544b34 100644 --- a/src/main/java/com/simibubi/create/compat/computercraft/peripherals/PeripheralBase.java +++ b/src/main/java/com/simibubi/create/compat/computercraft/peripherals/PeripheralBase.java @@ -1,19 +1,47 @@ package com.simibubi.create.compat.computercraft.peripherals; -import com.simibubi.create.compat.computercraft.ComputerControllable; +import java.util.concurrent.atomic.AtomicInteger; + +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import dan200.computercraft.api.peripheral.IPeripheral; -import net.minecraft.world.level.block.entity.BlockEntity; +import com.simibubi.create.compat.computercraft.AttachedComputerPacket; +import com.simibubi.create.compat.computercraft.ComputerBehaviour; +import com.simibubi.create.foundation.networking.AllPackets; +import com.simibubi.create.foundation.tileEntity.SmartTileEntity; -public abstract class PeripheralBase implements IPeripheral { +import dan200.computercraft.api.peripheral.IComputerAccess; +import dan200.computercraft.api.peripheral.IPeripheral; +import net.minecraftforge.network.PacketDistributor; + +public abstract class PeripheralBase implements IPeripheral { protected final T tile; + private final AtomicInteger computers = new AtomicInteger(); public PeripheralBase(T tile) { this.tile = 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.getBehaviour(ComputerBehaviour.TYPE).setHasAttachedComputer(hasAttachedComputer); + AllPackets.channel.send(PacketDistributor.ALL.noArg(), new AttachedComputerPacket(tile.getBlockPos(), hasAttachedComputer)); + } + @Override public boolean equals(@Nullable IPeripheral other) { return this == other; diff --git a/src/main/java/com/simibubi/create/compat/computercraft/peripherals/SequencedGearshiftPeripheral.java b/src/main/java/com/simibubi/create/compat/computercraft/peripherals/SequencedGearshiftPeripheral.java index fc5e5e616..607224925 100644 --- a/src/main/java/com/simibubi/create/compat/computercraft/peripherals/SequencedGearshiftPeripheral.java +++ b/src/main/java/com/simibubi/create/compat/computercraft/peripherals/SequencedGearshiftPeripheral.java @@ -1,6 +1,5 @@ package com.simibubi.create.compat.computercraft.peripherals; -import com.simibubi.create.compat.computercraft.peripherals.SyncedPeripheralBase; import org.jetbrains.annotations.NotNull; import com.simibubi.create.content.contraptions.relays.advanced.sequencer.Instruction; @@ -12,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 SyncedPeripheralBase { +public class SequencedGearshiftPeripheral extends PeripheralBase { public SequencedGearshiftPeripheral(SequencedGearshiftTileEntity tile) { super(tile); diff --git a/src/main/java/com/simibubi/create/compat/computercraft/peripherals/SyncedPeripheralBase.java b/src/main/java/com/simibubi/create/compat/computercraft/peripherals/SyncedPeripheralBase.java deleted file mode 100644 index d5ba33237..000000000 --- a/src/main/java/com/simibubi/create/compat/computercraft/peripherals/SyncedPeripheralBase.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.simibubi.create.compat.computercraft.peripherals; - -import java.util.concurrent.atomic.AtomicInteger; - -import com.simibubi.create.compat.computercraft.AttachedComputerPacket; -import com.simibubi.create.compat.computercraft.SyncedComputerControllable; -import com.simibubi.create.compat.computercraft.peripherals.PeripheralBase; -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/SpeedControllerTileEntity.java b/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/SpeedControllerTileEntity.java index c155670f4..2206517f2 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/SpeedControllerTileEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/relays/advanced/SpeedControllerTileEntity.java @@ -5,7 +5,7 @@ import java.util.List; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import com.simibubi.create.compat.computercraft.ComputerControllable; +import com.simibubi.create.compat.computercraft.ComputerBehaviour; import com.simibubi.create.compat.computercraft.peripherals.SpeedControllerPeripheral; import com.simibubi.create.content.contraptions.RotationPropagator; import com.simibubi.create.content.contraptions.base.KineticTileEntity; @@ -20,7 +20,6 @@ import com.simibubi.create.foundation.tileEntity.behaviour.scrollvalue.ScrollVal import com.simibubi.create.foundation.utility.Lang; import com.simibubi.create.foundation.utility.VecHelper; -import dan200.computercraft.api.peripheral.IPeripheral; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.world.level.block.entity.BlockEntityType; @@ -29,15 +28,14 @@ import net.minecraft.world.phys.Vec3; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.util.LazyOptional; -public class SpeedControllerTileEntity extends KineticTileEntity implements ComputerControllable { +public class SpeedControllerTileEntity extends KineticTileEntity { public static final int DEFAULT_SPEED = 16; protected ScrollValueBehaviour targetSpeed; + ComputerBehaviour computerBehaviour; boolean hasBracket; - private LazyOptional peripheral; - public SpeedControllerTileEntity(BlockEntityType type, BlockPos pos, BlockState state) { super(type, pos, state); hasBracket = false; @@ -63,6 +61,7 @@ public class SpeedControllerTileEntity extends KineticTileEntity implements Comp targetSpeed.withCallback(i -> this.updateTargetRotation()); targetSpeed.withStepFunction(CreativeMotorTileEntity::step); behaviours.add(targetSpeed); + behaviours.add(computerBehaviour = new ComputerBehaviour(this, () -> new SpeedControllerPeripheral(this, targetSpeed))); registerAwardables(behaviours, AllAdvancements.SPEED_CONTROLLER); } @@ -140,33 +139,18 @@ public class SpeedControllerTileEntity extends KineticTileEntity implements Comp @NotNull @Override public LazyOptional getCapability(@NotNull Capability cap, @Nullable Direction side) { - LazyOptional peripheralCap = getPeripheralCapability(cap); + if (ComputerBehaviour.isPeripheralCap(cap)) + return computerBehaviour.getPeripheralCapability(); - return peripheralCap.isPresent() ? peripheralCap : super.getCapability(cap, side); + return super.getCapability(cap, side); } @Override public void invalidateCaps() { super.invalidateCaps(); - removePeripheral(); + computerBehaviour.removePeripheral(); } - @Override - public IPeripheral createPeripheral() { - return new SpeedControllerPeripheral(this, targetSpeed); - } - - @Override - public void setPeripheral(LazyOptional peripheral) { - this.peripheral = peripheral; - } - - @Override - public LazyOptional getPeripheral() { - return this.peripheral; - } - - private class ControllerValueBoxTransform extends ValueBoxTransform.Sided { @Override 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 3fd7cd98b..fa77415c4 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.hasAttachedComputer) + if (te.computerBehaviour.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 83a62ffcb..f4d7e1923 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 @@ -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.hasAttachedComputer = te.hasAttachedComputer; + this.hasAttachedComputer = te.computerBehaviour.hasAttachedComputer(); compareTag = 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 ad6bca4db..c0e97bff3 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 @@ -1,15 +1,16 @@ package com.simibubi.create.content.contraptions.relays.advanced.sequencer; +import java.util.List; import java.util.Vector; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import com.simibubi.create.compat.computercraft.ComputerBehaviour; import com.simibubi.create.compat.computercraft.peripherals.SequencedGearshiftPeripheral; -import com.simibubi.create.compat.computercraft.SyncedComputerControllable; import com.simibubi.create.content.contraptions.relays.encased.SplitShaftTileEntity; +import com.simibubi.create.foundation.tileEntity.TileEntityBehaviour; -import dan200.computercraft.api.peripheral.IPeripheral; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; @@ -19,7 +20,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 SyncedComputerControllable { +public class SequencedGearshiftTileEntity extends SplitShaftTileEntity { Vector instructions; int currentInstruction; @@ -28,8 +29,7 @@ public class SequencedGearshiftTileEntity extends SplitShaftTileEntity implement int timer; boolean poweredPreviously; - LazyOptional peripheral; - boolean hasAttachedComputer; + ComputerBehaviour computerBehaviour; public SequencedGearshiftTileEntity(BlockEntityType type, BlockPos pos, BlockState state) { super(type, pos, state); @@ -39,7 +39,12 @@ public class SequencedGearshiftTileEntity extends SplitShaftTileEntity implement currentInstructionProgress = 0; timer = 0; poweredPreviously = false; - hasAttachedComputer = false; + } + + @Override + public void addBehaviours(List behaviours) { + super.addBehaviours(behaviours); + behaviours.add(computerBehaviour = new ComputerBehaviour(this, () -> new SequencedGearshiftPeripheral(this))); } @Override @@ -84,7 +89,7 @@ public class SequencedGearshiftTileEntity extends SplitShaftTileEntity implement } public void onRedstoneUpdate(boolean isPowered, boolean isRunning) { - if (hasAttachedComputer) + if (computerBehaviour.hasAttachedComputer()) return; if (!poweredPreviously && isPowered) risingFlank(); @@ -173,35 +178,16 @@ public class SequencedGearshiftTileEntity extends SplitShaftTileEntity implement @NotNull @Override public LazyOptional getCapability(@NotNull Capability cap, @Nullable Direction side) { - LazyOptional peripheralCap = getPeripheralCapability(cap); + if (ComputerBehaviour.isPeripheralCap(cap)) + return computerBehaviour.getPeripheralCapability(); - return peripheralCap.isPresent() ? peripheralCap : super.getCapability(cap, side); + return super.getCapability(cap, side); } @Override public void invalidateCaps() { super.invalidateCaps(); - removePeripheral(); - } - - @Override - public IPeripheral createPeripheral() { - return new SequencedGearshiftPeripheral(this); - } - - @Override - public void setPeripheral(LazyOptional peripheral) { - this.peripheral = peripheral; - } - - @Override - public LazyOptional getPeripheral() { - return this.peripheral; - } - - @Override - public void setHasAttachedComputer(boolean hasAttachedComputer) { - this.hasAttachedComputer = hasAttachedComputer; + computerBehaviour.removePeripheral(); } @Override diff --git a/src/main/java/com/simibubi/create/content/contraptions/relays/gauge/GaugeTileEntity.java b/src/main/java/com/simibubi/create/content/contraptions/relays/gauge/GaugeTileEntity.java index 373858942..bc320e85d 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/relays/gauge/GaugeTileEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/relays/gauge/GaugeTileEntity.java @@ -2,33 +2,23 @@ package com.simibubi.create.content.contraptions.relays.gauge; import java.util.List; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import com.simibubi.create.compat.computercraft.ComputerControllable; import com.simibubi.create.content.contraptions.base.KineticTileEntity; import com.simibubi.create.content.contraptions.goggles.IHaveGoggleInformation; import com.simibubi.create.foundation.utility.Lang; -import dan200.computercraft.api.peripheral.IPeripheral; import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; -import net.minecraftforge.common.capabilities.Capability; -import net.minecraftforge.common.util.LazyOptional; -public abstract class GaugeTileEntity extends KineticTileEntity implements IHaveGoggleInformation, ComputerControllable { +public abstract class GaugeTileEntity extends KineticTileEntity implements IHaveGoggleInformation { public float dialTarget; public float dialState; public float prevDialState; public int color; - private LazyOptional peripheral; - public GaugeTileEntity(BlockEntityType typeIn, BlockPos pos, BlockState state) { super(typeIn, pos, state); } @@ -63,28 +53,4 @@ public abstract class GaugeTileEntity extends KineticTileEntity implements IHave return true; } - @NotNull - @Override - public LazyOptional getCapability(@NotNull Capability cap, @Nullable Direction side) { - LazyOptional peripheralCap = getPeripheralCapability(cap); - - return peripheralCap.isPresent() ? peripheralCap : super.getCapability(cap, side); - } - - @Override - public void invalidateCaps() { - super.invalidateCaps(); - removePeripheral(); - } - - @Override - public void setPeripheral(LazyOptional peripheral) { - this.peripheral = peripheral; - } - - @Override - public LazyOptional getPeripheral() { - return this.peripheral; - } - } diff --git a/src/main/java/com/simibubi/create/content/contraptions/relays/gauge/SpeedGaugeTileEntity.java b/src/main/java/com/simibubi/create/content/contraptions/relays/gauge/SpeedGaugeTileEntity.java index ef0bee076..86b11ea65 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/relays/gauge/SpeedGaugeTileEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/relays/gauge/SpeedGaugeTileEntity.java @@ -2,26 +2,41 @@ package com.simibubi.create.content.contraptions.relays.gauge; import java.util.List; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import com.simibubi.create.compat.computercraft.ComputerBehaviour; import com.simibubi.create.compat.computercraft.peripherals.SpeedGaugePeripheral; import com.simibubi.create.content.contraptions.base.IRotate.SpeedLevel; import com.simibubi.create.foundation.config.AllConfigs; +import com.simibubi.create.foundation.tileEntity.TileEntityBehaviour; import com.simibubi.create.foundation.utility.Color; import com.simibubi.create.foundation.utility.Lang; -import dan200.computercraft.api.peripheral.IPeripheral; import net.minecraft.ChatFormatting; import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; import net.minecraft.network.chat.Component; import net.minecraft.util.Mth; import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.util.LazyOptional; public class SpeedGaugeTileEntity extends GaugeTileEntity { + ComputerBehaviour computerBehaviour; + public SpeedGaugeTileEntity(BlockEntityType type, BlockPos pos, BlockState state) { super(type, pos, state); } + @Override + public void addBehaviours(List behaviours) { + super.addBehaviours(behaviours); + behaviours.add(computerBehaviour = new ComputerBehaviour(this, () -> new SpeedGaugePeripheral(this))); + } + @Override public void onSpeedChanged(float prevSpeed) { super.onSpeedChanged(prevSpeed); @@ -65,9 +80,19 @@ public class SpeedGaugeTileEntity extends GaugeTileEntity { return true; } + @NotNull @Override - public IPeripheral createPeripheral() { - return new SpeedGaugePeripheral(this); + public LazyOptional getCapability(@NotNull Capability cap, @Nullable Direction side) { + if (ComputerBehaviour.isPeripheralCap(cap)) + return computerBehaviour.getPeripheralCapability(); + + return super.getCapability(cap, side); + } + + @Override + public void invalidateCaps() { + super.invalidateCaps(); + computerBehaviour.removePeripheral(); } } diff --git a/src/main/java/com/simibubi/create/content/contraptions/relays/gauge/StressGaugeTileEntity.java b/src/main/java/com/simibubi/create/content/contraptions/relays/gauge/StressGaugeTileEntity.java index 09a74f246..9304bcc03 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/relays/gauge/StressGaugeTileEntity.java +++ b/src/main/java/com/simibubi/create/content/contraptions/relays/gauge/StressGaugeTileEntity.java @@ -2,6 +2,10 @@ package com.simibubi.create.content.contraptions.relays.gauge; import java.util.List; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import com.simibubi.create.compat.computercraft.ComputerBehaviour; import com.simibubi.create.compat.computercraft.peripherals.StressGaugePeripheral; import com.simibubi.create.content.contraptions.base.IRotate.StressImpact; import com.simibubi.create.foundation.advancement.AllAdvancements; @@ -12,17 +16,20 @@ import com.simibubi.create.foundation.utility.Color; import com.simibubi.create.foundation.utility.Lang; import com.simibubi.create.foundation.utility.LangBuilder; -import dan200.computercraft.api.peripheral.IPeripheral; import net.minecraft.ChatFormatting; import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; import net.minecraft.util.Mth; import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.util.LazyOptional; public class StressGaugeTileEntity extends GaugeTileEntity { + ComputerBehaviour computerBehaviour; static BlockPos lastSent; public StressGaugeTileEntity(BlockEntityType type, BlockPos pos, BlockState state) { @@ -32,6 +39,7 @@ public class StressGaugeTileEntity extends GaugeTileEntity { @Override public void addBehaviours(List behaviours) { super.addBehaviours(behaviours); + behaviours.add(computerBehaviour = new ComputerBehaviour(this, () -> new StressGaugePeripheral(this))); registerAwardables(behaviours, AllAdvancements.STRESSOMETER, AllAdvancements.STRESSOMETER_MAXED); } @@ -143,9 +151,19 @@ public class StressGaugeTileEntity extends GaugeTileEntity { award(AllAdvancements.STRESSOMETER_MAXED); } + @NotNull @Override - public IPeripheral createPeripheral() { - return new StressGaugePeripheral(this); + public LazyOptional getCapability(@NotNull Capability cap, @Nullable Direction side) { + if (ComputerBehaviour.isPeripheralCap(cap)) + return computerBehaviour.getPeripheralCapability(); + + return super.getCapability(cap, side); + } + + @Override + public void invalidateCaps() { + super.invalidateCaps(); + computerBehaviour.removePeripheral(); } } diff --git a/src/main/java/com/simibubi/create/content/logistics/block/display/DisplayLinkTileEntity.java b/src/main/java/com/simibubi/create/content/logistics/block/display/DisplayLinkTileEntity.java index 33372f119..9b3bcf5b9 100644 --- a/src/main/java/com/simibubi/create/content/logistics/block/display/DisplayLinkTileEntity.java +++ b/src/main/java/com/simibubi/create/content/logistics/block/display/DisplayLinkTileEntity.java @@ -5,7 +5,7 @@ import java.util.List; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import com.simibubi.create.compat.computercraft.ComputerControllable; +import com.simibubi.create.compat.computercraft.ComputerBehaviour; import com.simibubi.create.compat.computercraft.peripherals.DisplayLinkPeripheral; import com.simibubi.create.content.logistics.block.display.source.DisplaySource; import com.simibubi.create.content.logistics.block.display.target.DisplayTarget; @@ -16,7 +16,6 @@ import com.simibubi.create.foundation.utility.NBTHelper; import com.simibubi.create.foundation.utility.animation.LerpedFloat; import com.simibubi.create.foundation.utility.animation.LerpedFloat.Chaser; -import dan200.computercraft.api.peripheral.IPeripheral; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; @@ -27,7 +26,7 @@ import net.minecraft.world.level.block.state.BlockState; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.util.LazyOptional; -public class DisplayLinkTileEntity extends SmartTileEntity implements ComputerControllable { +public class DisplayLinkTileEntity extends SmartTileEntity { protected BlockPos targetOffset; @@ -42,7 +41,7 @@ public class DisplayLinkTileEntity extends SmartTileEntity implements ComputerCo public int refreshTicks; - private LazyOptional peripheral; + ComputerBehaviour computerBehaviour; public DisplayLinkTileEntity(BlockEntityType type, BlockPos pos, BlockState state) { super(type, pos, state); @@ -54,6 +53,13 @@ public class DisplayLinkTileEntity extends SmartTileEntity implements ComputerCo glow.chase(0, 0.5f, Chaser.EXP); } + @Override + public void addBehaviours(List behaviours) { + behaviours.add(computerBehaviour = new ComputerBehaviour(this, () -> new DisplayLinkPeripheral(this))); + + registerAwardables(behaviours, AllAdvancements.DISPLAY_LINK, AllAdvancements.DISPLAY_BOARD); + } + @Override public void tick() { super.tick(); @@ -128,11 +134,6 @@ public class DisplayLinkTileEntity extends SmartTileEntity implements ComputerCo award(AllAdvancements.DISPLAY_LINK); } - @Override - public void addBehaviours(List behaviours) { - registerAwardables(behaviours, AllAdvancements.DISPLAY_LINK, AllAdvancements.DISPLAY_BOARD); - } - @Override public void writeSafe(CompoundTag tag) { super.writeSafe(tag); @@ -186,30 +187,16 @@ public class DisplayLinkTileEntity extends SmartTileEntity implements ComputerCo @NotNull @Override public LazyOptional getCapability(@NotNull Capability cap, @Nullable Direction side) { - LazyOptional peripheralCap = getPeripheralCapability(cap); + if (ComputerBehaviour.isPeripheralCap(cap)) + return computerBehaviour.getPeripheralCapability(); - return peripheralCap.isPresent() ? peripheralCap : super.getCapability(cap, side); + return super.getCapability(cap, side); } @Override public void invalidateCaps() { super.invalidateCaps(); - removePeripheral(); - } - - @Override - public IPeripheral createPeripheral() { - return new DisplayLinkPeripheral(this); - } - - @Override - public void setPeripheral(LazyOptional peripheral) { - this.peripheral = peripheral; - } - - @Override - public LazyOptional getPeripheral() { - return this.peripheral; + computerBehaviour.removePeripheral(); } public void target(BlockPos targetPosition) { diff --git a/src/main/java/com/simibubi/create/content/logistics/trains/management/edgePoint/station/StationTileEntity.java b/src/main/java/com/simibubi/create/content/logistics/trains/management/edgePoint/station/StationTileEntity.java index 95510baf8..df7fa11af 100644 --- a/src/main/java/com/simibubi/create/content/logistics/trains/management/edgePoint/station/StationTileEntity.java +++ b/src/main/java/com/simibubi/create/content/logistics/trains/management/edgePoint/station/StationTileEntity.java @@ -18,7 +18,7 @@ import com.simibubi.create.AllBlocks; import com.simibubi.create.AllItems; import com.simibubi.create.AllSoundEvents; import com.simibubi.create.Create; -import com.simibubi.create.compat.computercraft.ComputerControllable; +import com.simibubi.create.compat.computercraft.ComputerBehaviour; import com.simibubi.create.compat.computercraft.peripherals.StationPeripheral; import com.simibubi.create.content.contraptions.components.structureMovement.AssemblyException; import com.simibubi.create.content.contraptions.components.structureMovement.ITransformableTE; @@ -55,7 +55,6 @@ import com.simibubi.create.foundation.utility.WorldAttached; import com.simibubi.create.foundation.utility.animation.LerpedFloat; import com.simibubi.create.foundation.utility.animation.LerpedFloat.Chaser; -import dan200.computercraft.api.peripheral.IPeripheral; import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos.MutableBlockPos; import net.minecraft.core.Direction; @@ -82,7 +81,7 @@ import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.network.PacketDistributor; -public class StationTileEntity extends SmartTileEntity implements ITransformableTE, ComputerControllable { +public class StationTileEntity extends SmartTileEntity implements ITransformableTE { public TrackTargetingBehaviour edgePoint; public LerpedFloat flag; @@ -90,6 +89,7 @@ public class StationTileEntity extends SmartTileEntity implements ITransformable protected int failedCarriageIndex; protected AssemblyException lastException; protected DepotBehaviour depotBehaviour; + ComputerBehaviour computerBehaviour; // for display UUID imminentTrain; @@ -103,7 +103,6 @@ public class StationTileEntity extends SmartTileEntity implements ITransformable boolean flagFlipped; public Component lastDisassembledTrainName; - private LazyOptional peripheral; public StationTileEntity(BlockEntityType type, BlockPos pos, BlockState state) { super(type, pos, state); @@ -122,6 +121,7 @@ public class StationTileEntity extends SmartTileEntity implements ITransformable depotBehaviour.addSubBehaviours(behaviours); registerAwardables(behaviours, AllAdvancements.CONTRAPTION_ACTORS, AllAdvancements.TRAIN, AllAdvancements.LONG_TRAIN, AllAdvancements.CONDUCTOR); + behaviours.add(computerBehaviour = new ComputerBehaviour(this, () -> new StationPeripheral(this))); } @Override @@ -706,18 +706,19 @@ public class StationTileEntity extends SmartTileEntity implements ITransformable @Override public @NotNull LazyOptional getCapability(@NotNull Capability cap, Direction side) { - LazyOptional peripheralCap = getPeripheralCapability(cap); - if (isItemHandlerCap(cap)) return depotBehaviour.getItemCapability(cap, side); - return peripheralCap.isPresent() ? peripheralCap : super.getCapability(cap, side); + if (ComputerBehaviour.isPeripheralCap(cap)) + return computerBehaviour.getPeripheralCapability(); + + return super.getCapability(cap, side); } @Override public void invalidateCaps() { super.invalidateCaps(); - removePeripheral(); + computerBehaviour.removePeripheral(); } private void applyAutoSchedule() { @@ -781,19 +782,4 @@ public class StationTileEntity extends SmartTileEntity implements ITransformable edgePoint.transform(transform); } - @Override - public IPeripheral createPeripheral() { - return new StationPeripheral(this); - } - - @Override - public void setPeripheral(LazyOptional peripheral) { - this.peripheral = peripheral; - } - - @Override - public LazyOptional getPeripheral() { - return peripheral; - } - }