From f9cafb766115d5342f7d7e1f8b9e3a56504f0273 Mon Sep 17 00:00:00 2001 From: Brady Date: Sat, 8 Jun 2024 06:48:29 -0230 Subject: Backend: Packet Events (#2025) Co-authored-by: Cal --- .../at/hannibal2/skyhanni/events/PacketEvent.kt | 47 ---------------------- .../events/minecraft/packet/PacketReceivedEvent.kt | 6 +++ .../events/minecraft/packet/PacketSentEvent.kt | 22 ++++++++++ 3 files changed, 28 insertions(+), 47 deletions(-) delete mode 100644 src/main/java/at/hannibal2/skyhanni/events/PacketEvent.kt create mode 100644 src/main/java/at/hannibal2/skyhanni/events/minecraft/packet/PacketReceivedEvent.kt create mode 100644 src/main/java/at/hannibal2/skyhanni/events/minecraft/packet/PacketSentEvent.kt (limited to 'src/main/java/at/hannibal2/skyhanni/events') diff --git a/src/main/java/at/hannibal2/skyhanni/events/PacketEvent.kt b/src/main/java/at/hannibal2/skyhanni/events/PacketEvent.kt deleted file mode 100644 index 961699a40..000000000 --- a/src/main/java/at/hannibal2/skyhanni/events/PacketEvent.kt +++ /dev/null @@ -1,47 +0,0 @@ -package at.hannibal2.skyhanni.events - -import net.minecraft.network.Packet -import net.minecraftforge.fml.common.eventhandler.Cancelable - -@Cancelable -/** - * Note: This event is async and may not be executed on the main minecraft thread. - */ -abstract class PacketEvent : LorenzEvent() { - - abstract val direction: Direction - abstract val packet: Packet<*> - - /** - * Note: This event is async and may not be executed on the main minecraft thread. - */ - data class ReceiveEvent(override val packet: Packet<*>) : PacketEvent() { - - override val direction = Direction.INBOUND - } - - /** - * Note: This event is async and may not be executed on the main minecraft thread. - */ - data class SendEvent(override val packet: Packet<*>) : PacketEvent() { - - override val direction = Direction.OUTBOUND - - fun findOriginatingModCall(skipSkyhanni: Boolean = false): StackTraceElement? { - val nonMinecraftOriginatingStack = Thread.currentThread().stackTrace - // Skip calls before the event is being called - .dropWhile { it.className != "net.minecraft.client.network.NetHandlerPlayClient" } - // Limit the remaining callstack until only the main entrypoint to hide the relauncher - .takeWhile { !it.className.endsWith(".Main") } - // Drop minecraft or skyhanni call frames - .dropWhile { it.className.startsWith("net.minecraft.") || (skipSkyhanni && it.className.startsWith("at.hannibal2.skyhanni.")) } - .firstOrNull() - return nonMinecraftOriginatingStack - } - } - - enum class Direction { - INBOUND, - OUTBOUND, - } -} diff --git a/src/main/java/at/hannibal2/skyhanni/events/minecraft/packet/PacketReceivedEvent.kt b/src/main/java/at/hannibal2/skyhanni/events/minecraft/packet/PacketReceivedEvent.kt new file mode 100644 index 000000000..bbb29acdd --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/events/minecraft/packet/PacketReceivedEvent.kt @@ -0,0 +1,6 @@ +package at.hannibal2.skyhanni.events.minecraft.packet + +import at.hannibal2.skyhanni.api.event.CancellableSkyHanniEvent +import net.minecraft.network.Packet + +class PacketReceivedEvent(val packet: Packet<*>) : CancellableSkyHanniEvent() diff --git a/src/main/java/at/hannibal2/skyhanni/events/minecraft/packet/PacketSentEvent.kt b/src/main/java/at/hannibal2/skyhanni/events/minecraft/packet/PacketSentEvent.kt new file mode 100644 index 000000000..b01bd94fb --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/events/minecraft/packet/PacketSentEvent.kt @@ -0,0 +1,22 @@ +package at.hannibal2.skyhanni.events.minecraft.packet + +import at.hannibal2.skyhanni.api.event.CancellableSkyHanniEvent +import net.minecraft.client.network.NetHandlerPlayClient +import net.minecraft.network.Packet + +class PacketSentEvent(val network: NetHandlerPlayClient, val packet: Packet<*>) : CancellableSkyHanniEvent() { + fun findOriginatingModCall(skipSkyhanni: Boolean = false): StackTraceElement? { + val nonMinecraftOriginatingStack = Thread.currentThread().stackTrace + // Skip calls before the event is being called + .dropWhile { it.className != "net.minecraft.client.network.NetHandlerPlayClient" } + // Limit the remaining callstack until only the main entrypoint to hide the relauncher + .takeWhile { !it.className.endsWith(".Main") } + // Drop minecraft or skyhanni call frames + .dropWhile { + it.className.startsWith("net.minecraft.") || + (skipSkyhanni && it.className.startsWith("at.hannibal2.skyhanni.")) + } + .firstOrNull() + return nonMinecraftOriginatingStack + } +} -- cgit