mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2026-08-15 13:04:36 +00:00
Convert switch statements to expressions and adopt collection expressions
- DirectionExtensions.cs: Convert GetOpposite() to switch expression, use file-scoped namespace, use collection expression for HORIZONTAL - McClient.cs: Convert InteractType switch to expression, use collection expressions for array literals - Protocol18.cs: Replace Array.Empty<byte>() with [], use collection expressions for byte/int array literals - DataTypes.cs: Use collection expression for TAG_End byte array - ChatBot.cs: Use collection expressions for string/char arrays - Location.cs: Use collection expressions and modernize null check Co-authored-by: milutinke <441903+milutinke@users.noreply.github.com> Agent-Logs-Url: https://github.com/milutinke/Minecraft-Console-Client/sessions/4e9bd25b-22c5-47c2-98f9-6025d927b1d6
This commit is contained in:
parent
c5df6a49c6
commit
2ae31e269f
6 changed files with 66 additions and 92 deletions
|
|
@ -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)];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -1205,7 +1205,7 @@ namespace MinecraftClient
|
|||
/// <returns></returns>
|
||||
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
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -1351,7 +1351,7 @@ namespace MinecraftClient.Protocol.Handlers
|
|||
private byte[] GetNbt(Dictionary<string, object>? nbt, bool root)
|
||||
{
|
||||
if (nbt is null || nbt.Count == 0)
|
||||
return new byte[] { 0 }; // TAG_End
|
||||
return [0]; // TAG_End
|
||||
|
||||
List<byte> bytes = new();
|
||||
|
||||
|
|
|
|||
|
|
@ -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<byte>();
|
||||
: [];
|
||||
|
||||
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>();
|
||||
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<byte>(),
|
||||
: [],
|
||||
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<byte>(),
|
||||
: [],
|
||||
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<byte>(),
|
||||
: [],
|
||||
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<byte>(),
|
||||
: [],
|
||||
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);
|
||||
|
|
|
|||
|
|
@ -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<string>();
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue