Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/examples/digger.js
Views: 789
/*1* Never spend hours mining from ground to bedrock again!2*3* Learn how to create a simple bot that is capable of digging the block4* below his feet and then going back up by creating a dirt column to the top.5*6* As always, you can send the bot commands using chat messages, and monitor7* his inventory at any time.8*9* Remember that in survival mode he might not have enough dirt to get back up,10* so be sure to teach him a few more tricks before leaving him alone at night.11*/12const mineflayer = require('mineflayer')13const vec3 = require('vec3')1415if (process.argv.length < 4 || process.argv.length > 6) {16console.log('Usage : node digger.js <host> <port> [<name>] [<password>]')17process.exit(1)18}1920const bot = mineflayer.createBot({21host: process.argv[2],22port: parseInt(process.argv[3]),23username: process.argv[4] ? process.argv[4] : 'digger',24password: process.argv[5]25})2627bot.on('chat', async (username, message) => {28if (username === bot.username) return29switch (message) {30case 'loaded':31await bot.waitForChunksToLoad()32bot.chat('Ready!')33break34case 'list':35sayItems()36break37case 'dig':38dig()39break40case 'build':41build()42break43case 'equip dirt':44equipDirt()45break46}47})4849function sayItems (items = bot.inventory.items()) {50const output = items.map(itemToString).join(', ')51if (output) {52bot.chat(output)53} else {54bot.chat('empty')55}56}5758async function dig () {59let target60if (bot.targetDigBlock) {61bot.chat(`already digging ${bot.targetDigBlock.name}`)62} else {63target = bot.blockAt(bot.entity.position.offset(0, -1, 0))64if (target && bot.canDigBlock(target)) {65bot.chat(`starting to dig ${target.name}`)66try {67await bot.dig(target)68bot.chat(`finished digging ${target.name}`)69} catch (err) {70console.log(err.stack)71}72} else {73bot.chat('cannot dig')74}75}76}7778function build () {79const referenceBlock = bot.blockAt(bot.entity.position.offset(0, -1, 0))80const jumpY = Math.floor(bot.entity.position.y) + 1.081bot.setControlState('jump', true)82bot.on('move', placeIfHighEnough)8384let tryCount = 08586async function placeIfHighEnough () {87if (bot.entity.position.y > jumpY) {88try {89await bot.placeBlock(referenceBlock, vec3(0, 1, 0))90bot.setControlState('jump', false)91bot.removeListener('move', placeIfHighEnough)92bot.chat('Placing a block was successful')93} catch (err) {94tryCount++95if (tryCount > 10) {96bot.chat(err.message)97bot.setControlState('jump', false)98bot.removeListener('move', placeIfHighEnough)99}100}101}102}103}104105async function equipDirt () {106let itemsByName107if (bot.supportFeature('itemsAreNotBlocks')) {108itemsByName = 'itemsByName'109} else if (bot.supportFeature('itemsAreAlsoBlocks')) {110itemsByName = 'blocksByName'111}112try {113await bot.equip(bot.registry[itemsByName].dirt.id, 'hand')114bot.chat('equipped dirt')115} catch (err) {116bot.chat(`unable to equip dirt: ${err.message}`)117}118}119120function itemToString (item) {121if (item) {122return `${item.name} x ${item.count}`123} else {124return '(nothing)'125}126}127128129