Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/test/externalTests/heldItemChanged.js
9427 views
1
const assert = require('assert')
2
const { onceWithCleanup } = require('../../lib/promise_utils')
3
4
module.exports = () => async (bot) => {
5
await bot.test.becomeCreative()
6
await bot.test.clearInventory()
7
await bot.test.wait(100)
8
9
// Give the bot a stone in the held slot (slot 36 = first hotbar slot)
10
const stoneId = bot.registry.itemsByName.stone.id
11
const diamondId = bot.registry.itemsByName.diamond.id
12
13
// Put stone in the current held slot
14
await bot.test.setInventorySlot(bot.quickBarSlot + bot.inventory.hotbarStart, new (require('prismarine-item')(bot.registry))(stoneId, 1))
15
await bot.test.wait(100)
16
assert.strictEqual(bot.heldItem.type, stoneId, 'should be holding stone')
17
18
// Now change the held slot contents to diamond and verify heldItemChanged fires
19
const heldItemPromise = onceWithCleanup(bot, 'heldItemChanged', {
20
timeout: 5000
21
})
22
await bot.test.setInventorySlot(bot.quickBarSlot + bot.inventory.hotbarStart, new (require('prismarine-item')(bot.registry))(diamondId, 1))
23
24
const [newItem] = await heldItemPromise
25
assert(newItem, 'heldItemChanged should fire with the new item')
26
assert.strictEqual(newItem.type, diamondId, 'new item should be diamond')
27
assert.strictEqual(bot.heldItem.type, diamondId, 'bot.heldItem should reflect diamond')
28
}
29
30