Skip to content

Commit 20b427f

Browse files
committed
fixed flickering output pins
1 parent 676297e commit 20b427f

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

cranberri-server-plugin/src/main/kotlin/jupiterpi/cranberri/CranberriPlugin.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import jupiterpi.cranberri.runtime.compilation.ProjectCompiler
44
import jupiterpi.cranberri.tools.computerToolListener
55
import jupiterpi.cranberri.tools.loggerToolListener
66
import jupiterpi.cranberri.tools.toolsCommand
7+
import jupiterpi.cranberri.util.DATA_ROOT
8+
import jupiterpi.cranberri.util.TextFile
79
import net.kyori.adventure.text.Component
810
import net.kyori.adventure.text.JoinConfiguration
911
import net.kyori.adventure.text.format.Style
@@ -15,8 +17,6 @@ import org.bukkit.event.EventHandler
1517
import org.bukkit.event.Listener
1618
import org.bukkit.event.player.PlayerJoinEvent
1719
import org.bukkit.plugin.java.JavaPlugin
18-
import jupiterpi.cranberri.util.DATA_ROOT
19-
import jupiterpi.cranberri.util.TextFile
2020

2121
val cranberriLettering = Component.join(JoinConfiguration.noSeparators(),
2222
Component.text("Cranberri", Style.style(TextColor.color(Color.WHITE.asRGB()), TextDecoration.BOLD)),
@@ -42,6 +42,7 @@ class CranberriPlugin : JavaPlugin(), Listener {
4242
getCommand("cranberri")!!.setExecutor(toolsCommand)
4343
Bukkit.getPluginManager().registerEvents(loggerToolListener, this)
4444
Bukkit.getPluginManager().registerEvents(computerToolListener, this)
45+
Bukkit.getPluginManager().registerEvents(outputPinListener, this)
4546

4647
// computers
4748

cranberri-server-plugin/src/main/kotlin/jupiterpi/cranberri/RunningScript.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ open class RunningScript(private val computer: Computer, val script: Script) {
4949
protected open fun startScript(invokeTickOrLoop: () -> Unit) {
5050
Bukkit.getScheduler().runTaskTimer(plugin, { task ->
5151
if (shutdown) {
52-
pins.filterIsInstance<OutputPin>().forEach {
53-
it.writeValue(IO.PinValue.LOW)
54-
it.fulfillValue()
55-
}
52+
pins.filterIsInstance<OutputPin>().forEach { it.writeValue(IO.PinValue.LOW) }
5653
task.cancel()
5754
} else {
5855
invokeTickOrLoop()

cranberri-server-plugin/src/main/kotlin/jupiterpi/cranberri/computer-pins.kt

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import jupiterpi.cranberri.runtime.api.IO
44
import org.bukkit.Location
55
import org.bukkit.Material
66
import org.bukkit.block.data.type.Repeater
7+
import org.bukkit.event.EventHandler
8+
import org.bukkit.event.Listener
9+
import org.bukkit.event.block.BlockRedstoneEvent
710
import org.bukkit.util.Vector
811

912
val PIN_BASE_MATERIAL = Material.GLASS
@@ -14,19 +17,31 @@ open class Pin(
1417
)
1518

1619
class OutputPin(location: Location) : Pin(location) {
17-
private var value = IO.PinValue.LOW
20+
var value = IO.PinValue.LOW
1821

1922
fun writeValue(value: IO.PinValue) {
2023
this.value = value
21-
}
22-
23-
fun fulfillValue() {
2424
location.block.blockData = (location.block.blockData as Repeater).also {
2525
it.isPowered = value.toBoolean()
2626
}
2727
}
2828
}
2929

30+
val outputPinListener = object : Listener {
31+
@EventHandler
32+
@Suppress("unused")
33+
fun onRedstone(event: BlockRedstoneEvent) {
34+
if (event.block.type == Material.REPEATER) {
35+
for (computer in Computers.computers) {
36+
if (computer.runningScript == null) continue
37+
val pin = computer.runningScript!!.pins.filterIsInstance<OutputPin>().singleOrNull { it.location == event.block.location } ?: continue
38+
event.newCurrent = if (pin.value == IO.PinValue.HIGH) 15 else 0
39+
break
40+
}
41+
}
42+
}
43+
}
44+
3045
class InputPin(location: Location) : Pin(location) {
3146
fun readValue(): IO.PinValue {
3247
return IO.PinValue.fromBoolean((location.block.blockData as Repeater).isPowered)

cranberri-server-plugin/src/main/kotlin/jupiterpi/cranberri/computers.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import org.bukkit.event.block.Action
1414
import org.bukkit.event.block.BlockBreakEvent
1515
import org.bukkit.event.player.PlayerInteractEvent
1616
import org.bukkit.util.Vector
17-
import java.util.Date
17+
import java.util.*
1818

1919
val COMPUTER_MATERIAL = Material.TARGET
2020

@@ -95,12 +95,6 @@ object Computers {
9595
return Computer(block.location).also { computers += it }
9696
}
9797

98-
init {
99-
Bukkit.getScheduler().runTaskTimer(plugin, { _ ->
100-
computers.forEach { it.runningScript?.pins?.forEach { if (it is OutputPin) it.fulfillValue() } }
101-
}, 0, 1)
102-
}
103-
10498
// persistence
10599

106100
private const val PERSISTENCE_FILE = "$DATA_ROOT/computers.csv"

0 commit comments

Comments
 (0)