aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt
blob: 8ad8d26abc02b5dc6067dd8c480c8c108378caec (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
package moe.nea.ledger

import net.minecraft.client.gui.GuiScreen
import net.minecraft.client.gui.inventory.GuiChest
import net.minecraft.inventory.ContainerChest
import net.minecraft.nbt.NBTTagCompound
import net.minecraftforge.client.event.GuiScreenEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

class ItemIdProvider {

    @SubscribeEvent
    fun onMouseInput(event: GuiScreenEvent.MouseInputEvent.Pre) {
        saveInventoryIds(event.gui)
    }

    @SubscribeEvent
    fun onKeyInput(event: GuiScreenEvent.KeyboardInputEvent.Pre) {
        saveInventoryIds(event.gui)
    }

    private val knownNames = mutableMapOf<String, String>()

    fun saveInventoryIds(gui: GuiScreen) {
        val chest = (gui as? GuiChest) ?: return
        val slots = chest.inventorySlots as ContainerChest
        slots.inventorySlots.forEach {
            val stack = it.stack ?: return@forEach
            val nbt = stack.tagCompound ?: NBTTagCompound()
            val display = nbt.getCompoundTag("display")
            val name = display.getString("Name").unformattedString()
            val extraAttributes = nbt.getCompoundTag("ExtraAttributes")
            val id = extraAttributes.getString("id")
            if (id.isNotBlank() && name.isNotBlank()) {
                knownNames[name] = id
            }
        }
    }

    fun findForName(name: String): String? {
        return knownNames[name]
    }

}