diff --git a/MinecraftClient/Mapping/DirectionExtensions.cs b/MinecraftClient/Mapping/DirectionExtensions.cs index 9a42368c..4f965cde 100644 --- a/MinecraftClient/Mapping/DirectionExtensions.cs +++ b/MinecraftClient/Mapping/DirectionExtensions.cs @@ -1,63 +1,42 @@ using System; -namespace MinecraftClient.Mapping +namespace MinecraftClient.Mapping; + +public static class DirectionExtensions { - public static class DirectionExtensions + public static Direction GetOpposite(this Direction direction) => direction switch { - public static Direction GetOpposite(this Direction direction) - { - switch (direction) - { - case Direction.SouthEast: - return Direction.NorthEast; - case Direction.SouthWest: - return Direction.NorthWest; + Direction.SouthEast => Direction.NorthEast, + Direction.SouthWest => Direction.NorthWest, + Direction.NorthEast => Direction.SouthEast, + Direction.NorthWest => Direction.SouthWest, + Direction.West => Direction.East, + Direction.East => Direction.West, + Direction.North => Direction.South, + Direction.South => Direction.North, + Direction.Down => Direction.Up, + Direction.Up => Direction.Down, + _ => Direction.Up, + }; - case Direction.NorthEast: - return Direction.SouthEast; - case Direction.NorthWest: - return Direction.SouthWest; + public static Direction[] HORIZONTAL = + [ + Direction.South, + Direction.West, + Direction.North, + Direction.East, + ]; - case Direction.West: - return Direction.East; - case Direction.East: - return Direction.West; + public static Direction FromRotation(double rotation) + { + double floor = Math.Floor((rotation / 90.0) + 0.5); + int value = (int)floor & 3; - case Direction.North: - return Direction.South; - case Direction.South: - return Direction.North; + return FromHorizontal(value); + } - case Direction.Down: - return Direction.Up; - case Direction.Up: - return Direction.Down; - default: - return Direction.Up; - - } - } - - - public static Direction[] HORIZONTAL = - { - Direction.South, - Direction.West, - Direction.North, - Direction.East - }; - - public static Direction FromRotation(double rotation) - { - double floor = Math.Floor((rotation / 90.0) + 0.5); - int value = (int)floor & 3; - - return FromHorizontal(value); - } - - public static Direction FromHorizontal(int value) - { - return HORIZONTAL[Math.Abs(value % HORIZONTAL.Length)]; - } + public static Direction FromHorizontal(int value) + { + return HORIZONTAL[Math.Abs(value % HORIZONTAL.Length)]; } } diff --git a/MinecraftClient/Mapping/Location.cs b/MinecraftClient/Mapping/Location.cs index 4a6a7320..a0bbbc35 100644 --- a/MinecraftClient/Mapping/Location.cs +++ b/MinecraftClient/Mapping/Location.cs @@ -116,7 +116,7 @@ namespace MinecraftClient.Mapping public static bool TryParse(string x, string y, string z, out Location? location) { - string[] coord_str = new string[] { x.Trim(), y.Trim(), z.Trim() }; + string[] coord_str = [x.Trim(), y.Trim(), z.Trim()]; double[] coord_res = new double[3]; for (int i = 0; i < 3; ++i) @@ -144,7 +144,7 @@ namespace MinecraftClient.Mapping public static Location Parse(Location current, string x, string y, string z) { Location.TryParse(current, x, y, z, out Location? res); - if (res == null) + if (res is null) throw new FormatException(); else return (Location)res; @@ -152,9 +152,9 @@ namespace MinecraftClient.Mapping public static bool TryParse(Location current, string x, string y, string z, out Location? location) { - string[] coord_str = new string[] { x.Trim(), y.Trim(), z.Trim() }; + string[] coord_str = [x.Trim(), y.Trim(), z.Trim()]; double[] coord_res = new double[3]; - double[] coord_cur = new double[] { current.X, current.Y, current.Z }; + double[] coord_cur = [current.X, current.Y, current.Z]; for (int i = 0; i < 3; ++i) { diff --git a/MinecraftClient/McClient.cs b/MinecraftClient/McClient.cs index e9ea713c..96762e74 100644 --- a/MinecraftClient/McClient.cs +++ b/MinecraftClient/McClient.cs @@ -1010,9 +1010,9 @@ namespace MinecraftClient b.SetHandler(this); bots.Add(b); if (init) - DispatchBotEvent(bot => bot.Initialize(), new ChatBot[] { b }); + DispatchBotEvent(bot => bot.Initialize(), [b]); if (handler is not null) - DispatchBotEvent(bot => bot.AfterGameJoined(), new ChatBot[] { b }); + DispatchBotEvent(bot => bot.AfterGameJoined(), [b]); } /// @@ -1205,7 +1205,7 @@ namespace MinecraftClient /// public static char[] GetDisallowedChatCharacters() { - return new char[] { (char)167, (char)127 }; // Minecraft color code and ASCII code DEL + return [(char)167, (char)127]; // Minecraft color code and ASCII code DEL } /// @@ -2381,23 +2381,18 @@ namespace MinecraftClient if (entities.ContainsKey(entityID)) { - switch (type) + return type switch { - case InteractType.Interact: - return handler.SendInteractEntity(entityID, (int)type, (int)hand); - - case InteractType.InteractAt: - return handler.SendInteractEntity( - EntityID: entityID, - type: (int)type, - X: (float)entities[entityID].Location.X, - Y: (float)entities[entityID].Location.Y, - Z: (float)entities[entityID].Location.Z, - hand: (int)hand); - - default: - return handler.SendInteractEntity(entityID, (int)type); - } + InteractType.Interact => handler.SendInteractEntity(entityID, (int)type, (int)hand), + InteractType.InteractAt => handler.SendInteractEntity( + EntityID: entityID, + type: (int)type, + X: (float)entities[entityID].Location.X, + Y: (float)entities[entityID].Location.Y, + Z: (float)entities[entityID].Location.Z, + hand: (int)hand), + _ => handler.SendInteractEntity(entityID, (int)type), + }; } return false; diff --git a/MinecraftClient/Protocol/Handlers/DataTypes.cs b/MinecraftClient/Protocol/Handlers/DataTypes.cs index e6ad0368..94b1f3ff 100644 --- a/MinecraftClient/Protocol/Handlers/DataTypes.cs +++ b/MinecraftClient/Protocol/Handlers/DataTypes.cs @@ -1351,7 +1351,7 @@ namespace MinecraftClient.Protocol.Handlers private byte[] GetNbt(Dictionary? nbt, bool root) { if (nbt is null || nbt.Count == 0) - return new byte[] { 0 }; // TAG_End + return [0]; // TAG_End List bytes = new(); diff --git a/MinecraftClient/Protocol/Handlers/Protocol18.cs b/MinecraftClient/Protocol/Handlers/Protocol18.cs index 73ce0f78..31ae3394 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol18.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol18.cs @@ -691,7 +691,7 @@ namespace MinecraftClient.Protocol.Handlers var responseHeader = protocolVersion < MC_1_10_Version // After 1.10, the MC does not include resource pack hash in responses ? dataTypes.ConcatBytes(DataTypes.GetVarInt(hash.Length), Encoding.UTF8.GetBytes(hash)) - : Array.Empty(); + : []; var basePacketData = protocolVersion >= MC_1_20_4_Version && uuid != Guid.Empty ? dataTypes.ConcatBytes(responseHeader, DataTypes.GetUUID(uuid)) @@ -3486,9 +3486,9 @@ namespace MinecraftClient.Protocol.Handlers return -1; var transactionId = DataTypes.GetVarInt(autocomplete_transaction_id); - var assumeCommand = new byte[] { 0x00 }; - var hasPosition = new byte[] { 0x00 }; - var tabCompletePacket = Array.Empty(); + byte[] assumeCommand = [0x00]; + byte[] hasPosition = [0x00]; + byte[] tabCompletePacket = []; switch (protocolVersion) { @@ -4009,7 +4009,7 @@ namespace MinecraftClient.Protocol.Handlers { try { - SendPacket(PacketTypesOut.ClientStatus, new byte[] { 0 }); + SendPacket(PacketTypesOut.ClientStatus, [0]); return true; } catch (SocketException) @@ -4064,7 +4064,7 @@ namespace MinecraftClient.Protocol.Handlers fields.AddRange(protocolVersion >= MC_1_9_Version ? DataTypes.GetVarInt(chatMode) - : new byte[] { chatMode }); + : [chatMode]); fields.Add(chatColors ? (byte)1 : (byte)0); if (protocolVersion < MC_1_8_Version) @@ -4163,7 +4163,7 @@ namespace MinecraftClient.Protocol.Handlers dataTypes.GetDouble(location.Y), protocolVersion < MC_1_8_Version ? dataTypes.GetDouble(location.Y + 1.62) - : Array.Empty(), + : [], dataTypes.GetDouble(location.Z), dataTypes.GetFloat(yaw.Value), dataTypes.GetFloat(pitch.Value), @@ -4181,7 +4181,7 @@ namespace MinecraftClient.Protocol.Handlers dataTypes.GetDouble(location.Y), protocolVersion < MC_1_8_Version ? dataTypes.GetDouble(location.Y + 1.62) - : Array.Empty(), + : [], dataTypes.GetDouble(location.Z), new[] { flags }); } @@ -4223,7 +4223,7 @@ namespace MinecraftClient.Protocol.Handlers dataTypes.GetDouble(location.Y), protocolVersion < MC_1_8_Version ? dataTypes.GetDouble(location.Y + 1.62) - : Array.Empty(), + : [], dataTypes.GetDouble(location.Z), dataTypes.GetFloat(yaw.Value), dataTypes.GetFloat(pitch.Value), @@ -4241,7 +4241,7 @@ namespace MinecraftClient.Protocol.Handlers dataTypes.GetDouble(location.Y), protocolVersion < MC_1_8_Version ? dataTypes.GetDouble(location.Y + 1.62) - : Array.Empty(), + : [], dataTypes.GetDouble(location.Z), new[] { flags }); } @@ -4549,7 +4549,7 @@ namespace MinecraftClient.Protocol.Handlers if (playerInventory?.Items is null) return false; - var slotWindowIds = new int[]{ 36, 37, 38, 39, 40, 41, 42, 43, 44 }; + int[] slotWindowIds = [36, 37, 38, 39, 40, 41, 42, 43, 44]; var currentSlot = ((McClient)handler).GetCurrentSlot(); playerInventory.Items.TryGetValue(slotWindowIds[currentSlot], out var item); diff --git a/MinecraftClient/Scripting/ChatBot.cs b/MinecraftClient/Scripting/ChatBot.cs index 1c84b7e7..8b612ab8 100644 --- a/MinecraftClient/Scripting/ChatBot.cs +++ b/MinecraftClient/Scripting/ChatBot.cs @@ -785,7 +785,7 @@ namespace MinecraftClient.Scripting string prefix = tmp[0]; string user = tmp[1]; string semicolon = tmp[2]; - if (prefix.All(c => char.IsLetterOrDigit(c) || new char[] { '*', '<', '>', '_' }.Contains(c)) + if (prefix.All(c => char.IsLetterOrDigit(c) || new[] { '*', '<', '>', '_' }.Contains(c)) && semicolon == ":") { message = text[(prefix.Length + user.Length + 4)..]; @@ -878,7 +878,7 @@ namespace MinecraftClient.Scripting catch { return; /* Invalid file name or access denied */ } } - File.AppendAllLines(logfile, new string[] { GetTimestamp() + ' ' + text }); + File.AppendAllLines(logfile, [GetTimestamp() + ' ' + text]); } } @@ -898,7 +898,7 @@ namespace MinecraftClient.Scripting catch { return; /* Invalid file name or access denied */ } } - File.AppendAllLines(logfile, new string[] { GetTimestamp() + ' ' + text }); + File.AppendAllLines(logfile, [GetTimestamp() + ' ' + text]); } } @@ -1218,7 +1218,7 @@ namespace MinecraftClient.Scripting else { LogToConsole("File not found: " + Path.GetFullPath(file)); - return Array.Empty(); + return []; } }