From 42a6a55ca8d91bb87a8dc42d19f963638488e458 Mon Sep 17 00:00:00 2001 From: MRH0 Date: Wed, 26 Apr 2023 17:45:57 +0200 Subject: [PATCH] 20230426a --- COMPUTERCRAFT.md | 4 ++ .../ElectricMotorTileEntity.java | 45 +------------------ .../DigitalAdapterPeripheral.java | 26 ++++++++--- .../ElectricMotorPeripheral.java | 4 +- 4 files changed, 29 insertions(+), 50 deletions(-) diff --git a/COMPUTERCRAFT.md b/COMPUTERCRAFT.md index 44e75287..d245d5fc 100644 --- a/COMPUTERCRAFT.md +++ b/COMPUTERCRAFT.md @@ -251,4 +251,8 @@ da.print("Text on first line again") ``` The function `getMaxLines()` will return the max number of lines that can be displayable using the Digital Adapter (will always return 16). +### Other +The function `getDurationDistance(blocks, rpm)` will return the time needed to push a Mechanical Piston, Pulley or Gantry a number of blocks at the given rpm. + +The function `getDurationAngle(degrees, rpm)` will return the time needed to rotate a Mechanical Bearing by a number of degrees at the given rpm. diff --git a/src/main/java/com/mrh0/createaddition/blocks/electric_motor/ElectricMotorTileEntity.java b/src/main/java/com/mrh0/createaddition/blocks/electric_motor/ElectricMotorTileEntity.java index a32f4898..976c8dad 100644 --- a/src/main/java/com/mrh0/createaddition/blocks/electric_motor/ElectricMotorTileEntity.java +++ b/src/main/java/com/mrh0/createaddition/blocks/electric_motor/ElectricMotorTileEntity.java @@ -231,49 +231,9 @@ public void tick() { updateGeneratedRotation(); } } - - /*if (world.isRemote) - return; - if (currentInstructionDuration < 0) - return; - if (timer < currentInstructionDuration) { - timer++; - return; - }*/ - - //currentTarget = -1; - //currentInstruct = Instruct.NONE; - //currentInstructionDuration = -1; - //timer = 0; } - /*@Override - public void onSpeedChanged(float previousSpeed) { - super.onSpeedChanged(previousSpeed); - if (currentInstruct == Instruct.NONE) - return; - float currentSpeed = Math.abs(speed); - if (Math.abs(previousSpeed) == currentSpeed) - return; - - float initialProgress = timer / (float) currentInstructionDuration; - if(currentInstruct == Instruct.ANGLE) - currentInstructionDuration = getDurationAngle(currentTarget, initialProgress, generatedSpeed.getValue()); - timer = 0; - }*/ - - /*public float runAngle(int angle, int speed) { - generatedSpeed.setValue(angle < 0 ? -speed : speed); - currentInstructionDuration = getDurationAngle(Math.abs(angle), 0, speed); - //currentTarget = angle; - //timer = 0; - - return (float)currentInstructionDuration / 20f; - }*/ - - - - public int getDurationAngle(int deg, float initialProgress, float speed) { + public static int getDurationAngle(int deg, float initialProgress, float speed) { speed = Math.abs(speed); deg = Math.abs(deg); if(speed < 0.1f) @@ -282,7 +242,7 @@ public int getDurationAngle(int deg, float initialProgress, float speed) { return (int) ((1 - initialProgress) * deg / degreesPerTick + 1); } - public int getDurationDistance(int dis, float initialProgress, float speed) { + public static int getDurationDistance(int dis, float initialProgress, float speed) { speed = Math.abs(speed); dis = Math.abs(dis); if(speed < 0.1f) @@ -292,7 +252,6 @@ public int getDurationDistance(int dis, float initialProgress, float speed) { } public boolean setRPM(int rpm) { - //System.out.println("SETSPEED" + rpm); rpm = Math.max(Math.min(rpm, RPM_RANGE), -RPM_RANGE); cc_new_rpm = rpm; cc_update_rpm = true; diff --git a/src/main/java/com/mrh0/createaddition/compat/computercraft/DigitalAdapterPeripheral.java b/src/main/java/com/mrh0/createaddition/compat/computercraft/DigitalAdapterPeripheral.java index 8334b305..90dd432b 100644 --- a/src/main/java/com/mrh0/createaddition/compat/computercraft/DigitalAdapterPeripheral.java +++ b/src/main/java/com/mrh0/createaddition/compat/computercraft/DigitalAdapterPeripheral.java @@ -1,8 +1,10 @@ package com.mrh0.createaddition.compat.computercraft; import com.mrh0.createaddition.blocks.digital_adapter.DigitalAdapterTileEntity; +import com.mrh0.createaddition.blocks.electric_motor.ElectricMotorTileEntity; import com.simibubi.create.foundation.config.AllConfigs; import com.simibubi.create.foundation.config.CServer; +import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.lua.LuaFunction; import dan200.computercraft.api.peripheral.IPeripheral; import net.minecraft.core.Direction; @@ -10,6 +12,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Optional; + public class DigitalAdapterPeripheral implements IPeripheral { private final String type; private final DigitalAdapterTileEntity tileEntity; @@ -50,14 +54,16 @@ public final void clear() { @LuaFunction(mainThread = true) public final void print(String text) { - this.tileEntity.setTextLine(this.tileEntity.getLine(), new TextComponent(text.substring(0, 128))); + this.tileEntity.setTextLine(this.tileEntity.getLine(), new TextComponent(text.substring(0, Math.min(text.length(), 128)))); this.tileEntity.incrementLine(); } - //@LuaFunction(mainThread = true) - //public final void write(String text) { - // this.tileEntity.append(this.tileEntity.getLine(), new TextComponent(text)); - //} + /* + @LuaFunction(mainThread = true) + public final void write(String text) { + this.tileEntity.append(this.tileEntity.getLine(), new TextComponent(text)); + } + */ @LuaFunction(mainThread = true) public final int getLine() { @@ -162,4 +168,14 @@ public final int getBearingAngle(String direction) { if(mp == null) return 0; return (int) mp.getInterpolatedAngle(.5f); } + + @LuaFunction(mainThread = true) + public final float getDurationAngle(int deg, int rpm) throws LuaException { + return ElectricMotorTileEntity.getDurationAngle(deg, 0, rpm) / 20f; + } + + @LuaFunction(mainThread = true) + public final float getDurationDistance(int blocks, int rpm) throws LuaException { + return ElectricMotorTileEntity.getDurationDistance(blocks, 0, rpm) / 20f; + } } diff --git a/src/main/java/com/mrh0/createaddition/compat/computercraft/ElectricMotorPeripheral.java b/src/main/java/com/mrh0/createaddition/compat/computercraft/ElectricMotorPeripheral.java index 69c59a2b..3fb45d33 100644 --- a/src/main/java/com/mrh0/createaddition/compat/computercraft/ElectricMotorPeripheral.java +++ b/src/main/java/com/mrh0/createaddition/compat/computercraft/ElectricMotorPeripheral.java @@ -93,7 +93,7 @@ public final float rotate(int deg, Optional rpm) throws LuaException { int _rpm = rpm.orElse(getSpeed()); if(rpm.isPresent()) setSpeed(deg < 0 ? -_rpm : _rpm); - return tileEntity.getDurationAngle(deg, 0, _rpm) / 20f; + return ElectricMotorTileEntity.getDurationAngle(deg, 0, _rpm) / 20f; } return 0f; } @@ -104,7 +104,7 @@ public final float translate(int blocks, Optional rpm) throws LuaExcept int _rpm = rpm.orElse(getSpeed()); if(rpm.isPresent()) setSpeed(blocks < 0 ? -_rpm : _rpm); - return tileEntity.getDurationDistance(blocks, 0, _rpm) / 20f; + return ElectricMotorTileEntity.getDurationDistance(blocks, 0, _rpm) / 20f; } return 0f; }