aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/test/command/CopyNearbyParticlesCommand.kt
blob: c76d7318ff40ad4a8ff7881b92afa03c635a1e36 (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
package at.hannibal2.skyhanni.test.command

import at.hannibal2.skyhanni.events.PacketEvent
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.LocationUtils
import at.hannibal2.skyhanni.utils.LorenzUtils.round
import at.hannibal2.skyhanni.utils.LorenzVec
import at.hannibal2.skyhanni.utils.OSUtils
import at.hannibal2.skyhanni.utils.toLorenzVec
import net.minecraft.network.play.server.S2APacketParticles
import net.minecraftforge.fml.common.eventhandler.EventPriority
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

// Note: Each particle is copied anywhere between 1-3 times. Different each time. Shouldn't affect using this for debugging or developing
object CopyNearbyParticlesCommand {

    private var searchRadius = 0
    private var saveNextTick = false
    private var searchTime: Long = 0
    private val resultList = mutableListOf<String>()
    private var tickTime: Long = 0
    private var counter = 0

    fun command(args: Array<String>) {
        searchRadius = 10
        if (args.size == 1) {
            searchRadius = args[0].toInt()
        }
        saveNextTick = true
        searchTime = System.currentTimeMillis()
        resultList.clear()
        counter = 0
        tickTime = 0L
    }

    @SubscribeEvent(priority = EventPriority.LOW, receiveCanceled = true)
    fun onChatPacket(event: PacketEvent.ReceiveEvent) {
        if (!saveNextTick) return
        // command was sent in or around a tick so skipping the tick
        if (System.currentTimeMillis() <= searchTime + 5) return

        if (resultList.isEmpty() && tickTime == 0L) tickTime = System.currentTimeMillis()

        if (System.currentTimeMillis() > tickTime + 30) {
            if (counter == 0) ChatUtils.chat("No particles found nearby, try a larger search radius") else {
                val string = resultList.joinToString("\n")
                OSUtils.copyToClipboard(string)
                ChatUtils.chat("$counter particles copied into the clipboard!")
            }
            saveNextTick = false
            return
        }

        val packet = event.packet
        if (packet is S2APacketParticles) {
            val location = packet.toLorenzVec().round(2)
            if (LocationUtils.playerLocation().distance(location) > searchRadius) return
            val offset = LorenzVec(packet.xOffset, packet.yOffset, packet.zOffset).round(2)
            resultList.add("particle type: ${packet.particleType}")
            resultList.add("particle location: $location")
            resultList.add("distance from player: ${LocationUtils.playerLocation().distance(location).round(2)}")
            resultList.add("particle offset: $offset")
            resultList.add("is long distance: ${packet.isLongDistance}")
            resultList.add("particle count: ${packet.particleCount}")
            resultList.add("particle speed: ${packet.particleSpeed}")
            resultList.add("particle arguments: ${packet.particleArgs.asList()}")
            resultList.add("")
            resultList.add("")
            counter++
        }
    }
}