From ae4af78e29efb97d8898d6cf8d1e817357412bb0 Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Sun, 2 Apr 2023 17:27:12 +0200 Subject: Counting items in inventory and from minions to the bingo step helper collection goal display --- .../at/hannibal2/skyhanni/data/InventoryData.kt | 98 ----------------- .../hannibal2/skyhanni/data/OtherInventoryData.kt | 98 +++++++++++++++++ .../at/hannibal2/skyhanni/data/OwnInventoryData.kt | 121 ++++++++++++--------- 3 files changed, 165 insertions(+), 152 deletions(-) delete mode 100644 src/main/java/at/hannibal2/skyhanni/data/InventoryData.kt create mode 100644 src/main/java/at/hannibal2/skyhanni/data/OtherInventoryData.kt (limited to 'src/main/java/at/hannibal2/skyhanni/data') diff --git a/src/main/java/at/hannibal2/skyhanni/data/InventoryData.kt b/src/main/java/at/hannibal2/skyhanni/data/InventoryData.kt deleted file mode 100644 index d3630c159..000000000 --- a/src/main/java/at/hannibal2/skyhanni/data/InventoryData.kt +++ /dev/null @@ -1,98 +0,0 @@ -package at.hannibal2.skyhanni.data - -import at.hannibal2.skyhanni.events.* -import net.minecraft.item.ItemStack -import net.minecraft.network.play.server.S2DPacketOpenWindow -import net.minecraft.network.play.server.S2FPacketSetSlot -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent -import net.minecraftforge.fml.common.gameevent.TickEvent - -class InventoryData { - private var currentInventory: Inventory? = null - private var acceptItems = false - private var lateEvent: LateInventoryOpenEvent? = null - - @SubscribeEvent - fun onCloseWindow(event: GuiContainerEvent.CloseWindowEvent) { - close() - } - - private fun close() { - currentInventory?.let { - InventoryCloseEvent(it).postAndCatch() - currentInventory = null - } - } - - @SubscribeEvent - fun onTick(event: TickEvent.ClientTickEvent) { - if (event.phase != TickEvent.Phase.START) return - lateEvent?.let { - it.postAndCatch() - lateEvent = null - } - } - - @SubscribeEvent - fun onChatPacket(event: PacketEvent.ReceiveEvent) { - val packet = event.packet - - if (packet is S2DPacketOpenWindow) { - val windowId = packet.windowId - val title = packet.windowTitle.unformattedText - val slotCount = packet.slotCount - close() - - currentInventory = Inventory(windowId, title, slotCount) - acceptItems = true - } - - if (packet is S2FPacketSetSlot) { - if (!acceptItems) { - currentInventory?.let { - if (it.windowId != packet.func_149175_c()) return - - val slot = packet.func_149173_d() - if (slot < it.slotCount) { - val itemStack = packet.func_149174_e() - if (itemStack != null) { - it.items[slot] = itemStack - lateEvent = LateInventoryOpenEvent(it) - } - } - } - return - } - currentInventory?.let { - if (it.windowId != packet.func_149175_c()) return - - val slot = packet.func_149173_d() - if (slot < it.slotCount) { - val itemStack = packet.func_149174_e() - if (itemStack != null) { - it.items[slot] = itemStack - } - } else { - done(it) - return - } - - if (it.items.size == it.slotCount) { - done(it) - } - } - } - } - - private fun done(inventory: Inventory) { - InventoryOpenEvent(inventory).postAndCatch() - acceptItems = false - } - - class Inventory( - val windowId: Int, - val title: String, - val slotCount: Int, - val items: MutableMap = mutableMapOf(), - ) -} \ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/data/OtherInventoryData.kt b/src/main/java/at/hannibal2/skyhanni/data/OtherInventoryData.kt new file mode 100644 index 000000000..580383e0c --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/data/OtherInventoryData.kt @@ -0,0 +1,98 @@ +package at.hannibal2.skyhanni.data + +import at.hannibal2.skyhanni.events.* +import net.minecraft.item.ItemStack +import net.minecraft.network.play.server.S2DPacketOpenWindow +import net.minecraft.network.play.server.S2FPacketSetSlot +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import net.minecraftforge.fml.common.gameevent.TickEvent + +class OtherInventoryData { + private var currentInventory: Inventory? = null + private var acceptItems = false + private var lateEvent: LateInventoryOpenEvent? = null + + @SubscribeEvent + fun onCloseWindow(event: GuiContainerEvent.CloseWindowEvent) { + close() + } + + private fun close() { + currentInventory?.let { + InventoryCloseEvent(it).postAndCatch() + currentInventory = null + } + } + + @SubscribeEvent + fun onTick(event: TickEvent.ClientTickEvent) { + if (event.phase != TickEvent.Phase.START) return + lateEvent?.let { + it.postAndCatch() + lateEvent = null + } + } + + @SubscribeEvent + fun onChatPacket(event: PacketEvent.ReceiveEvent) { + val packet = event.packet + + if (packet is S2DPacketOpenWindow) { + val windowId = packet.windowId + val title = packet.windowTitle.unformattedText + val slotCount = packet.slotCount + close() + + currentInventory = Inventory(windowId, title, slotCount) + acceptItems = true + } + + if (packet is S2FPacketSetSlot) { + if (!acceptItems) { + currentInventory?.let { + if (it.windowId != packet.func_149175_c()) return + + val slot = packet.func_149173_d() + if (slot < it.slotCount) { + val itemStack = packet.func_149174_e() + if (itemStack != null) { + it.items[slot] = itemStack + lateEvent = LateInventoryOpenEvent(it) + } + } + } + return + } + currentInventory?.let { + if (it.windowId != packet.func_149175_c()) return + + val slot = packet.func_149173_d() + if (slot < it.slotCount) { + val itemStack = packet.func_149174_e() + if (itemStack != null) { + it.items[slot] = itemStack + } + } else { + done(it) + return + } + + if (it.items.size == it.slotCount) { + done(it) + } + } + } + } + + private fun done(inventory: Inventory) { + InventoryOpenEvent(inventory).postAndCatch() + acceptItems = false + } + + class Inventory( + val windowId: Int, + val title: String, + val slotCount: Int, + val items: MutableMap = mutableMapOf(), + ) +} \ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/data/OwnInventoryData.kt b/src/main/java/at/hannibal2/skyhanni/data/OwnInventoryData.kt index 1572468d8..dd6a37bcd 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/OwnInventoryData.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/OwnInventoryData.kt @@ -1,16 +1,23 @@ package at.hannibal2.skyhanni.data +import at.hannibal2.skyhanni.api.CollectionAPI +import at.hannibal2.skyhanni.events.InventoryCloseEvent import at.hannibal2.skyhanni.events.OwnInventorItemUpdateEvent import at.hannibal2.skyhanni.events.PacketEvent +import at.hannibal2.skyhanni.features.bazaar.BazaarApi +import at.hannibal2.skyhanni.utils.ItemUtils.getInternalName +import at.hannibal2.skyhanni.utils.ItemUtils.name +import at.hannibal2.skyhanni.utils.NEUItems +import net.minecraft.item.ItemStack import net.minecraft.network.play.server.S2FPacketSetSlot +import net.minecraftforge.event.world.WorldEvent import net.minecraftforge.fml.common.eventhandler.EventPriority import net.minecraftforge.fml.common.eventhandler.SubscribeEvent class OwnInventoryData { -// private var itemNames = mutableMapOf() -// private var itemAmount = mutableMapOf() -// private var counter = mutableMapOf() + private var itemNames = mutableMapOf() + private var itemAmount = mutableMapOf() @SubscribeEvent(priority = EventPriority.LOW, receiveCanceled = true) fun onChatPacket(event: PacketEvent.ReceiveEvent) { @@ -22,57 +29,63 @@ class OwnInventoryData { OwnInventorItemUpdateEvent(item).postAndCatch() } } -// if (packet is S2FPacketSetSlot) { -//// println("S2FPacketSetSlot") -// val windowId = packet.func_149175_c() -// val item = packet.func_149174_e() -// val slot = packet.func_149173_d() -// if (windowId != 0) return -// -// val name = item?.name ?: "null" -// -// val oldItem = itemNames.getOrDefault(slot, "null") -// val oldAmount = itemAmount.getOrDefault(slot, 0) -// -//// println(" ") -//// println("windowId: $windowId") -// val amount = item?.stackSize ?: 0 -// if (name == oldItem) { -// if (amount > oldAmount) { -// val diff = amount - oldAmount -//// println("added $diff $name") -// add(name, diff) -// } -// } else { -// if (name != "null") { -//// println("added new $amount $name") -// add(name, amount) -// } -// } -//// println("$slot $oldItem x$oldAmount -> $name x$amount") -// itemNames[slot] = name -// itemAmount[slot] = amount -// } + if (packet is S2FPacketSetSlot) { + val windowId = packet.func_149175_c() + val item = packet.func_149174_e() + val slot = packet.func_149173_d() + if (windowId != 0) return + val name = item?.name ?: "null" + + val oldItem = itemNames.getOrDefault(slot, "null") + val oldAmount = itemAmount.getOrDefault(slot, 0) + + val amount = item?.stackSize ?: 0 + if (name == oldItem) { + val diff = amount - oldAmount + if (amount > oldAmount) { + add(item, diff) + } + } else { + if (name != "null") { + add(item, amount) + } + } + itemNames[slot] = name + itemAmount[slot] = amount + } + } + + @SubscribeEvent + fun onInventoryClose(event: InventoryCloseEvent) { + BazaarApi.inBazaarInventory = false + lastClose = System.currentTimeMillis() + } + + @SubscribeEvent + fun onWorldLoad(event: WorldEvent.Load) { + lastWorldSwitch = System.currentTimeMillis() } -// private fun add(name: String, add: Int) { -// if (name == "§fHay Bale") return -// if (name == "§fSeeds") return -// if (name.contains("Hoe")) return -// -// // TODO remove later -// if (name.contains("Mushroom")) return -// -//// println("added $add $name") -// val old = counter.getOrDefault(name, 0) -//// if (name == "§fWheat") { -//// if (old == 1502) { -//// old = 2504173 -//// } -//// } -// val new = old + add -// val format = LorenzUtils.formatInteger(new) -// println("have $name $format") -// counter[name] = new -// } + private var lastClose = 0L + private var lastWorldSwitch = 0L + + private fun add(item: ItemStack?, add: Int) { + if (item == null) return + + val diffClose = System.currentTimeMillis() - lastClose + if (diffClose < 500) return + + val diffWorld = System.currentTimeMillis() - lastWorldSwitch + if (diffWorld < 3_000) return + + val internalName = item.getInternalName() + val (_, amount) = NEUItems.getMultiplier(internalName) + if (amount > 1) return + + addMultiplier(internalName, add) + } + + private fun addMultiplier(internalName: String, amount: Int) { + CollectionAPI.addFromInventory(internalName, amount) + } } \ No newline at end of file -- cgit