Added Rotation Speed Controller and Sequenced Gearshift as peripherals

- Rotation Speed Controller can get and set targetSpeed
- Sequenced Gearshift can rotate by a certain angle and move a certain distance
This commit is contained in:
caelwarner 2022-10-03 20:05:25 -07:00
parent 1420406ab7
commit 654476d9f3
No known key found for this signature in database
GPG key ID: 514BEF5EADE889FF
6 changed files with 204 additions and 6 deletions

View file

@ -0,0 +1,63 @@
package com.simibubi.create.compat.computercraft;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.simibubi.create.content.contraptions.relays.advanced.sequencer.Instruction;
import com.simibubi.create.content.contraptions.relays.advanced.sequencer.InstructionSpeedModifiers;
import com.simibubi.create.content.contraptions.relays.advanced.sequencer.SequencedGearshiftTileEntity;
import com.simibubi.create.content.contraptions.relays.advanced.sequencer.SequencerInstructions;
import dan200.computercraft.api.lua.IArguments;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.peripheral.IPeripheral;
public class SequencedGearshiftPeripheral implements IPeripheral {
private final SequencedGearshiftTileEntity tile;
public SequencedGearshiftPeripheral(SequencedGearshiftTileEntity tile) {
this.tile = tile;
}
@LuaFunction(mainThread = true)
public void rotate(IArguments arguments) throws LuaException {
runInstruction(arguments, SequencerInstructions.TURN_ANGLE);
}
@LuaFunction(mainThread = true)
public void move(IArguments arguments) throws LuaException {
runInstruction(arguments, SequencerInstructions.TURN_DISTANCE);
}
@LuaFunction
public boolean isRunning() {
return !this.tile.isIdle();
}
private void runInstruction(IArguments arguments, SequencerInstructions instructionType) throws LuaException {
int speedModifier = arguments.count() > 1 ? arguments.getInt(1) : 1;
this.tile.getInstructions().clear();
this.tile.getInstructions().add(new Instruction(
instructionType,
InstructionSpeedModifiers.getByModifier(speedModifier),
Math.abs(arguments.getInt(0))));
this.tile.getInstructions().add(new Instruction(SequencerInstructions.END));
this.tile.run(0);
}
@NotNull
@Override
public String getType() {
return "Create_SequencedGearshift";
}
@Override
public boolean equals(@Nullable IPeripheral other) {
return this == other;
}
}

View file

@ -0,0 +1,40 @@
package com.simibubi.create.compat.computercraft;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.simibubi.create.foundation.tileEntity.behaviour.scrollvalue.ScrollValueBehaviour;
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.api.peripheral.IPeripheral;
public class SpeedControllerPeripheral implements IPeripheral {
private final ScrollValueBehaviour targetSpeed;
public SpeedControllerPeripheral(ScrollValueBehaviour targetSpeed) {
this.targetSpeed = targetSpeed;
}
@LuaFunction(mainThread = true)
public void setTargetSpeed(int speed) {
this.targetSpeed.setValue(speed);
}
@LuaFunction
public float getTargetSpeed() {
return this.targetSpeed.getValue();
}
@NotNull
@Override
public String getType() {
return "Create_RotationSpeedController";
}
@Override
public boolean equals(@Nullable IPeripheral other) {
return this == other;
}
}

View file

