aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt')
-rw-r--r--src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt67
1 files changed, 50 insertions, 17 deletions
diff --git a/src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt b/src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt
index 43f2cb7..f582015 100644
--- a/src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt
+++ b/src/main/kotlin/moe/nea/ledger/ItemIdProvider.kt
@@ -1,11 +1,12 @@
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.client.Minecraft
+import net.minecraft.item.ItemStack
import net.minecraft.nbt.NBTTagCompound
import net.minecraftforge.client.event.GuiScreenEvent
import net.minecraftforge.common.MinecraftForge
+import net.minecraftforge.fml.common.eventhandler.EventPriority
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import org.lwjgl.input.Mouse
@@ -24,24 +25,39 @@ class ItemIdProvider {
private val knownNames = mutableMapOf<String, ItemId>()
- @SubscribeEvent
- fun saveInventoryIds(event: BeforeGuiAction) {
- val chest = (event.gui as? GuiChest) ?: return
- val slots = chest.inventorySlots as ContainerChest
+ @SubscribeEvent(priority = EventPriority.HIGH)
+ fun savePlayerInventoryIds(event: BeforeGuiAction) {
+ val player = Minecraft.getMinecraft().thePlayer ?: return
+ val inventory = player.inventory ?: return
+ inventory.mainInventory?.forEach { saveFromSlot(it) }
+ inventory.armorInventory?.forEach { saveFromSlot(it) }
+ }
+
+ fun saveFromSlot(stack: ItemStack?, preprocessName: (String) -> String = { it }) {
+ if (stack == null) return
+ val nbt = stack.tagCompound ?: NBTTagCompound()
+ val display = nbt.getCompoundTag("display")
+ var name = display.getString("Name").unformattedString()
+ name = preprocessName(name)
+ name = name.trim()
+ val id = stack.getInternalId()
+ if (id != null && name.isNotBlank()) {
+ knownNames[name] = id
+ }
+ }
+
+ @SubscribeEvent(priority = EventPriority.HIGH)
+ fun saveChestInventoryIds(event: BeforeGuiAction) {
+ val slots = event.chestSlots ?: return
val chestName = slots.lowerChestInventory.name.unformattedString()
val isOrderMenu = chestName == "Your Bazaar Orders" || chestName == "Co-op Bazaar Orders"
+ val preprocessor: (String) -> String = if (isOrderMenu) {
+ { it.removePrefix("BUY ").removePrefix("SELL ") }
+ } else {
+ { it }
+ }
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
- }
+ saveFromSlot(it?.stack, preprocessor)
}
}
@@ -49,4 +65,21 @@ class ItemIdProvider {
return knownNames[name]
}
+ private val coinRegex = "(?<amount>$SHORT_NUMBER_PATTERN) Coins?".toPattern()
+ private val stackedItem = "(?<name>.*) x(?<count>$SHORT_NUMBER_PATTERN)".toPattern()
+
+ fun findFromLore(name: String): Pair<ItemId, Double>? {
+ val properName = name.unformattedString()
+ coinRegex.useMatcher(properName) {
+ return Pair(ItemId.COINS, parseShortNumber(group("amount")))
+ }
+ stackedItem.useMatcher(properName) {
+ val item = findForName(group("name"))
+ if (item != null) {
+ val count = parseShortNumber(group("count"))
+ return Pair(item, count)
+ }
+ }
+ return findForName(properName)?.let { Pair(it, 1.0) }
+ }
} \ No newline at end of file