From 231f99cac5aa23fb7b8383d4173eba35cf9da560 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9leste=20Wouters?= Date: Sat, 24 Aug 2024 00:53:13 +0200 Subject: [PATCH] Add ComputerCraft integration to Train Observers - Emit CC events train_passing & train_passed with the name of the train originating these events - Add CC isTrainPassing() function to check passing train presence - Add CC getPassingTrainName() function to get the passing train name, or nil failing that --- .../computercraft/events/TrainPassEvent.java | 18 ++++++++ .../implementation/ComputerBehaviour.java | 4 ++ .../peripherals/TrackObserverPeripheral.java | 45 +++++++++++++++++++ .../observer/TrackObserverBlockEntity.java | 40 ++++++++++++++++- 4 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/simibubi/create/compat/computercraft/events/TrainPassEvent.java create mode 100644 src/main/java/com/simibubi/create/compat/computercraft/implementation/peripherals/TrackObserverPeripheral.java diff --git a/src/main/java/com/simibubi/create/compat/computercraft/events/TrainPassEvent.java b/src/main/java/com/simibubi/create/compat/computercraft/events/TrainPassEvent.java new file mode 100644 index 000000000..cdc26d2e6 --- /dev/null +++ b/src/main/java/com/simibubi/create/compat/computercraft/events/TrainPassEvent.java @@ -0,0 +1,18 @@ +package com.simibubi.create.compat.computercraft.events; + +import org.jetbrains.annotations.NotNull; + + +import com.simibubi.create.content.trains.entity.Train; + +public class TrainPassEvent implements ComputerEvent { + + public @NotNull Train train; + public boolean passing; + + public TrainPassEvent(@NotNull Train train, boolean passing) { + this.train = train; + this.passing = passing; + } + +} diff --git a/src/main/java/com/simibubi/create/compat/computercraft/implementation/ComputerBehaviour.java b/src/main/java/com/simibubi/create/compat/computercraft/implementation/ComputerBehaviour.java index 255a4daa0..f8fa166da 100644 --- a/src/main/java/com/simibubi/create/compat/computercraft/implementation/ComputerBehaviour.java +++ b/src/main/java/com/simibubi/create/compat/computercraft/implementation/ComputerBehaviour.java @@ -14,12 +14,14 @@ import com.simibubi.create.compat.computercraft.implementation.peripherals.Speed import com.simibubi.create.compat.computercraft.implementation.peripherals.StationPeripheral; import com.simibubi.create.compat.computercraft.implementation.peripherals.StressGaugePeripheral; import com.simibubi.create.compat.computercraft.implementation.peripherals.SyncedPeripheral; +import com.simibubi.create.compat.computercraft.implementation.peripherals.TrackObserverPeripheral; import com.simibubi.create.content.kinetics.gauge.SpeedGaugeBlockEntity; import com.simibubi.create.content.kinetics.gauge.StressGaugeBlockEntity; import com.simibubi.create.content.kinetics.speedController.SpeedControllerBlockEntity; import com.simibubi.create.content.kinetics.transmission.sequencer.SequencedGearshiftBlockEntity; import com.simibubi.create.content.redstone.displayLink.DisplayLinkBlockEntity; import com.simibubi.create.content.redstone.nixieTube.NixieTubeBlockEntity; +import com.simibubi.create.content.trains.observer.TrackObserverBlockEntity; import com.simibubi.create.content.trains.signal.SignalBlockEntity; import com.simibubi.create.content.trains.station.StationBlockEntity; import com.simibubi.create.foundation.blockEntity.SmartBlockEntity; @@ -61,6 +63,8 @@ public class ComputerBehaviour extends AbstractComputerBehaviour { return () -> new StressGaugePeripheral(sgbe); if (be instanceof StationBlockEntity sbe) return () -> new StationPeripheral(sbe); + if (be instanceof TrackObserverBlockEntity tobe) + return () -> new TrackObserverPeripheral(tobe); throw new IllegalArgumentException("No peripheral available for " + be.getType() .getRegistryName()); diff --git a/src/main/java/com/simibubi/create/compat/computercraft/implementation/peripherals/TrackObserverPeripheral.java b/src/main/java/com/simibubi/create/compat/computercraft/implementation/peripherals/TrackObserverPeripheral.java new file mode 100644 index 000000000..8cbe5c564 --- /dev/null +++ b/src/main/java/com/simibubi/create/compat/computercraft/implementation/peripherals/TrackObserverPeripheral.java @@ -0,0 +1,45 @@ +package com.simibubi.create.compat.computercraft.implementation.peripherals; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + + +import com.simibubi.create.Create; +import com.simibubi.create.compat.computercraft.events.ComputerEvent; +import com.simibubi.create.compat.computercraft.events.TrainPassEvent; +import com.simibubi.create.content.trains.entity.Train; +import com.simibubi.create.content.trains.observer.TrackObserverBlockEntity; + +import dan200.computercraft.api.lua.LuaFunction; + +public class TrackObserverPeripheral extends SyncedPeripheral { + + public TrackObserverPeripheral(TrackObserverBlockEntity blockEntity) { + super(blockEntity); + } + + @LuaFunction + public boolean isTrainPassing() { + return Create.RAILWAYS.trains.containsKey(blockEntity.passingTrainUUID); + } + + @LuaFunction + public @Nullable String getPassingTrainName() { + Train train = Create.RAILWAYS.trains.get(blockEntity.passingTrainUUID); + return train == null ? null : train.name.getString(); + } + + @Override + public void prepareComputerEvent(@NotNull ComputerEvent event) { + if (event instanceof TrainPassEvent tpe) { + queueEvent(tpe.passing ? "train_passing" : "train_passed", tpe.train.name.getString()); + } + } + + @NotNull + @Override + public String getType() { + return "Create_TrainObserver"; + } + +} diff --git a/src/main/java/com/simibubi/create/content/trains/observer/TrackObserverBlockEntity.java b/src/main/java/com/simibubi/create/content/trains/observer/TrackObserverBlockEntity.java index 48e01b499..b60bbe6fa 100644 --- a/src/main/java/com/simibubi/create/content/trains/observer/TrackObserverBlockEntity.java +++ b/src/main/java/com/simibubi/create/content/trains/observer/TrackObserverBlockEntity.java @@ -1,11 +1,18 @@ package com.simibubi.create.content.trains.observer; import java.util.List; +import java.util.UUID; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; -import javax.annotation.Nullable; import com.jozufozu.flywheel.util.transform.TransformStack; import com.mojang.blaze3d.vertex.PoseStack; +import com.simibubi.create.Create; +import com.simibubi.create.compat.computercraft.AbstractComputerBehaviour; +import com.simibubi.create.compat.computercraft.ComputerCraftProxy; +import com.simibubi.create.compat.computercraft.events.TrainPassEvent; import com.simibubi.create.content.contraptions.ITransformableBlockEntity; import com.simibubi.create.content.contraptions.StructureTransform; import com.simibubi.create.content.redstone.displayLink.DisplayLinkBlock; @@ -18,11 +25,14 @@ import com.simibubi.create.foundation.blockEntity.behaviour.filtering.FilteringB import com.simibubi.create.foundation.utility.Lang; import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.phys.AABB; import net.minecraft.world.phys.Vec3; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.util.LazyOptional; public class TrackObserverBlockEntity extends SmartBlockEntity implements ITransformableBlockEntity { @@ -30,6 +40,9 @@ public class TrackObserverBlockEntity extends SmartBlockEntity implements ITrans private FilteringBehaviour filtering; + public AbstractComputerBehaviour computerBehaviour; + public @Nullable UUID passingTrainUUID; + public TrackObserverBlockEntity(BlockEntityType type, BlockPos pos, BlockState state) { super(type, pos, state); } @@ -38,6 +51,7 @@ public class TrackObserverBlockEntity extends SmartBlockEntity implements ITrans public void addBehaviours(List behaviours) { behaviours.add(edgePoint = new TrackTargetingBehaviour<>(this, EdgePointType.OBSERVER)); behaviours.add(filtering = createFilter().withCallback(this::onFilterChanged)); + behaviours.add(computerBehaviour = ComputerCraftProxy.behaviour(this)); filtering.setLabel(Lang.translateDirect("logistics.train_observer.cargo_filter")); } @@ -63,6 +77,17 @@ public class TrackObserverBlockEntity extends SmartBlockEntity implements ITrans if (isBlockPowered() == shouldBePowered) return; + if (observer != null && computerBehaviour.hasAttachedComputer()) { + if (shouldBePowered) + passingTrainUUID = observer.getCurrentTrain(); + if (passingTrainUUID != null) { + computerBehaviour.prepareComputerEvent( + new TrainPassEvent(Create.RAILWAYS.trains.get(passingTrainUUID), shouldBePowered)); + if (!shouldBePowered) + passingTrainUUID = null; + } + } + BlockState blockState = getBlockState(); if (blockState.hasProperty(TrackObserverBlock.POWERED)) level.setBlock(worldPosition, blockState.setValue(TrackObserverBlock.POWERED, shouldBePowered), 3); @@ -110,4 +135,17 @@ public class TrackObserverBlockEntity extends SmartBlockEntity implements ITrans }); } + @Override + public @NotNull LazyOptional getCapability(@NotNull Capability cap, Direction side) { + if (computerBehaviour.isPeripheralCap(cap)) + return computerBehaviour.getPeripheralCapability(); + return super.getCapability(cap, side); + } + + @Override + public void invalidateCaps() { + super.invalidateCaps(); + computerBehaviour.removePeripheral(); + } + }