Skip to content

Commit

Permalink
Add Chunk Batch support (#3263)
Browse files Browse the repository at this point in the history
* Add Chunk Batch support

* Fix linting errors.

* I really hate the linter right now.

* Comment what chunkbatches are for.

* Update chunk batch calculation to use Vanilla's calc but updated for Milliseconds vs Nanoseconds

* Fix usage of variables starting with capital letters
  • Loading branch information
wgaylord authored Jan 2, 2024
1 parent 8771e3d commit c5982bd
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/plugins/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,24 @@ function inject (bot, { version, storageBuilder, hideErrors }) {
}
})

// Chunk batches are used by the server to throttle the chunks per tick for players based on their connection speed.
let chunkBatchStartTime = 0
let weightedAverage = 2 // The Vanilla client uses nano seconds with its weighted average starting at 2000000 converted to milliseconds that is 2
let oldSampleWeight = 1 // This is used for keeping track of the weight of the old average when updating it.

bot._client.on('chunk_batch_start', (packet) => {
chunkBatchStartTime = Date.now() // Get the time the chunk batch is starting.
})

bot._client.on('chunk_batch_finished', (packet) => {
const milliPerChunk = (Date.now() - chunkBatchStartTime) / packet.batchSize // Gets millisecond per chunk
const clampedMilliPerChunk = Math.min(Math.max(milliPerChunk, weightedAverage / 3.0), weightedAverage * 3.0) // Prevents the MilliPerChunk from being hugely different then the average, Vanilla uses 3 as a constant here.
weightedAverage = ((weightedAverage * oldSampleWeight) + clampedMilliPerChunk) / (oldSampleWeight + 1)
oldSampleWeight = Math.min(49, oldSampleWeight + 1) // 49 is used in Vanilla client to limit it to 50 samples
bot._client.write('chunk_batch_received', {
chunksPerTick: 7 / weightedAverage // Vanilla uses 7000000 as a constant here, since we are using milliseconds that is now 7. Not sure why they pick this constant to convert from nano seconds per chunk to chunks per tick.
})
})
bot._client.on('map_chunk', (packet) => {
addColumn({
x: packet.x,
Expand Down

0 comments on commit c5982bd

Please sign in to comment.