Last active
July 11, 2020 05:59
-
-
Save TheDudeFromCI/f51eb12ca8b62f2aad22cbe9ba692a81 to your computer and use it in GitHub Desktop.
A quick xray mining bot.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mineflayer = require('mineflayer'); | |
const { pathfinder, Movements, goals } = require('mineflayer-pathfinder'); | |
const bot = mineflayer.createBot({ | |
host: process.argv[2], | |
port: process.argv[3], | |
username: process.argv[4], | |
password: process.argv[5], | |
}); | |
bot.loadPlugin(pathfinder); | |
let blocks; | |
bot.once('spawn', () => | |
{ | |
const mcData = require('minecraft-data')(bot.version); | |
const defaultMove = new Movements(bot, mcData); | |
bot.pathfinder.setMovements(defaultMove); | |
blocks = [ | |
mcData.blocksByName.diamond_ore.id, | |
mcData.blocksByName.coal_ore.id, | |
mcData.blocksByName.iron_ore.id, | |
mcData.blocksByName.gold_ore.id, | |
mcData.blocksByName.emerald_ore.id, | |
mcData.blocksByName.redstone_ore.id, | |
mcData.blocksByName.lapis_ore.id, | |
]; | |
bot.on('goal_reached', () => | |
{ | |
const tool = getBestTool(block); | |
if (tool) | |
{ | |
bot.equip(tool, 'hand', () => | |
{ | |
bot.dig(block, () => findOre()); | |
}); | |
} | |
else | |
bot.dig(block, () => findOre()); | |
}); | |
findOre(); | |
}); | |
let block; | |
function findOre() | |
{ | |
block = bot.findBlock({ | |
matching: (b) => blocks.indexOf(b.type) > -1, | |
maxDistance: 32 | |
}); | |
if (!block) | |
{ | |
bot.chat('No more ore nearby.'); | |
return; | |
} | |
const p = block.position; | |
bot.pathfinder.setGoal(new goals.GoalNear(p.x, p.y, p.z, 1)); | |
} | |
function getBestTool(block) | |
{ | |
const items = bot.inventory.items(); | |
for (const i in block.harvestTools) | |
{ | |
const id = parseInt(i, 10); | |
for (const j in items) | |
{ | |
const item = items[j]; | |
if (item.type === id) return item; | |
} | |
} | |
return undefined; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment