aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/com/dulkirfabric/features/CooldownDisplays.kt
blob: e562a2eeda641651d5263ad4e6db3a3a2bf36730 (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
package com.dulkirfabric.features

import com.dulkirfabric.config.DulkirConfig
import com.dulkirfabric.events.PlaySoundEvent
import com.dulkirfabric.events.WorldLoadEvent
import com.dulkirfabric.util.SoundInfo
import com.dulkirfabric.util.TrackedCooldown
import com.dulkirfabric.util.Utils
import meteordevelopment.orbit.EventHandler
import net.minecraft.client.sound.Sound
import net.minecraft.component.DataComponentTypes
import net.minecraft.item.ItemStack
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable
import kotlin.math.round

object CooldownDisplays {
    private val trackedCooldowns: Map<SoundInfo, TrackedCooldown> = mapOf(
        Pair(
            SoundInfo("entity.zombie_villager.cure", 1f, .5f),
            TrackedCooldown("(REAPER_CHESTPLATE)|(REAPER_LEGGINGS)|(REAPER_BOOTS)".toRegex(), 25000, 0)
        ),
        Pair(
            SoundInfo("entity.zombie_villager.cure", 0.6984127f, 1f),
            TrackedCooldown("(HYPERION|ASTRAEA|SCYLLA|VALKYRIE)".toRegex(), 5000, 0)
        ),
        Pair(
            SoundInfo("entity.firework_rocket.launch", 1f, 3f),
            TrackedCooldown("SOS_FLARE".toRegex(), 20000, 0)
        ),
        Pair(
            SoundInfo("block.lever.click", 2f, .55f),
            TrackedCooldown("RAGNAROCK_AXE".toRegex(), 20000, 0)
        ),
        Pair(
            SoundInfo("entity.generic.drink", 1.7936507f, 1f),
            TrackedCooldown("HOLY_ICE".toRegex(), 4000, 0)
        ),
        Pair(
            SoundInfo("entity.wolf.howl", 1.5238096f, .5f),
            TrackedCooldown("WEIRDER_TUBA".toRegex(), 20000, 0)
        ),
        Pair(
            SoundInfo("block.lava.pop", 2f, .4f),
            TrackedCooldown("ROGUE_SWORD".toRegex(), 30000, 0)
        ),
        Pair(
            SoundInfo("block.anvil.land", 0.4920635f, 1f),
            TrackedCooldown("GIANTS_SWORD".toRegex(), 30000, 0)
        )
    )

    private var lastRagTick: Long = 0

    fun shouldDisplay(stack: ItemStack, cir: CallbackInfoReturnable<Boolean>) {
        val cooldown = fetchCooldownItem(stack) ?: return
        cir.returnValue = System.currentTimeMillis() - cooldown.lastUsage < cooldown.cooldownDuration
    }

    fun calcDurability(stack: ItemStack, cir: CallbackInfoReturnable<Int>) {
        val cooldown = fetchCooldownItem(stack) ?: return
        cir.returnValue = round(13f * (System.currentTimeMillis() - cooldown.lastUsage) / cooldown.cooldownDuration).toInt()
    }
    @EventHandler
    fun onSound(event: PlaySoundEvent) {
        if (!DulkirConfig.configOptions.duraCooldown) return
        val path = event.sound.id.path
        val pitch = event.sound.pitch
        val volume = event.sound.volume

        // Figure out if we have a match in trackedCooldowns
        val matchResult = trackedCooldowns[SoundInfo(path, pitch, volume)] ?: return

        if (matchResult.itemID matches "RAGNAROCK_AXE") {
            if (System.currentTimeMillis() - lastRagTick < 5000) {
                lastRagTick = System.currentTimeMillis()
                return
            } else {
                lastRagTick = System.currentTimeMillis()
            }
        }
        matchResult.lastUsage = System.currentTimeMillis()
    }

    @EventHandler
    fun onWorldLoad(event: WorldLoadEvent) {
        if (!DulkirConfig.configOptions.duraCooldown) return
        trackedCooldowns.forEach {
            it.value.lastUsage = 0
        }
    }

    private fun fetchCooldownItem(stack: ItemStack): TrackedCooldown? {
        val tag = stack.get(DataComponentTypes.CUSTOM_DATA)?.nbt ?: return null
        val id = tag.get("id") ?: return null
        val idStr = id.toString().trim('"')
        trackedCooldowns.forEach {
            if (idStr matches it.value.itemID)
                return it.value
        }
        return null
    }
}