Skip to content

Commit 182718f

Browse files
committed
Add altimeter peripheral
1 parent 8686524 commit 182718f

File tree

3 files changed

+136
-1
lines changed

3 files changed

+136
-1
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 org.valkyrienskies.clockwork.content.curiosities.altmeter.AltMeterBlockEntity
9+
import org.valkyrienskies.mod.api.toJOML
10+
import org.valkyrienskies.mod.common.getLoadedShipManagingPos
11+
12+
class AltMeterPeripheral(private val be: AltMeterBlockEntity): IPeripheral {
13+
@LuaFunction
14+
fun getHeight(): Double {
15+
val ship = be.level.getLoadedShipManagingPos(be.blockPos)
16+
val pos = be.blockPos.center
17+
val height = ship?.transform?.shipToWorld?.transformPosition(pos.toJOML())?.y ?: pos.y
18+
return height
19+
}
20+
21+
@LuaFunction
22+
fun getOutput(): Int = be.signalStrength
23+
24+
@LuaFunction
25+
fun getTargetHeight(): Int = be.triggerHeight
26+
27+
@LuaFunction
28+
fun setTargetHeight(height: Int) {
29+
be.triggerHeight = height
30+
be.notifyUpdate()
31+
}
32+
33+
@LuaFunction
34+
fun getSensitivity(): Int = be.triggerSensitivity
35+
36+
@LuaFunction
37+
fun setSensitivity(sensitivity: Int) {
38+
be.triggerSensitivity = sensitivity
39+
be.notifyUpdate()
40+
}
41+
42+
@LuaFunction
43+
fun getDirection(): String = be.triggerDirection.name
44+
45+
@LuaFunction
46+
fun setDirection(direction: String) {
47+
try {
48+
be.triggerDirection = AltMeterBlockEntity.AltMeterDirection.valueOf(direction.uppercase())
49+
be.notifyUpdate()
50+
} catch (e: IllegalArgumentException) {
51+
throw LuaException(
52+
"Direction must be one of: " +
53+
AltMeterBlockEntity.AltMeterDirection.entries.joinToString(", ") { it.name }
54+
)
55+
}
56+
}
57+
58+
override fun equals(p0: IPeripheral?): Boolean = be.blockPos == (p0 as? AltMeterPeripheral)?.be?.blockPos
59+
override fun getType(): String = "cw_alt_meter"
60+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import net.minecraft.world.level.block.entity.BlockEntityType
77
import org.valkyrienskies.clockwork.ClockworkBlockEntities
88
import org.valkyrienskies.clockwork.content.contraptions.flap.FlapBearingBlockEntity
99
import org.valkyrienskies.clockwork.content.contraptions.phys.bearing.PhysBearingBlockEntity
10+
import org.valkyrienskies.clockwork.content.curiosities.altmeter.AltMeterBlockEntity
1011
import org.valkyrienskies.clockwork.content.logistics.gas.pockets.nozzle.GasNozzleBlockEntity
1112

1213
val PERIPHERALS = mapOf<BlockEntityType<*>, (BlockEntity, Direction?) -> IPeripheral>(
1314
ClockworkBlockEntities.PHYS_BEARING.get() to {be, _ -> PhysBearingPeripheral(be as PhysBearingBlockEntity)},
1415
ClockworkBlockEntities.GAS_NOZZLE.get() to {be, _ -> GasNozzlePeripheral(be as GasNozzleBlockEntity)},
1516
ClockworkBlockEntities.FLAP_BEARING.get() to {be, _ -> FlapBearingPeripheral(be as FlapBearingBlockEntity)},
16-
ClockworkBlockEntities.SMART_FLAP_BEARING.get() to {be, _ -> FlapBearingPeripheral(be as FlapBearingBlockEntity)}
17+
ClockworkBlockEntities.SMART_FLAP_BEARING.get() to {be, _ -> FlapBearingPeripheral(be as FlapBearingBlockEntity)},
18+
ClockworkBlockEntities.ALT_METER.get() to {be, _ -> AltMeterPeripheral(be as AltMeterBlockEntity)}
1719
)
1820

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

peripherals.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,79 @@ Returns `true`, unless the bearing was already disassembled, in which case it wi
394394

395395
---
396396

397+
## Altimeter peripheral
398+
399+
Peripheral type: `cw_alt_meter`
400+
401+
---
402+
403+
```lua
404+
getHeight() -> number
405+
```
406+
407+
Returns the current height the altimeter is at.
408+
If the altimeter is on a ship, the height is automatically transformed from the shipyard into worldspace.
409+
410+
---
411+
412+
```lua
413+
getOutput() -> number
414+
```
415+
416+
Gets the current redstone signal strength the altimeter is outputting, in the range `0..15`.
417+
418+
---
419+
420+
```lua
421+
getTargetHeight() -> number
422+
```
423+
424+
Returns the target height the altimeter is currently set to.
425+
426+
---
427+
428+
```lua
429+
setTargetHeight(height: number)
430+
```
431+
432+
Sets the target height of the altimeter. If `height` has decimals, they will be truncated.
433+
434+
---
435+
436+
```lua
437+
getSensitivity() -> number
438+
```
439+
440+
Returns the sensitivity the altimeter is currently set to.
441+
442+
---
443+
444+
```lua
445+
setSensitivity(sensitivity: number)
446+
```
447+
448+
Sets the sensitivity of the altimeter. If `sensitivity` has decimals, they will be truncated.
449+
450+
---
451+
452+
```lua
453+
getDirection() -> string
454+
```
455+
456+
Returns the current direction mode of the altimeter. Will be one of: `UP`, `DOWN` or `BOTH`.
457+
458+
---
459+
460+
```lua
461+
setDirection(direction: string)
462+
```
463+
464+
Sets the current direction mode of the altimeter. Not uppercase sensitive.
465+
466+
Throws if `direction` is not one of: `UP`, `DOWN` or `BOTH`.
467+
468+
---
469+
397470
This page is still a work in progress.
398471

399472
Spot an error, or want to add extra information?

0 commit comments

Comments
 (0)