Skip to content

Commit

Permalink
Merge pull request #8 from milutinke/1.20.2
Browse files Browse the repository at this point in the history
1.20.2
  • Loading branch information
milutinke authored Nov 26, 2023
2 parents eb7bfff + 2fb5c16 commit a41b671
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions MinecraftClient/Protocol/Handlers/Protocol18.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ class Protocol18Handler : IMinecraftCom
private bool isOnlineMode = false;
private readonly BlockingCollection<Tuple<int, Queue<byte>>> packetQueue = new();
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 object MessageSigningLock = new();
Expand Down Expand Up @@ -115,6 +117,7 @@ public Protocol18Handler(TcpClient Client, int protocolVersion, IMinecraftComHan
log = handler.GetLogger();
randomGen = RandomNumberGenerator.Create();
lastSeenMessagesCollector = protocolVersion >= MC_1_19_3_Version ? new(20) : new(5);
chunkBatchStartTime = GetNanos();

if (handler.GetTerrainEnabled() && protocolVersion > MC_1_20_2_Version)
{
Expand Down Expand Up @@ -958,15 +961,22 @@ private bool HandlePlayPackets(int packetId, Queue<byte> packetData)

break;
case PacketTypesIn.ChunkBatchFinished:
dataTypes.ReadNextVarInt(packetData); // Number of chunks received
var time = (DateTimeOffset.Now.ToUnixTimeMilliseconds() - lastCHunkBatchStartedAt);
SendChunkBatchReceived(
// ReSharper disable once PossibleLossOfFraction
25 / time == 0 ? 5 : time
);
var batchSize = dataTypes.ReadNextVarInt(packetData); // Number of chunks received

if (batchSize > 0)
{
var d = GetNanos() - chunkBatchStartTime;
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;
case PacketTypesIn.ChunkBatchStarted:
lastCHunkBatchStartedAt = DateTimeOffset.Now.ToUnixTimeMilliseconds();
chunkBatchStartTime = GetNanos();
break;
case PacketTypesIn.StartConfiguration:
SendAcknowledgeConfiguration();
Expand Down Expand Up @@ -4451,6 +4461,14 @@ private byte[] GenerateSalt()
randomGen.GetNonZeroBytes(salt);
return salt;
}

public static long GetNanos()
{
var nano = 10000L * Stopwatch.GetTimestamp();
nano /= TimeSpan.TicksPerMillisecond;
nano *= 100L;
return nano;
}
}

internal enum CurrentState
Expand Down

0 comments on commit a41b671

Please sign in to comment.