Description
- The FAQ doesn't contain a resolution to my issue
Versions
- mineflayer: 4.27.0
- server: vanilla 1.21.4
- node: v23.5.0
Detailed description of a problem
The pitch and yaw of an entity (in this case another player) doesn't get updated correctly, when the client receives sync_entity_position
. (Seems to be send when the entity/player jumps or moves down from a block)
The pitch and yaw gets corrected, once the entity looks somewhere else and the entity_look
or entity_move_look
packages are processed.
This makes accurate tracking of the looking direction of e.g. a player impossible.
Minimal example
const mineflayer = require('mineflayer')
const bot = mineflayer.createBot({
host: 'localhost',
username: 'Bot',
auth: 'offline',
port: 50622,
version: '1.21.4'
})
bot.on('physicTick', _ => {
const nearestPlayer = bot.nearestEntity(entity => entity.type === 'player')
if (nearestPlayer) {
console.log("Yaw:",nearestPlayer.yaw, " Pitch:",nearestPlayer.pitch)
} else {
console.log('No nearby players found.')
}
})
This will log something like this.:
...
Yaw: 4.3933209765044765 Pitch: 0.09817477042468115
Yaw: 4.3933209765044765 Pitch: 0.09817477042468115
Yaw: 4.3933209765044765 Pitch: 0.09817477042468115
Yaw: -71.379150390625 Pitch: -5.099963665008545
Yaw: -71.379150390625 Pitch: -5.099963665008545
...
Yaw: -71.379150390625 Pitch: -5.099963665008545
Yaw: -71.379150390625 Pitch: -5.099963665008545
Yaw: 4.663301595172349 Pitch: 0.07363107781851097
Yaw: 4.663301595172349 Pitch: 0.07363107781851097
Yaw: 4.663301595172349 Pitch: 0.07363107781851097
...
The first change (to -71 and -5) happened after the player pressed space to jump, and the second change (back to (4 and 0) happened after the player moved the mouse aka. looked around.
Expected behavior
The yaw and pitch should not change when jumping or moving down blocks / when that package gets send.
Possible Solution
I don't know what exactly the package sync_entity_position
does, but it appears that the send yaw and pitch are neither radians nor the notchianYawBytes, but degrees instead (as seen in the F3 Debug menu). So they need to be converted into radians before being written to the entity.yaw and entity.pitch.
So the current code:
mineflayer/lib/plugins/entities.js
Lines 345 to 353 in 2b6bd7d
Should be modified like this:
// 1.21.3 - merges the packets above
bot._client.on('sync_entity_position', (packet) => {
const entity = fetchEntity(packet.entityId)
entity.position.set(packet.x, packet.y, packet.z)
entity.velocity.update(packet.dx, packet.dy, packet.dz)
entity.yaw = ((-packet.yaw)+180)* Math.PI/180
entity.pitch = -(packet.pitch* Math.PI/180)
bot.emit('entityMoved', entity)
})
That way the log from above will look something like this:
Yaw: 4.466952054322987 Pitch: 0.04908738521234035
Yaw: 4.466952054322987 Pitch: 0.04908738521234035
Yaw: 4.447611113222395 Pitch: 0.044505254270480264
Yaw: 4.447611113222395 Pitch: 0.044505254270480264
There is still a change in the yaw and pitch, but I think that's just because of the conversion from degree to radians and should be tolerable.
I also don't know if and how this affects 1.21.3 as it seems that that function was created for 1.21.3 specifically but apparently gets used by 1.21.4 as well.