blob: d2f81a6d8191f71bb5654d9b9e9833a92ad2edf6 (
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
|
package at.hannibal2.skyhanni.data
import at.hannibal2.skyhanni.api.event.HandleEvent
import at.hannibal2.skyhanni.events.EntityHealthUpdateEvent
import at.hannibal2.skyhanni.events.EntityMaxHealthUpdateEvent
import at.hannibal2.skyhanni.events.LorenzTickEvent
import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent
import at.hannibal2.skyhanni.events.SecondPassedEvent
import at.hannibal2.skyhanni.events.entity.EntityDisplayNameEvent
import at.hannibal2.skyhanni.events.entity.EntityHealthDisplayEvent
import at.hannibal2.skyhanni.events.minecraft.packet.PacketReceivedEvent
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.EntityUtils
import at.hannibal2.skyhanni.utils.LorenzUtils.baseMaxHealth
import at.hannibal2.skyhanni.utils.LorenzUtils.derpy
import at.hannibal2.skyhanni.utils.TimeLimitedCache
import net.minecraft.client.entity.EntityOtherPlayerMP
import net.minecraft.client.entity.EntityPlayerSP
import net.minecraft.entity.Entity
import net.minecraft.entity.EntityLivingBase
import net.minecraft.entity.boss.EntityWither
import net.minecraft.entity.item.EntityArmorStand
import net.minecraft.entity.item.EntityItem
import net.minecraft.entity.item.EntityItemFrame
import net.minecraft.entity.item.EntityXPOrb
import net.minecraft.network.play.server.S1CPacketEntityMetadata
import net.minecraft.util.ChatComponentText
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.time.Duration.Companion.milliseconds
@SkyHanniModule
object EntityData {
private val maxHealthMap = mutableMapOf<EntityLivingBase, Int>()
private val nametagCache = TimeLimitedCache<Entity, ChatComponentText>(50.milliseconds)
private val healthDisplayCache = TimeLimitedCache<String, String>(50.milliseconds)
@SubscribeEvent
fun onTick(event: LorenzTickEvent) {
for (entity in EntityUtils.getEntities<EntityLivingBase>()) {
val maxHealth = entity.baseMaxHealth
val oldMaxHealth = maxHealthMap.getOrDefault(entity, -1)
if (oldMaxHealth != maxHealth) {
maxHealthMap[entity] = maxHealth
EntityMaxHealthUpdateEvent(entity, maxHealth.derpy()).postAndCatch()
}
}
}
@SubscribeEvent
fun onSecondPassed(event: SecondPassedEvent) {
if (event.repeatSeconds(30)) {
maxHealthMap.keys.removeIf { it.isDead }
}
}
@SubscribeEvent
fun onWorldChange(event: LorenzWorldChangeEvent) {
maxHealthMap.clear()
}
@HandleEvent
fun onHealthUpdatePacket(event: PacketReceivedEvent) {
val packet = event.packet
if (packet !is S1CPacketEntityMetadata) return
val watchableObjects = packet.func_149376_c() ?: return
val entityId = packet.entityId
val entity = EntityUtils.getEntityByID(entityId) ?: return
if (entity is EntityArmorStand) return
if (entity is EntityXPOrb) return
if (entity is EntityItem) return
if (entity is EntityItemFrame) return
if (entity is EntityOtherPlayerMP) return
if (entity is EntityPlayerSP) return
for (watchableObject in watchableObjects) {
val dataValueId = watchableObject.dataValueId
val any = watchableObject.`object`
if (dataValueId != 6) continue
val health = (any as Float).toInt()
if (entity is EntityWither && health == 300 && entityId < 0) {
return
}
if (entity is EntityLivingBase) {
EntityHealthUpdateEvent(entity, health.derpy()).postAndCatch()
}
}
}
@JvmStatic
fun getDisplayName(entity: Entity, oldValue: ChatComponentText): ChatComponentText {
return postRenderNametag(entity, oldValue)
}
private fun postRenderNametag(entity: Entity, chatComponent: ChatComponentText) = nametagCache.getOrPut(entity) {
val event = EntityDisplayNameEvent(entity, chatComponent)
event.postAndCatch()
event.chatComponent
}
@JvmStatic
fun getHealthDisplay(text: String) = healthDisplayCache.getOrPut(text) {
val event = EntityHealthDisplayEvent(text)
event.postAndCatch()
event.text
}
}
|