Path: blob/master/test/externalTests/heldItemChanged.js
9427 views
const assert = require('assert')1const { onceWithCleanup } = require('../../lib/promise_utils')23module.exports = () => async (bot) => {4await bot.test.becomeCreative()5await bot.test.clearInventory()6await bot.test.wait(100)78// Give the bot a stone in the held slot (slot 36 = first hotbar slot)9const stoneId = bot.registry.itemsByName.stone.id10const diamondId = bot.registry.itemsByName.diamond.id1112// Put stone in the current held slot13await bot.test.setInventorySlot(bot.quickBarSlot + bot.inventory.hotbarStart, new (require('prismarine-item')(bot.registry))(stoneId, 1))14await bot.test.wait(100)15assert.strictEqual(bot.heldItem.type, stoneId, 'should be holding stone')1617// Now change the held slot contents to diamond and verify heldItemChanged fires18const heldItemPromise = onceWithCleanup(bot, 'heldItemChanged', {19timeout: 500020})21await bot.test.setInventorySlot(bot.quickBarSlot + bot.inventory.hotbarStart, new (require('prismarine-item')(bot.registry))(diamondId, 1))2223const [newItem] = await heldItemPromise24assert(newItem, 'heldItemChanged should fire with the new item')25assert.strictEqual(newItem.type, diamondId, 'new item should be diamond')26assert.strictEqual(bot.heldItem.type, diamondId, 'bot.heldItem should reflect diamond')27}282930