Skip to content

Commit 9865ab7

Browse files
authored
Fix bot.heldItem and bot.entity.equipment (#3225)
* implement `bot.heldItem` with getter function * fix bot.entity.equipment * restart CI * add test for `bot.heldItem`
1 parent 48c3ca7 commit 9865ab7

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

lib/plugins/inventory.js

+20-4
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,26 @@ function inject (bot, { hideErrors }) {
4343
bot.quickBarSlot = null
4444
bot.inventory = windows.createWindow(0, 'minecraft:inventory', 'Inventory')
4545
bot.currentWindow = null
46-
bot.heldItem = null
4746
bot.usingHeldItem = false
47+
Object.defineProperty(bot, 'heldItem', {
48+
get: function () {
49+
return bot.inventory.slots[bot.QUICK_BAR_START + bot.quickBarSlot]
50+
}
51+
})
52+
53+
bot.on('spawn', () => {
54+
Object.defineProperty(bot.entity, 'equipment', {
55+
get: bot.supportFeature('doesntHaveOffHandSlot')
56+
? function () {
57+
return [bot.heldItem, bot.inventory.slots[8], bot.inventory.slots[7],
58+
bot.inventory.slots[6], bot.inventory.slots[5]]
59+
}
60+
: function () {
61+
return [bot.heldItem, bot.inventory.slots[45], bot.inventory.slots[8],
62+
bot.inventory.slots[7], bot.inventory.slots[6], bot.inventory.slots[5]]
63+
}
64+
})
65+
})
4866

4967
bot._client.on('entity_status', (packet) => {
5068
if (packet.entityId === bot.entity.id && packet.entityStatus === 9 && !eatingTask.done) {
@@ -364,9 +382,7 @@ function inject (bot, { hideErrors }) {
364382
}
365383

366384
function updateHeldItem () {
367-
bot.heldItem = bot.inventory.slots[bot.QUICK_BAR_START + bot.quickBarSlot]
368-
bot.entity.heldItem = bot.heldItem
369-
bot.emit('heldItemChanged', bot.entity.heldItem)
385+
bot.emit('heldItemChanged', bot.heldItem)
370386
}
371387

372388
function closeWindow (window) {

test/externalTests/heldItem.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const assert = require('assert')
2+
3+
module.exports = () => async (bot) => {
4+
const Item = require('prismarine-item')(bot.registry)
5+
6+
await bot.test.becomeCreative()
7+
await bot.test.clearInventory()
8+
assert.equal(bot.heldItem, null)
9+
10+
const stoneId = bot.registry.itemsByName.stone.id
11+
await bot.test.setInventorySlot(36, new Item(stoneId, 1))
12+
assert.strictEqual(bot.heldItem.id, bot.stoneId)
13+
14+
await bot.tossStack(bot.heldItem)
15+
assert.equal(bot.heldItem, null)
16+
}

0 commit comments

Comments
 (0)