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

Minecraft 1.20.2 support #3262

Merged
merged 18 commits into from
Jan 14, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add Chunk Batch support (#3263)
* 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
commit c5982bd2d378b37e275d218ed48ce9dad7fbb3d2
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