mineflayer
mineflayer copied to clipboard
`soundEffectHeard` event is not emitted in 1.20.4
- [x] The [FAQ](https://github.com/PrismarineJS/mineflayer/blob/master/docs/FAQ.md) doesn't contain a resolution to my issue
Versions
- mineflayer: 4.20.0
- server: fabric 1.20.4
- mods:
- fabricloader 0.15.7
- mixinextras 0.3.5
- java 17
- minecraft 1.20.4
- skinrestorer 1.2.4+1.20.2
- fabricloader 0.15.7
- node: 20.9.0
Detailed description of a problem
I'm developing a bot that can hear something from Minecraft. But I found soundEffectHeard
event can not be emitted anyway. The following code snippet does not output the corresponding soundName
at all.
bot.on('soundEffectHeard', async (soundName, position, volume, pitch) => {
console.log(`${soundName}`) // No output at all
})
However, the hardcodedSoundEffectHeard
event works well. The code snippet is as follows.
bot.on('hardcodedSoundEffectHeard', async (soundId, soundCategory, position, volume, pitch) => {
console.log(`${soundId}`) // Ouput some numeric sound ID, like 80, 78, 1517...
})
What did you try yet?
Edit the source code and test
I have tried to edit the source code in mineflayer/lib/plugins/sound.js
at line 7 to see what will happen.
function inject (bot) {
bot._client.on('named_sound_effect', (packet) => {
console.log('Test')
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
bot.emit('soundEffectHeard', soundName, pt, volume, pitch)
})
...
Unfortunately, console.log('Test')
does not output anything, inferring that this function is not being called for some reason.
Run command in Minecraft to play sounds
I also tried to run the /playsound
command in Minecraft,
/playsound minecraft:entity.zombie.ambient neutral @a
and I can hear the sound being played correctly in Minecraft.
However, there is still no output of the corresponding soundName
in JavaScript console.
Maybe because namedSound
or this packet from server is not received correctly?
What I noticed at [Protocol - wiki.vg](https://wiki.vg/Protocol#Sound_Effect) is that soundname
presents only if the sound ID is 0.
So I ran the command
/playsound 0 neutral @a
The server told me that "Played sound minecraft:0 to 2 players" but there is still no output of the corresponding soundName
in JavaScript console.
Your current code
This is my code and it doesn't have any warnings or errors when I running it.
if (process.argv.length < 4 || process.argv.length > 6) {
console.log('Usage : node guard.js <host> <port> [<name>] [<password>]')
process.exit(1)
}
const mineflayer = require('mineflayer')
const {pathfinder, Movements, goals} = require('mineflayer-pathfinder')
const pvp = require('mineflayer-pvp').plugin
const bot = mineflayer.createBot({
host: process.argv[2],
port: parseInt(process.argv[3]),
username: process.argv[4] ? process.argv[4] : 'Guard',
password: process.argv[5]
})
bot.on('soundEffectHeard', async (soundName, position, volume, pitch) => {
console.log(`${soundName}`)
})
bot.on('hardcodedSoundEffectHeard', async (soundId, soundCategory, position, volume, pitch) => {
console.log(`${soundId}`)
})
Expected behavior
I want to be able to get soundName
, position
values from emitted soundEffectHeard
event since I need to judge the sound source and position to set my bot's face orientation. While using hardcodedSoundEffectHeard
event might work, it's not a good idea due to the sound ID might change for different Minecraft version.
Additional context
- The bot joined the game and spawned properly. I can see the bot and I can interact with it.
- Use command
/playsound 0 neutral @a
in Minecraft does not emithardcodedSoundEffectHeard
event.