Skip to content

Commit

Permalink
Implemented 1.20.2 fully, needs more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
milutinke committed Nov 17, 2023
1 parent 4f957ce commit 93112d2
Show file tree
Hide file tree
Showing 3 changed files with 2,384 additions and 2,254 deletions.
2 changes: 1 addition & 1 deletion MinecraftClient/ChatBots/Farmer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ private bool SwitchToItem(ItemType itemType)
{
var playerInventory = GetPlayerInventory();

if (playerInventory.Items.TryGetValue(GetCurrentSlot() - 36, out Item value) && value.Type == itemType)
if (playerInventory.Items.TryGetValue(GetCurrentSlot() - 36, out var value) && value.Type == itemType)
return true; // Already selected

// Search the full inventory
Expand Down
102 changes: 64 additions & 38 deletions MinecraftClient/Protocol/Handlers/DataTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,10 +425,10 @@ public Dictionary<string, object> ReadNextNbt(Queue<byte> cache)
if (itemPresent)
{
int itemID = ReadNextVarInt(cache);

if (itemID == -1)
return null;

ItemType type = itemPalette.FromId(itemID);
byte itemCount = ReadNextByte(cache);
Dictionary<string, object> nbt = ReadNextNbt(cache);
Expand All @@ -440,15 +440,15 @@ public Dictionary<string, object> ReadNextNbt(Queue<byte> cache)
{
// MC 1.13 and lower
short itemID = ReadNextShort(cache);

if (itemID == -1)
return null;

byte itemCount = ReadNextByte(cache);
if(protocolversion < Protocol18Handler.MC_1_13_Version)

if (protocolversion < Protocol18Handler.MC_1_13_Version)
ReadNextShort(cache);

Dictionary<string, object> nbt = ReadNextNbt(cache);
return new Item(itemPalette.FromId(itemID), itemCount, nbt);
}
Expand Down Expand Up @@ -501,7 +501,7 @@ public Entity ReadNextEntity(Queue<byte> cache, EntityPalette entityPalette, boo
entityY = ReadNextDouble(cache); // Y
entityZ = ReadNextDouble(cache); // Z
}

int data = -1;
byte entityPitch, entityYaw;

Expand All @@ -520,8 +520,9 @@ public Entity ReadNextEntity(Queue<byte> cache, EntityPalette entityPalette, boo
entityYaw = ReadNextByte(cache); // Head Yaw

// Data
data = protocolversion >= Protocol18Handler.MC_1_19_Version
? ReadNextVarInt(cache) : ReadNextInt(cache);
data = protocolversion >= Protocol18Handler.MC_1_19_Version
? ReadNextVarInt(cache)
: ReadNextInt(cache);
}

// In 1.8 those 3 fields for Velocity are optional
Expand All @@ -541,7 +542,8 @@ public Entity ReadNextEntity(Queue<byte> cache, EntityPalette entityPalette, boo
ReadNextShort(cache);
}

return new Entity(entityID, entityType, new Location(entityX, entityY, entityZ), entityYaw, entityPitch, data);
return new Entity(entityID, entityType, new Location(entityX, entityY, entityZ), entityYaw, entityPitch,
data);
}

/// <summary>
Expand All @@ -563,11 +565,14 @@ private Dictionary<string, object> ReadNextNbt(Queue<byte> cache, bool root)
throw new System.IO.InvalidDataException("Failed to decode NBT: Does not start with TAG_Compound");
ReadNextByte(cache); // Tag type (TAG_Compound)

// NBT root name
string rootName = Encoding.ASCII.GetString(ReadData(ReadNextUShort(cache), cache));
if (protocolversion < Protocol18Handler.MC_1_20_2_Version)
{
// NBT root name
var rootName = Encoding.ASCII.GetString(ReadData(ReadNextUShort(cache), cache));

if (!String.IsNullOrEmpty(rootName))
nbtData[""] = rootName;
if (!string.IsNullOrEmpty(rootName))
nbtData[""] = rootName;
}
}

