aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/events/PacketEvent.kt
blob: 961699a40275488db625fb79887df61a47d24211 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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,
    }
}