const { Vec3 } = require('vec3')
module.exports = inject
function inject (bot) {
bot._client.on('named_sound_effect', (packet) => {
const soundName = packet.soundName
const pt = new Vec3(packet.x / 8, packet.y / 8, packet.z / 8)
const volume = packet.volume
const pitch = packet.pitch
const normalizedSoundName = bot.supportFeature('playsoundUsesResourceLocation')
? `minecraft:${soundName.replace(/\./g, '_')}`
: soundName
bot.emit('soundEffectHeard', normalizedSoundName, pt, volume, pitch)
bot.emit('hardcodedSoundEffectHeard', 0, 'master', pt, volume, pitch)
})
bot._client.on('sound_effect', (packet) => {
const soundCategory = packet.soundCategory
const pt = new Vec3(packet.x / 8, packet.y / 8, packet.z / 8)
const volume = packet.volume
const pitch = packet.pitch
let soundId, soundName
if (packet.sound) {
if (packet.sound.data) soundName = packet.sound.data.soundName
else soundId = packet.sound.soundId
} else {
soundId = packet.soundId
}
soundName ??= bot.registry?.sounds?.[soundId]?.name
if (soundName) {
bot.emit('soundEffectHeard', soundName, pt, volume, pitch)
} else if (soundId !== null) {
bot.emit('hardcodedSoundEffectHeard', soundId, soundCategory, pt, volume, pitch)
}
})
}