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

import at.hannibal2.skyhanni.events.EntityHealthUpdateEvent
import at.hannibal2.skyhanni.events.PacketEvent
import at.hannibal2.skyhanni.utils.LorenzUtils
import net.minecraft.client.Minecraft
import net.minecraft.entity.EntityLivingBase
import net.minecraft.network.play.server.S1CPacketEntityMetadata
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

class EntityData {

    @SubscribeEvent
    fun onHealthUpdatePacket(event: PacketEvent.ReceiveEvent) {
        val packet = event.packet
        if (packet !is S1CPacketEntityMetadata) return

        if (packet == null) {
            LorenzUtils.debug("packet is null in EntityData!")
            return
        }

        val watchableObjects = packet.func_149376_c() ?: return
        for (watchableObject in watchableObjects) {
            if (watchableObject.dataValueId != 6) continue

            val theWorld = Minecraft.getMinecraft().theWorld
            if (theWorld == null) {
                LorenzUtils.debug("theWorld is null in EntityData!")
                continue
            }
            val entityId = packet.entityId
            if (entityId == null) {
                LorenzUtils.debug("entityId is null in EntityData!")
                continue
            }

            val entity = theWorld.getEntityByID(entityId) ?: continue
            if (entity !is EntityLivingBase) continue

            val health = watchableObject.`object` as Float
            EntityHealthUpdateEvent(entity, health).postAndCatch()
            return
        }
    }
}