aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/test/PacketTest.kt
blob: 3653e01fa554bd9fe0881bd7142d32ab97743ccc (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package at.hannibal2.skyhanni.test

import at.hannibal2.skyhanni.events.PacketEvent
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.EntityUtils
import at.hannibal2.skyhanni.utils.LocationUtils.distanceToPlayer
import at.hannibal2.skyhanni.utils.LorenzUtils.round
import at.hannibal2.skyhanni.utils.LorenzVec
import at.hannibal2.skyhanni.utils.NumberUtil.isInt
import at.hannibal2.skyhanni.utils.ReflectionUtils.makeAccessible
import at.hannibal2.skyhanni.utils.getLorenzVec
import at.hannibal2.skyhanni.utils.toLorenzVec
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.S04PacketEntityEquipment
import net.minecraft.network.play.server.S0BPacketAnimation
import net.minecraft.network.play.server.S0CPacketSpawnPlayer
import net.minecraft.network.play.server.S0EPacketSpawnObject
import net.minecraft.network.play.server.S0FPacketSpawnMob
import net.minecraft.network.play.server.S12PacketEntityVelocity
import net.minecraft.network.play.server.S13PacketDestroyEntities
import net.minecraft.network.play.server.S14PacketEntity
import net.minecraft.network.play.server.S18PacketEntityTeleport
import net.minecraft.network.play.server.S19PacketEntityHeadLook
import net.minecraft.network.play.server.S19PacketEntityStatus
import net.minecraft.network.play.server.S1BPacketEntityAttach
import net.minecraft.network.play.server.S1CPacketEntityMetadata
import net.minecraft.network.play.server.S1DPacketEntityEffect
import net.minecraft.network.play.server.S20PacketEntityProperties
import net.minecraft.network.play.server.S28PacketEffect
import net.minecraft.network.play.server.S2APacketParticles
import net.minecraftforge.fml.common.eventhandler.EventPriority
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

object PacketTest {

    private var enabled = false
    private var full = false

    private val entityMap = mutableMapOf<Int, MutableList<Packet<*>>>()

    fun command(args: Array<String>) {
        if (args.size == 1 && args[0].isInt()) {
            sendEntityPacketData(args[0].toInt())
            return
        }
        if (args.size == 1 && (args[0] == "full" || args[0] == "all")) {
            full = !full
            ChatUtils.chat("Packet test full: $full")
            return
        }

        toggle()
    }

    private fun sendEntityPacketData(id: Int) {
        ChatUtils.chat("Packet Entity Data: $id")
        entityMap[id]?.forEach {
            it.print()
        }
        println("End of Data")
    }

    private fun toggle() {
        enabled = !enabled
        ChatUtils.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 onPacketReceive(event: PacketEvent.ReceiveEvent) {
        if (!enabled) return
        val packet = event.packet
        packet.print()
        if (packet is S13PacketDestroyEntities) {
            packet.entityIDs.forEach {
                entityMap.getOrDefault(it, mutableListOf()).add(packet)
            }
        } else {
            val id = packet.getEntityId() ?: return
            entityMap.getOrDefault(id, mutableListOf()).add(packet)
        }
    }

    private fun Packet<*>.print() {
        val packetName = 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 (!full && packetName == "S2APacketParticles") return

        // Entity
        if (this is S13PacketDestroyEntities) {
            println("Receive: $packetName with IDs: ${entityIDs.joinToString(", ")}")
            return
        }

        if (!full) {
            if (packetName == "S18PacketEntityTeleport") return
            if (packetName == "S15PacketEntityRelMove") return
            if (packetName == "S04PacketEntityEquipment") return
            if (packetName == "S17PacketEntityLookMove") return
            if (packetName == "S19PacketEntityHeadLook") return
            if (packetName == "S16PacketEntityLook") return
            if (packetName == "S12PacketEntityVelocity") return
            if (packetName == "S1CPacketEntityMetadata") return
            if (packetName == "S20PacketEntityProperties") return
            if (packetName == "S0BPacketAnimation") return
        }

//        if (packetName == "S0EPacketSpawnObject") return
//        if (packetName == "S06PacketUpdateHealth") return
//        if (packetName == "S1DPacketEntityEffect") return
//        if (packetName == "S19PacketEntityStatus") return
//        if (packetName == "S1BPacketEntityAttach") return

        buildString {
            append("Receive: $packetName")

            val id = getEntityId()
            if (id != null) {
                append(" ID: $id")
            }

            val entity = getEntity(this@print, id)
            val distance = getDistance(getLocation(this@print, entity))

            if (entity != null) {
                if (entity == Minecraft.getMinecraft().thePlayer) {
                    append(" own")
                    return@buildString
                } else {
                    append(" distance: $distance other")
                }
            } else {
                if (id == null) {
                    return@buildString
                }
                append(" entity is null.")
            }
        }.let { println(it) }
    }

    private fun getDistance(location: LorenzVec?): Double {
        return location?.distanceToPlayer()?.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 EntityUtils.getEntityByID(id)
        }

        return null
    }

    private fun Packet<*>.getEntityId() = when (this) {
        is S1CPacketEntityMetadata -> entityId
        is S20PacketEntityProperties -> entityId
        is S04PacketEntityEquipment -> entityID
        is S12PacketEntityVelocity -> entityID
        is S1BPacketEntityAttach -> entityId
        is S0BPacketAnimation -> entityID
        is S18PacketEntityTeleport -> entityId
        is S1DPacketEntityEffect -> entityId
        is S0CPacketSpawnPlayer -> entityID
        is S0FPacketSpawnMob -> entityID
        is S0EPacketSpawnObject -> entityID
        is S19PacketEntityHeadLook -> javaClass.getDeclaredField("entityId").makeAccessible().get(this) as Int
        is S19PacketEntityStatus -> javaClass.getDeclaredField("entityId").makeAccessible().get(this) as Int
        /* is S14PacketEntity.S15PacketEntityRelMove -> packet.javaClass.getDeclaredField("entityId").makeAccessible().get(packet) as Int
        is S14PacketEntity.S16PacketEntityLook -> packet.javaClass.getDeclaredField("entityId").makeAccessible().get(packet) as Int
        is S14PacketEntity.S17PacketEntityLookMove -> packet.javaClass.getDeclaredField("entityId").makeAccessible().get(packet) as Int */
        else -> null
    }
}