Skip to content

Commit

Permalink
Import changedSlots computation from prismarine-windows (PrismarineJS…
Browse files Browse the repository at this point in the history
…#3134)

* copy changedSlots logic from prismarine-windows

* added possibility for returning slots directly

* lint fix

* fix lint

* depend on other pr for tests

* fix slot comparing

* fix

* Update package.json

---------

Co-authored-by: Romain Beaumont <romain.rom1@gmail.com>
  • Loading branch information
kaduvert and rom1504 authored Aug 5, 2023
1 parent bbdd93a commit e5b5eee
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
42 changes: 41 additions & 1 deletion lib/plugins/inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,20 @@ function inject (bot, { hideErrors }) {
}
}

function getChangedSlots (oldSlots, newSlots) {
assert.equal(oldSlots.length, newSlots.length)

const changedSlots = []

for (let i = 0; i < newSlots.length; i++) {
if (!Item.equal(oldSlots[i], newSlots[i])) {
changedSlots.push(i)
}
}

return changedSlots
}

async function clickWindow (slot, mouseButton, mode) {
// if you click on the quick bar and have dug recently,
// wait a bit
Expand Down Expand Up @@ -506,7 +520,33 @@ function inject (bot, { hideErrors }) {
if (bot.supportFeature('transactionPacketExists')) {
windowClickQueue.push(click)
} else {
changedSlots = window.acceptClick(click)
if (
// this array indicates the clicks that return changedSlots
[
0,
// 1,
// 2,
3,
4
// 5,
// 6
].includes(click.mode)) {
changedSlots = window.acceptClick(click)
} else {
// this is used as a fallback
const oldSlots = JSON.parse(JSON.stringify(window.slots))

window.acceptClick(click)

changedSlots = getChangedSlots(oldSlots, window.slots)
}

changedSlots = changedSlots.map(slot => {
return {
location: slot,
item: Item.toNotch(window.slots[slot])
}
})
}

// WHEN ADDING SUPPORT FOR OTHER CLICKS, MAKE SURE TO CHANGE changedSlots TO SUPPORT THEM
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"prismarine-physics": "^1.7.0",
"prismarine-recipe": "^1.3.0",
"prismarine-registry": "^1.5.0",
"prismarine-windows": "^2.5.0",
"prismarine-windows": "^2.8.0",
"prismarine-world": "^3.6.0",
"protodef": "^1.14.0",
"typed-emitter": "^1.0.0",
Expand Down

0 comments on commit e5b5eee

Please sign in to comment.