bot.oxygenLevel / breath are driven by any nearby entity's air_supply, not only the bot's
- The FAQ doesn't contain a resolution to my issue
Versions
- mineflayer: 4.37.1 (same code on master @ aa8fdfa)
- server: vanilla 1.21.11 (any server version where
mcDataHasEntityMetadataholds — 1.19.4+ — on mineflayer ≥ 4.21.0) - node: 22.20.0
Detailed description of a problem
bot.oxygenLevel (documented as a value in [0, 20]) and the breath event (documented as "Fires when your oxygen level
change") are driven by the air_supply metadata of whatever entity the entity_metadata packet is about, not only the bot. In lib/plugins/entities.js
(4.37.1 lines 493–497, identical on master):
// Breathing (formerly in breath.js)
if (metas.air_supply != null) {
bot.oxygenLevel = Math.round(metas.air_supply / 15)
bot.emit('breath')
}There is no packet.entityId === bot.entity.id check. air_supply is metadata slot 1 of the base Entity, so every
living entity in tracking range sends it when its air changes. The legacy lib/plugins/breath.js has the guard
(if (bot.entity.id !== packet.entityId) return) but returns early at line 7 whenever mcDataHasEntityMetadata is
supported, so on every modern version the unguarded block is the only writer.
The server only sends air_supply when it changes, so a bot standing on dry land never receives its own value: its
oxygenLevel becomes the last nearby swimmer's. I'm building agents whose "am I in danger" check reads bot.oxygenLevel;
two bots on dry land reported drowning for three minutes because a cow was swimming in view.
The unguarded entities.js path dates from #3412 ("1.20.6", 44fad41, released in 4.21.0), which moved breathing for
mcDataHasEntityMetadata versions into entities.js. The symptom itself is older: the 4.20.1 breath.js guard it replaced,
if (!bot?.entity?.id === packet?.entityId) return, was a no-op (operator precedence), so the legacy branch applied any
entity's value too. #3412 fixed that guard in the legacy branch; the new entities.js path never received one.
What did you try yet?
Reproduced deterministically on a vanilla 1.21.11 server: a bot standing on dry land, a cow summoned into a sealed
glass tank of water a few metres away (so it cannot surface). Over 22 s, bot.oxygenLevel fell 19 → −1 (outside its
documented [0, 20] range) in lockstep with the cow's Air (server data get entity … Air: 290 → −11), while the server
held the bot's own Air at 300 and Health at 20. Every entity_metadata packet carrying slot 1 (400 of them) came
from the cow's entity id, none from the bot.
Your current code
const mineflayer = require('mineflayer')
const bot = mineflayer.createBot({ host: '127.0.0.1', port: 25565, username: 'probe', version: '1.21.11', auth: 'offline' })
bot.once('spawn', () => {
const AIR = bot.registry.entitiesByName.cow.metadataKeys.indexOf('air_supply') // 1 — same slot for every entity
bot._client.on('entity_metadata', (p) => {
for (const m of p.metadata) if (m.key === AIR) console.log(`air_supply ${m.value} from entity ${p.entityId} (me: ${bot.entity.id})`)
})
bot.on('breath', () => console.log('breath event, bot.oxygenLevel =', bot.oxygenLevel))
})
// then, from the server console (or RCON), with the bot standing on dry land — `execute at probe` so the `~` are the bot's:
// execute at probe run fill ~-2 ~5 ~2 ~2 ~9 ~6 glass hollow (a sealed tank in the air a few blocks from the bot)
// execute at probe run fill ~-1 ~6 ~3 ~1 ~8 ~5 water
// execute at probe run summon cow ~ ~6.2 ~4 {NoAI:1b}Expected behavior
bot.oxygenLevel and breath only change when the bot's own air_supply changes. A one-line guard restores that,
comparing against bot.entity?.id the same way the fireworks block a few lines up does:
if (metas.air_supply != null && entity.id === bot.entity?.id) {Happy to open a PR with that change and an internal test if you'd like.
Additional context
- #3532 / #3534 / #3535 are a different (crash) bug in the legacy breath.js branch; they don't touch this.
- Investigated with the help of an AI coding agent; the reproduction above was run against a real 1.21.11 server.
Source: PrismarineJS/mineflayer