Skip to content

Commit 52a3fb9

Browse files
author
Simeon Kummer
committed
Fix clearInventory to listen for slot-specific updateSlot events
1 parent e6161a0 commit 52a3fb9

1 file changed

Lines changed: 23 additions & 4 deletions

File tree

test/externalTests/plugins/testCommon.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,29 @@ function inject (bot, wrap) {
126126
// Use bot.chat for /give (server console /give doesn't send inventory
127127
// update packets on 1.21.9+). Use server console for /clear.
128128
bot.chat('/give @a stone 1')
129-
await onceWithCleanup(bot.inventory, 'updateSlot', {
130-
timeout: 10000,
131-
checkCondition: (slot, oldItem, newItem) => newItem?.name === 'stone'
132-
})
129+
130+
// Listen for each slot update individually since updateSlot event
131+
// without namespace isn't fired for slot changes in newer versions
132+
const stoneSlots = []
133+
const checkCondition = (slot, oldItem, newItem) => {
134+
if (newItem?.name === 'stone') {
135+
stoneSlots.push(slot)
136+
return true
137+
}
138+
return false
139+
}
140+
141+
const cleanupPromises = []
142+
for (const slot of bot.inventory.slots.keys()) {
143+
cleanupPromises.push(
144+
onceWithCleanup(bot.inventory, `updateSlot:${slot}`, {
145+
timeout: 10000,
146+
checkCondition
147+
})
148+
)
149+
}
150+
await Promise.all(cleanupPromises)
151+
133152
const clearMsg = onceWithCleanup(bot, 'message', {
134153
timeout: 10000,
135154
checkCondition: msg => msg.translate === 'commands.clear.success.single' || msg.translate === 'commands.clear.success'

0 commit comments

Comments
 (0)