refactor: modernize null-check patterns in McClient.cs

Replace '== null' with 'is null' and '!= null' with 'is not null'
across 51 occurrences to use idiomatic C# pattern matching.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2026-03-24 00:21:37 +00:00
parent 74def7c512
commit cf34db526b

View file

@ -210,9 +210,9 @@ namespace MinecraftClient
{
scope.SetTag("Protocol Version", protocolversion.ToString());
scope.SetTag("Minecraft Version", ProtocolHandler.ProtocolVersion2MCVer(protocolversion));
scope.SetTag("MCC Build", Program.BuildInfo == null ? "Debug" : Program.BuildInfo);
scope.SetTag("MCC Build", Program.BuildInfo is null ? "Debug" : Program.BuildInfo);
if (forgeInfo != null)
if (forgeInfo is not null)
scope.SetTag("Forge Version", forgeInfo?.Version.ToString());
scope.Contexts["Server Information"] = new
@ -288,7 +288,7 @@ namespace MinecraftClient
return;
Retry:
if (timeoutdetector != null)
if (timeoutdetector is not null)
{
timeoutdetector.Item2.Cancel();
timeoutdetector = null;
@ -377,7 +377,7 @@ namespace MinecraftClient
Log.Error($"Transfer to {newHost}:{newPort} failed: {ex.Message}");
// Handle reconnection attempts
if (timeoutdetector != null)
if (timeoutdetector is not null)
{
timeoutdetector.Item2.Cancel();
timeoutdetector = null;
@ -514,8 +514,8 @@ namespace MinecraftClient
UpdatePathfindingInput();
// Sync yaw/pitch if explicitly set (by commands/bots)
if (_yaw != null) playerPhysics.Yaw = _yaw.Value;
if (_pitch != null) playerPhysics.Pitch = _pitch.Value;
if (_yaw is not null) playerPhysics.Yaw = _yaw.Value;
if (_pitch is not null) playerPhysics.Pitch = _pitch.Value;
// Update environment flags (water, lava, climbable)
playerPhysics.UpdateEnvironment(world);
@ -563,7 +563,7 @@ namespace MinecraftClient
{
if (RemainingDiggingTime > 0)
{
if (--RemainingDiggingTime == 0 && LastDigPosition != null)
if (--RemainingDiggingTime == 0 && LastDigPosition is not null)
{
handler.SendPlayerDigging(2, LastDigPosition.Item1, LastDigPosition.Item2, sequenceId++);
Log.Info(string.Format(Translations.cmd_dig_end, LastDigPosition.Item1));
@ -627,25 +627,25 @@ namespace MinecraftClient
botsOnHold.Clear();
botsOnHold.AddRange(bots);
if (handler != null)
if (handler is not null)
{
handler.Disconnect();
handler.Dispose();
}
if (cmdprompt != null)
if (cmdprompt is not null)
{
cmdprompt.Cancel();
cmdprompt = null;
}
if (timeoutdetector != null)
if (timeoutdetector is not null)
{
timeoutdetector.Item2.Cancel();
timeoutdetector = null;
}
if (client != null)
if (client is not null)
client.Close();
}
@ -660,9 +660,9 @@ namespace MinecraftClient
world.Clear();
if (timeoutdetector != null)
if (timeoutdetector is not null)
{
if (timeoutdetector != null && Thread.CurrentThread != timeoutdetector.Item1)
if (timeoutdetector is not null && Thread.CurrentThread != timeoutdetector.Item1)
timeoutdetector.Item2.Cancel();
timeoutdetector = null;
}
@ -728,7 +728,7 @@ namespace MinecraftClient
private void ConsoleReaderOnMessageReceived(object? sender, string e)
{
if (client.Client == null)
if (client.Client is null)
return;
if (client.Client.Connected)
@ -980,7 +980,7 @@ namespace MinecraftClient
get
{
int callingThreadId = Environment.CurrentManagedThreadId;
if (handler != null)
if (handler is not null)
{
return handler.GetNetMainThreadId() != callingThreadId;
}
@ -1011,7 +1011,7 @@ namespace MinecraftClient
bots.Add(b);
if (init)
DispatchBotEvent(bot => bot.Initialize(), new ChatBot[] { b });
if (handler != null)
if (handler is not null)
DispatchBotEvent(bot => bot.AfterGameJoined(), new ChatBot[] { b });
}
@ -1353,7 +1353,7 @@ namespace MinecraftClient
{
pathTarget = null;
path = Movement.CalculatePath(world, location, goal, allowUnsafe, maxOffset, minOffset, timeout ?? TimeSpan.FromSeconds(5));
return path != null;
return path is not null;
}
}
}
@ -1591,7 +1591,7 @@ namespace MinecraftClient
// Update our inventory base on action type
Container inventory = GetInventory(windowId)!;
Container playerInventory = GetInventory(0)!;
if (inventory != null)
if (inventory is not null)
{
switch (action)
{
@ -1738,7 +1738,7 @@ namespace MinecraftClient
case WindowActionType.ShiftClick:
case WindowActionType.ShiftRightClick:
if (slotId == 0) break;
if (item != null)
if (item is not null)
{
/* Target slot have item */
@ -1758,7 +1758,7 @@ namespace MinecraftClient
upper2backpack = true;
lowerStartSlot = 9;
}
else if (item != null && false /* Check if wearable */)
else if (item is not null && false /* Check if wearable */)
{
lower2upper = true;
// upperStartSlot = ?;
@ -1894,7 +1894,7 @@ namespace MinecraftClient
upper2backpack = true;
lowerStartSlot = 1;
}
else if (item != null && item.Count == 1 && (item.Type == ItemType.NetheriteIngot ||
else if (item is not null && item.Count == 1 && (item.Type == ItemType.NetheriteIngot ||
item.Type == ItemType.Emerald || item.Type == ItemType.Diamond || item.Type == ItemType.GoldIngot ||
item.Type == ItemType.IronIngot) && !inventory.Items.ContainsKey(0))
{
@ -1927,7 +1927,7 @@ namespace MinecraftClient
upper2backpack = true;
lowerStartSlot = 3;
}
else if (item != null && false /* Check if it can be burned */)
else if (item is not null && false /* Check if it can be burned */)
{
lower2upper = true;
upperStartSlot = 0;
@ -1954,7 +1954,7 @@ namespace MinecraftClient
upper2backpack = true;
lowerStartSlot = 5;
}
else if (item != null && item.Type == ItemType.BlazePowder)
else if (item is not null && item.Type == ItemType.BlazePowder)
{
lower2upper = true;
if (!inventory.Items.ContainsKey(4) || inventory.Items[4].Count < 64)
@ -1962,12 +1962,12 @@ namespace MinecraftClient
else
upperStartSlot = upperEndSlot = 3;
}
else if (item != null && false /* Check if it can be used for alchemy */)
else if (item is not null && false /* Check if it can be used for alchemy */)
{
lower2upper = true;
upperStartSlot = upperEndSlot = 3;
}
else if (item != null && (item.Type == ItemType.Potion || item.Type == ItemType.GlassBottle))
else if (item is not null && (item.Type == ItemType.Potion || item.Type == ItemType.GlassBottle))
{
lower2upper = true;
upperStartSlot = 0;
@ -2009,7 +2009,7 @@ namespace MinecraftClient
upper2backpack = true;
lowerStartSlot = 5;
}
else if (item != null && item.Type == ItemType.LapisLazuli)
else if (item is not null && item.Type == ItemType.LapisLazuli)
{
lower2upper = true;
upperStartSlot = upperEndSlot = 1;
@ -2029,7 +2029,7 @@ namespace MinecraftClient
upper2backpack = true;
lowerStartSlot = 3;
}
else if (item != null && false /* Check */)
else if (item is not null && false /* Check */)
{
lower2upper = true;
upperStartSlot = 0;
@ -2066,7 +2066,7 @@ namespace MinecraftClient
upper2backpack = true;
lowerStartSlot = 4;
}
else if (item != null && false /* Check for availability for staining */)
else if (item is not null && false /* Check for availability for staining */)
{
lower2upper = true;
// upperStartSlot = ?;
@ -2095,7 +2095,7 @@ namespace MinecraftClient
upper2backpack = true;
lowerStartSlot = 3;
}
else if (item != null && false /* Check if it is available for trading */)
else if (item is not null && false /* Check if it is available for trading */)
{
lower2upper = true;
upperStartSlot = 0;
@ -2124,12 +2124,12 @@ namespace MinecraftClient
upper2backpack = true;
lowerStartSlot = 3;
}
else if (item != null && item.Type == ItemType.FilledMap)
else if (item is not null && item.Type == ItemType.FilledMap)
{
lower2upper = true;
upperStartSlot = upperEndSlot = 0;
}
else if (item != null && item.Type == ItemType.Map)
else if (item is not null && item.Type == ItemType.Map)
{
lower2upper = true;
upperStartSlot = upperEndSlot = 1;
@ -2157,7 +2157,7 @@ namespace MinecraftClient
upper2backpack = true;
lowerStartSlot = 2;
}
else if (item != null && false /* Check if it is available for stone cutteing */)
else if (item is not null && false /* Check if it is available for stone cutteing */)
{
lower2upper = true;
upperStartSlot = 0;
@ -2442,7 +2442,7 @@ namespace MinecraftClient
lock (DigLock)
{
if (RemainingDiggingTime > 0 && LastDigPosition != null)
if (RemainingDiggingTime > 0 && LastDigPosition is not null)
{
handler.SendPlayerDigging(1, LastDigPosition.Item1, LastDigPosition.Item2, sequenceId++);
Log.Info(string.Format(Translations.cmd_dig_cancel, LastDigPosition.Item1));
@ -2587,7 +2587,7 @@ namespace MinecraftClient
{
ChatBot[] selectedBots;
if (botList != null)
if (botList is not null)
{
selectedBots = botList.ToArray();
}
@ -2639,7 +2639,7 @@ namespace MinecraftClient
/// </summary>
public void OnGameJoined(bool isOnlineMode)
{
if (protocolversion < Protocol18Handler.MC_1_19_3_Version || playerKeyPair == null || !isOnlineMode)
if (protocolversion < Protocol18Handler.MC_1_19_3_Version || playerKeyPair is null || !isOnlineMode)
SetCanSendMessage(true);
else
SetCanSendMessage(false);
@ -2659,7 +2659,7 @@ namespace MinecraftClient
(byte)Config.MCSettings.MainHand);
if (protocolversion >= Protocol18Handler.MC_1_19_3_Version
&& playerKeyPair != null && isOnlineMode)
&& playerKeyPair is not null && isOnlineMode)
handler.SendPlayerSession(playerKeyPair);
if (inventoryHandlingRequested)
@ -2709,10 +2709,10 @@ namespace MinecraftClient
physicsInput.Reset();
// Still heading toward a target (even if path queue is empty)
if (pathTarget != null && ReachedWaypoint(pathTarget.Value))
if (pathTarget is not null && ReachedWaypoint(pathTarget.Value))
{
// Arrived at current waypoint — advance to next, or finish
if (path != null && path.Count > 0)
if (path is not null && path.Count > 0)
{
pathTarget = path.Dequeue();
if (Config.Main.Advanced.MoveHeadWhileWalking)
@ -2726,14 +2726,14 @@ namespace MinecraftClient
}
// Need a first target from a fresh path
if (pathTarget == null && path != null && path.Count > 0)
if (pathTarget is null && path is not null && path.Count > 0)
{
pathTarget = path.Dequeue();
if (Config.Main.Advanced.MoveHeadWhileWalking)
UpdateLocation(location, pathTarget.Value + new Location(0, 1, 0));
}
if (pathTarget != null)
if (pathTarget is not null)
{
SetInputToward(pathTarget.Value);
}
@ -2787,7 +2787,7 @@ namespace MinecraftClient
/// <returns>true if a movement is currently handled</returns>
public bool ClientIsMoving()
{
return terrainAndMovementsEnabled && locationReceived && ((steps != null && steps.Count > 0) || (path != null && path.Count > 0));
return terrainAndMovementsEnabled && locationReceived && ((steps is not null && steps.Count > 0) || (path is not null && path.Count > 0));
}
/// <summary>
@ -2796,7 +2796,7 @@ namespace MinecraftClient
/// <returns>Current goal of movement. Location.Zero if not set.</returns>
public Location GetCurrentMovementGoal()
{
return (ClientIsMoving() || path == null) ? Location.Zero : path.Last();
return (ClientIsMoving() || path is null) ? Location.Zero : path.Last();
}
/// <summary>
@ -2959,7 +2959,7 @@ namespace MinecraftClient
{
if ((bool)message.isSignatureLegal!)
{
if (Config.Signature.ShowModifiedChat && message.unsignedContent != null)
if (Config.Signature.ShowModifiedChat && message.unsignedContent is not null)
{
if (Config.Signature.MarkModifiedMsg)
color = "§6▌§r"; // Background Yellow
@ -3181,7 +3181,7 @@ namespace MinecraftClient
inventoryID = 0; // Prevent key not found for some bots relied to this event
if (inventories.ContainsKey(0))
{
if (item != null)
if (item is not null)
inventories[0].Items[-1] = item;
else
inventories[0].Items.Remove(-1);
@ -3191,7 +3191,7 @@ namespace MinecraftClient
{
if (inventories.ContainsKey(inventoryID))
{
if (item == null || item.IsEmpty)
if (item is null || item.IsEmpty)
{
if (inventories[inventoryID].Items.ContainsKey(slotID))
inventories[inventoryID].Items.Remove(slotID);
@ -3353,7 +3353,7 @@ namespace MinecraftClient
Entity entity = entities[entityid];
if (entity.Equipment.ContainsKey(slot))
entity.Equipment.Remove(slot);
if (item != null)
if (item is not null)
entity.Equipment[slot] = item;
DispatchBotEvent(bot => bot.OnEntityEquipment(entities[entityid], slot, item));
}
@ -3729,24 +3729,24 @@ namespace MinecraftClient
entity.Metadata = metadata;
int itemEntityMetadataFieldIndex = protocolversion < Protocol18Handler.MC_1_17_Version ? 7 : 8;
if (entity.Type.ContainsItem() && metadata.TryGetValue(itemEntityMetadataFieldIndex, out object? itemObj) && itemObj != null && itemObj.GetType() == typeof(Item))
if (entity.Type.ContainsItem() && metadata.TryGetValue(itemEntityMetadataFieldIndex, out object? itemObj) && itemObj is not null && itemObj.GetType() == typeof(Item))
{
Item item = (Item)itemObj;
if (item == null)
if (item is null)
entity.Item = new Item(ItemType.Air, 0, null);
else entity.Item = item;
}
if (metadata.TryGetValue(6, out object? poseObj) && poseObj != null && poseObj.GetType() == typeof(Int32))
if (metadata.TryGetValue(6, out object? poseObj) && poseObj is not null && poseObj.GetType() == typeof(Int32))
{
entity.Pose = (EntityPose)poseObj;
}
if (metadata.TryGetValue(2, out object? nameObj) && nameObj != null && nameObj.GetType() == typeof(string))
if (metadata.TryGetValue(2, out object? nameObj) && nameObj is not null && nameObj.GetType() == typeof(string))
{
string name = nameObj.ToString() ?? string.Empty;
entity.CustomNameJson = name;
entity.CustomName = ChatParser.ParseText(name);
}
if (metadata.TryGetValue(3, out object? nameVisableObj) && nameVisableObj != null && nameVisableObj.GetType() == typeof(bool))
if (metadata.TryGetValue(3, out object? nameVisableObj) && nameVisableObj is not null && nameVisableObj.GetType() == typeof(bool))
{
entity.IsCustomNameVisible = bool.Parse(nameVisableObj.ToString() ?? string.Empty);
}