Skip to content

Commit

Permalink
Fix bot.heldItem and bot.entity.equipment (#3225)
Browse files Browse the repository at this point in the history
* implement `bot.heldItem` with getter function

* fix bot.entity.equipment

* restart CI

* add test for `bot.heldItem`
  • Loading branch information
szdytom authored Dec 30, 2023
1 parent 48c3ca7 commit 9865ab7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/plugins/inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,26 @@ function inject (bot, { hideErrors }) {
bot.quickBarSlot = null
bot.inventory = windows.createWindow(0, 'minecraft:inventory', 'Inventory')
bot.currentWindow = null
bot.heldItem = null
bot.usingHeldItem = false
Object.defineProperty(bot, 'heldItem', {
get: function () {
return bot.inventory.slots[bot.QUICK_BAR_START + bot.quickBarSlot]
}
})

bot.on('spawn', () => {
Object.defineProperty(bot.entity, 'equipment', {
get: bot.supportFeature('doesntHaveOffHandSlot')
? function () {
return [bot.heldItem, bot.inventory.slots[8], bot.inventory.slots[7],
bot.inventory.slots[6], bot.inventory.slots[5]]
}
: function () {
return [bot.heldItem, bot.inventory.slots[45], bot.inventory.slots[8],
bot.inventory.slots[7], bot.inventory.slots[6], bot.inventory.slots[5]]
}
})
})

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

function updateHeldItem () {
bot.heldItem = bot.inventory.slots[bot.QUICK_BAR_START + bot.quickBarSlot]
bot.entity.heldItem = bot.heldItem
bot.emit('heldItemChanged', bot.entity.heldItem)
bot.emit('heldItemChanged', bot.heldItem)
}

function closeWindow (window) {
Expand Down
16 changes: 16 additions & 0 deletions test/externalTests/heldItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const assert = require('assert')

module.exports = () => async (bot) => {
const Item = require('prismarine-item')(bot.registry)

await bot.test.becomeCreative()
await bot.test.clearInventory()
assert.equal(bot.heldItem, null)

const stoneId = bot.registry.itemsByName.stone.id
await bot.test.setInventorySlot(36, new Item(stoneId, 1))
assert.strictEqual(bot.heldItem.id, bot.stoneId)

await bot.tossStack(bot.heldItem)
assert.equal(bot.heldItem, null)
}

0 comments on commit 9865ab7

Please sign in to comment.