#4147·mineflayer

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:

javascript
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:

json
"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=undefined

The key appears on spawn (not on login), which is consistent with the separate difficulty packet being the one that assigns.

Two consequences

  1. The value is unusable on these versions.
  2. if (packet.difficulty) is also a truthiness test where 0 means peaceful, so on protocol versions where the field is still a number, a peaceful server leaves bot.game.difficulty unset 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:

javascript
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:

javascript
bot._client?.on('difficulty', (packet) => {
  const name = normalizeDifficulty(packet?.difficulty)
  if (name) bot.__difficulty = name
})