Skip to content

Ignore empty chunk batches like the vanilla client (fixes NaN chunksPerTick) - #4203

Open
yuan-nvwa wants to merge 1 commit into
PrismarineJS:masterfrom
yuan-nvwa:fix/empty-chunk-batch-nan
Open

yuan-nvwa wants to merge 1 commit into
PrismarineJS:masterfrom
yuan-nvwa:fix/empty-chunk-batch-nan

Conversation

@yuan-nvwa

Copy link
Copy Markdown

Fixes #%d

What

lib/plugins/blocks.js handles chunk_batch_finished without the guard the vanilla client has. Vanilla's ChunkBatchSizeCalculator.onBatchFinished(int count) opens with if (count > 0) { ... }; mineflayer omits it, so a batch with batchSize: 0 computes (now - start) / 0.

Because chunk_batch_start and an empty chunk_batch_finished are written back to back and Date.now() has millisecond resolution, the elapsed time is usually 0 as well: 0 / 0 is NaN, Math.min(Math.max(NaN, lo), hi) is NaN, and weightedAverage becomes NaN. NaN is absorbing, so it stays NaN for every later sample and chunk_batch_received reports chunksPerTick: NaN for the rest of the session.

Fix

Skip the average update for empty batches, exactly like vanilla; still answer chunk_batch_received with the current average (vanilla does not send a reply at all, but keeping the reply preserves the existing contract with the server's batching).

Verification

Reproduced by arithmetic, no server needed: run the handler twice over the same packet sequence (one empty batch, then five normal batches).

before: NaN, NaN, NaN, NaN, NaN, NaN
after:  3.50, 1.75, 1.17, 1.00, 0.92, 0.88

The default value comes from weightedAverage = 2, so chunksPerTick: 7 / 2 = 3.5 on the first reply.

Not verified

I could not run the mocha suite here (no npm install in this environment), so this is the arithmetic reproduction only, not an integration test against a server. Happy to adjust if you would rather drop the reply for empty batches entirely.

ChunkBatchSizeCalculator.onBatchFinished starts with `if (count > 0)`; mineflayer dropped that guard, so a chunk_batch_finished with batchSize 0 does 0/0 and Math.min/Math.max propagate NaN into weightedAverage. NaN is absorbing, so every later sample keeps it and chunk_batch_received reports chunksPerTick: NaN for the rest of the session.

Fixes PrismarineJS#4171

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant