blob: b6dab180a34c52662ead537d5014fd22bea2db26 (
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
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.S2EPacketCloseWindow
import net.minecraft.network.play.server.S2FPacketSetSlot
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
object OtherInventoryData {
private var currentInventory: Inventory? = null
private var acceptItems = false
private var lateEvent: InventoryUpdatedEvent? = null
@SubscribeEvent
fun onCloseWindow(event: GuiContainerEvent.CloseWindowEvent) {
close()
}
fun close() {
currentInventory?.let {
InventoryCloseEvent(it).postAndCatch()
currentInventory = null
}
}
@SubscribeEvent
fun onTick(event: LorenzTickEvent) {
lateEvent?.let {
it.postAndCatch()
lateEvent = null
}
}
@SubscribeEvent
fun onChatPacket(event: PacketEvent.ReceiveEvent) {
val packet = event.packet
if (packet is S2EPacketCloseWindow) {
close()
}
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 = InventoryUpdatedEvent(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) {
InventoryFullyOpenedEvent(inventory).postAndCatch()
InventoryUpdatedEvent(inventory).postAndCatch()
acceptItems = false
}
class Inventory(
val windowId: Int,
val title: String,
val slotCount: Int,
val items: MutableMap<Int, ItemStack> = mutableMapOf(),
)
}
|