aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/features/inventory/ItemRarityCosmetics.kt
blob: f3af27d3dba86f0eddb72099ccea1806a2636967 (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
/*
 * SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

package moe.nea.firmament.features.inventory

import java.awt.Color
import net.minecraft.client.gui.DrawContext
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NbtElement
import net.minecraft.util.Formatting
import net.minecraft.util.Identifier
import moe.nea.firmament.events.HotbarItemRenderEvent
import moe.nea.firmament.events.SlotRenderEvents
import moe.nea.firmament.features.FirmamentFeature
import moe.nea.firmament.gui.config.ManagedConfig
import moe.nea.firmament.util.MC

object ItemRarityCosmetics : FirmamentFeature {
    override val identifier: String
        get() = "item-rarity-cosmetics"

    object TConfig : ManagedConfig(identifier) {
        val showItemRarityBackground by toggle("background") { false }
        val showItemRarityInHotbar by toggle("background-hotbar") { false }
    }

    override val config: ManagedConfig
        get() = TConfig

    private val rarityToColor = mapOf(
        "UNCOMMON" to Formatting.GREEN,
        "COMMON" to Formatting.WHITE,
        "RARE" to Formatting.DARK_BLUE,
        "EPIC" to Formatting.DARK_PURPLE,
        "LEGENDARY" to Formatting.GOLD,
        "LEGENJERRY" to Formatting.GOLD,
        "MYTHIC" to Formatting.LIGHT_PURPLE,
        "DIVINE" to Formatting.BLUE,
        "SPECIAL" to Formatting.DARK_RED,
        "SUPREME" to Formatting.DARK_RED,
    ).mapValues {
        val c = Color(it.value.colorValue!!)
        Triple(c.red / 255F, c.green / 255F, c.blue / 255F)
    }
    private val ItemStack.skyblockLoreRarityColor: Triple<Float, Float, Float>?
        get() {
            val lore =
                getOrCreateSubNbt(ItemStack.DISPLAY_KEY).getList(ItemStack.LORE_KEY, NbtElement.STRING_TYPE.toInt())
            val entry = lore.getString(lore.size - 1)
            return rarityToColor.entries.find { (k, v) -> k in entry }?.value
        }


    fun drawItemStackRarity(drawContext: DrawContext, x: Int, y: Int, item: ItemStack) {
        val (r, g, b) = item.skyblockLoreRarityColor ?: return
        drawContext.drawSprite(
            x, y,
            0,
            16, 16,
            MC.guiAtlasManager.getSprite(Identifier("firmament:item_rarity_background")),
            r, g, b, 1F
        )
    }

    override fun onLoad() {
        HotbarItemRenderEvent.subscribe {
            if (!TConfig.showItemRarityInHotbar) return@subscribe
            val stack = it.item
            drawItemStackRarity(it.context, it.x, it.y, stack)
        }
        SlotRenderEvents.Before.subscribe {
            if (!TConfig.showItemRarityBackground) return@subscribe
            val stack = it.slot.stack ?: return@subscribe
            drawItemStackRarity(it.context, it.slot.x, it.slot.y, stack)
        }
    }
}