Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/lib/plugins/health.js
1467 views
1
module.exports = inject
2
3
function inject (bot, options) {
4
bot.isAlive = true
5
6
bot._client.on('respawn', (packet) => {
7
bot.isAlive = false
8
bot.emit('respawn')
9
})
10
11
bot._client.once('update_health', (packet) => {
12
if (packet.health > 0) {
13
bot.emit('spawn')
14
}
15
})
16
17
bot._client.on('update_health', (packet) => {
18
bot.health = packet.health
19
bot.food = packet.food
20
bot.foodSaturation = packet.foodSaturation
21
bot.emit('health')
22
if (bot.health <= 0) {
23
if (bot.isAlive) {
24
bot.isAlive = false
25
bot.emit('death')
26
}
27
if (!options.respawn) return
28
bot.respawn()
29
} else if (bot.health > 0 && !bot.isAlive) {
30
bot.isAlive = true
31
bot.emit('spawn')
32
}
33
})
34
35
const respawn = () => {
36
if (bot.isAlive) return
37
bot._client.write('client_command', bot.supportFeature('respawnIsPayload') ? { payload: 0 } : { actionId: 0 })
38
}
39
40
bot.respawn = respawn
41
}
42
43