From c00468c1036fa2f7658c57da83df2efbcd8df22a Mon Sep 17 00:00:00 2001 From: BruceChen Date: Thu, 15 Sep 2022 21:11:47 +0800 Subject: [PATCH] Fix 1.19.2 entity metadata handle --- MinecraftClient/Mapping/Entity.cs | 2 +- MinecraftClient/McClient.cs | 21 ++++----- .../Protocol/Handlers/DataTypes.cs | 45 ++++++++++++------- .../Protocol/Handlers/Protocol18.cs | 7 +-- .../Protocol/IMinecraftComHandler.cs | 2 +- MinecraftClient/Scripting/ChatBot.cs | 2 +- 6 files changed, 48 insertions(+), 31 deletions(-) diff --git a/MinecraftClient/Mapping/Entity.cs b/MinecraftClient/Mapping/Entity.cs index f2ad04bb..e32f43e8 100644 --- a/MinecraftClient/Mapping/Entity.cs +++ b/MinecraftClient/Mapping/Entity.cs @@ -91,7 +91,7 @@ namespace MinecraftClient.Mapping /// /// Entity metadata /// - public Dictionary Metadata; + public Dictionary Metadata; /// /// Entity equipment diff --git a/MinecraftClient/McClient.cs b/MinecraftClient/McClient.cs index 8ba106fa..a3819153 100644 --- a/MinecraftClient/McClient.cs +++ b/MinecraftClient/McClient.cs @@ -3134,31 +3134,32 @@ namespace MinecraftClient /// /// Entity ID /// The metadata of the entity - public void OnEntityMetadata(int entityID, Dictionary metadata) + public void OnEntityMetadata(int entityID, Dictionary metadata) { if (entities.ContainsKey(entityID)) { Entity entity = entities[entityID]; entity.Metadata = metadata; - if (entity.Type.ContainsItem() && metadata.ContainsKey(7) && metadata[7] != null && metadata[7].GetType() == typeof(Item)) + if (entity.Type.ContainsItem() && metadata.TryGetValue(7, out object? itemObj) && itemObj != null && itemObj.GetType() == typeof(Item)) { - Item item = (Item)metadata[7]; + Item item = (Item)itemObj; if (item == null) entity.Item = new Item(ItemType.Air, 0, null); else entity.Item = item; } - if (metadata.ContainsKey(6) && metadata[6] != null && metadata[6].GetType() == typeof(Int32)) + if (metadata.TryGetValue(6, out object? poseObj) && poseObj != null && poseObj.GetType() == typeof(Int32)) { - entity.Pose = (EntityPose)metadata[6]; + entity.Pose = (EntityPose)poseObj; } - if (metadata.ContainsKey(2) && metadata[2] != null && metadata[2].GetType() == typeof(string)) + if (metadata.TryGetValue(2, out object? nameObj) && nameObj != null && nameObj.GetType() == typeof(string)) { - entity.CustomNameJson = metadata[2].ToString(); - entity.CustomName = ChatParser.ParseText(metadata[2].ToString()); + string name = nameObj.ToString()!; + entity.CustomNameJson = name; + entity.CustomName = ChatParser.ParseText(name); } - if (metadata.ContainsKey(3) && metadata[3] != null && metadata[3].GetType() == typeof(bool)) + if (metadata.TryGetValue(3, out object? nameVisableObj) && nameVisableObj != null && nameVisableObj.GetType() == typeof(bool)) { - entity.IsCustomNameVisible = bool.Parse(metadata[3].ToString()); + entity.IsCustomNameVisible = bool.Parse(nameVisableObj.ToString()!); } DispatchBotEvent(bot => bot.OnEntityMetadata(entity, metadata)); } diff --git a/MinecraftClient/Protocol/Handlers/DataTypes.cs b/MinecraftClient/Protocol/Handlers/DataTypes.cs index d026c919..9a0d50f1 100644 --- a/MinecraftClient/Protocol/Handlers/DataTypes.cs +++ b/MinecraftClient/Protocol/Handlers/DataTypes.cs @@ -580,9 +580,9 @@ namespace MinecraftClient.Protocol.Handlers } } - public Dictionary ReadNextMetadata(Queue cache, ItemPalette itemPalette) + public Dictionary ReadNextMetadata(Queue cache, ItemPalette itemPalette) { - Dictionary data = new Dictionary(); + Dictionary data = new(); byte key = ReadNextByte(cache); while (key != 0xff) { @@ -600,7 +600,7 @@ namespace MinecraftClient.Protocol.Handlers } } // Value's data type is depended on Type - object value = null; + object? value = null; // This is backward compatible since new type is appended to the end // Version upgrade note @@ -625,9 +625,7 @@ namespace MinecraftClient.Protocol.Handlers break; case 5: // Optional Chat if (ReadNextBool(cache)) - { value = ReadNextString(cache); - } break; case 6: // Slot value = ReadNextItemSlot(cache, itemPalette); @@ -636,11 +634,12 @@ namespace MinecraftClient.Protocol.Handlers value = ReadNextBool(cache); break; case 8: // Rotation (3x floats) - List t = new List(); - t.Add(ReadNextFloat(cache)); - t.Add(ReadNextFloat(cache)); - t.Add(ReadNextFloat(cache)); - value = t; + value = new List + { + ReadNextFloat(cache), + ReadNextFloat(cache), + ReadNextFloat(cache) + }; break; case 9: // Position value = ReadNextLocation(cache); @@ -689,11 +688,12 @@ namespace MinecraftClient.Protocol.Handlers } break; case 16: // Villager Data (3x VarInt) - List d = new List(); - d.Add(ReadNextVarInt(cache)); - d.Add(ReadNextVarInt(cache)); - d.Add(ReadNextVarInt(cache)); - value = d; + value = new List + { + ReadNextVarInt(cache), + ReadNextVarInt(cache), + ReadNextVarInt(cache) + }; break; case 17: // Optional VarInt if (ReadNextBool(cache)) @@ -704,6 +704,21 @@ namespace MinecraftClient.Protocol.Handlers case 18: // Pose value = ReadNextVarInt(cache); break; + case 19: // Cat Variant + value = ReadNextVarInt(cache); + break; + case 20: // Frog Varint + value = ReadNextVarInt(cache); + break; + case 21: // GlobalPos at 1.19.2+; Painting Variant at 1.19- + if (protocolversion <= Protocol18Handler.MC_1_19_Version) + value = ReadNextVarInt(cache); + else + value = null; // Dimension and blockPos, currently not in use + break; + case 22: // Painting Variant + value = ReadNextVarInt(cache); + break; default: throw new System.IO.InvalidDataException("Unknown Metadata Type ID " + type + ". Is this up to date for new MC Version?"); } diff --git a/MinecraftClient/Protocol/Handlers/Protocol18.cs b/MinecraftClient/Protocol/Handlers/Protocol18.cs index 184adb0f..196cbfc8 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol18.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol18.cs @@ -1470,7 +1470,7 @@ namespace MinecraftClient.Protocol.Handlers if (handler.GetEntityHandlingEnabled()) { int EntityID = dataTypes.ReadNextVarInt(packetData); - Dictionary metadata = dataTypes.ReadNextMetadata(packetData, itemPalette); + Dictionary metadata = dataTypes.ReadNextMetadata(packetData, itemPalette); int healthField; // See https://wiki.vg/Entity_metadata#Living_Entity if (protocolVersion > MC_1_19_2_Version) @@ -1484,8 +1484,9 @@ namespace MinecraftClient.Protocol.Handlers 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]); + if (metadata.TryGetValue(healthField, out object? healthObj) && healthObj != null && healthObj.GetType() == typeof(float)) + handler.OnEntityHealth(EntityID, (float)healthObj); + handler.OnEntityMetadata(EntityID, metadata); } break; diff --git a/MinecraftClient/Protocol/IMinecraftComHandler.cs b/MinecraftClient/Protocol/IMinecraftComHandler.cs index 5ef147dd..e77dde34 100644 --- a/MinecraftClient/Protocol/IMinecraftComHandler.cs +++ b/MinecraftClient/Protocol/IMinecraftComHandler.cs @@ -318,7 +318,7 @@ namespace MinecraftClient.Protocol /// /// Entity ID /// Entity metadata - void OnEntityMetadata(int EntityID, Dictionary metadata); + void OnEntityMetadata(int EntityID, Dictionary metadata); /// /// Called when and explosion occurs on the server diff --git a/MinecraftClient/Scripting/ChatBot.cs b/MinecraftClient/Scripting/ChatBot.cs index 4d4ac0ac..6840e0bb 100644 --- a/MinecraftClient/Scripting/ChatBot.cs +++ b/MinecraftClient/Scripting/ChatBot.cs @@ -383,7 +383,7 @@ namespace MinecraftClient /// /// Entity /// The metadata of the entity - public virtual void OnEntityMetadata(Entity entity, Dictionary metadata) { } + public virtual void OnEntityMetadata(Entity entity, Dictionary metadata) { } /// /// Called when the status of client player have been changed