Skip to content

Commit

Permalink
Implement tick event
Browse files Browse the repository at this point in the history
Currently only end phase (but i think we only use end phase)
  • Loading branch information
Sychic committed Nov 8, 2023
1 parent 91d8ee5 commit ccf1a8a
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 1 deletion.
1 change: 1 addition & 0 deletions events/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
42 changes: 42 additions & 0 deletions events/src/main/java/gg/skytils/event/mixins/MixinMinecraft.java
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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());
}
}
22 changes: 22 additions & 0 deletions events/src/main/kotlin/gg/skytils/event/Event.kt
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

package gg.skytils.event

open class Event {
}
45 changes: 45 additions & 0 deletions events/src/main/kotlin/gg/skytils/event/Events.kt
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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<Event>()
val events = _events.asSharedFlow()

suspend fun <T : Event> post(event: T) =
_events.emit(event)

fun <T : Event> postSync(event: T) =
runBlocking {
post(event)
}

suspend inline fun <reified T : Event> on(noinline block: suspend (T) -> Unit) =
events.filterIsInstance<T>().onEach(block).launchIn(CoroutineScope(currentCoroutineContext()))

inline fun <reified T : Event> onSync(noinline block: (T) -> Unit) =
runBlocking {
on<T>(block)
}
}
24 changes: 24 additions & 0 deletions events/src/main/kotlin/gg/skytils/event/impl/TickEvent.kt
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

package gg.skytils.event.impl

import gg.skytils.event.Event

class TickEvent : Event() {
}
10 changes: 10 additions & 0 deletions events/src/main/resources/mixins.skytils-events.json
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
Expand Down

0 comments on commit ccf1a8a

Please sign in to comment.