Skip to content

Commit e513891

Browse files
committed
Add flap bearing peripheral
1 parent 68c30d3 commit e513891

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
lines changed

common/src/main/kotlin/org/valkyrienskies/clockwork/ClockworkConfig.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,5 +177,8 @@ object ClockworkConfig {
177177

178178
@ConfigEntry(description = "Maximum amount of blocks a smart flap bearing can assemble", min = 0.0, max = Int.MAX_VALUE.toDouble())
179179
var smartFlapBearingMaxSize = 24
180+
181+
@ConfigEntry(description = "Whether the (smart) flap bearing peripheral can use setAngle without rotational power")
182+
var cheatFlapBearingPeripheral = false
180183
}
181184
}

common/src/main/kotlin/org/valkyrienskies/clockwork/content/contraptions/flap/FlapBearingBlockEntity.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ open class FlapBearingBlockEntity(type: BlockEntityType<*>?, pos: BlockPos, stat
4141
private var lastPower: Int = 0
4242
private var currentPower: Int = 0
4343

44+
/**
45+
* Only used by the CC peripheral so that it can set the angle manually
46+
* without it resetting / being meddled with by redstone
47+
*/
48+
var isLocked = false
4449

4550
val angularSpeed: Double
4651
get() {
@@ -64,6 +69,9 @@ open class FlapBearingBlockEntity(type: BlockEntityType<*>?, pos: BlockPos, stat
6469
assemble()
6570
}
6671

72+
// Don't update from redstone if we're locked
73+
if (isLocked) return
74+
6775
lastPower = currentPower
6876
currentPower = getPower()
6977

@@ -80,6 +88,7 @@ open class FlapBearingBlockEntity(type: BlockEntityType<*>?, pos: BlockPos, stat
8088
bearingAngle.setValue(tag.getFloat("BearingAngle").toDouble())
8189
bearingAngle.chase(tag.getFloat("TargetAngle").toDouble(), tag.getDouble("AngularSpeed"), chaser)
8290
isRunning = tag.getBoolean("IsRunning")
91+
isLocked = tag.getBoolean("IsLocked")
8392

8493
lastException = AssemblyException.read(tag)
8594

@@ -91,6 +100,7 @@ open class FlapBearingBlockEntity(type: BlockEntityType<*>?, pos: BlockPos, stat
91100
tag.putFloat("TargetAngle",bearingAngle.chaseTarget)
92101
tag.putBoolean("IsRunning",isRunning)
93102
tag.putDouble("AngularSpeed",angularSpeed)
103+
tag.putBoolean("IsLocked", isLocked)
94104

95105
AssemblyException.write(tag,lastAssemblyException)
96106
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
@file:Suppress("unused")
2+
3+
package org.valkyrienskies.clockwork.integration.cc
4+
5+
import dan200.computercraft.api.lua.LuaException
6+
import dan200.computercraft.api.lua.LuaFunction
7+
import dan200.computercraft.api.peripheral.IPeripheral
8+
import net.createmod.catnip.animation.LerpedFloat
9+
import org.valkyrienskies.clockwork.ClockworkBlocks
10+
import org.valkyrienskies.clockwork.ClockworkConfig
11+
import org.valkyrienskies.clockwork.ClockworkMod
12+
import org.valkyrienskies.clockwork.content.contraptions.flap.FlapBearingBlockEntity
13+
import org.valkyrienskies.clockwork.content.contraptions.flap.smart_flap.SmartFlapBearingBlockEntity
14+
import org.valkyrienskies.clockwork.content.logistics.gas.pockets.nozzle.GasNozzleBlockEntity
15+
import java.util.Optional
16+
17+
class FlapBearingPeripheral(private val be: FlapBearingBlockEntity): IPeripheral {
18+
19+
@LuaFunction
20+
fun isSmart(): Boolean = be is SmartFlapBearingBlockEntity
21+
22+
@LuaFunction
23+
fun getAngle(): Float = be.getInterpolatedAngle(0.0f)
24+
25+
// Note: CC does not accept Float arguments, only Doubles.
26+
// CC also doesn't understand kotlin optional parameters,
27+
// so we use a Java Optional instead.
28+
@LuaFunction
29+
fun setAngle(angle: Double, setLocked: Optional<Boolean>) {
30+
if (be !is SmartFlapBearingBlockEntity) {
31+
throw LuaException("setAngle can only be used on a smart flap bearing")
32+
}
33+
34+
if (!(be.angularSpeed > 0) && !ClockworkConfig.SERVER.cheatFlapBearingPeripheral) {
35+
throw LuaException("Flap bearing is not connected to power")
36+
}
37+
38+
if (!be.isRunning) {
39+
throw LuaException("Flap bearing is not assembled")
40+
}
41+
42+
if (setLocked.isPresent) {
43+
be.isLocked = setLocked.get()
44+
} else {
45+
be.isLocked = true
46+
}
47+
48+
be.setAngle(angle.toFloat())
49+
}
50+
51+
@LuaFunction
52+
fun isLocked(): Boolean = be.isLocked
53+
54+
@LuaFunction
55+
fun setLocked(locked: Boolean) {
56+
be.isLocked = locked
57+
}
58+
59+
@LuaFunction(mainThread = true)
60+
fun isRunning(): Boolean = be.isRunning
61+
62+
@LuaFunction(mainThread = true)
63+
fun assemble(): Boolean {
64+
if (be.isRunning) return false
65+
be.assemble()
66+
return true
67+
}
68+
69+
@LuaFunction(mainThread = true)
70+
fun disassemble(): Boolean {
71+
if (!be.isRunning) return false
72+
be.disassemble()
73+
return true
74+
}
75+
76+
override fun equals(p0: IPeripheral?): Boolean = be.blockPos == (p0 as? FlapBearingPeripheral)?.be?.blockPos
77+
override fun getType(): String = "cw_flap_bearing"
78+
}

common/src/main/kotlin/org/valkyrienskies/clockwork/integration/cc/getPeripheralCommon.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import net.minecraft.core.Direction
55
import net.minecraft.world.level.block.entity.BlockEntity
66
import net.minecraft.world.level.block.entity.BlockEntityType
77
import org.valkyrienskies.clockwork.ClockworkBlockEntities
8+
import org.valkyrienskies.clockwork.content.contraptions.flap.FlapBearingBlockEntity
89
import org.valkyrienskies.clockwork.content.contraptions.phys.bearing.PhysBearingBlockEntity
910
import org.valkyrienskies.clockwork.content.logistics.gas.pockets.nozzle.GasNozzleBlockEntity
1011

1112
val PERIPHERALS = mapOf<BlockEntityType<*>, (BlockEntity, Direction?) -> IPeripheral>(
1213
ClockworkBlockEntities.PHYS_BEARING.get() to {be, _ -> PhysBearingPeripheral(be as PhysBearingBlockEntity)},
13-
ClockworkBlockEntities.GAS_NOZZLE.get() to {be, _ -> GasNozzlePeripheral(be as GasNozzleBlockEntity)}
14+
ClockworkBlockEntities.GAS_NOZZLE.get() to {be, _ -> GasNozzlePeripheral(be as GasNozzleBlockEntity)},
15+
ClockworkBlockEntities.FLAP_BEARING.get() to {be, _ -> FlapBearingPeripheral(be as FlapBearingBlockEntity)},
16+
ClockworkBlockEntities.SMART_FLAP_BEARING.get() to {be, _ -> FlapBearingPeripheral(be as FlapBearingBlockEntity)}
1417
)
1518

1619
fun getPeripheralCommon(be: BlockEntity, direction: Direction?): IPeripheral? = PERIPHERALS[be.type]?.invoke(be, direction)

0 commit comments

Comments
 (0)