Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Jumping is fun. Riding pigs is even funnier!
- *
- * Learn how to make your bot interactive with this example.
- *
- * This bot can move, jump, ride vehicles, attack nearby entities and much more.
- */
- const mineflayer = require('mineflayer')
- if (process.argv.length < 4 || process.argv.length > 6) {
- console.log('Usage : node jumper.js <host> <port> [<name>] [<password>]')
- process.exit(1)
- }
- let bots = []
- let i = 0
- function next() {
- if (i < 20) {
- i++
- setTimeout(() => {
- createBot(`mineflayer-bot${i}`)
- next()
- }, 4000)
- } else {
- //registerBots();
- }
- }
- next();
- let tempBot;
- function createBot() {
- tempBot = mineflayer.createBot({
- host: process.argv[2],
- port: parseInt(process.argv[3]),
- username: (process.argv[4] ? process.argv[4] : 'jumper') + i,
- password: process.argv[5],
- version: "1.12",
- verbose: true
- });
- console.log('created bot #' + i);
- registerBot(tempBot);
- bots.push(tempBot);
- }
- let target = null
- //function registerBots() {
- // bots.forEach(bot => {
- // registerBot(bot);
- // })
- //}
- function registerBot(bot) {
- console.log('registering bot:', bot.username);
- bot.on('chat', (username, message) => {
- if (username === bot.username) return
- target = bot.players[username].entity
- let entity
- if(message.startsWith('command')) {
- bot.chat(message.replace('command ', '/'));
- } else {
- switch (message) {
- case 'bots online':
- bot.chat('Total bots online: ' + bots.length);
- break
- case 'forward':
- bot.setControlState('forward', true)
- break
- case 'back':
- bot.setControlState('back', true)
- break
- case 'left':
- bot.setControlState('left', true)
- break
- case 'right':
- bot.setControlState('right', true)
- break
- case 'sprint':
- bot.setControlState('sprint', true)
- break
- case 'stop':
- bot.clearControlStates()
- break
- case 'jump':
- bot.setControlState('jump', true)
- bot.setControlState('jump', false)
- break
- case 'jump a lot':
- bot.setControlState('jump', true)
- break
- case 'stop jumping':
- bot.setControlState('jump', false)
- break
- case 'attack':
- entity = nearestEntity(bot)
- if (entity) {
- bot.attack(entity, true)
- } else {
- bot.chat('no nearby entities')
- }
- break
- case 'mount':
- entity = nearestEntity(bot, 'object')
- if (entity) {
- bot.mount(entity)
- } else {
- bot.chat('no nearby objects')
- }
- break
- case 'dismount':
- bot.dismount()
- break
- case 'move vehicle forward':
- bot.moveVehicle(0.0, 1.0)
- break
- case 'move vehicle backward':
- bot.moveVehicle(0.0, -1.0)
- break
- case 'move vehicle left':
- bot.moveVehicle(1.0, 0.0)
- break
- case 'move vehicle right':
- bot.moveVehicle(-1.0, 0.0)
- break
- case 'tp':
- bot.entity.position.y += 10
- break
- case 'pos':
- bot.chat(bot.entity.position.toString())
- break
- case 'yp':
- bot.chat(`Yaw ${bot.entity.yaw}, pitch: ${bot.entity.pitch}`)
- break
- }
- }
- });
- bot.once('spawn', () => {
- // keep your eyes on the target, so creepy!
- setInterval(watchTarget, 50);
- function watchTarget() {
- if (!target) return;
- bot.lookAt(target.position.offset(0, target.height, 0))
- }
- });
- bot.on('mount', () => {
- bot.chat(`mounted ${bot.vehicle.objectType}`)
- });
- bot.on('dismount', (vehicle) => {
- bot.chat(`dismounted ${vehicle.objectType}`)
- });
- }
- function nearestEntity(bot, type) {
- let id
- let entity
- let dist
- let best = null
- let bestDistance = null
- for (id in bot.entities) {
- entity = bot.entities[id]
- if (type && entity.type !== type) continue
- if (entity === bot.entity) continue
- dist = bot.entity.position.distanceTo(entity.position)
- if (!best || dist < bestDistance) {
- best = entity
- bestDistance = dist
- }
- }
- return best
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement