forked from PrismarineJS/mineflayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attack.js
45 lines (40 loc) · 1.1 KB
/
attack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
*
* A bot that attacks the player that sends a message or the nearest entity (excluding players)
*
*/
const mineflayer = require('mineflayer')
if (process.argv.length < 4 || process.argv.length > 6) {
console.log('Usage : node attack.js <host> <port> [<name>] [<password>]')
process.exit(1)
}
const bot = mineflayer.createBot({
host: process.argv[2],
port: parseInt(process.argv[3]),
username: process.argv[4] ? process.argv[4] : 'attack',
password: process.argv[5]
})
bot.on('spawn', () => {
bot.on('chat', (username, message) => {
if (message === 'attack me') attackPlayer(username)
else if (message === 'attack') attackEntity()
})
})
function attackPlayer (username) {
const player = bot.players[username]
if (!player || !player.entity) {
bot.chat('I can\'t see you')
} else {
bot.chat(`Attacking ${player.username}`)
bot.attack(player.entity)
}
}
function attackEntity () {
const entity = bot.nearestEntity()
if (!entity) {
bot.chat('No nearby entities')
} else {
bot.chat(`Attacking ${entity.name ?? entity.username}`)
bot.attack(entity)
}
}