while (true)
Expand Down Expand Up @@ -646,31 +651,33 @@ private object ReadNbtField(Queue<byte> cache, int fieldType)
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
/// <exception cref="System.IO.InvalidDataException"></exception>
public Dictionary<int, object?> ReadNextMetadata(Queue<byte> cache, ItemPalette itemPalette, EntityMetadataPalette metadataPalette)
public Dictionary<int, object?> ReadNextMetadata(Queue<byte> cache, ItemPalette itemPalette,
EntityMetadataPalette metadataPalette)
{
Dictionary<int, object?> data = new();
byte key = ReadNextByte(cache);
byte terminteValue = protocolversion <= Protocol18Handler.MC_1_8_Version
? (byte)0x7f // 1.8 (https://wiki.vg/index.php?title=Entity_metadata&oldid=6220#Entity_Metadata_Format)
? (byte)0x7f // 1.8 (https://wiki.vg/index.php?title=Entity_metadata&oldid=6220#Entity_Metadata_Format)
: (byte)0xff; // 1.9+

while (key != terminteValue)
{
int typeId = protocolversion <= Protocol18Handler.MC_1_8_Version
? key >> 5 // 1.8
: ReadNextVarInt(cache); // 1.9+


EntityMetaDataType type;
try
{
type = metadataPalette.GetDataType(typeId);
}
catch (KeyNotFoundException)
{
throw new System.IO.InvalidDataException("Unknown Metadata Type ID " + typeId + ". Is this up to date for new MC Version?");
throw new System.IO.InvalidDataException("Unknown Metadata Type ID " + typeId +
". Is this up to date for new MC Version?");
}

if (protocolversion <= Protocol18Handler.MC_1_8_Version)
key = (byte)(key & 0x1f);

Expand Down Expand Up @@ -737,6 +744,7 @@ private object ReadNbtField(Queue<byte> cache, int fieldType)
{
value = ReadNextLocation(cache);
}

break;
case EntityMetaDataType.Direction: // Direction (VarInt)
value = ReadNextVarInt(cache);
Expand All @@ -746,6 +754,7 @@ private object ReadNbtField(Queue<byte> cache, int fieldType)
{
value = ReadNextUUID(cache);
}

break;
case EntityMetaDataType.BlockId: // BlockID (VarInt)
value = ReadNextVarInt(cache);
Expand Down Expand Up @@ -773,6 +782,7 @@ private object ReadNbtField(Queue<byte> cache, int fieldType)
{
value = ReadNextVarInt(cache);
}

break;
case EntityMetaDataType.Pose: // Pose
value = ReadNextVarInt(cache);
Expand All @@ -795,6 +805,7 @@ private object ReadNbtField(Queue<byte> cache, int fieldType)
// Dimension and blockPos, currently not in use
value = new Tuple<string, Location>(ReadNextString(cache), ReadNextLocation(cache));
}

break;
case EntityMetaDataType.PaintingVariant: // Painting Variant
value = ReadNextVarInt(cache);
Expand Down Expand Up @@ -824,6 +835,7 @@ private object ReadNbtField(Queue<byte> cache, int fieldType)
data[key] = value;
key = ReadNextByte(cache);
}

return data;
}

