Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Chunk Batch support #3263

Merged
merged 6 commits into from
Jan 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a comment explaining what is the purpose of this system ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment and comments each part of the calculation now that I am doing it correctly.

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