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

import moe.nea.ledger.events.BeforeGuiAction
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.common.MinecraftForge
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import org.lwjgl.input.Mouse

class ItemIdProvider {

	@SubscribeEvent
	fun onMouseInput(event: GuiScreenEvent.MouseInputEvent.Pre) {
		if (Mouse.getEventButton() == -1) return
		MinecraftForge.EVENT_BUS.post(BeforeGuiAction(event.gui))
	}

	@SubscribeEvent
	fun onKeyInput(event: GuiScreenEvent.KeyboardInputEvent.Pre) {
		MinecraftForge.EVENT_BUS.post(BeforeGuiAction(event.gui))
	}

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

	@SubscribeEvent
	fun saveInventoryIds(event: BeforeGuiAction) {
		val chest = (event.gui as? GuiChest) ?: return
		val slots = chest.inventorySlots as ContainerChest
		val chestName = slots.lowerChestInventory.name.unformattedString()
		val isOrderMenu = chestName == "Your Bazaar Orders" || chestName == "Co-op Bazaar Orders"
		slots.inventorySlots.forEach {
			val stack = it.stack ?: return@forEach
			val nbt = stack.tagCompound ?: NBTTagCompound()
			val display = nbt.getCompoundTag("display")
			var name = display.getString("Name").unformattedString()
			if (isOrderMenu)
				name = name.removePrefix("BUY ").removePrefix("SELL ")
			name = name.trim()
			val id = stack.getInternalId()
			if (id != null && name.isNotBlank()) {
				knownNames[name] = id
			}
		}
	}

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

}