Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/lib/plugins/sound.js
1467 views
1
const { Vec3 } = require('vec3')
2
3
module.exports = inject
4
5
function inject (bot) {
6
bot._client.on('named_sound_effect', (packet) => {
7
const soundName = packet.soundName
8
const pt = new Vec3(packet.x / 8, packet.y / 8, packet.z / 8)
9
const volume = packet.volume
10
const pitch = packet.pitch
11
12
// In 1.8.8, sound names are in the format "note.harp" or "random.click"
13
// We need to convert them to the format expected by the test
14
const normalizedSoundName = bot.supportFeature('playsoundUsesResourceLocation')
15
? `minecraft:${soundName.replace(/\./g, '_')}`
16
: soundName
17
18
// Emit both events for compatibility with tests
19
bot.emit('soundEffectHeard', normalizedSoundName, pt, volume, pitch)
20
// Emit hardcodedSoundEffectHeard for compatibility (use 0, 'master' as dummy values)
21
bot.emit('hardcodedSoundEffectHeard', 0, 'master', pt, volume, pitch)
22
})
23
24
bot._client.on('sound_effect', (packet) => {
25
const soundCategory = packet.soundCategory
26
const pt = new Vec3(packet.x / 8, packet.y / 8, packet.z / 8)
27
const volume = packet.volume
28
const pitch = packet.pitch
29
30
let soundId, soundName
31
32
if (packet.sound) { // ItemSoundHolder
33
if (packet.sound.data) soundName = packet.sound.data.soundName
34
else soundId = packet.sound.soundId // Sound specified by ID (registry reference)
35
} else { // Legacy packet
36
soundId = packet.soundId
37
}
38
39
// If we have an ID but no name yet, try to look it up in the registry
40
soundName ??= bot.registry?.sounds?.[soundId]?.name
41
42
if (soundName) {
43
bot.emit('soundEffectHeard', soundName, pt, volume, pitch)
44
} else if (soundId !== null) {
45
bot.emit('hardcodedSoundEffectHeard', soundId, soundCategory, pt, volume, pitch)
46
}
47
})
48
}
49
50