Fix entity metadata for lower MC versions

This commit is contained in:
ReinforceZwei 2020-08-15 13:11:38 +08:00
parent 888297dd4b
commit 85c32b9a47
2 changed files with 32 additions and 4 deletions

View file

@ -59,6 +59,7 @@ namespace MinecraftClient
private object lastKeepAliveLock = new object(); private object lastKeepAliveLock = new object();
private int respawnTicks = 0; private int respawnTicks = 0;
private int gamemode = 0; private int gamemode = 0;
private int protocolVersion;
private int playerEntityID; private int playerEntityID;
@ -155,6 +156,7 @@ namespace MinecraftClient
this.username = user; this.username = user;
this.host = server_ip; this.host = server_ip;
this.port = port; this.port = port;
this.protocolVersion = protocolversion;
if (!singlecommand) if (!singlecommand)
{ {
@ -2122,9 +2124,19 @@ namespace MinecraftClient
if (entities.ContainsKey(entityID)) if (entities.ContainsKey(entityID))
{ {
// Get health data for an entity // 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];
} }
} }
} }

View file

@ -501,11 +501,25 @@ namespace MinecraftClient.Protocol.Handlers
while (Key != 0xff) while (Key != 0xff)
{ {
int Type = ReadNextVarInt(cache); 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 // Value's data type is depended on Type
object Value = null; object Value = null;
// We need to go through every data in order to get all fields in the packet // This is backward compatible since new type is appended to the end
// Store the value as needed // Version upgrade note
// - Check type ID got shifted or not
// - Add new type if any
switch (Type) switch (Type)
{ {
case 0: // byte case 0: // byte
@ -607,6 +621,8 @@ namespace MinecraftClient.Protocol.Handlers
case 18: // Pose case 18: // Pose
Value = ReadNextVarInt(cache); Value = ReadNextVarInt(cache);
break; 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); data.Add(Key, Value);
Key = ReadNextByte(cache); Key = ReadNextByte(cache);