summaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/garden/pests/PestParticleWaypoint.kt
blob: d3dd8a27ba10cf75698b37b33c3ab35ac182977a (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.features.garden.pests

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.api.event.HandleEvent
import at.hannibal2.skyhanni.data.ClickType
import at.hannibal2.skyhanni.data.IslandType
import at.hannibal2.skyhanni.events.ItemClickEvent
import at.hannibal2.skyhanni.events.LorenzRenderWorldEvent
import at.hannibal2.skyhanni.events.LorenzTickEvent
import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent
import at.hannibal2.skyhanni.events.ReceiveParticleEvent
import at.hannibal2.skyhanni.events.garden.pests.PestUpdateEvent
import at.hannibal2.skyhanni.events.minecraft.packet.PacketReceivedEvent
import at.hannibal2.skyhanni.features.garden.GardenAPI
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.CollectionUtils.editCopy
import at.hannibal2.skyhanni.utils.LocationUtils.distanceToPlayerIgnoreY
import at.hannibal2.skyhanni.utils.LocationUtils.playerLocation
import at.hannibal2.skyhanni.utils.LorenzVec
import at.hannibal2.skyhanni.utils.RenderUtils.draw3DLine
import at.hannibal2.skyhanni.utils.RenderUtils.drawDynamicText
import at.hannibal2.skyhanni.utils.RenderUtils.drawWaypointFilled
import at.hannibal2.skyhanni.utils.RenderUtils.exactPlayerEyeLocation
import at.hannibal2.skyhanni.utils.SimpleTimeMark
import net.minecraft.client.Minecraft
import net.minecraft.network.play.server.S0EPacketSpawnObject
import net.minecraft.util.EnumParticleTypes
import net.minecraftforge.fml.common.eventhandler.EventPriority
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.awt.Color
import kotlin.math.absoluteValue
import kotlin.time.Duration.Companion.seconds

// TODO delete workaround class PestParticleLine when this class works again
@SkyHanniModule
object PestParticleWaypoint {

    private val config get() = SkyHanniMod.feature.garden.pests.pestWaypoint

    private var lastPestTrackerUse = SimpleTimeMark.farPast()

    private var firstParticlePoint: LorenzVec? = null
    private var secondParticlePoint: LorenzVec? = null
    private var lastParticlePoint: LorenzVec? = null
    private var guessPoint: LorenzVec? = null
    private var locations = listOf<LorenzVec>()
    private var particles = 0
    private var lastParticles = 0
    private var isPointingToPest = false
    private var color: Color? = null

    @SubscribeEvent
    fun onItemClick(event: ItemClickEvent) {
        if (!isEnabled()) return
        if (PestAPI.hasVacuumInHand()) {
            if (event.clickType == ClickType.LEFT_CLICK && !Minecraft.getMinecraft().thePlayer.isSneaking) {
                reset()
                lastPestTrackerUse = SimpleTimeMark.now()
            }
        }
    }

    @SubscribeEvent
    fun onWorldChange(event: LorenzWorldChangeEvent) {
        reset()
    }

    private fun reset() {
        lastPestTrackerUse = SimpleTimeMark.farPast()
        locations = emptyList()
        guessPoint = null
        lastParticlePoint = null
        firstParticlePoint = null
        secondParticlePoint = null
        particles = 0
        lastParticles = 0
        isPointingToPest = false
    }

    @SubscribeEvent(priority = EventPriority.LOW, receiveCanceled = true)
    fun onReceiveParticle(event: ReceiveParticleEvent) {
        if (!isEnabled()) return
        if (event.type != EnumParticleTypes.REDSTONE || event.speed != 1f) return

        val darkYellow = LorenzVec(0.0, 0.8, 0.0)
        val yellow = LorenzVec(0.8, 0.8, 0.0)
        val redPest = LorenzVec(0.8, 0.4, 0.0)
        val redPlot = LorenzVec(0.8, 0.0, 0.0)
        isPointingToPest = when (event.offset.round(5)) {
            redPlot -> false
            redPest, yellow, darkYellow -> true
            else -> return
        }

        val location = event.location

        if (config.hideParticles) event.cancel()
        if (lastPestTrackerUse.passedSince() > 3.seconds) return

        if (particles > 5) return
        if (firstParticlePoint == null) {
            if (playerLocation().distance(location) > 5) return
            firstParticlePoint = location
            val (r, g, b) = event.offset.toDoubleArray().map { it.toFloat() }
            color = Color(r, g, b)
        } else if (secondParticlePoint == null) {
            secondParticlePoint = location
            lastParticlePoint = location
            locations = locations.editCopy {
                add(location)
            }
        } else {
            val firstDistance = secondParticlePoint?.let { firstParticlePoint?.distance(it) } ?: return
            val distance = lastParticlePoint?.distance(location) ?: return
            if ((distance - firstDistance).absoluteValue > 0.1) return
            lastParticlePoint = location
            locations = locations.editCopy {
                add(location)
            }
        }
        ++particles
    }

    @HandleEvent(onlyOnIslands = [IslandType.GARDEN])
    fun onFireWorkSpawn(event: PacketReceivedEvent) {
        if (event.packet !is S0EPacketSpawnObject) return
        if (!config.hideParticles) return
        val fireworkId = 76
        if (event.packet.type == fireworkId) event.cancel()
    }

    @SubscribeEvent
    fun onRenderWorld(event: LorenzRenderWorldEvent) {
        if (!isEnabled()) return
        if (locations.isEmpty()) return
        if (lastPestTrackerUse.passedSince() > config.showForSeconds.seconds) {
            reset()
            return
        }

        val waypoint = getWaypoint() ?: return

        val text = if (isPointingToPest) "§aPest Guess" else "§cInfested Plot Guess"
        val color = color ?: error("color is null")

        event.drawWaypointFilled(waypoint, color, beacon = true)
        event.drawDynamicText(waypoint, text, 1.3)
        if (config.drawLine) event.draw3DLine(
            event.exactPlayerEyeLocation(),
            waypoint,
            color,
            3,
            false
        )
    }

    private fun getWaypoint() = if (lastParticles != particles || guessPoint == null) {
        calculateWaypoint()?.also {
            guessPoint = it
            lastParticles = particles
        }
    } else guessPoint

    @SubscribeEvent
    fun onTick(event: LorenzTickEvent) {
        if (!isEnabled()) return
        val guessPoint = guessPoint ?: return

        if (guessPoint.distanceToPlayerIgnoreY() > 8) return
        if (isPointingToPest && lastPestTrackerUse.passedSince() !in 1.seconds..config.showForSeconds.seconds) return
        reset()
    }

    @HandleEvent
    fun onPestUpdate(event: PestUpdateEvent) {
        if (PestAPI.scoreboardPests == 0) reset()
    }

    private fun calculateWaypoint(): LorenzVec? {
        val firstParticle = firstParticlePoint ?: return null
        val list = locations.toList()
        var pos = LorenzVec(0.0, 0.0, 0.0)
        for ((i, particle) in list.withIndex()) {
            pos += (particle - firstParticle) / (i.toDouble() + 1.0)
        }
        return firstParticle + pos * (120.0 / list.size)
    }

    fun isEnabled() = GardenAPI.inGarden() && config.enabled

}