aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/test/PacketTest.kt
blob: 7adb36150a74c2a69c3188b17bdc719452c8ecdb (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package at.hannibal2.skyhanni.test

import at.hannibal2.skyhanni.events.PacketEvent
import at.hannibal2.skyhanni.utils.*
import at.hannibal2.skyhanni.utils.LorenzUtils.round
import net.minecraft.client.Minecraft
import net.minecraft.entity.Entity
import net.minecraft.network.Packet
import net.minecraft.network.play.client.C03PacketPlayer
import net.minecraft.network.play.server.*
import net.minecraftforge.fml.common.eventhandler.EventPriority
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

class PacketTest {
    companion object {
        private var enabled = false

        fun toggle() {
            enabled = !enabled
            LorenzUtils.chat("Packet test: $enabled")
        }
    }

    @SubscribeEvent
    fun onSendPacket(event: PacketEvent.SendEvent) {
        if (!enabled) return

        val packet = event.packet
        val packetName = packet.javaClass.simpleName

        if (packetName == "C00PacketKeepAlive") return

        if (packetName == "C0FPacketConfirmTransaction") return
        if (packetName == "C03PacketPlayer") return
        if (packetName == "C04PacketPlayerPosition") return

        if (packetName == "C06PacketPlayerPosLook") return
        if (packetName == "C0BPacketEntityAction") return
        if (packetName == "C05PacketPlayerLook") return
        if (packetName == "C09PacketHeldItemChange") return

        println("Send: $packetName")
    }

    @SubscribeEvent(priority = EventPriority.LOW, receiveCanceled = true)
    fun onChatPacket(event: PacketEvent.ReceiveEvent) {
        if (!enabled) return

        val packet = event.packet
        val packetName = packet.javaClass.simpleName

        // Keep alive
        if (packetName == "S00PacketKeepAlive") return
        if (packetName == "C00PacketKeepAlive") return
        if (packetName == "S32PacketConfirmTransaction") return

        // Gui
        if (packetName == "S3BPacketScoreboardObjective") return
        if (packetName == "S1FPacketSetExperience") return
        if (packetName == "S3EPacketTeams") return
        if (packetName == "S38PacketPlayerListItem") return
        if (packetName == "S3CPacketUpdateScore") return
        if (packetName == "S06PacketUpdateHealth") return

        // Block & World
        if (packetName == "S33PacketUpdateSign") return
        if (packetName == "S22PacketMultiBlockChange") return
        if (packetName == "S03PacketTimeUpdate") return
        if (packetName == "S21PacketChunkData") return
        if (packetName == "S23PacketBlockChange") return

        // Chat
        if (packetName == "S02PacketChat") return

        // Others
        if (packetName == "S29PacketSoundEffect") return
//        if (packetName == "S2APacketParticles") return

        // Entity
        if (packetName == "S13PacketDestroyEntities") return

        if (packetName == "S18PacketEntityTeleport") return
        if (packetName == "S15PacketEntityRelMove") return
        if (packetName == "S04PacketEntityEquipment") return

//        if (packetName == "S0EPacketSpawnObject") return
//        if (packetName == "S0BPacketAnimation") return
//        if (packetName == "S06PacketUpdateHealth") return
//        if (packetName == "S17PacketEntityLookMove") return
//        if (packetName == "S16PacketEntityLook") return
//        if (packetName == "S19PacketEntityHeadLook") return
//        if (packetName == "S1DPacketEntityEffect") return
//        if (packetName == "S12PacketEntityVelocity") return
//        if (packetName == "S19PacketEntityStatus") return
//        if (packetName == "S1CPacketEntityMetadata") return
//        if (packetName == "S20PacketEntityProperties") return
//        if (packetName == "S1BPacketEntityAttach") return


        val id = getEntityId(packet)
        val entity = getEntity(packet, id)
        val distance = getDistance(getLocation(packet, entity))
        if (distance > 10) return

        if (entity != null) {
            if (entity == Minecraft.getMinecraft().thePlayer) {
//                println("own: $distance $packetName")
                return
            } else {
                println("other: $distance")
            }
        } else {
            if (id != null) {
                return
            }


//            if (packetName.contains("")) {
//
//            }
            println("entity is null.")
        }

//        println("distance: $distance")
        println("Receive: $packetName")
        println(" ")
    }

    private fun getDistance(location: LorenzVec?): Double {
        return location?.distance(LocationUtils.playerLocation())?.round(1) ?: 0.0
    }

    private fun getLocation(packet: Packet<*>, entity: Entity?): LorenzVec? {
        if (packet is S2APacketParticles) {
            return LorenzVec(packet.xCoordinate, packet.yCoordinate, packet.zCoordinate)
        }
        if (packet is S0EPacketSpawnObject) {
            return LorenzVec(packet.x, packet.y, packet.z)
        }
        if (packet is S0CPacketSpawnPlayer) {
            return LorenzVec(packet.x, packet.y, packet.z)
        }
        if (packet is C03PacketPlayer) {
            return LorenzVec(packet.positionX, packet.positionY, packet.positionZ)
        }

        if (packet is S0FPacketSpawnMob) {
            return LorenzVec(packet.x, packet.y, packet.z)
        }
        if (packet is S28PacketEffect) {
            return packet.soundPos.toLorenzVec()
        }

        if (entity != null) {
            return entity.getLorenzVec()
        }

        return null
    }

    private fun getEntity(packet: Packet<*>, id: Int?): Entity? {
        val world = Minecraft.getMinecraft().theWorld
        if (packet is S14PacketEntity) {
            return packet.getEntity(world)
        }
        if (packet is S19PacketEntityHeadLook) {
            return packet.getEntity(world)
        }
        if (packet is S19PacketEntityStatus) {
            return packet.getEntity(world)
        }
        if (id != null) {
            return world.getEntityByID(id)
        }

        return null
    }

    private fun getEntityId(packet: Packet<*>) = when (packet) {
        is S1CPacketEntityMetadata -> packet.entityId
        is S20PacketEntityProperties -> packet.entityId
        is S04PacketEntityEquipment -> packet.entityID
        is S12PacketEntityVelocity -> packet.entityID
        is S1BPacketEntityAttach -> packet.entityId
        is S0BPacketAnimation -> packet.entityID
        is S18PacketEntityTeleport -> packet.entityId
        is S1DPacketEntityEffect -> packet.entityId

        else -> null
    }
}