Skip to content

Commit

Permalink
fixed flickering output pins
Browse files Browse the repository at this point in the history
  • Loading branch information
JupiterPi committed Nov 24, 2023
1 parent 676297e commit 20b427f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)),
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<OutputPin>().forEach {
it.writeValue(IO.PinValue.LOW)
it.fulfillValue()
}
pins.filterIsInstance<OutputPin>().forEach { it.writeValue(IO.PinValue.LOW) }
task.cancel()
} else {
invokeTickOrLoop()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<OutputPin>().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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 20b427f

Please sign in to comment.