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
This commit is contained in:
Céleste Wouters 2024-08-24 00:53:13 +02:00
parent a889d16c34
commit 231f99cac5
Failed to generate hash of commit
4 changed files with 106 additions and 1 deletions

View file

@ -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;
}
}

View file

@ -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());

View file

@ -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<TrackObserverBlockEntity> {
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";
}
}

View file

@ -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<BlockEntityBehaviour> 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 <T> @NotNull LazyOptional<T> getCapability(@NotNull Capability<T> cap, Direction side) {
if (computerBehaviour.isPeripheralCap(cap))
return computerBehaviour.getPeripheralCapability();
return super.getCapability(cap, side);
}
@Override
public void invalidateCaps() {
super.invalidateCaps();
computerBehaviour.removePeripheral();
}
}