aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/dulkirmod/features/ReaperDisplay.kt
blob: c595775b9bc7bdd8ad0c076ac5f18b310fcb29a6 (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
package dulkirmod.features

import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound
import net.minecraftforge.client.event.sound.PlaySoundEvent
import net.minecraftforge.event.world.WorldEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable
import kotlin.math.min

object ReaperDisplay {

    private var lastReaperUsage = 0L
    private val reaperRegex = "(REAPER_CHESTPLATE)|(REAPER_LEGGINGS)|(REAPER_BOOTS)".toRegex()

    fun shouldDisplay(stack: ItemStack, cir: CallbackInfoReturnable<Boolean>) {
        if (!isReaper(stack)) return
        cir.returnValue = System.currentTimeMillis() - lastReaperUsage < 25000
    }

    fun calcDurability(stack: ItemStack, cir: CallbackInfoReturnable<Double>) {
        if (!isReaper(stack)) return
        val time = (System.currentTimeMillis() - lastReaperUsage) / 25000.0
        cir.returnValue = min(1.0, 1.0 - time)
    }

    @SubscribeEvent
    fun onSound(event: PlaySoundEvent) {
        if (event.name != "mob.zombie.remedy") return
        if (event.sound.pitch != 1.0f) return
        if (event.sound.volume != .5f) return
        lastReaperUsage = System.currentTimeMillis()
    }

    @SubscribeEvent
    fun onWorldLoad(event: WorldEvent.Load) {
        lastReaperUsage = 0L
    }

    private fun isReaper(stack: ItemStack): Boolean {
        if (stack.hasTagCompound()) {
            val tag: NBTTagCompound = stack.tagCompound
            if (tag.hasKey("ExtraAttributes", 10) && tag.hasKey("display", 10)) {
                val ea: NBTTagCompound = tag.getCompoundTag("ExtraAttributes")
                if (ea.hasKey("id", 8)) {
                    val id = ea.getString("id")
                    return id matches reaperRegex
                }
            }
        }
        return false
    }
}