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/anvil.js
Views: 789
/**1* This example demonstrates how to use anvils w/ mineflayer2* the options are: (<Option> are required, [<Option>] are optional)3* 1. "anvil combine <itemName1> <itemName2> [<name>]"4* 2. "anvil rename <itemName> <name>"5*6* to use this:7* /op anvilman8* /gamemode anvilman creative9* /xp set anvilman 999 levels10*11* Put an anvil near the bot12* Give him a sword and an enchanted book13* say list14* say xp15* say anvil combine diamond_sword enchanted_book16*/17const mineflayer = require('mineflayer')1819if (process.argv.length < 4 || process.argv.length > 6) {20console.log('Usage : node anvil.js <host> <port> [<name>] [<password>]')21process.exit(1)22}2324const bot = mineflayer.createBot({25host: process.argv[2],26port: parseInt(process.argv[3]),27username: process.argv[4] ? process.argv[4] : 'anvilman',28password: process.argv[5]29})3031bot.on('chat', async (username, message) => {32const command = message.split(' ')3334switch (true) {35case /^list$/.test(message):36sayItems()37break38case /^toss \w+$/.test(message):39// toss name40// ex: toss diamond41tossItem(command[1])42break43case /^xp$/.test(message):44bot.chat(bot.experience.level)45break46case /^gamemode$/.test(message):47bot.chat(bot.game.gameMode)48break49case /^anvil combine \w+ \w+$/.test(message): // anvil firstSlot secondSlot50combine(bot, command[2], command[3])51break52case /^anvil combine \w+ \w+ (.+)$/.test(message): // anvil firstSlot secondSlot name53combine(bot, command[2], command[3], command.slice(4).join(' '))54break55case /^anvil rename \w+ (.+)/.test((message)):56rename(bot, command[2], command.slice(3).join(' '))57break58}59})6061async function tossItem (name, amount) {62amount = parseInt(amount, 10)63const item = itemByName(name)64if (!item) {65bot.chat(`I have no ${name}`)66} else {67try {68if (amount) {69await bot.toss(item.type, null, amount)70bot.chat(`tossed ${amount} x ${name}`)71} else {72await bot.tossStack(item)73bot.chat(`tossed ${name}`)74}75} catch (err) {76bot.chat(`unable to toss: ${err.message}`)77}78}79}8081function itemByName (name) {82return bot.inventory.items().filter(item => item.name === name)[0]83}8485function itemToString (item) {86if (item) {87return `${item.name} x ${item.count}`88} else {89return '(nothing)'90}91}9293function sayItems (items = bot.inventory.items()) {94const output = items.map(itemToString).join(', ')95if (output) {96bot.chat(output)97} else {98bot.chat('empty')99}100}101102function getAnvilIds () {103const matchingBlocks = [bot.registry.blocksByName.anvil.id]104if (bot.registry.blocksByName?.chipped_anvil) {105matchingBlocks.push(bot.registry.blocksByName.chipped_anvil.id)106matchingBlocks.push(bot.registry.blocksByName.damaged_anvil.id)107}108return matchingBlocks109}110111async function rename (bot, itemName, name) {112const anvilBlock = bot.findBlock({113matching: getAnvilIds()114})115const anvil = await bot.openAnvil(anvilBlock)116try {117await anvil.rename(itemByName(itemName), name)118bot.chat('Anvil used successfully.')119} catch (err) {120bot.chat(err.message)121}122anvil.close()123}124125async function combine (bot, itemName1, itemName2, name) {126const anvilBlock = bot.findBlock({127matching: getAnvilIds()128})129const anvil = await bot.openAnvil(anvilBlock)130try {131bot.chat('Using the anvil...')132await anvil.combine(itemByName(itemName1), itemByName(itemName2), name)133bot.chat('Anvil used successfully.')134} catch (err) {135bot.chat(err.message)136}137anvil.close()138}139140bot.on('error', console.log)141142143