Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/lib/plugins/title.js
1467 views
1
module.exports = inject
2
function inject (bot) {
3
function parseTitle (text) {
4
try {
5
const parsed = JSON.parse(text)
6
return typeof parsed === 'string' ? parsed : (parsed.text || text)
7
} catch {
8
return typeof text === 'string' ? text.replace(/^"|"$/g, '') : text
9
}
10
}
11
12
if (bot.supportFeature('titleUsesLegacyPackets')) {
13
bot._client.on('title', (packet) => {
14
if (packet.action === 0) bot.emit('title', parseTitle(packet.text), 'title')
15
else if (packet.action === 1) bot.emit('title', parseTitle(packet.text), 'subtitle')
16
else if (packet.action === 2) bot.emit('title_times', packet.fadeIn, packet.stay, packet.fadeOut)
17
else if (packet.action === 3) {
18
if (packet.fadeIn !== undefined) bot.emit('title_times', packet.fadeIn, packet.stay, packet.fadeOut)
19
else bot.emit('title_clear')
20
} else if (packet.action === 4) bot.emit('title_clear')
21
})
22
} else if (bot.supportFeature('titleUsesNewPackets')) {
23
function getText (packet) {
24
let text = packet.text
25
if (typeof text === 'object' && text.value !== undefined) text = text.value
26
return parseTitle(text)
27
}
28
bot._client.on('set_title_text', (packet) => bot.emit('title', getText(packet), 'title'))
29
bot._client.on('set_title_subtitle', (packet) => bot.emit('title', getText(packet), 'subtitle'))
30
bot._client.on('set_title_time', (packet) => {
31
if (typeof packet.fadeIn === 'number' && typeof packet.stay === 'number' && typeof packet.fadeOut === 'number') {
32
bot.emit('title_times', packet.fadeIn, packet.stay, packet.fadeOut)
33
}
34
})
35
bot._client.on('clear_titles', () => bot.emit('title_clear'))
36
}
37
}
38
39