Replace the useKey method with a get operator function in EventSnowy.kt

This commit is contained in:
Username404 2021-10-27 10:23:16 +02:00
parent 05319d5aeb
commit 5cc5303b32
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
3 changed files with 5 additions and 6 deletions

View File

@ -4,12 +4,12 @@ interface EventSnowy {
val type: String
@SafeVarargs
fun <T> fire(vararg args: T?) {
useKey(type).forEach {
get(type).forEach {
it(args)
}
}
companion object {
fun useKey(key: String): MutableSet<argsLambda> = eventMap.getOrPut(key, ::mutableSetOf)
operator fun get(key: String): MutableSet<argsLambda> = eventMap.getOrPut(key, ::mutableSetOf)
private val eventMap: MutableMap<String, MutableSet<argsLambda>> = mutableMapOf()
}
}

View File

@ -1,6 +1,5 @@
package fr.username404.snowygui
import fr.username404.snowygui.EventSnowy.Companion.useKey
import fr.username404.snowygui.config.Configuration
import fr.username404.snowygui.gui.feature.ButtonImpl
import fr.username404.snowygui.gui.feature.ButtonInfo
@ -20,7 +19,7 @@ abstract class Snowy {
@JvmStatic
protected const val FeaturePackage: String = "fr.username404.snowygui.gui.feature"
lateinit var annotatedButtons: Set<Class<out ButtonImpl>>
fun onEvent(e: Any, lambda: argsLambda) = useKey(e.toString()).add(lambda)
fun onEvent(e: Any, lambda: argsLambda) = EventSnowy[e.toString()].add(lambda)
@JvmField
val logs: Logger = LogManager.getLogger()
}

View File

@ -1,7 +1,7 @@
package fr.username404.snowygui.gui.feature
import com.mojang.blaze3d.vertex.PoseStack
import fr.username404.snowygui.EventSnowy.Companion.useKey
import fr.username404.snowygui.EventSnowy
import fr.username404.snowygui.argsLambda
import fr.username404.snowygui.gui.Renderable
@ -9,7 +9,7 @@ sealed class ButtonImplWithHud: ButtonImpl() {
protected abstract val hudRenderLambda: Renderable
private val generatedLambda: argsLambda = { hudRenderLambda.render(it.first() as PoseStack?) }
final override fun execAction() {
useKey("HudRender").run {
EventSnowy["HudRender"].run {
if (toggled) add(generatedLambda)
else remove(generatedLambda)
}