diff options
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/data')
| -rw-r--r-- | src/main/java/at/hannibal2/skyhanni/data/InventoryData.kt | 72 |
1 files changed, 72 insertions, 0 deletions
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<Int, ItemStack> = mutableMapOf() + ) +}
\ No newline at end of file |
