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/crossbower.js
Views: 789
// This example will shoot the player that said "fire" in chat, when it is said in chat.1const mineflayer = require('mineflayer')23if (process.argv.length < 4 || process.argv.length > 6) {4console.log('Usage : node crossbower.js <host> <port> [<name>] [<password>]')5process.exit(1)6}78const bot = mineflayer.createBot({9host: process.argv[2],10port: parseInt(process.argv[3]),11username: process.argv[4] ? process.argv[4] : 'crossbower',12password: process.argv[5]13})1415bot.on('spawn', function () {16bot.chat(`/give ${bot.username} crossbow{Enchantments:[{id:quick_charge,lvl:3},{id:unbreaking,lvl:3}]} 1`) // Test with fast charge17// bot.chat(`/give ${bot.username} crossbow 1`) // Test with slow charge18bot.chat(`/give ${bot.username} minecraft:arrow 64`)19})2021bot.on('chat', async (username, message) => {22if (message === 'fire') {23// Check if weapon is equipped24const slotID = bot.getEquipmentDestSlot('hand')25if (bot.inventory.slots[slotID] === null || bot.inventory.slots[slotID].name !== 'crossbow') {26const weaponFound = bot.inventory.items().find(item => item.name === 'crossbow')27if (weaponFound) {28await bot.equip(weaponFound, 'hand')29} else {30console.log('No weapon in inventory')31return32}33}3435const isEnchanted = bot.heldItem.nbt.value.Enchantments ? bot.heldItem.nbt.value.Enchantments.value.value.find(enchant => enchant.id.value === 'quick_charge') : undefined3637const timeForCharge = 1250 - ((isEnchanted ? isEnchanted.lvl.value : 0) * 250)3839bot.activateItem() // charge40await sleep(timeForCharge) // wait for crossbow to charge41bot.deactivateItem() // raise weapon4243try {44bot.lookAt(bot.players[username].entity.position, true)45await bot.waitForTicks(5) // wait for lookat to finish46bot.activateItem() // fire47bot.deactivateItem()48} catch (err) {49bot.chat('Player disappeared, crossbow is charged now.')50}51}52})5354function sleep (ms) {55return new Promise(resolve => setTimeout(resolve, ms))56}575859