bot.game.difficulty is always undefined on 1.21.x — the protocol field is a mapper that already yields a string
Author: elysianx138Created Sep 22, 2026Updated Sep 22, 2026
Labelspossible bugStage1
bot.game.difficulty is always undefined on 1.21.x, on every difficulty.
Where
lib/plugins/game.js:
const difficultyNames = ['peaceful', 'easy', 'normal', 'hard']
...
if (packet.difficulty) {
bot.game.difficulty = difficultyNames[packet.difficulty]
}
...
bot._client.on('difficulty', (packet) => {
bot.game.difficulty = difficultyNames[packet.difficulty]
})Why it fails
In the 1.21.x protocol data, packet_difficulty.difficulty is declared as a
mapper over a varint:
"packet_difficulty": ["container", [
{ "name": "difficulty", "type": ["mapper", { "type": "varint", "mappings": {
"0": "peaceful", "1": "easy", "2": "normal", "3": "hard" } }] },
{ "name": "difficultyLocked", "type": "bool" }
]]so packet.difficulty arrives as the string 'easy', not 1.
difficultyNames['easy'] is undefined, so bot.game.difficulty is assigned
undefined.
Verified live against a 1.21.6 server whose difficulty is easy:
on login: difficulty=undefined
on spawn: difficulty=undefined (the key exists, the value never does)
spawn+6s: difficulty=undefinedThe key appears on spawn (not on login), which is consistent with the
separate difficulty packet being the one that assigns.
Two consequences
- The value is unusable on these versions.
if (packet.difficulty)is also a truthiness test where0meanspeaceful, so on protocol versions where the field is still a number, a peaceful server leavesbot.game.difficultyunset rather than'peaceful'— the one case where knowing the difficulty matters most.
lib/plugins/explosion.js reads the same field for damage scaling, so it is
affected too.
Suggested fix
Normalize both shapes and drop the truthiness guard:
const difficultyNames = ['peaceful', 'easy', 'normal', 'hard']
function difficultyName (d) {
if (typeof d === 'string') return d
return difficultyNames[d] ?? null
}
bot.game.difficulty = difficultyName(packet.difficulty) // with `packet.difficulty != null`Our side worked around it by listening to the raw packet:
bot._client?.on('difficulty', (packet) => {
const name = normalizeDifficulty(packet?.difficulty)
if (name) bot.__difficulty = name
})Source: PrismarineJS/mineflayer