CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

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