Skip to content

Commit

Permalink
Improve crafting stacks (#2915)
Browse files Browse the repository at this point in the history
* Avoid close windows when still are crafting the stack

* Fix close windows when any error found
  • Loading branch information
sefirosweb authored Feb 3, 2023
1 parent e87d7f4 commit c736d95
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions lib/plugins/craft.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,44 @@ module.exports = inject
function inject (bot) {
const Item = require('prismarine-item')(bot.registry)
const Recipe = require('prismarine-recipe')(bot.registry).Recipe
let windowCraftingTable

async function craft (recipe, count, craftingTable) {
assert.ok(recipe)
count = parseInt(count ?? 1, 10)
if (recipe.requiresTable && !craftingTable) {
throw new Error('recipe requires craftingTable')
}
for (let i = 0; i < count; i++) {
await craftOnce(recipe, craftingTable)

try {
for (let i = 0; i < count; i++) {
await craftOnce(recipe, craftingTable)
}

if (windowCraftingTable) {
bot.closeWindow(windowCraftingTable)
windowCraftingTable = undefined
}
} catch (err) {
if (windowCraftingTable) {
bot.closeWindow(windowCraftingTable)
windowCraftingTable = undefined
}
throw new Error(err)
}
}

async function craftOnce (recipe, craftingTable) {
if (craftingTable) {
bot.activateBlock(craftingTable)
const [window] = await once(bot, 'windowOpen')
if (!window.type.startsWith('minecraft:crafting')) {
if (!windowCraftingTable) {
bot.activateBlock(craftingTable)
const [window] = await once(bot, 'windowOpen')
windowCraftingTable = window
}
if (!windowCraftingTable.type.startsWith('minecraft:crafting')) {
throw new Error('crafting: non craftingTable used as craftingTable')
}
await startClicking(window, 3, 3)
await startClicking(windowCraftingTable, 3, 3)
} else {
await startClicking(bot.inventory, 2, 2)
}
Expand Down Expand Up @@ -73,7 +91,7 @@ function inject (bot) {
if (ingredient.id === -1) return nextShapeClick()
if (!window.selectedItem || window.selectedItem.type !== ingredient.id ||
(ingredient.metadata != null &&
window.selectedItem.metadata !== ingredient.metadata)) {
window.selectedItem.metadata !== ingredient.metadata)) {
// we are not holding the item we need. click it.
const sourceItem = window.findInventoryItem(ingredient.id, ingredient.metadata)
if (!sourceItem) throw new Error('missing ingredient')
Expand All @@ -89,7 +107,7 @@ function inject (bot) {
const destSlot = extraSlots.pop()
if (!window.selectedItem || window.selectedItem.type !== ingredient.id ||
(ingredient.metadata != null &&
window.selectedItem.metadata !== ingredient.metadata)) {
window.selectedItem.metadata !== ingredient.metadata)) {
// we are not holding the item we need. click it.
const sourceItem = window.findInventoryItem(ingredient.id, ingredient.metadata)
if (!sourceItem) throw new Error('missing ingredient')
Expand Down Expand Up @@ -126,7 +144,6 @@ function inject (bot) {
for (let i = 1; i <= w * h; i++) {
window.updateSlot(i, null)
}
closeTheWindow()
return
}
const slotsToClick = []
Expand All @@ -145,11 +162,6 @@ function inject (bot) {
for (const _slot of slotsToClick) {
await bot.putAway(_slot)
}
closeTheWindow()
}

function closeTheWindow () {
bot.closeWindow(window)
}

function slot (x, y) {
Expand Down

0 comments on commit c736d95

Please sign in to comment.