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/guard.js
Views: 789
/**1* This bot example shows the basic usage of the mineflayer-pvp plugin for guarding a defined area from nearby mobs.2*/34if (process.argv.length < 4 || process.argv.length > 6) {5console.log('Usage : node guard.js <host> <port> [<name>] [<password>]')6process.exit(1)7}89const mineflayer = require('mineflayer')10const { pathfinder, Movements, goals } = require('mineflayer-pathfinder')11const pvp = require('mineflayer-pvp').plugin1213const bot = mineflayer.createBot({14host: process.argv[2],15port: parseInt(process.argv[3]),16username: process.argv[4] ? process.argv[4] : 'Guard',17password: process.argv[5]18})1920bot.loadPlugin(pathfinder)21bot.loadPlugin(pvp)2223let guardPos = null2425// Assign the given location to be guarded26function guardArea (pos) {27guardPos = pos2829// We we are not currently in combat, move to the guard pos30if (!bot.pvp.target) {31moveToGuardPos()32}33}3435// Cancel all pathfinder and combat36function stopGuarding () {37guardPos = null38bot.pvp.stop()39bot.pathfinder.setGoal(null)40}4142// Pathfinder to the guard position43function moveToGuardPos () {44bot.pathfinder.setMovements(new Movements(bot))45bot.pathfinder.setGoal(new goals.GoalBlock(guardPos.x, guardPos.y, guardPos.z))46}4748// Called when the bot has killed it's target.49bot.on('stoppedAttacking', () => {50if (guardPos) {51moveToGuardPos()52}53})5455// Check for new enemies to attack56bot.on('physicsTick', () => {57if (!guardPos) return // Do nothing if bot is not guarding anything5859// Only look for mobs within 16 blocks60const filter = e => e.type === 'mob' && e.position.distanceTo(bot.entity.position) < 16 &&61e.displayName !== 'Armor Stand' // Mojang classifies armor stands as mobs for some reason?6263const entity = bot.nearestEntity(filter)64if (entity) {65// Start attacking66bot.pvp.attack(entity)67}68})6970// Listen for player commands71bot.on('chat', (username, message) => {72// Guard the location the player is standing73if (message === 'guard') {74const player = bot.players[username]7576if (!player) {77bot.chat("I can't see you.")78return79}8081bot.chat('I will guard that location.')82guardArea(player.entity.position)83}8485// Stop guarding86if (message === 'stop') {87bot.chat('I will no longer guard this area.')88stopGuarding()89}90})919293