@ -2,6 +2,11 @@ package com.simibubi.create.content.contraptions.relays.advanced;
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.SpeedControllerPeripheral;
import com.simibubi.create.content.contraptions.RotationPropagator;
import com.simibubi.create.content.contraptions.base.KineticTileEntity;
import com.simibubi.create.content.contraptions.components.motor.CreativeMotorTileEntity;
@ -15,19 +20,24 @@ 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;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
public class SpeedControllerTileEntity extends KineticTileEntity {
public class SpeedControllerTileEntity extends KineticTileEntity implements ComputerControllable {
public static final int DEFAULT_SPEED = 16;
protected ScrollValueBehaviour targetSpeed;
boolean hasBracket;
private LazyOptional<IPeripheral> peripheral;
public SpeedControllerTileEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state);
hasBracket = false;
@ -53,7 +63,7 @@ public class SpeedControllerTileEntity extends KineticTileEntity {
targetSpeed.withCallback(i -> this.updateTargetRotation());
targetSpeed.withStepFunction(CreativeMotorTileEntity::step);
behaviours.add(targetSpeed);
registerAwardables(behaviours, AllAdvancements.SPEED_CONTROLLER);
}
@ -63,7 +73,7 @@ public class SpeedControllerTileEntity extends KineticTileEntity {
RotationPropagator.handleRemoved(level, worldPosition, this);
removeSource();
attachKinetics();
if (isCogwheelPresent() && getSpeed() != 0)
award(AllAdvancements.SPEED_CONTROLLER);
}
@ -127,6 +137,36 @@ public class SpeedControllerTileEntity extends KineticTileEntity {
&& stateAbove.getValue(CogWheelBlock.AXIS).isHorizontal();
}
@NotNull
@Override
public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
LazyOptional<T> peripheralCap = getPeripheralCapability(cap);
return peripheralCap.isPresent() ? peripheralCap : super.getCapability(cap, side);
}
@Override
public void setRemoved() {
super.setRemoved();
removePeripheral();
}
@Override
public IPeripheral createPeripheral() {
return new SpeedControllerPeripheral(targetSpeed);
}
@Override
public void setPeripheral(LazyOptional<IPeripheral> peripheral) {
this.peripheral = peripheral;
}
@Override
public LazyOptional<IPeripheral> getPeripheral() {
return this.peripheral;
}
private class ControllerValueBoxTransform extends ValueBoxTransform.Sided {
@Override

View file

@ -19,8 +19,12 @@ public class Instruction {
}
public Instruction(SequencerInstructions instruction, int value) {
this(instruction, InstructionSpeedModifiers.FORWARD, value);
}
public Instruction(SequencerInstructions instruction, InstructionSpeedModifiers speedModifier, int value) {
this.instruction = instruction;
speedModifier = InstructionSpeedModifiers.FORWARD;
this.speedModifier = speedModifier;
this.value = value;
}

View file

@ -1,6 +1,7 @@
package com.simibubi.create.content.contraptions.relays.advanced.sequencer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.simibubi.create.foundation.utility.Components;
@ -36,4 +37,11 @@ public enum InstructionSpeedModifiers {
return options;
}
public static InstructionSpeedModifiers getByModifier(int modifier) {
return Arrays.stream(InstructionSpeedModifiers.values())
.filter(speedModifier -> speedModifier.value == modifier)
.findAny()
.orElse(InstructionSpeedModifiers.FORWARD);
}
}

View file

@ -2,16 +2,24 @@ package com.simibubi.create.content.contraptions.relays.advanced.sequencer;
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.content.contraptions.relays.encased.SplitShaftTileEntity;
import dan200.computercraft.api.peripheral.IPeripheral;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
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 SequencedGearshiftTileEntity extends SplitShaftTileEntity {
public class SequencedGearshiftTileEntity extends SplitShaftTileEntity implements ComputerControllable {
Vector<Instruction> instructions;
int currentInstruction;
@ -20,6 +28,8 @@ public class SequencedGearshiftTileEntity extends SplitShaftTileEntity {
int timer;
boolean poweredPreviously;
LazyOptional<IPeripheral> peripheral;
public SequencedGearshiftTileEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state);
instructions = Instruction.createDefault();
@ -105,7 +115,7 @@ public class SequencedGearshiftTileEntity extends SplitShaftTileEntity {
}
}
protected void run(int instructionIndex) {
public void run(int instructionIndex) {
Instruction instruction = getInstruction(instructionIndex);
if (instruction == null || instruction.instruction == SequencerInstructions.END) {
if (getModifier() != 0)
@ -156,6 +166,35 @@ public class SequencedGearshiftTileEntity extends SplitShaftTileEntity {
super.read(compound, clientPacket);
}
@NotNull
@Override
public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
LazyOptional<T> peripheralCap = getPeripheralCapability(cap);
return peripheralCap.isPresent() ? peripheralCap : super.getCapability(cap, side);
}
@Override
public void setRemoved() {
super.setRemoved();
removePeripheral();
}
@Override
public IPeripheral createPeripheral() {
return new SequencedGearshiftPeripheral(this);
}
@Override
public void setPeripheral(LazyOptional<IPeripheral> peripheral) {
this.peripheral = peripheral;
}
@Override
public LazyOptional<IPeripheral> getPeripheral() {
return this.peripheral;
}
@Override
public float getRotationSpeedModifier(Direction face) {
if (isVirtual())
@ -171,4 +210,8 @@ public class SequencedGearshiftTileEntity extends SplitShaftTileEntity {
.getSpeedModifier();
}
public Vector<Instruction> getInstructions() {
return this.instructions;
}
}