From 07351036391533533cd059ef6910c6bc1efeb36f Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Thu, 16 Feb 2023 20:31:04 +0100 Subject: Show copper to coin prices inside the Sky Mart inventory. --- .../at/hannibal2/skyhanni/data/InventoryData.kt | 72 ++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/main/java/at/hannibal2/skyhanni/data/InventoryData.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 new file mode 100644 index 000000000..3495f2177 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/data/InventoryData.kt @@ -0,0 +1,72 @@ +package at.hannibal2.skyhanni.data + +import at.hannibal2.skyhanni.events.GuiContainerEvent +import at.hannibal2.skyhanni.events.InventoryCloseEvent +import at.hannibal2.skyhanni.events.InventoryOpenEvent +import at.hannibal2.skyhanni.events.PacketEvent +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 + +class InventoryData { + private var currentInventory: Inventory? = null + + @SubscribeEvent + fun onCloseWindow(event: GuiContainerEvent.CloseWindowEvent) { + close() + } + + private fun close() { + currentInventory?.let { + InventoryCloseEvent(it).postAndCatch() + currentInventory = 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) + } + + if (packet is S2FPacketSetSlot) { + 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() + } + + class Inventory( + val windowId: Int, + val title: String, + val slotCount: Int, + val items: MutableMap = mutableMapOf() + ) +} \ No newline at end of file -- cgit