Chunk Batch error should be fixed now

This commit is contained in:
Anon 2023-11-27 00:00:57 +01:00
parent 549f39fab1
commit 0ad892ef50

View file

@ -78,7 +78,9 @@ namespace MinecraftClient.Protocol.Handlers
private bool isOnlineMode = false; private bool isOnlineMode = false;
private readonly BlockingCollection<Tuple<int, Queue<byte>>> packetQueue = new(); private readonly BlockingCollection<Tuple<int, Queue<byte>>> packetQueue = new();
private float LastYaw, LastPitch; private float LastYaw, LastPitch;
private long lastCHunkBatchStartedAt; private long chunkBatchStartTime;
private double aggregatedNanosPerChunk = 2000000.0;
private int oldSamplesWeight = 1;
private bool receiveDeclareCommands = false, receivePlayerInfo = false; private bool receiveDeclareCommands = false, receivePlayerInfo = false;
private object MessageSigningLock = new(); private object MessageSigningLock = new();
@ -115,6 +117,7 @@ namespace MinecraftClient.Protocol.Handlers
log = handler.GetLogger(); log = handler.GetLogger();
randomGen = RandomNumberGenerator.Create(); randomGen = RandomNumberGenerator.Create();
lastSeenMessagesCollector = protocolVersion >= MC_1_19_3_Version ? new(20) : new(5); lastSeenMessagesCollector = protocolVersion >= MC_1_19_3_Version ? new(20) : new(5);
chunkBatchStartTime = GetNanos();
if (handler.GetTerrainEnabled() && protocolVersion > MC_1_20_2_Version) if (handler.GetTerrainEnabled() && protocolVersion > MC_1_20_2_Version)
{ {
@ -958,15 +961,20 @@ namespace MinecraftClient.Protocol.Handlers
break; break;
case PacketTypesIn.ChunkBatchFinished: case PacketTypesIn.ChunkBatchFinished:
dataTypes.ReadNextVarInt(packetData); // Number of chunks received var batchSize = dataTypes.ReadNextVarInt(packetData); // Number of chunks received
var time = (DateTimeOffset.Now.ToUnixTimeMilliseconds() - lastCHunkBatchStartedAt);
SendChunkBatchReceived( if (batchSize > 0) {
// ReSharper disable once PossibleLossOfFraction var d = GetNanos() - chunkBatchStartTime;
25 / time == 0 ? 5 : time var d2 = d / (double)batchSize;
); var d3 = Math.Clamp(d2, aggregatedNanosPerChunk / 3.0, aggregatedNanosPerChunk * 3.0);
aggregatedNanosPerChunk = (aggregatedNanosPerChunk * oldSamplesWeight + d3) / (oldSamplesWeight + 1);
oldSamplesWeight = Math.Min(49, oldSamplesWeight + 1);
}
SendChunkBatchReceived((float)(7000000.0 / aggregatedNanosPerChunk));
break; break;
case PacketTypesIn.ChunkBatchStarted: case PacketTypesIn.ChunkBatchStarted:
lastCHunkBatchStartedAt = DateTimeOffset.Now.ToUnixTimeMilliseconds(); chunkBatchStartTime = GetNanos();
break; break;
case PacketTypesIn.StartConfiguration: case PacketTypesIn.StartConfiguration:
SendAcknowledgeConfiguration(); SendAcknowledgeConfiguration();
@ -4451,6 +4459,14 @@ namespace MinecraftClient.Protocol.Handlers
randomGen.GetNonZeroBytes(salt); randomGen.GetNonZeroBytes(salt);
return salt; return salt;
} }
public static long GetNanos()
{
var nano = 10000L * Stopwatch.GetTimestamp();
nano /= TimeSpan.TicksPerMillisecond;
nano *= 100L;
return nano;
}
} }
internal enum CurrentState internal enum CurrentState