Skip to content

Commit aaa0d5a

Browse files
committed
Add propeller bearing peripheral
Note: blade angle doesn't update properly on the blade controller while its assembled. I think data isn't being synced to the client. But thats to fix later, the peripheral still works, you just can't tell from the visual.
1 parent 036f836 commit aaa0d5a

File tree

4 files changed

+169
-2
lines changed

4 files changed

+169
-2
lines changed

common/src/main/kotlin/org/valkyrienskies/clockwork/content/contraptions/propeller/PropellerBearingBlockEntity.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ open class PropellerBearingBlockEntity(type: BlockEntityType<*>, pos: BlockPos,
9898
var lastPowerOne = 0
9999
var lastPowerTwo = 0
100100

101+
/**
102+
* Only used by the CC peripheral so that it can set the blade angle manually
103+
* without it resetting / being meddled with by redstone
104+
*/
105+
var isLocked = false
106+
101107
override fun newCreateData(): PropCreateData {
102108
return PropCreateData(worldPosition.toJOML(), blockState.getValue(BlockStateProperties.FACING).normal.toJOMLD(), angle, currentOmega, ArrayList(sailPositions), isInverted(), active, brass && blades.isEmpty(), ArrayList(blades))
103109
}
@@ -269,7 +275,7 @@ open class PropellerBearingBlockEntity(type: BlockEntityType<*>, pos: BlockPos,
269275
}
270276
// reminder: override this for copter bearing since their redstone controls something different
271277
open fun applyPowerEffect() {
272-
if (!this.brass || blades.isEmpty() || (powerOne == 0 && powerTwo == 0)) return
278+
if (!this.brass || blades.isEmpty() || (powerOne == 0 && powerTwo == 0) || isLocked) return
273279

274280
val powerEffect = Mth.clamp((powerOne + powerTwo).toFloat() / 30f, -1f, 1f)
275281
val angleChange = 2f * powerEffect
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 org.valkyrienskies.clockwork.ClockworkBlocks
9+
import org.valkyrienskies.clockwork.content.contraptions.propeller.PropellerBearingBlockEntity
10+
import java.util.*
11+
12+
class PropellerBearingPeripheral(private val be: PropellerBearingBlockEntity): IPeripheral {
13+
14+
@LuaFunction
15+
fun isBrass(): Boolean = be.blockState.block == ClockworkBlocks.BRASS_PROPELLER_BEARING
16+
17+
@LuaFunction
18+
fun getAngle(): Float = be.getInterpolatedAngle(0.0f)
19+
20+
@LuaFunction
21+
fun getBladeAngle(): Double = be.blades.first().angle
22+
23+
// Note: CC does not accept Float arguments, only Doubles.
24+
// CC also doesn't understand kotlin optional parameters,
25+
// so we use a Java Optional instead.
26+
@LuaFunction
27+
fun setBladeAngle(angle: Double, setLocked: Optional<Boolean>) {
28+
if (be.blockState.block == ClockworkBlocks.BRASS_PROPELLER_BEARING) {
29+
throw LuaException("setBladeAngle can only be used on a brass propeller bearing")
30+
}
31+
32+
if (!be.running) {
33+
throw LuaException("Propeller bearing is not assembled")
34+
}
35+
36+
if ((angle < -180) || (angle > 180)) {
37+
throw LuaException("Angle must be within range -180..180")
38+
}
39+
40+
if (setLocked.isPresent) {
41+
be.isLocked = setLocked.get()
42+
} else {
43+
be.isLocked = true
44+
}
45+
46+
be.setNewBladeAngle(angle)
47+
be.blades.clear()
48+
be.getBlades()
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.running
61+
62+
@LuaFunction(mainThread = true)
63+
fun assemble(): Boolean {
64+
if (be.running) return false
65+
be.assemble()
66+
return true
67+
}
68+
69+
@LuaFunction(mainThread = true)
70+
fun disassemble(): Boolean {
71+
if (!be.running) return false
72+
be.disassemble()
73+
return true
74+
}
75+
76+
override fun equals(p0: IPeripheral?): Boolean = be.level?.getBlockState(be.blockPos)?.`is`(ClockworkBlocks.BRASS_PROPELLER_BEARING.get()) == true
77+
override fun getType(): String = "cw_prop_bearing"
78+
}

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,6 +7,7 @@ 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.contraptions.propeller.PropellerBearingBlockEntity
1011
import org.valkyrienskies.clockwork.content.curiosities.altmeter.AltMeterBlockEntity
1112
import org.valkyrienskies.clockwork.content.logistics.gas.pockets.nozzle.GasNozzleBlockEntity
1213

@@ -15,7 +16,8 @@ val PERIPHERALS = mapOf<BlockEntityType<*>, (BlockEntity, Direction?) -> IPeriph
1516
ClockworkBlockEntities.GAS_NOZZLE.get() to {be, _ -> GasNozzlePeripheral(be as GasNozzleBlockEntity)},
1617
ClockworkBlockEntities.FLAP_BEARING.get() to {be, _ -> FlapBearingPeripheral(be as FlapBearingBlockEntity)},
1718
ClockworkBlockEntities.SMART_FLAP_BEARING.get() to {be, _ -> FlapBearingPeripheral(be as FlapBearingBlockEntity)},
18-
ClockworkBlockEntities.ALT_METER.get() to {be, _ -> AltMeterPeripheral(be as AltMeterBlockEntity)}
19+
ClockworkBlockEntities.ALT_METER.get() to {be, _ -> AltMeterPeripheral(be as AltMeterBlockEntity)},
20+
ClockworkBlockEntities.PROPELLER_BEARING.get() to {be, _ -> PropellerBearingPeripheral(be as PropellerBearingBlockEntity)}
1921
)
2022

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

peripherals.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,87 @@ Returns `true`, unless the bearing was already disassembled, in which case it wi
396396

397397
---
398398

399+
## Propeller Bearing peripheral
400+
401+
Peripheral type: `cw_prop_bearing`
402+
403+
---
404+
405+
```lua
406+
isBrass() -> bool
407+
```
408+
409+
Whether the propeller bearing is a brass propeller bearing (false if it is a jury rigged bearing)
410+
411+
---
412+
413+
```lua
414+
getAngle() -> number
415+
```
416+
417+
The current angle the bearing is at. Will be within the range `-360 .. 360`
418+
419+
---
420+
421+
```lua
422+
setBladeAngle(angle: number, locked: bool | nil)
423+
```
424+
425+
Sets the blade angle of any blade controllers on the bearing. `angle` must be in the range `-180 .. 180`.
426+
427+
If `locked` is not specified, the bearing will be "locked"
428+
and no longer respond to any redstone input (only computer input) until `setLocked` is used.
429+
If `locked` is specified as `false`, the bearing will not be locked, and will respond to redstone input, but will also reset every tick.
430+
431+
Throws if:
432+
- The peripheral is not a brass propeller bearing
433+
- The propeller bearing is not assembled
434+
435+
---
436+
437+
```lua
438+
isLocked() -> bool
439+
```
440+
441+
Whether the propeller bearing is currently locked, and will only respond to computer input (not redstone).
442+
443+
---
444+
445+
```lua
446+
setLocked(locked: bool)
447+
```
448+
449+
Set the propeller bearing `locked` to true/false. If true, the propeller bearing will only respond to computer blade angle input.
450+
If false, the propeller bearing can accept redstone _or_ computer input, but will reset to the redstone input each tick.
451+
452+
---
453+
454+
```lua
455+
isRunning() -> bool
456+
```
457+
458+
Is the propeller bearing currently assembled
459+
460+
---
461+
462+
```lua
463+
assemble() -> bool
464+
```
465+
466+
Assembles the propeller bearing.
467+
Returns `true`, unless the bearing was already assembled, in which case it will return `false`
468+
469+
---
470+
471+
```lua
472+
disassemble() -> bool
473+
```
474+
475+
Disassembles the propeller bearing.
476+
Returns `true`, unless the bearing was already disassembled, in which case it will return `false`
477+
478+
---
479+
399480
## Altimeter peripheral
400481

401482
Peripheral type: `cw_alt_meter`

0 commit comments

Comments
 (0)