aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/mixins/hooks/RenderLivingEntityHelper.kt
blob: 036c756270b9bf8256b10f528dba04c9ce40c5f6 (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
package at.hannibal2.skyhanni.mixins.hooks

import at.hannibal2.skyhanni.events.RenderMobColoredEvent
import at.hannibal2.skyhanni.events.ResetEntityHurtEvent
import net.minecraft.entity.EntityLivingBase
import net.minecraftforge.event.world.WorldEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

class RenderLivingEntityHelper {

    @SubscribeEvent
    fun onWorldChange(event: WorldEvent.Load) {
        entityColorMap.clear()
        entityColorCondition.clear()

        entityNoHurTime.clear()
        entityNoHurTimeCondition.clear()
    }

    companion object {
        private val entityColorMap = mutableMapOf<EntityLivingBase, Int>()
        private val entityColorCondition = mutableMapOf<EntityLivingBase, () -> Boolean>()

        private val entityNoHurTime = mutableListOf<EntityLivingBase>()
        private val entityNoHurTimeCondition = mutableMapOf<EntityLivingBase, () -> Boolean>()

        fun <T : EntityLivingBase> setEntityColor(entity: T, color: Int, condition: () -> Boolean) {
            entityColorMap[entity] = color
            entityColorCondition[entity] = condition
        }

        fun <T : EntityLivingBase> setNoHurtTime(entity: T, condition: () -> Boolean) {
            entityNoHurTime.add(entity)
            entityNoHurTimeCondition[entity] = condition
        }

        fun <T : EntityLivingBase> setColorMultiplier(entity: T): Int {
            if (entityColorMap.containsKey(entity)) {
                val condition = entityColorCondition[entity]!!
                if (condition.invoke()) {
                    return entityColorMap[entity]!!
                }
            }

            //TODO remove event
            val event = RenderMobColoredEvent(entity, 0)
            event.postAndCatch()
            return event.color
        }

        fun <T : EntityLivingBase> changeHurtTime(entity: T): Int {
            if (entityNoHurTime.contains(entity)) {
                val condition = entityNoHurTimeCondition[entity]!!
                if (condition.invoke()) {
                    return 0
                }
            }

            //TODO remove event
            val event = ResetEntityHurtEvent(entity, false)
            event.postAndCatch()
            return if (event.shouldReset) 0 else entity.hurtTime
        }
    }
}