Modernize lock object declarations from object to System.Threading.Lock

Replace all lock object declarations using 'object' type with the C# 13
System.Threading.Lock type across 12 files. The Lock type provides a
more efficient locking mechanism - when used with lock(), the compiler
automatically uses Lock.EnterScope() instead of Monitor.Enter/Exit.

Also made two previously non-readonly lock fields readonly:
- McClient.DigLock
- Protocol18.MessageSigningLock

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2026-03-24 00:15:21 +00:00
parent e09b997cdf
commit 74def7c512
12 changed files with 24 additions and 16 deletions

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Brigadier.NET.Builder;
using MinecraftClient.CommandHandler;
using MinecraftClient.CommandHandler.Patch;
@ -102,7 +103,7 @@ namespace MinecraftClient.ChatBots
private bool inventoryEnabled;
private int counter = 0;
private readonly object stateLock = new();
private readonly Lock stateLock = new();
private State state = State.WaitJoinGame;
bool AlreadyWaitting = false;

View file

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Brigadier.NET.Builder;
using MinecraftClient.CommandHandler;
using MinecraftClient.CommandHandler.Patch;
@ -175,7 +176,7 @@ namespace MinecraftClient.ChatBots
private Entity fishItem = new(-1, EntityType.Item, Location.Zero);
private int counter = 0;
private readonly object stateLock = new();
private readonly Lock stateLock = new();
private FishingState state = FishingState.WaitJoinGame;
private int curLocationIdx = 0, moveDir = 1;

View file

@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Threading;
using MinecraftClient.CommandHandler;
using MinecraftClient.Scripting;
using Tomlet.Attributes;
@ -50,7 +51,7 @@ namespace MinecraftClient.ChatBots
private bool saveChat = true;
private bool savePrivate = true;
private bool saveInternal = true;
private readonly object logfileLock = new();
private readonly Lock logfileLock = new();
/// <summary>
/// This bot saves the messages received in the specified file, with some filters and date/time tagging.

View file

@ -361,8 +361,8 @@ namespace MinecraftClient.ChatBots
private readonly byte[] _buffer = new byte[PipeFrame.MAX_SIZE];
private readonly Queue<PipeFrame> _frameQueue = new();
private readonly object _frameQueueLock = new();
private readonly object _streamLock = new();
private readonly Lock _frameQueueLock = new();
private readonly Lock _streamLock = new();
private int _connectedPipe;
private NamedPipeClientStream? _stream;

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading;
using Brigadier.NET;
using Brigadier.NET.Builder;
using MinecraftClient.CommandHandler;
@ -218,7 +219,7 @@ namespace MinecraftClient.ChatBots
private IgnoreList ignoreList = new();
private FileMonitor? mailDbFileMonitor;
private FileMonitor? ignoreListFileMonitor;
private readonly object readWriteLock = new();
private readonly Lock readWriteLock = new();
/// <summary>
/// Initialization of the Mailer bot

View file

@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Threading;
using MinecraftClient.Scripting;
namespace MinecraftClient.Logger
@ -8,7 +9,7 @@ namespace MinecraftClient.Logger
{
private readonly string logFile;
private readonly bool prependTimestamp;
private readonly object logFileLock = new();
private readonly Lock logFileLock = new();
public FileLogLogger(string file, bool prependTimestamp = false)
{

View file

@ -43,7 +43,7 @@ namespace MinecraftClient
private static DateTime nextMessageSendTime = DateTime.MinValue;
private readonly Queue<Action> threadTasks = new();
private readonly object threadTasksLock = new();
private readonly Lock threadTasksLock = new();
private readonly List<ChatBot> bots = new();
private static readonly List<ChatBot> botsOnHold = new();
@ -58,7 +58,7 @@ namespace MinecraftClient
private bool inventoryHandlingRequested = false;
private bool entityHandlingEnabled;
private readonly object locationLock = new();
private readonly Lock locationLock = new();
private bool locationReceived = false;
private readonly World world = new();
private Queue<Location>? steps;
@ -86,7 +86,7 @@ namespace MinecraftClient
private readonly string sessionid;
private readonly PlayerKeyPair? playerKeyPair;
private DateTime lastKeepAlive;
private readonly object lastKeepAliveLock = new();
private readonly Lock lastKeepAliveLock = new();
private int respawnTicks = 0;
private int gamemode = 0;
private bool isSupportPreviewsChat;
@ -94,7 +94,7 @@ namespace MinecraftClient
private int playerEntityID;
private object DigLock = new();
private readonly Lock DigLock = new();
private Tuple<Location, Direction>? LastDigPosition;
private int RemainingDiggingTime = 0;

View file

@ -102,7 +102,7 @@ namespace MinecraftClient.Protocol.Handlers
private int oldSamplesWeight = 1;
private bool receiveDeclareCommands = false, receivePlayerInfo = false;
private object MessageSigningLock = new();
private readonly Lock MessageSigningLock = new();
private Guid chatUuid = Guid.NewGuid();
private int pendingAcknowledgments = 0, messageIndex = 0;
private LastSeenMessagesCollector lastSeenMessagesCollector;

View file

@ -4,6 +4,7 @@ using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using Brigadier.NET;
using MinecraftClient.CommandHandler;
using MinecraftClient.Inventory;
@ -42,7 +43,7 @@ namespace MinecraftClient.Scripting
private McClient? _handler = null;
private ChatBot? master = null;
private readonly List<string> registeredPluginChannels = new();
private readonly object delayTasksLock = new();
private readonly Lock delayTasksLock = new();
private readonly List<TaskWithDelay> delayedTasks = new();
protected McClient Handler
{

View file

@ -1011,7 +1011,7 @@ namespace MinecraftClient
private readonly Dictionary<string, object> VarObject = new();
[NonSerialized]
readonly object varLock = new();
readonly Lock varLock = new();
/// <summary>
/// Set a custom %variable% which will be available through expandVars()

View file

@ -14,7 +14,7 @@ namespace MinecraftClient
private T? result = default;
private Exception? exception = null;
private bool taskRun = false;
private readonly object taskRunLock = new();
private readonly Lock taskRunLock = new();
/// <summary>
/// Create a new asynchronous task with return value

View file

@ -4,12 +4,14 @@ MCC.LoadBot(new PacketCadenceCaptureBot());
//MCCScript Extensions
using System.Threading;
public class PacketCadenceCaptureBot : ChatBot
{
private const int CaptureDurationSeconds = 5;
private const int CaptureDurationTicks = CaptureDurationSeconds * 20;
private readonly object _countsLock = new();
private readonly Lock _countsLock = new();
private readonly Dictionary<string, int> _counts = new()
{
{ "PlayerMovement", 0 },