From 85c32b9a4730eaec8509f206738009b44eb266bd Mon Sep 17 00:00:00 2001 From: ReinforceZwei <39955851+ReinforceZwei@users.noreply.github.com> Date: Sat, 15 Aug 2020 13:11:38 +0800 Subject: [PATCH] Fix entity metadata for lower MC versions --- MinecraftClient/McClient.cs | 16 +++++++++++++-- .../Protocol/Handlers/DataTypes.cs | 20 +++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/MinecraftClient/McClient.cs b/MinecraftClient/McClient.cs index 14ae10aa..efa1580c 100644 --- a/MinecraftClient/McClient.cs +++ b/MinecraftClient/McClient.cs @@ -59,6 +59,7 @@ namespace MinecraftClient private object lastKeepAliveLock = new object(); private int respawnTicks = 0; private int gamemode = 0; + private int protocolVersion; private int playerEntityID; @@ -155,6 +156,7 @@ namespace MinecraftClient this.username = user; this.host = server_ip; this.port = port; + this.protocolVersion = protocolversion; if (!singlecommand) { @@ -2122,9 +2124,19 @@ namespace MinecraftClient if (entities.ContainsKey(entityID)) { // Get health data for an entity - if (metadata.ContainsKey(8) && metadata[8].GetType() == typeof(float)) + int key; + // Key for 1.10+ is 7 and 1.14+ is 8 + if (protocolVersion >= Protocol.Handlers.Protocol18Handler.MC114Version) { - entities[entityID].Health = (float)metadata[8]; + key = 8; + } + else + { + key = 7; + } + if (metadata.ContainsKey(key) && metadata[key].GetType() == typeof(float)) + { + entities[entityID].Health = (float)metadata[key]; } } } diff --git a/MinecraftClient/Protocol/Handlers/DataTypes.cs b/MinecraftClient/Protocol/Handlers/DataTypes.cs index 21161d36..df66bc61 100644 --- a/MinecraftClient/Protocol/Handlers/DataTypes.cs +++ b/MinecraftClient/Protocol/Handlers/DataTypes.cs @@ -501,11 +501,25 @@ namespace MinecraftClient.Protocol.Handlers while (Key != 0xff) { int Type = ReadNextVarInt(cache); + + // starting from 1.13, Optional Chat is inserted as number 5 in 1.13 and IDs after 5 got shifted. + // Increase type ID by 1 if + // - below 1.13 + // - type ID larger than 4 + if (protocolversion < Protocol18Handler.MC113Version) + { + if (Type > 4) + { + Type += 1; + } + } // Value's data type is depended on Type object Value = null; - // We need to go through every data in order to get all fields in the packet - // Store the value as needed + // This is backward compatible since new type is appended to the end + // Version upgrade note + // - Check type ID got shifted or not + // - Add new type if any switch (Type) { case 0: // byte @@ -607,6 +621,8 @@ namespace MinecraftClient.Protocol.Handlers case 18: // Pose Value = ReadNextVarInt(cache); break; + default: + throw new System.IO.InvalidDataException("Unknown Metadata Type ID " + Type + ". Is this up to date for new MC Version?"); } data.Add(Key, Value); Key = ReadNextByte(cache);