Expand Down Expand Up @@ -856,7 +868,8 @@ protected void ReadParticleData(Queue<byte> cache, ItemPalette itemPalette)
case 3:
if (protocolversion < Protocol18Handler.MC_1_17_Version
|| protocolversion > Protocol18Handler.MC_1_17_1_Version)
ReadNextVarInt(cache); // Block State (minecraft:block before 1.18, minecraft:block_marker after 1.18)
ReadNextVarInt(
cache); // Block State (minecraft:block before 1.18, minecraft:block_marker after 1.18)
break;
case 4:
if (protocolversion == Protocol18Handler.MC_1_17_Version
Expand All @@ -870,38 +883,44 @@ protected void ReadParticleData(Queue<byte> cache, ItemPalette itemPalette)
break;
case 14:
// 1.15 - 1.16.5 and 1.18 - 1.19.4
if ((protocolversion >= Protocol18Handler.MC_1_15_Version && protocolversion < Protocol18Handler.MC_1_17_Version)
if ((protocolversion >= Protocol18Handler.MC_1_15_Version &&
protocolversion < Protocol18Handler.MC_1_17_Version)
|| protocolversion > Protocol18Handler.MC_1_17_1_Version)
ReadDustParticle(cache);
break;
case 15:
if (protocolversion == Protocol18Handler.MC_1_17_Version || protocolversion == Protocol18Handler.MC_1_17_1_Version)
if (protocolversion == Protocol18Handler.MC_1_17_Version ||
protocolversion == Protocol18Handler.MC_1_17_1_Version)
ReadDustParticle(cache);
else
{
if (protocolversion > Protocol18Handler.MC_1_17_1_Version)
ReadDustParticleColorTransition(cache);
}

break;
case 16:
if (protocolversion == Protocol18Handler.MC_1_17_Version || protocolversion == Protocol18Handler.MC_1_17_1_Version)
if (protocolversion == Protocol18Handler.MC_1_17_Version ||
protocolversion == Protocol18Handler.MC_1_17_1_Version)
ReadDustParticleColorTransition(cache);
break;
case 23:
// 1.15 - 1.16.5
if (protocolversion >= Protocol18Handler.MC_1_15_Version && protocolversion < Protocol18Handler.MC_1_17_Version)
if (protocolversion >= Protocol18Handler.MC_1_15_Version &&
protocolversion < Protocol18Handler.MC_1_17_Version)
ReadNextVarInt(cache); // Block State (minecraft:falling_dust)
break;
case 24:
// 1.18 - 1.19.2 onwards
if (protocolversion > Protocol18Handler.MC_1_17_1_Version && protocolversion < Protocol18Handler.MC_1_19_3_Version)
if (protocolversion > Protocol18Handler.MC_1_17_1_Version &&
protocolversion < Protocol18Handler.MC_1_19_3_Version)
ReadNextVarInt(cache); // Block State (minecraft:falling_dust)
break;
case 25:
// 1.17 - 1.17.1 and 1.19.3 onwards
if (protocolversion == Protocol18Handler.MC_1_17_Version
|| protocolversion == Protocol18Handler.MC_1_17_1_Version
|| protocolversion >= Protocol18Handler.MC_1_19_3_Version)
|| protocolversion == Protocol18Handler.MC_1_17_1_Version
|| protocolversion >= Protocol18Handler.MC_1_19_3_Version)
ReadNextVarInt(cache); // Block State (minecraft:falling_dust)
break;
case 27:
Expand All @@ -915,27 +934,31 @@ protected void ReadParticleData(Queue<byte> cache, ItemPalette itemPalette)
break;
case 32:
// 1.15 - 1.16.5
if (protocolversion >= Protocol18Handler.MC_1_15_Version && protocolversion < Protocol18Handler.MC_1_17_Version)
if (protocolversion >= Protocol18Handler.MC_1_15_Version &&
protocolversion < Protocol18Handler.MC_1_17_Version)
ReadNextItemSlot(cache, itemPalette); // Item (minecraft:item)
break;
case 36:
// 1.17 - 1.17.1
if (protocolversion == Protocol18Handler.MC_1_17_Version || protocolversion == Protocol18Handler.MC_1_17_1_Version)
if (protocolversion == Protocol18Handler.MC_1_17_Version ||
protocolversion == Protocol18Handler.MC_1_17_1_Version)
{
ReadNextItemSlot(cache, itemPalette); // Item (minecraft:item)
}
else if (protocolversion > Protocol18Handler.MC_1_17_1_Version && protocolversion < Protocol18Handler.MC_1_19_3_Version)
else if (protocolversion > Protocol18Handler.MC_1_17_1_Version &&
protocolversion < Protocol18Handler.MC_1_19_3_Version)
{
// minecraft:vibration
ReadNextLocation(cache); // Origin (Starting Position)
ReadNextLocation(cache); // Desitination (Ending Position)
ReadNextVarInt(cache); // Ticks
}

break;
case 37:
// minecraft:vibration
if (protocolversion == Protocol18Handler.MC_1_17_Version
|| protocolversion == Protocol18Handler.MC_1_17_1_Version)
|| protocolversion == Protocol18Handler.MC_1_17_1_Version)
{
ReadNextDouble(cache); // Origin X
ReadNextDouble(cache); // Origin Y
Expand All @@ -945,6 +968,7 @@ protected void ReadParticleData(Queue<byte> cache, ItemPalette itemPalette)
ReadNextDouble(cache); // Destination Z
ReadNextInt(cache); // Ticks
}

break;
case 39:
if (protocolversion >= Protocol18Handler.MC_1_19_3_Version)
Expand All @@ -966,6 +990,7 @@ protected void ReadParticleData(Queue<byte> cache, ItemPalette itemPalette)

ReadNextVarInt(cache);
}

break;
}
}
Expand Down Expand Up @@ -1186,7 +1211,8 @@ private byte[] GetNbtField(object obj, out byte fieldType)
}
else
{
throw new System.IO.InvalidDataException("GetNbt: Cannot encode data type " + obj.GetType().Name + " into NBT!");
throw new System.IO.InvalidDataException("GetNbt: Cannot encode data type " + obj.GetType().Name +
" into NBT!");
}
}

Expand Down Expand Up @@ -1349,13 +1375,13 @@ public byte[] GetLocation(Location location)
if (protocolversion >= Protocol18Handler.MC_1_14_Version)
{
locationBytes = BitConverter.GetBytes(((((ulong)location.X) & 0x3FFFFFF) << 38) |
((((ulong)location.Z) & 0x3FFFFFF) << 12) |
(((ulong)location.Y) & 0xFFF));
((((ulong)location.Z) & 0x3FFFFFF) << 12) |
(((ulong)location.Y) & 0xFFF));
}
else
locationBytes = BitConverter.GetBytes(((((ulong)location.X) & 0x3FFFFFF) << 38) |
((((ulong)location.Y) & 0xFFF) << 26) |
(((ulong)location.Z) & 0x3FFFFFF));
((((ulong)location.Y) & 0xFFF) << 26) |
(((ulong)location.Z) & 0x3FFFFFF));

Array.Reverse(locationBytes); //Endianness
return locationBytes;
Expand Down
Loading

0 comments on commit 93112d2

Please sign in to comment.