diff --git a/MinecraftClient/McClient.cs b/MinecraftClient/McClient.cs index f4852a65..a133b206 100644 --- a/MinecraftClient/McClient.cs +++ b/MinecraftClient/McClient.cs @@ -61,7 +61,6 @@ namespace MinecraftClient private float playerYaw; private float playerPitch; private double motionY; - private CancellationTokenSource chunkProcessCancelSource = new(); public enum MovementType { Sneak, Walk, Sprint} public int currentMovementSpeed = 4; private int sequenceId; // User for player block synchronization (Aka. digging, placing blocks, etc..) @@ -113,7 +112,6 @@ namespace MinecraftClient public int GetSequenceId() { return sequenceId; } public float GetPitch() { return playerPitch; } public World GetWorld() { return world; } - public CancellationToken GetChunkProcessCancelToken() { return chunkProcessCancelSource.Token; } public Double GetServerTPS() { return averageTPS; } public bool GetIsSupportPreviewsChat() { return isSupportPreviewsChat; } public float GetHealth() { return playerHealth; } @@ -478,9 +476,7 @@ namespace MinecraftClient /// public void OnConnectionLost(ChatBot.DisconnectReason reason, string message) { - chunkProcessCancelSource.Cancel(); world.Clear(); - chunkProcessCancelSource = new(); if (timeoutdetector != null) { @@ -908,9 +904,7 @@ namespace MinecraftClient terrainAndMovementsEnabled = false; terrainAndMovementsRequested = false; locationReceived = false; - chunkProcessCancelSource.Cancel(); world.Clear(); - chunkProcessCancelSource = new(); } return true; } @@ -1951,9 +1945,7 @@ namespace MinecraftClient if (terrainAndMovementsEnabled) { - chunkProcessCancelSource.Cancel(); world.Clear(); - chunkProcessCancelSource = new(); } entities.Clear(); diff --git a/MinecraftClient/Protocol/Handlers/Protocol18.cs b/MinecraftClient/Protocol/Handlers/Protocol18.cs index 4d652c6f..38d89d50 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol18.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol18.cs @@ -538,8 +538,6 @@ namespace MinecraftClient.Protocol.Handlers case PacketTypesIn.ChunkData: if (handler.GetTerrainEnabled()) { - CancellationToken cancellationToken = handler.GetChunkProcessCancelToken(); - Interlocked.Increment(ref handler.GetWorld().chunkCnt); Interlocked.Increment(ref handler.GetWorld().chunkLoadNotCompleted); @@ -563,8 +561,8 @@ namespace MinecraftClient.Protocol.Handlers int dataSize = dataTypes.ReadNextVarInt(packetData); // Size - if (pTerrain.ProcessChunkColumnData(chunkX, chunkZ, verticalStripBitmask, packetData, cancellationToken)) - Interlocked.Decrement(ref handler.GetWorld().chunkLoadNotCompleted); + pTerrain.ProcessChunkColumnData(chunkX, chunkZ, verticalStripBitmask, packetData); + Interlocked.Decrement(ref handler.GetWorld().chunkLoadNotCompleted); // Block Entity data: ignored // Light data: ignored @@ -584,8 +582,8 @@ namespace MinecraftClient.Protocol.Handlers byte[] compressed = dataTypes.ReadData(compressedDataSize, packetData); byte[] decompressed = ZlibUtils.Decompress(compressed); - if (pTerrain.ProcessChunkColumnData(chunkX, chunkZ, chunkMask, addBitmap, currentDimension == 0, chunksContinuous, currentDimension, new Queue(decompressed), cancellationToken)) - Interlocked.Decrement(ref handler.GetWorld().chunkLoadNotCompleted); + pTerrain.ProcessChunkColumnData(chunkX, chunkZ, chunkMask, addBitmap, currentDimension == 0, chunksContinuous, currentDimension, new Queue(decompressed)); + Interlocked.Decrement(ref handler.GetWorld().chunkLoadNotCompleted); } else { @@ -610,8 +608,8 @@ namespace MinecraftClient.Protocol.Handlers } int dataSize = dataTypes.ReadNextVarInt(packetData); - if (pTerrain.ProcessChunkColumnData(chunkX, chunkZ, chunkMask, 0, false, chunksContinuous, currentDimension, packetData, cancellationToken)) - Interlocked.Decrement(ref handler.GetWorld().chunkLoadNotCompleted); + pTerrain.ProcessChunkColumnData(chunkX, chunkZ, chunkMask, 0, false, chunksContinuous, currentDimension, packetData); + Interlocked.Decrement(ref handler.GetWorld().chunkLoadNotCompleted); } } } @@ -829,8 +827,6 @@ namespace MinecraftClient.Protocol.Handlers case PacketTypesIn.MapChunkBulk: if (protocolversion < MC_1_9_Version && handler.GetTerrainEnabled()) { - CancellationToken cancellationToken = handler.GetChunkProcessCancelToken(); - int chunkCount; bool hasSkyLight; Queue chunkData = packetData; @@ -867,17 +863,11 @@ namespace MinecraftClient.Protocol.Handlers } //Process chunk records - Parallel.Invoke(() => + for (int chunkColumnNo = 0; chunkColumnNo < chunkCount; chunkColumnNo++) { - for (int chunkColumnNo = 0; chunkColumnNo < chunkCount; chunkColumnNo++) - { - if (cancellationToken.IsCancellationRequested) - break; - bool loaded = pTerrain.ProcessChunkColumnData(chunkXs[chunkColumnNo], chunkZs[chunkColumnNo], chunkMasks[chunkColumnNo], addBitmaps[chunkColumnNo], hasSkyLight, true, currentDimension, chunkData, cancellationToken); - if (loaded) - Interlocked.Decrement(ref handler.GetWorld().chunkLoadNotCompleted); - } - }); + pTerrain.ProcessChunkColumnData(chunkXs[chunkColumnNo], chunkZs[chunkColumnNo], chunkMasks[chunkColumnNo], addBitmaps[chunkColumnNo], hasSkyLight, true, currentDimension, chunkData); + Interlocked.Decrement(ref handler.GetWorld().chunkLoadNotCompleted); + } } break; diff --git a/MinecraftClient/Protocol/Handlers/Protocol18Terrain.cs b/MinecraftClient/Protocol/Handlers/Protocol18Terrain.cs index cf4b932f..b99ecf3c 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol18Terrain.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol18Terrain.cs @@ -148,13 +148,9 @@ namespace MinecraftClient.Protocol.Handlers /// Chunk mask for reading data, store in bitset, used in 1.17 and 1.17.1 /// Cache for reading chunk data /// token to cancel the task - /// true if successfully loaded [MethodImpl(MethodImplOptions.AggressiveOptimization)] - public bool ProcessChunkColumnData(int chunkX, int chunkZ, ulong[]? verticalStripBitmask, Queue cache, CancellationToken cancellationToken) + public void ProcessChunkColumnData(int chunkX, int chunkZ, ulong[]? verticalStripBitmask, Queue cache) { - if (cancellationToken.IsCancellationRequested) - return false; - World world = handler.GetWorld(); int chunkColumnSize = (World.GetDimension().height + 15) / 16; // Round up @@ -184,9 +180,6 @@ namespace MinecraftClient.Protocol.Handlers lastChunkY = chunkColumnSize - 1; } - if (cancellationToken.IsCancellationRequested) - return false; - // 1.18 and above always contains all chunk section in data // 1.17 and 1.17.1 need vertical strip bitmask to know if the chunk section is included if ((protocolversion >= Protocol18Handler.MC_1_18_1_Version) || @@ -198,10 +191,6 @@ namespace MinecraftClient.Protocol.Handlers // Read Block states (Type: Paletted Container) Chunk? chunk = ReadBlockStatesField(cache); - // check before store chunk - if (cancellationToken.IsCancellationRequested) - return false; - //We have our chunk, save the chunk into the world handler.InvokeOnMainThread(() => { @@ -235,7 +224,6 @@ namespace MinecraftClient.Protocol.Handlers // Don't worry about skipping remaining data since there is no useful data afterwards in 1.9 // (plus, it would require parsing the tile entity lists' NBT) } - return true; } /// @@ -250,13 +238,9 @@ namespace MinecraftClient.Protocol.Handlers /// Current dimension type (0 = overworld) /// Cache for reading chunk data /// token to cancel the task - /// true if successfully loaded [MethodImpl(MethodImplOptions.AggressiveOptimization)] - public bool ProcessChunkColumnData(int chunkX, int chunkZ, ushort chunkMask, ushort chunkMask2, bool hasSkyLight, bool chunksContinuous, int currentDimension, Queue cache, CancellationToken cancellationToken) + public void ProcessChunkColumnData(int chunkX, int chunkZ, ushort chunkMask, ushort chunkMask2, bool hasSkyLight, bool chunksContinuous, int currentDimension, Queue cache) { - if (cancellationToken.IsCancellationRequested) - return false; - World world = handler.GetWorld(); const int chunkColumnSize = 16; @@ -267,9 +251,6 @@ namespace MinecraftClient.Protocol.Handlers int maxChunkY = sizeof(int) * 8 - 1 - BitOperations.LeadingZeroCount(chunkMask); for (int chunkY = 0; chunkY <= maxChunkY; chunkY++) { - if (cancellationToken.IsCancellationRequested) - return false; - if ((chunkMask & (1 << chunkY)) != 0) { // 1.14 and above Non-air block count inside chunk section, for lighting purposes @@ -385,10 +366,6 @@ namespace MinecraftClient.Protocol.Handlers } } - // check before store chunk - if (cancellationToken.IsCancellationRequested) - return false; - //We have our chunk, save the chunk into the world handler.InvokeOnMainThread(() => { @@ -429,9 +406,6 @@ namespace MinecraftClient.Protocol.Handlers int maxChunkY = sizeof(int) * 8 - 1 - BitOperations.LeadingZeroCount(chunkMask); for (int chunkY = 0; chunkY <= maxChunkY; chunkY++) { - if (cancellationToken.IsCancellationRequested) - return false; - if ((chunkMask & (1 << chunkY)) != 0) { Chunk chunk = new Chunk(); @@ -443,10 +417,6 @@ namespace MinecraftClient.Protocol.Handlers for (int blockX = 0; blockX < Chunk.SizeX; blockX++) chunk.SetWithoutCheck(blockX, blockY, blockZ, new Block(queue.Dequeue())); - // check before store chunk - if (cancellationToken.IsCancellationRequested) - return false; - //We have our chunk, save the chunk into the world handler.InvokeOnMainThread(() => { @@ -530,10 +500,6 @@ namespace MinecraftClient.Protocol.Handlers for (int blockX = 0; blockX < Chunk.SizeX; blockX++) chunk.SetWithoutCheck(blockX, blockY, blockZ, new Block(blockTypes.Dequeue(), blockMeta.Dequeue())); - // check before store chunk - if (cancellationToken.IsCancellationRequested) - return false; - handler.InvokeOnMainThread(() => { world.StoreChunk(chunkX, chunkY, chunkZ, chunkColumnSize, chunk, chunkY == maxChunkY); @@ -542,7 +508,6 @@ namespace MinecraftClient.Protocol.Handlers } } } - return true; } } } diff --git a/MinecraftClient/Protocol/IMinecraftComHandler.cs b/MinecraftClient/Protocol/IMinecraftComHandler.cs index 60300976..96b4e4b7 100644 --- a/MinecraftClient/Protocol/IMinecraftComHandler.cs +++ b/MinecraftClient/Protocol/IMinecraftComHandler.cs @@ -29,7 +29,6 @@ namespace MinecraftClient.Protocol PlayerInfo? GetPlayerInfo(Guid uuid); Location GetCurrentLocation(); World GetWorld(); - public System.Threading.CancellationToken GetChunkProcessCancelToken(); bool GetIsSupportPreviewsChat(); bool GetTerrainEnabled(); bool SetTerrainEnabled(bool enabled);