From 3e19b97b52fae7422f8b0654da9341bf8c2393cb Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Thu, 15 Feb 2024 18:41:10 +0100 Subject: Added item ids --- src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt | 44 ++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt (limited to 'src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt') diff --git a/src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt b/src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt new file mode 100644 index 0000000..8ad8d26 --- /dev/null +++ b/src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt @@ -0,0 +1,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() + + 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] + } + +} \ No newline at end of file -- cgit