function tossItem (name, amount, toPerson) {
bot.lookAt(bot.players[toPerson].entity.position, false, () => {
amount = parseInt(amount, 10);
const item = itemByName(name);
if (!item) {
bot.chat(I have no ${name});
} else if (amount) {
bot.toss(item.type, null, amount, checkIfTossed);
} else {
bot.tossStack(item, checkIfTossed);
}
function checkIfTossed (err) {
if (err) {
bot.chat(`might be a few short of what you wanted`);
} else {
bot.chat(`dropped the ${name}`);
}
}
});
}
function nearbyBlocks(maxDist = 30) {
bot.chat('One sec, just counting blocks...');
let nearbyBlocks = {};
for(let y = maxDist -1; y <= maxDist; y++) {
for(let x = maxDist -1; x <= maxDist; x++) {
for(let z = maxDist * -1; z <= maxDist; z++) {
let block = bot.blockAt(bot.entity.position.offset(x, y, z));
function itemByNameIndex() {
let itemsByName
if (bot.supportFeature('itemsAreNotBlocks')) {
itemsByName = 'itemsByName'
} else if (bot.supportFeature('itemsAreAlsoBlocks')) {
itemsByName = 'blocksByName'
}
return itemsByName;
}
function equipByName (name, output = true) {
const item = mcData[itemByNameIndex()][name];
if(!item) return bot.chat(Equip a ${name}? What do you mean?);
function info(messageParts) {
const playerName = messageParts[1];
bot.chat("Info about " + playerName);
const player = bot.players[playerName];
let info = null;
if(player) {
info = "Pos: " + player.entity.position + "\r\n";
info += "Vel: " + player.entity.velocity;
} else {
info = 'No-one is called ' + playerName;
}
bot.chat(info);
}
function stop() {
bot.pathfinder.setGoal(null);
}
function follow(target, movement) {
bot.pathfinder.setMovements(movement);
bot.pathfinder.setGoal(new GoalFollow(target, 3), true);
}
function goToTarget(target, movement, dist = 0, cb) {
if (!target) {
bot.chat('I can\'t see there!');
cb();
return;
}
const p = target.position;
Heres my code: const mineflayer = require('mineflayer') const pathfinder = require('mineflayer-pathfinder').pathfinder const Movements = require('mineflayer-pathfinder').Movements const { GoalNear, GoalBlock, GoalXZ, GoalY, GoalInvert, GoalFollow } = require('mineflayer-pathfinder').goals let mcData;
const bot = mineflayer.createBot({ host: process.argv[2], port: parseInt(process.argv[3]), username: process.argv[4] ? process.argv[4] : 'botfren', });
bot.loadPlugin(pathfinder)
bot.once('spawn', () => {
});
function hunt(movement, amount, maxDist = 30) { const mobs = Object.values(bot.entities) .filter(entity => entity.kind === 'Passive mobs') .filter(mob => !['squid', 'horse', 'salmon', 'wolf'].includes(mob.name)) .filter(mob => mob.position.distanceTo(bot.entity.position) < maxDist) .sort((mobA, mobB) => { return (mobA.position.distanceTo(bot.entity.position) - mobB.position.distanceTo(bot.entity.position)); }).slice(0, amount); kill(movement, mobs, () => { bot.chat(
Finished hunting
); }); }function kill(movement, mobs, cb) { if(mobs.length == 0) return cb(); const tool = bestTool({material: 'flesh'}); if(tool) { bot.equip(tool, 'hand'); } const mob = mobs.shift();
}
function collectDrops(movement, drops, maxDist, cb) { if(!drops) drops = Object.values(bot.entities) .filter(entity => entity.kind === 'Drops') .filter(drop => drop.position.distanceTo(bot.entity.position) < maxDist) .sort((dropA, dropB) => { return (dropA.position.distanceTo(bot.entity.position) - dropB.position.distanceTo(bot.entity.position)); }); if(drops.length == 0) { cb(); return; } goToTarget(drops.shift(), movement, 0, () => { setImmediate(collectDrops.bind(this, movement, drops, maxDist, cb)); }); }
function harvest(blockName, movement, amount) { if(amount <= 0) return bot.chat(
I collected all the ${blockName} you asked for
);}
function itemByName (name) { return bot.inventory.items().filter(item => item.name === name)[0]; }
function tossItem (name, amount, toPerson) { bot.lookAt(bot.players[toPerson].entity.position, false, () => { amount = parseInt(amount, 10); const item = itemByName(name); if (!item) { bot.chat(
I have no ${name}
); } else if (amount) { bot.toss(item.type, null, amount, checkIfTossed); } else { bot.tossStack(item, checkIfTossed); }}
function nearbyBlocks(maxDist = 30) { bot.chat('One sec, just counting blocks...'); let nearbyBlocks = {}; for(let y = maxDist -1; y <= maxDist; y++) { for(let x = maxDist -1; x <= maxDist; x++) { for(let z = maxDist * -1; z <= maxDist; z++) { let block = bot.blockAt(bot.entity.position.offset(x, y, z));
}
function itemByNameIndex() { let itemsByName if (bot.supportFeature('itemsAreNotBlocks')) { itemsByName = 'itemsByName' } else if (bot.supportFeature('itemsAreAlsoBlocks')) { itemsByName = 'blocksByName' } return itemsByName; }
function equipByName (name, output = true) { const item = mcData[itemByNameIndex()][name]; if(!item) return bot.chat(
Equip a ${name}? What do you mean?
);}
function sayItems (items) { const output = items.map(itemToString).join(', ') if (output) { bot.chat(output) } else { bot.chat('nothing') } }
function itemToString (item) { if (item) { return
${item.name} x ${item.count}
} else { return '(nothing)' } }function hole(messageParts, defaultMove) { if(messageParts.length < 2) { bot.chat("How big though?"); return; } // width, length, depth const size = messageParts[1].split('x'); bot.chat(size[0] + " along x, " + size[1] + " along z and " + size[2] + " deep - got it!"); let offsets = { x: Math.floor(Number(size[0])/2), z: Math.floor(Number(size[1])/2), y: Math.floor(Number(size[2])) } const positions = []; for(let yO = 0; yO >= offsets.y -1; yO--) { for(let xO = offsets.x -1; xO <= offsets.x; xO++) { for(let zO = offsets.z * -1; zO <= offsets.z; zO++) { positions.push(bot.entity.position.offset(xO, yO, zO)); } } }
}
function digBlocksInOrder(positions, onComplete, defaultMove) { if(positions == null || positions == undefined || positions.length == 0) return onComplete ? onComplete() : null;
}
function bestTool(block) { let tool = bot.pathfinder.bestHarvestTool(block); if(tool) return tool;
}
function bestToolOfTypeInInv(toolname, materials) { const tools = materials.map(x => x + '_' + toolname); for(let i = tools.length - 1; i >= 0; i--) { const tool = tools[i]; let matches = bot.inventory.items().filter(item => item.name === tool); if(matches.length > 0) return matches[0]; } return null; }
function digBlockAt(position, onComplete) { var target = bot.blockAt(position); bot.lookAt(target.position); const tool = bestTool(target);
}
function info(messageParts) { const playerName = messageParts[1]; bot.chat("Info about " + playerName);
}
function stop() { bot.pathfinder.setGoal(null); }
function follow(target, movement) { bot.pathfinder.setMovements(movement); bot.pathfinder.setGoal(new GoalFollow(target, 3), true); }
function goToTarget(target, movement, dist = 0, cb) { if (!target) { bot.chat('I can\'t see there!'); cb(); return; } const p = target.position;
}