diff --git a/cranberri-server-plugin/src/main/kotlin/jupiterpi/cranberri/CranberriPlugin.kt b/cranberri-server-plugin/src/main/kotlin/jupiterpi/cranberri/CranberriPlugin.kt index 2587eb7..1737e7d 100644 --- a/cranberri-server-plugin/src/main/kotlin/jupiterpi/cranberri/CranberriPlugin.kt +++ b/cranberri-server-plugin/src/main/kotlin/jupiterpi/cranberri/CranberriPlugin.kt @@ -4,6 +4,8 @@ import jupiterpi.cranberri.runtime.compilation.ProjectCompiler import jupiterpi.cranberri.tools.computerToolListener import jupiterpi.cranberri.tools.loggerToolListener import jupiterpi.cranberri.tools.toolsCommand +import jupiterpi.cranberri.util.DATA_ROOT +import jupiterpi.cranberri.util.TextFile import net.kyori.adventure.text.Component import net.kyori.adventure.text.JoinConfiguration import net.kyori.adventure.text.format.Style @@ -15,8 +17,6 @@ import org.bukkit.event.EventHandler import org.bukkit.event.Listener import org.bukkit.event.player.PlayerJoinEvent import org.bukkit.plugin.java.JavaPlugin -import jupiterpi.cranberri.util.DATA_ROOT -import jupiterpi.cranberri.util.TextFile val cranberriLettering = Component.join(JoinConfiguration.noSeparators(), Component.text("Cranberri", Style.style(TextColor.color(Color.WHITE.asRGB()), TextDecoration.BOLD)), @@ -42,6 +42,7 @@ class CranberriPlugin : JavaPlugin(), Listener { getCommand("cranberri")!!.setExecutor(toolsCommand) Bukkit.getPluginManager().registerEvents(loggerToolListener, this) Bukkit.getPluginManager().registerEvents(computerToolListener, this) + Bukkit.getPluginManager().registerEvents(outputPinListener, this) // computers diff --git a/cranberri-server-plugin/src/main/kotlin/jupiterpi/cranberri/RunningScript.kt b/cranberri-server-plugin/src/main/kotlin/jupiterpi/cranberri/RunningScript.kt index 8355e67..46cf017 100644 --- a/cranberri-server-plugin/src/main/kotlin/jupiterpi/cranberri/RunningScript.kt +++ b/cranberri-server-plugin/src/main/kotlin/jupiterpi/cranberri/RunningScript.kt @@ -49,10 +49,7 @@ open class RunningScript(private val computer: Computer, val script: Script) { protected open fun startScript(invokeTickOrLoop: () -> Unit) { Bukkit.getScheduler().runTaskTimer(plugin, { task -> if (shutdown) { - pins.filterIsInstance().forEach { - it.writeValue(IO.PinValue.LOW) - it.fulfillValue() - } + pins.filterIsInstance().forEach { it.writeValue(IO.PinValue.LOW) } task.cancel() } else { invokeTickOrLoop() diff --git a/cranberri-server-plugin/src/main/kotlin/jupiterpi/cranberri/computer-pins.kt b/cranberri-server-plugin/src/main/kotlin/jupiterpi/cranberri/computer-pins.kt index d011ee0..e54626f 100644 --- a/cranberri-server-plugin/src/main/kotlin/jupiterpi/cranberri/computer-pins.kt +++ b/cranberri-server-plugin/src/main/kotlin/jupiterpi/cranberri/computer-pins.kt @@ -4,6 +4,9 @@ import jupiterpi.cranberri.runtime.api.IO import org.bukkit.Location import org.bukkit.Material import org.bukkit.block.data.type.Repeater +import org.bukkit.event.EventHandler +import org.bukkit.event.Listener +import org.bukkit.event.block.BlockRedstoneEvent import org.bukkit.util.Vector val PIN_BASE_MATERIAL = Material.GLASS @@ -14,19 +17,31 @@ open class Pin( ) class OutputPin(location: Location) : Pin(location) { - private var value = IO.PinValue.LOW + var value = IO.PinValue.LOW fun writeValue(value: IO.PinValue) { this.value = value - } - - fun fulfillValue() { location.block.blockData = (location.block.blockData as Repeater).also { it.isPowered = value.toBoolean() } } } +val outputPinListener = object : Listener { + @EventHandler + @Suppress("unused") + fun onRedstone(event: BlockRedstoneEvent) { + if (event.block.type == Material.REPEATER) { + for (computer in Computers.computers) { + if (computer.runningScript == null) continue + val pin = computer.runningScript!!.pins.filterIsInstance().singleOrNull { it.location == event.block.location } ?: continue + event.newCurrent = if (pin.value == IO.PinValue.HIGH) 15 else 0 + break + } + } + } +} + class InputPin(location: Location) : Pin(location) { fun readValue(): IO.PinValue { return IO.PinValue.fromBoolean((location.block.blockData as Repeater).isPowered) diff --git a/cranberri-server-plugin/src/main/kotlin/jupiterpi/cranberri/computers.kt b/cranberri-server-plugin/src/main/kotlin/jupiterpi/cranberri/computers.kt index 5d52d6a..9a2dce8 100644 --- a/cranberri-server-plugin/src/main/kotlin/jupiterpi/cranberri/computers.kt +++ b/cranberri-server-plugin/src/main/kotlin/jupiterpi/cranberri/computers.kt @@ -14,7 +14,7 @@ import org.bukkit.event.block.Action import org.bukkit.event.block.BlockBreakEvent import org.bukkit.event.player.PlayerInteractEvent import org.bukkit.util.Vector -import java.util.Date +import java.util.* val COMPUTER_MATERIAL = Material.TARGET @@ -95,12 +95,6 @@ object Computers { return Computer(block.location).also { computers += it } } - init { - Bukkit.getScheduler().runTaskTimer(plugin, { _ -> - computers.forEach { it.runningScript?.pins?.forEach { if (it is OutputPin) it.fulfillValue() } } - }, 0, 1) - } - // persistence private const val PERSISTENCE_FILE = "$DATA_ROOT/computers.csv"