mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-08-06 15:25:42 +02:00
Merge pull request #482 from icwhite/fork_copy
Updated takeFromChest skill to withdraw items from multiple slots
This commit is contained in:
commit
b27c69a2b6
1 changed files with 23 additions and 7 deletions
|
@ -830,7 +830,7 @@ export async function putInChest(bot, itemName, num=-1) {
|
||||||
|
|
||||||
export async function takeFromChest(bot, itemName, num=-1) {
|
export async function takeFromChest(bot, itemName, num=-1) {
|
||||||
/**
|
/**
|
||||||
* Take the given item from the nearest chest.
|
* Take the given item from the nearest chest, potentially from multiple slots.
|
||||||
* @param {MinecraftBot} bot, reference to the minecraft bot.
|
* @param {MinecraftBot} bot, reference to the minecraft bot.
|
||||||
* @param {string} itemName, the item or block name to take from the chest.
|
* @param {string} itemName, the item or block name to take from the chest.
|
||||||
* @param {number} num, the number of items to take from the chest. Defaults to -1, which takes all items.
|
* @param {number} num, the number of items to take from the chest. Defaults to -1, which takes all items.
|
||||||
|
@ -845,17 +845,33 @@ export async function takeFromChest(bot, itemName, num=-1) {
|
||||||
}
|
}
|
||||||
await goToPosition(bot, chest.position.x, chest.position.y, chest.position.z, 2);
|
await goToPosition(bot, chest.position.x, chest.position.y, chest.position.z, 2);
|
||||||
const chestContainer = await bot.openContainer(chest);
|
const chestContainer = await bot.openContainer(chest);
|
||||||
let item = chestContainer.containerItems().find(item => item.name === itemName);
|
|
||||||
if (!item) {
|
// Find all matching items in the chest
|
||||||
|
let matchingItems = chestContainer.containerItems().filter(item => item.name === itemName);
|
||||||
|
if (matchingItems.length === 0) {
|
||||||
log(bot, `Could not find any ${itemName} in the chest.`);
|
log(bot, `Could not find any ${itemName} in the chest.`);
|
||||||
await chestContainer.close();
|
await chestContainer.close();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
let to_take = num === -1 ? item.count : Math.min(num, item.count);
|
|
||||||
await chestContainer.withdraw(item.type, null, to_take);
|
let totalAvailable = matchingItems.reduce((sum, item) => sum + item.count, 0);
|
||||||
|
let remaining = num === -1 ? totalAvailable : Math.min(num, totalAvailable);
|
||||||
|
let totalTaken = 0;
|
||||||
|
|
||||||
|
// Take items from each slot until we've taken enough or run out
|
||||||
|
for (const item of matchingItems) {
|
||||||
|
if (remaining <= 0) break;
|
||||||
|
|
||||||
|
let toTakeFromSlot = Math.min(remaining, item.count);
|
||||||
|
await chestContainer.withdraw(item.type, null, toTakeFromSlot);
|
||||||
|
|
||||||
|
totalTaken += toTakeFromSlot;
|
||||||
|
remaining -= toTakeFromSlot;
|
||||||
|
}
|
||||||
|
|
||||||
await chestContainer.close();
|
await chestContainer.close();
|
||||||
log(bot, `Successfully took ${to_take} ${itemName} from the chest.`);
|
log(bot, `Successfully took ${totalTaken} ${itemName} from the chest.`);
|
||||||
return true;
|
return totalTaken > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function viewChest(bot) {
|
export async function viewChest(bot) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue