made item collecting mode better

This commit is contained in:
MaxRobinsonTheGreat 2024-02-02 15:47:17 -06:00
parent 578fcf99ba
commit f203b7ca4e
2 changed files with 38 additions and 18 deletions

View file

@ -273,7 +273,7 @@ export async function attackEntity(bot, entity, kill=true) {
}
}
log(bot, `Successfully killed ${entity.name}.`);
await pickupNearbyItem(bot);
await pickupNearbyItems(bot);
return true;
}
}
@ -376,24 +376,30 @@ export async function collectBlock(bot, blockType, num=1) {
return true;
}
export async function pickupNearbyItem(bot) {
export async function pickupNearbyItems(bot) {
/**
* Pick up all nearby items.
* @param {MinecraftBot} bot, reference to the minecraft bot.
* @returns {Promise<boolean>} true if the items were picked up, false otherwise.
* @example
* await skills.pickupNearbyItem(bot);
* await skills.pickupNearbyItems(bot);
**/
const distance = 10;
let nearestItem = bot.nearestEntity(entity => entity.name === 'item' && bot.entity.position.distanceTo(entity.position) < distance);
if (!nearestItem) {
log(bot, `Didn't pick up items.`);
return false;
const distance = 8;
const getNearestItem = bot => bot.nearestEntity(entity => entity.name === 'item' && bot.entity.position.distanceTo(entity.position) < distance);
let nearestItem = getNearestItem(bot);
let pickedUp = 0;
while (nearestItem) {
bot.pathfinder.setMovements(new pf.Movements(bot));
await bot.pathfinder.goto(new pf.goals.GoalFollow(nearestItem, 0.8), true);
await new Promise(resolve => setTimeout(resolve, 200));
let prev = nearestItem;
nearestItem = getNearestItem(bot);
if (prev === nearestItem) {
break;
}
pickedUp++;
}
bot.pathfinder.setMovements(new pf.Movements(bot));
await bot.pathfinder.goto(new pf.goals.GoalFollow(nearestItem, 0.8), true);
log(bot, `Successfully picked up a dropped item.`);
log(bot, `Picked up ${pickedUp} items.`);
return true;
}

View file

@ -53,15 +53,29 @@ const modes = [
description: 'Automatically collect nearby items when idle.',
on: true,
active: false,
wait: 2, // number of seconds to wait after noticing an item to pick it up
prev_item: null,
noticed_at: -1,
update: async function (agent) {
if (this.active) return;
if (agent.isIdle()) {
let item = world.getNearestEntityWhere(agent.bot, entity => entity.name === 'item', 8);
if (item && await world.isClearPath(agent.bot, item)) {
execute(this, agent, async () => {
// wait 2 seconds for the item to settle
await new Promise(resolve => setTimeout(resolve, 2000));
await skills.pickupNearbyItem(agent.bot);
});
if (item && item !== this.prev_item && await world.isClearPath(agent.bot, item)) {
if (this.noticed_at === -1) {
this.noticed_at = Date.now();
}
if (Date.now() - this.noticed_at > this.wait * 1000) {
agent.bot.chat(`Picking up ${item.name}!`);
this.prev_item = item;
execute(this, agent, async () => {
await skills.pickupNearbyItems(agent.bot);
});
this.noticed_at = -1;
}
}
else {
this.noticed_at = -1;
}
}
}