From ccf1a8a0e57c1e2504d0ed5e8c7e49b7dcc1a30e Mon Sep 17 00:00:00 2001 From: sychic <47618543+Sychic@users.noreply.github.com> Date: Wed, 8 Nov 2023 12:58:01 -0500 Subject: [PATCH] Implement tick event Currently only end phase (but i think we only use end phase) --- events/build.gradle.kts | 1 + .../skytils/event/mixins/MixinMinecraft.java | 42 +++++++++++++++++ .../src/main/kotlin/gg/skytils/event/Event.kt | 22 +++++++++ .../main/kotlin/gg/skytils/event/Events.kt | 45 +++++++++++++++++++ .../kotlin/gg/skytils/event/impl/TickEvent.kt | 24 ++++++++++ .../main/resources/mixins.skytils-events.json | 10 +++++ .../skytilsmod/mixins/SkytilsMixinPlugin.kt | 3 +- 7 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 events/src/main/java/gg/skytils/event/mixins/MixinMinecraft.java create mode 100644 events/src/main/kotlin/gg/skytils/event/Event.kt create mode 100644 events/src/main/kotlin/gg/skytils/event/Events.kt create mode 100644 events/src/main/kotlin/gg/skytils/event/impl/TickEvent.kt create mode 100644 events/src/main/resources/mixins.skytils-events.json diff --git a/events/build.gradle.kts b/events/build.gradle.kts index 5adcb04f2..862099f5c 100644 --- a/events/build.gradle.kts +++ b/events/build.gradle.kts @@ -22,6 +22,7 @@ plugins { } dependencies { + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.6.4") annotationProcessor("com.github.LlamaLad7:MixinExtras:0.1.1") annotationProcessor("org.spongepowered:mixin:0.8.5:processor") compileOnly("org.spongepowered:mixin:0.8.5") diff --git a/events/src/main/java/gg/skytils/event/mixins/MixinMinecraft.java b/events/src/main/java/gg/skytils/event/mixins/MixinMinecraft.java new file mode 100644 index 000000000..f6c0a01e2 --- /dev/null +++ b/events/src/main/java/gg/skytils/event/mixins/MixinMinecraft.java @@ -0,0 +1,42 @@ +/* + * Skytils - Hypixel Skyblock Quality of Life Mod + * Copyright (C) 2020-2023 Skytils + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package gg.skytils.event.mixins; + +import gg.skytils.event.Events; +import gg.skytils.event.impl.TickEvent; +import net.minecraft.client.Minecraft; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(Minecraft.class) +public class MixinMinecraft { + @Inject( + method = "runTick", + at = @At( + value = "INVOKE", + target = "Lnet/minecraft/profiler/Profiler;endSection()V", + shift = At.Shift.BEFORE + ) + ) + private void tick(CallbackInfo ci) { + Events.INSTANCE.postSync(new TickEvent()); + } +} diff --git a/events/src/main/kotlin/gg/skytils/event/Event.kt b/events/src/main/kotlin/gg/skytils/event/Event.kt new file mode 100644 index 000000000..fd1a90b4f --- /dev/null +++ b/events/src/main/kotlin/gg/skytils/event/Event.kt @@ -0,0 +1,22 @@ +/* + * Skytils - Hypixel Skyblock Quality of Life Mod + * Copyright (C) 2020-2023 Skytils + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package gg.skytils.event + +open class Event { +} \ No newline at end of file diff --git a/events/src/main/kotlin/gg/skytils/event/Events.kt b/events/src/main/kotlin/gg/skytils/event/Events.kt new file mode 100644 index 000000000..0de4015a0 --- /dev/null +++ b/events/src/main/kotlin/gg/skytils/event/Events.kt @@ -0,0 +1,45 @@ +/* + * Skytils - Hypixel Skyblock Quality of Life Mod + * Copyright (C) 2020-2023 Skytils + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package gg.skytils.event + +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.currentCoroutineContext +import kotlinx.coroutines.flow.* +import kotlinx.coroutines.runBlocking + +object Events { + private val _events = MutableSharedFlow() + val events = _events.asSharedFlow() + + suspend fun post(event: T) = + _events.emit(event) + + fun postSync(event: T) = + runBlocking { + post(event) + } + + suspend inline fun on(noinline block: suspend (T) -> Unit) = + events.filterIsInstance().onEach(block).launchIn(CoroutineScope(currentCoroutineContext())) + + inline fun onSync(noinline block: (T) -> Unit) = + runBlocking { + on(block) + } +} \ No newline at end of file diff --git a/events/src/main/kotlin/gg/skytils/event/impl/TickEvent.kt b/events/src/main/kotlin/gg/skytils/event/impl/TickEvent.kt new file mode 100644 index 000000000..dea51dbfd --- /dev/null +++ b/events/src/main/kotlin/gg/skytils/event/impl/TickEvent.kt @@ -0,0 +1,24 @@ +/* + * Skytils - Hypixel Skyblock Quality of Life Mod + * Copyright (C) 2020-2023 Skytils + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package gg.skytils.event.impl + +import gg.skytils.event.Event + +class TickEvent : Event() { +} \ No newline at end of file diff --git a/events/src/main/resources/mixins.skytils-events.json b/events/src/main/resources/mixins.skytils-events.json new file mode 100644 index 000000000..8e361d04a --- /dev/null +++ b/events/src/main/resources/mixins.skytils-events.json @@ -0,0 +1,10 @@ +{ + "compatibilityLevel": "JAVA_8", + "minVersion": "0.8", + "package": "gg.skytils.event.mixins", + "refmap": "mixins.skytils-events.refmap.json", + "client": [ + "MixinMinecraft" + ], + "verbose": true +} \ No newline at end of file diff --git a/src/main/kotlin/gg/skytils/skytilsmod/mixins/SkytilsMixinPlugin.kt b/src/main/kotlin/gg/skytils/skytilsmod/mixins/SkytilsMixinPlugin.kt index 3a96f8bcd..1f86585fe 100644 --- a/src/main/kotlin/gg/skytils/skytilsmod/mixins/SkytilsMixinPlugin.kt +++ b/src/main/kotlin/gg/skytils/skytilsmod/mixins/SkytilsMixinPlugin.kt @@ -26,6 +26,7 @@ import org.spongepowered.asm.mixin.extensibility.IMixinInfo class SkytilsMixinPlugin : IMixinConfigPlugin { val mixinPackage = "gg.skytils.skytilsmod.mixins.transformers" + val eventsPackage = "gg.skytils.events.mixins" var deobfEnvironment = false override fun onLoad(mixinPackage: String) { @@ -39,7 +40,7 @@ class SkytilsMixinPlugin : IMixinConfigPlugin { override fun getRefMapperConfig(): String? = null override fun shouldApplyMixin(targetClassName: String, mixinClassName: String): Boolean { - if (!mixinClassName.startsWith(mixinPackage)) { + if (!mixinClassName.startsWith(mixinPackage) && !mixinClassName.startsWith(eventsPackage)) { println("Woah, how did mixin $mixinClassName for $targetClassName get here?") return false }