|
1 | 1 | // This is an example that uses mineflayer-pathfinder to showcase how simple it is to walk to goals |
2 | 2 |
|
3 | | -const mineflayer = require('../../') |
4 | | -const { pathfinder, Movements, goals: { GoalNear } } = require('mineflayer-pathfinder') |
| 3 | +const mineflayer = require("../../"); |
| 4 | +const { |
| 5 | + pathfinder, |
| 6 | + Movements, |
| 7 | + goals: { GoalNear }, |
| 8 | +} = require("mineflayer-pathfinder"); |
5 | 9 |
|
6 | 10 | if (process.argv.length < 4 || process.argv.length > 6) { |
7 | | - console.log('Usage : node gps.js <host> <port> [<name>] [<password>]') |
8 | | - process.exit(1) |
| 11 | + console.log("Usage : node gps.js <host> <port> [<name>] [<password>]"); |
| 12 | + process.exit(1); |
9 | 13 | } |
10 | 14 |
|
11 | 15 | const bot = mineflayer.createBot({ |
12 | 16 | host: process.argv[2], |
13 | 17 | port: parseInt(process.argv[3]), |
14 | | - username: process.argv[4] ? process.argv[4] : 'gps', |
15 | | - password: process.argv[5] |
16 | | -}) |
| 18 | + username: process.argv[4] ? process.argv[4] : "gps", |
| 19 | + password: process.argv[5], |
| 20 | +}); |
17 | 21 |
|
18 | | -const RANGE_GOAL = 1 // get within this radius of the player |
| 22 | +const RANGE_GOAL = 1; // get within this radius of the player |
19 | 23 |
|
20 | | -bot.loadPlugin(pathfinder) |
| 24 | +bot.loadPlugin(pathfinder); |
21 | 25 |
|
22 | | -bot.once('spawn', () => { |
23 | | - const defaultMove = new Movements(bot) |
| 26 | +bot.once("spawn", () => { |
| 27 | + const defaultMove = new Movements(bot); |
24 | 28 |
|
25 | | - bot.on('chat', (username, message) => { |
26 | | - if (username === bot.username) return |
27 | | - if (message !== 'come') return |
28 | | - const target = bot.players[username]?.entity |
29 | | - if (!target) { |
30 | | - bot.chat("I don't see you !") |
31 | | - return |
32 | | - } |
33 | | - const { x: playerX, y: playerY, z: playerZ } = target.position |
| 29 | + bot.on("chat", (username, message) => { |
| 30 | + console.log("Chat message received: ", username, message); |
| 31 | + if (username === bot.username) return; |
| 32 | + |
| 33 | + const [cmd, ...args] = message.split(" "); |
| 34 | + |
| 35 | + const target = bot.players[username]?.entity; |
| 36 | + if (!target) { |
| 37 | + bot.chat("I don't see you !"); |
| 38 | + return; |
| 39 | + } |
34 | 40 |
|
35 | | - bot.pathfinder.setMovements(defaultMove) |
36 | | - bot.pathfinder.setGoal(new GoalNear(playerX, playerY, playerZ, RANGE_GOAL)) |
37 | | - }) |
38 | | -}) |
| 41 | + switch (cmd) { |
| 42 | + case "come": { |
| 43 | + |
| 44 | + const { x: playerX, y: playerY, z: playerZ } = target.position; |
| 45 | + |
| 46 | + bot.pathfinder.setMovements(defaultMove); |
| 47 | + bot.pathfinder.setGoal(new GoalNear(playerX, playerY, playerZ, RANGE_GOAL)); |
| 48 | + } |
| 49 | + case "forward": { |
| 50 | + bot.lookAt(target.position); |
| 51 | + bot.setControlState("forward", true) |
| 52 | + } |
| 53 | + } |
| 54 | + }); |
| 55 | +}); |
39 | 56 |
|
40 | | -bot.on('error', err => console.log(err)) |
| 57 | +bot.on("error", (err) => console.log(err)); |
0 commit comments