From 6cb0c35ab84f5d0f9880e4ad5bf856ea2a53830f Mon Sep 17 00:00:00 2001 From: BruceChen Date: Fri, 2 Sep 2022 22:38:59 +0800 Subject: [PATCH] Trim --- MinecraftClient/Mapping/World.cs | 6 +-- .../Protocol/Handlers/DataTypes.cs | 24 ++++----- .../Protocol/Handlers/Protocol18.cs | 50 ++++++++++--------- 3 files changed, 40 insertions(+), 40 deletions(-) diff --git a/MinecraftClient/Mapping/World.cs b/MinecraftClient/Mapping/World.cs index 4b745644..6b0efbd9 100644 --- a/MinecraftClient/Mapping/World.cs +++ b/MinecraftClient/Mapping/World.cs @@ -116,12 +116,10 @@ namespace MinecraftClient.Mapping /// The dimension type (NBT Tag Compound) public static void SetDimension(string name) { - if (dimensionList!.TryGetValue(name, out Dimension? dimension)) - curDimension = dimension; - else - Console.WriteLine("Can't find dimension \"" + name + "\""); + curDimension = dimensionList![name]; // Should not fail } + /// /// Get current dimension /// diff --git a/MinecraftClient/Protocol/Handlers/DataTypes.cs b/MinecraftClient/Protocol/Handlers/DataTypes.cs index d4e82e2e..a5257979 100644 --- a/MinecraftClient/Protocol/Handlers/DataTypes.cs +++ b/MinecraftClient/Protocol/Handlers/DataTypes.cs @@ -1122,18 +1122,16 @@ namespace MinecraftClient.Protocol.Handlers public byte[] GetLastSeenMessageList(Message.LastSeenMessageList msgList, bool isOnlineMode) { if (!isOnlineMode) - { - return GetVarInt(0); - } + return GetVarInt(0); // Message list size else { - List fields = new List(); - fields.AddRange(GetVarInt(msgList.entries.Length)); + List fields = new(); + fields.AddRange(GetVarInt(msgList.entries.Length)); // Message list size foreach (Message.LastSeenMessageList.Entry entry in msgList.entries) { - fields.AddRange(entry.profileId.ToBigEndianBytes()); - fields.AddRange(GetVarInt(entry.lastSignature.Length)); - fields.AddRange(entry.lastSignature); + fields.AddRange(entry.profileId.ToBigEndianBytes()); // UUID + fields.AddRange(GetVarInt(entry.lastSignature.Length)); // Signature length + fields.AddRange(entry.lastSignature); // Signature data } return fields.ToArray(); } @@ -1147,16 +1145,16 @@ namespace MinecraftClient.Protocol.Handlers /// Acknowledgment Packet Data public byte[] GetAcknowledgment(Message.LastSeenMessageList.Acknowledgment ack, bool isOnlineMode) { - List fields = new List(); + List fields = new(); fields.AddRange(GetLastSeenMessageList(ack.lastSeen, isOnlineMode)); if (!isOnlineMode || ack.lastReceived == null) - fields.AddRange(GetBool(false)); + fields.AddRange(GetBool(false)); // Has last received message else { fields.AddRange(GetBool(true)); - fields.AddRange(ack.lastReceived.profileId.ToBigEndianBytes()); - fields.AddRange(GetVarInt(ack.lastReceived.lastSignature.Length)); - fields.AddRange(ack.lastReceived.lastSignature); + fields.AddRange(ack.lastReceived.profileId.ToBigEndianBytes()); // Has last received message + fields.AddRange(GetVarInt(ack.lastReceived.lastSignature.Length)); // Last received message signature length + fields.AddRange(ack.lastReceived.lastSignature); // Last received message signature data } return fields.ToArray(); } diff --git a/MinecraftClient/Protocol/Handlers/Protocol18.cs b/MinecraftClient/Protocol/Handlers/Protocol18.cs index e987e527..28918e63 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol18.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol18.cs @@ -24,7 +24,7 @@ using MinecraftClient.Protocol.Message; namespace MinecraftClient.Protocol.Handlers { /// - /// Implementation for Minecraft 1.7.X+ Protocols + /// Implementation for Minecraft 1.8.X+ Protocols /// /// /// Typical update steps for implementing protocol changes for a new Minecraft version: @@ -1417,14 +1417,17 @@ namespace MinecraftClient.Protocol.Handlers int EntityID = dataTypes.ReadNextVarInt(packetData); Dictionary metadata = dataTypes.ReadNextMetadata(packetData, itemPalette); - // See https://wiki.vg/Entity_metadata#Living_Entity - int healthField = 7; // From 1.10 to 1.13.2 - if (protocolVersion >= MC_1_14_Version) - healthField = 8; // 1.14 and above - if (protocolVersion >= MC_1_17_Version) - healthField = 9; // 1.17 and above + int healthField; // See https://wiki.vg/Entity_metadata#Living_Entity if (protocolVersion > MC_1_19_2_Version) throw new NotImplementedException(Translations.Get("exception.palette.healthfield")); + else if (protocolVersion >= MC_1_17_Version) // 1.17 and above + healthField = 9; + else if (protocolVersion >= MC_1_14_Version) // 1.14 and above + healthField = 8; + else if (protocolVersion >= MC_1_10_Version) // 1.10 and above + healthField = 7; + else + throw new NotImplementedException(Translations.Get("exception.palette.healthfield")); if (metadata.ContainsKey(healthField) && metadata[healthField] != null && metadata[healthField].GetType() == typeof(float)) handler.OnEntityHealth(EntityID, (float)metadata[healthField]); @@ -1675,19 +1678,19 @@ namespace MinecraftClient.Protocol.Handlers } if (protocolVersion >= MC_1_19_2_Version) { - string uuid = handler.GetUserUuidStr(); - if (uuid == "0") + Guid uuid = handler.GetUserUuid(); + if (uuid == Guid.Empty) fullLoginPacket.AddRange(dataTypes.GetBool(false)); // Has UUID else { fullLoginPacket.AddRange(dataTypes.GetBool(true)); // Has UUID - fullLoginPacket.AddRange(dataTypes.GetUUID(Guid.Parse(uuid))); // UUID + fullLoginPacket.AddRange(dataTypes.GetUUID(uuid)); // UUID } } SendPacket(0x00, fullLoginPacket); int packetID = -1; - Queue packetData = new Queue(); + Queue packetData = new(); while (true) { ReadNextPacket(ref packetID, packetData); @@ -2034,12 +2037,12 @@ namespace MinecraftClient.Protocol.Handlers /// /// Command /// List< Argument Name, Argument Value > - private List> CollectCommandArguments(string command) + private List>? CollectCommandArguments(string command) { - List> needSigned = new(); - if (!isOnlineMode || !Settings.SignMessageInCommand) - return needSigned; + return null; + + List> needSigned = new(); string[] argStage1 = command.Split(' ', 2, StringSplitOptions.None); if (argStage1.Length == 2) @@ -2076,7 +2079,7 @@ namespace MinecraftClient.Protocol.Handlers } /// - /// Send a chat command to the server + /// Send a chat command to the server - 1.19 and above /// /// Command /// PlayerKeyPair @@ -2105,8 +2108,9 @@ namespace MinecraftClient.Protocol.Handlers DateTimeOffset timeNow = DateTimeOffset.UtcNow; fields.AddRange(dataTypes.GetLong(timeNow.ToUnixTimeMilliseconds())); - List> needSigned = CollectCommandArguments(command); // List< Argument Name, Argument Value > - if (!isOnlineMode || needSigned.Count == 0 || playerKeyPair == null || !Settings.SignMessageInCommand) + List>? needSigned = + playerKeyPair != null ? CollectCommandArguments(command) : null; // List< Argument Name, Argument Value > + if (needSigned == null || needSigned!.Count == 0) { fields.AddRange(dataTypes.GetLong(0)); // Salt: Long fields.AddRange(dataTypes.GetVarInt(0)); // Signature Length: VarInt @@ -2121,8 +2125,8 @@ namespace MinecraftClient.Protocol.Handlers { fields.AddRange(dataTypes.GetString(argument.Item1)); // Argument name: String byte[] sign = (protocolVersion >= MC_1_19_2_Version) ? - playerKeyPair.PrivateKey.SignMessage(argument.Item2, uuid, timeNow, ref salt, acknowledgment!.lastSeen) : - playerKeyPair.PrivateKey.SignMessage(argument.Item2, uuid, timeNow, ref salt); + playerKeyPair!.PrivateKey.SignMessage(argument.Item2, uuid, timeNow, ref salt, acknowledgment!.lastSeen) : + playerKeyPair!.PrivateKey.SignMessage(argument.Item2, uuid, timeNow, ref salt); fields.AddRange(dataTypes.GetVarInt(sign.Length)); // Signature length: VarInt fields.AddRange(sign); // Signature: Byte Array } @@ -2162,9 +2166,6 @@ namespace MinecraftClient.Protocol.Handlers try { - LastSeenMessageList.Acknowledgment? acknowledgment = - (protocolVersion >= MC_1_19_2_Version) ? this.consumeAcknowledgment() : null; - List fields = new(); // Message: String (up to 256 chars) @@ -2172,6 +2173,9 @@ namespace MinecraftClient.Protocol.Handlers if (protocolVersion >= MC_1_19_Version) { + LastSeenMessageList.Acknowledgment? acknowledgment = + (protocolVersion >= MC_1_19_2_Version) ? this.consumeAcknowledgment() : null; + // Timestamp: Instant(Long) DateTimeOffset timeNow = DateTimeOffset.UtcNow; fields.AddRange(dataTypes.GetLong(timeNow.ToUnixTimeMilliseconds()));