aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/data/EntityMovementData.kt
blob: d38b6824b86484e10e410474b51a3e9418fe999b (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
package at.hannibal2.skyhanni.data

import at.hannibal2.skyhanni.events.EntityMoveEvent
import at.hannibal2.skyhanni.events.IslandChangeEvent
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.events.LorenzTickEvent
import at.hannibal2.skyhanni.events.LorenzWarpEvent
import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.DelayedRun
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.LorenzUtils.isInIsland
import at.hannibal2.skyhanni.utils.LorenzVec
import at.hannibal2.skyhanni.utils.RegexUtils.matches
import at.hannibal2.skyhanni.utils.SimpleTimeMark
import at.hannibal2.skyhanni.utils.getLorenzVec
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import net.minecraft.client.Minecraft
import net.minecraft.entity.Entity
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds

@SkyHanniModule
object EntityMovementData {

    private val warpingPattern by RepoPattern.pattern(
        "data.entity.warping",
        "§7(?:Warping|Warping you to your SkyBlock island|Warping using transfer token|Finding player|Sending a visit request)\\.\\.\\.",
    )

    private var nextTeleport: OnNextTeleport? = null

    fun onNextTeleport(island: IslandType, action: () -> Unit) {
        nextTeleport = OnNextTeleport(island, action)
    }

    class OnNextTeleport(val island: IslandType, val action: () -> Unit) {
        val startTime: SimpleTimeMark = SimpleTimeMark.now()
    }

    private val entityLocation = mutableMapOf<Entity, LorenzVec>()

    fun addToTrack(entity: Entity) {
        if (entity !in entityLocation) {
            entityLocation[entity] = entity.getLorenzVec()
        }
    }

    @SubscribeEvent
    fun onIslandChange(event: IslandChangeEvent) {
        val nextData = nextTeleport ?: return
        if (nextData.island != event.newIsland) return
        val passedSince = nextData.startTime.passedSince()
        if (passedSince > 5.seconds) {
            nextTeleport = null
            return
        }

        DelayedRun.runDelayed(100.milliseconds) {
            nextData.action()
        }
        nextTeleport = null
    }

    @SubscribeEvent
    fun onPlayerMove(event: EntityMoveEvent) {
        if (!LorenzUtils.inSkyBlock || event.entity != Minecraft.getMinecraft().thePlayer) return

        val nextData = nextTeleport ?: return

        val passedSince = nextData.startTime.passedSince()
        if (passedSince > 5.seconds) {
            nextTeleport = null
            return
        }
        if (passedSince > 50.milliseconds && nextData.island.isInIsland()) {
            nextData.action()
            nextTeleport = null
            return
        }
    }

    @SubscribeEvent
    fun onTick(event: LorenzTickEvent) {
        if (!LorenzUtils.inSkyBlock) return
        addToTrack(Minecraft.getMinecraft().thePlayer)

        for (entity in entityLocation.keys) {
            if (entity.isDead) continue

            val newLocation = entity.getLorenzVec()
            val oldLocation = entityLocation[entity]!!
            val distance = newLocation.distance(oldLocation)
            if (distance > 0.01) {
                entityLocation[entity] = newLocation
                EntityMoveEvent(entity, oldLocation, newLocation, distance).postAndCatch()
            }
        }
    }

    @SubscribeEvent
    fun onChat(event: LorenzChatEvent) {
        if (!LorenzUtils.inSkyBlock) return
        if (!warpingPattern.matches(event.message)) return
        DelayedRun.runNextTick {
            LorenzWarpEvent().postAndCatch()
        }
    }

    @SubscribeEvent
    fun onWorldChange(event: LorenzWorldChangeEvent) {
        entityLocation.clear()
    }
}