mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-08-02 21:35:31 +02:00
made item collecting mode better
This commit is contained in:
parent
578fcf99ba
commit
f203b7ca4e
2 changed files with 38 additions and 18 deletions
|
@ -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));
|
bot.pathfinder.setMovements(new pf.Movements(bot));
|
||||||
await bot.pathfinder.goto(new pf.goals.GoalFollow(nearestItem, 0.8), true);
|
await bot.pathfinder.goto(new pf.goals.GoalFollow(nearestItem, 0.8), true);
|
||||||
log(bot, `Successfully picked up a dropped item.`);
|
await new Promise(resolve => setTimeout(resolve, 200));
|
||||||
|
let prev = nearestItem;
|
||||||
|
nearestItem = getNearestItem(bot);
|
||||||
|
if (prev === nearestItem) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
pickedUp++;
|
||||||
|
}
|
||||||
|
log(bot, `Picked up ${pickedUp} items.`);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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)) {
|
||||||
|
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 () => {
|
execute(this, agent, async () => {
|
||||||
// wait 2 seconds for the item to settle
|
await skills.pickupNearbyItems(agent.bot);
|
||||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
||||||
await skills.pickupNearbyItem(agent.bot);
|
|
||||||
});
|
});
|
||||||
|
this.noticed_at = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.noticed_at = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue