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

View file

@ -53,15 +53,29 @@ const modes = [
description: 'Automatically collect nearby items when idle.', description: 'Automatically collect nearby items when idle.',
on: true, on: true,
active: false, 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) { update: async function (agent) {
if (this.active) return;
if (agent.isIdle()) { if (agent.isIdle()) {
let item = world.getNearestEntityWhere(agent.bot, entity => entity.name === 'item', 8); let item = world.getNearestEntityWhere(agent.bot, entity => entity.name === 'item', 8);
if (item && await world.isClearPath(agent.bot, item)) { if (item && item !== this.prev_item && await world.isClearPath(agent.bot, item)) {
execute(this, agent, async () => { if (this.noticed_at === -1) {
// wait 2 seconds for the item to settle this.noticed_at = Date.now();
await new Promise(resolve => setTimeout(resolve, 2000)); }
await skills.pickupNearbyItem(agent.bot); 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;
} }
} }
} }