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

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.test.GriffinJavaUtils
import at.hannibal2.skyhanni.utils.EntityUtils.getNameTagWith
import at.hannibal2.skyhanni.utils.ItemUtils.getSkullTexture
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.LorenzVec
import at.hannibal2.skyhanni.utils.RenderUtils.drawString
import at.hannibal2.skyhanni.utils.getLorenzVec
import net.minecraft.client.Minecraft
import net.minecraft.entity.EntityLiving
import net.minecraft.entity.item.EntityArmorStand
import net.minecraftforge.client.event.RenderWorldLastEvent
import net.minecraftforge.event.world.WorldEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.gameevent.TickEvent

class SummoningSoulsName {

    var tick = 0
    private val texture =
        "ewogICJ0aW1lc3RhbXAiIDogMTYwMTQ3OTI2NjczMywKICAicHJvZmlsZUlkIiA6ICJmMzA1ZjA5NDI0NTg0ZjU" +
                "4YmEyYjY0ZjAyZDcyNDYyYyIsCiAgInByb2ZpbGVOYW1lIiA6ICJqcm9ja2EzMyIsCiAgInNpZ25hdH" +
                "VyZVJlcXVpcmVkIiA6IHRydWUsCiAgInRleHR1cmVzIiA6IHsKICAgICJTS0lOIiA6IHsKICAgICAgI" +
                "nVybCIgOiAiaHR0cDovL3RleHR1cmVzLm1pbmVjcmFmdC5uZXQvdGV4dHVyZS81YWY0MDM1ZWMwZGMx" +
                "NjkxNzc4ZDVlOTU4NDAxNzAyMjdlYjllM2UyOTQzYmVhODUzOTI5Y2U5MjNjNTk4OWFkIgogICAgfQogIH0KfQ"

    private val souls = mutableMapOf<EntityArmorStand, String>()
    private val mobsLastLocation = mutableMapOf<EntityLiving, LorenzVec>()
    private val mobsName = mutableMapOf<EntityLiving, String>()

    @SubscribeEvent
    fun onTick(event: TickEvent.ClientTickEvent) {
        if (!isEnabled()) return

        tick++
        //TODO use packets instead of this
        if (tick % 1 == 0) {
            check()
        }
    }

    private fun check() {
        val minecraft = Minecraft.getMinecraft()
        val world = minecraft.theWorld
        for (entity in world.loadedEntityList) {
            if (souls.contains(entity)) continue

            if (entity is EntityArmorStand) {
                if (isSoul(entity)) {
                    val soulLocation = entity.getLorenzVec()

                    val map = mutableMapOf<EntityLiving, Double>()
                    for ((mob, loc) in mobsLastLocation) {
                        val distance = loc.distance(soulLocation)
                        map[mob] = distance
                    }

                    val nearestMob = GriffinJavaUtils.sortByValueAsc(map).firstNotNullOfOrNull { it.key }
                    if (nearestMob != null) {
//                        val mobDistance = nearestMob.getLorenzVec().add(0.0, -1.4375, 0.0)
//                        val distance = mobDistance.distance(soulLocation)
//                        val diff = mobDistance.add(soulLocation.multiply(-1))

//                        println(" ")
//                        println("mobDistance: $mobDistance")
//                        println("soulLocation: $soulLocation")
//                        println("diff: $diff")
//                        LorenzUtils.chat("distance: $distance")
                        val name = mobsName[nearestMob]!!
//                        LorenzUtils.chat("maybe its $name")
                        souls[entity] = name
                    }

                }
            }
        }

        for (entity in world.loadedEntityList) {
            if (entity is EntityLiving) {
                val consumer = entity.getNameTagWith(2, "§c❤")
                if (consumer != null) {
                    if (!consumer.name.contains("§e0")) {
                        mobsLastLocation[entity] = entity.getLorenzVec()
                        mobsName[entity] = consumer.name
                    }
                }
            }
        }

        souls.keys.removeIf { it !in world.loadedEntityList }
        //TODO fix overhead!
//        mobs.keys.removeIf { it !in world.loadedEntityList }
    }

    @SubscribeEvent
    fun onWorldRender(event: RenderWorldLastEvent) {
        if (!isEnabled()) return

        for ((entity, name) in souls) {
            val vec = entity.getLorenzVec()
            event.drawString(vec.add(0.0, 2.5, 0.0), name)
        }
    }

    @SubscribeEvent
    fun onWorldChange(event: WorldEvent.Load) {
        souls.clear()
        mobsLastLocation.clear()
        mobsName.clear()
    }

    private fun isSoul(entity: EntityArmorStand): Boolean {
        for (stack in entity.inventory) {
            if (stack != null) {
                val skullTexture = stack.getSkullTexture()
                if (skullTexture != null) {
                    if (skullTexture == texture) {
                        return true
                    }
                }
            }
        }

        return false
    }

    private fun isEnabled(): Boolean {
        return LorenzUtils.inSkyblock && SkyHanniMod.feature.misc.summonSoulDisplay
    }
}