Add support for Minecraft 1.16.2 (#1214)

* Implement MC version 1.16.2 basic support
All packets ID update done
Tested in 1.16.2 craftbukkit server
* Implement MC 1.16.2 entity handling
New EntityPalette
* Add back protocol version checking for entity handling
Was removed during testing and forgot to add it back
* Implement inventory handling for MC 1.16+
Item ID got changed in 1.16+ so a palette is needed.
* Fix ChangeSlot command
What a joke
* Handle 1.16 new entity properties name
Convert new naming style to old style
* Revert "Handle 1.16 new entity properties name"
This reverts commit 52c7d29062.
* Update AutoAttack to use the new entity properties key
* Fix item type to ID conversion
* Sort item types by name
* Remove ZombiePigmanSpawnEgg
User ZombifiedPiglinSpawnEgg instead (new name for same item)
* Add missing 1.16.2 version strings
* Remove old ItemTypeGenerator
* Sort entity types by name
* Palette loading, instructions, NotImplemented err
Co-authored-by: ORelio <ORelio@users.noreply.github.com>
This commit is contained in:
ReinforceZwei 2020-08-17 23:08:50 +08:00 committed by GitHub
parent ee5199f760
commit 6bbb7236e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 4296 additions and 988 deletions

View file

@ -7,6 +7,7 @@ using MinecraftClient.Mapping;
using MinecraftClient.Crypto;
using MinecraftClient.Inventory;
using MinecraftClient.Mapping.EntityPalettes;
using MinecraftClient.Inventory.ItemPalettes;
namespace MinecraftClient.Protocol.Handlers
{
@ -325,7 +326,7 @@ namespace MinecraftClient.Protocol.Handlers
/// Read a single item slot from a cache of bytes and remove it from the cache
/// </summary>
/// <returns>The item that was read or NULL for an empty slot</returns>
public Item ReadNextItemSlot(Queue<byte> cache)
public Item ReadNextItemSlot(Queue<byte> cache, ItemPalette itemPalette)
{
List<byte> slotData = new List<byte>();
if (protocolversion > Protocol18Handler.MC113Version)
@ -334,10 +335,10 @@ namespace MinecraftClient.Protocol.Handlers
bool itemPresent = ReadNextBool(cache);
if (itemPresent)
{
int itemID = ReadNextVarInt(cache);
ItemType type = itemPalette.FromId(ReadNextVarInt(cache));
byte itemCount = ReadNextByte(cache);
Dictionary<string, object> nbt = ReadNextNbt(cache);
return new Item(itemID, itemCount, nbt);
return new Item(type, itemCount, nbt);
}
else return null;
}
@ -350,7 +351,7 @@ namespace MinecraftClient.Protocol.Handlers
byte itemCount = ReadNextByte(cache);
short itemDamage = ReadNextShort(cache);
Dictionary<string, object> nbt = ReadNextNbt(cache);
return new Item(itemID, itemCount, nbt);
return new Item(itemPalette.FromId(itemID), itemCount, nbt);
}
}
@ -494,7 +495,7 @@ namespace MinecraftClient.Protocol.Handlers
}
}
public Dictionary<int, object> ReadNextMetadata(Queue<byte> cache)
public Dictionary<int, object> ReadNextMetadata(Queue<byte> cache, ItemPalette itemPalette)
{
Dictionary<int, object> data = new Dictionary<int, object>();
byte key = ReadNextByte(cache);
@ -544,7 +545,7 @@ namespace MinecraftClient.Protocol.Handlers
}
break;
case 6: // Slot
value = ReadNextItemSlot(cache);
value = ReadNextItemSlot(cache, itemPalette);
break;
case 7: // Boolean
value = ReadNextBool(cache);
@ -601,7 +602,7 @@ namespace MinecraftClient.Protocol.Handlers
ReadNextVarInt(cache);
break;
case 32:
ReadNextItemSlot(cache);
ReadNextItemSlot(cache, itemPalette);
break;
}
break;
@ -954,8 +955,9 @@ namespace MinecraftClient.Protocol.Handlers
/// Get a byte array representing the given item as an item slot
/// </summary>
/// <param name="item">Item</param>
/// <param name="itemPalette">Item Palette</param>
/// <returns>Item slot representation</returns>
public byte[] GetItemSlot(Item item)
public byte[] GetItemSlot(Item item, ItemPalette itemPalette)
{
List<byte> slotData = new List<byte>();
if (protocolversion > Protocol18Handler.MC113Version)
@ -966,7 +968,7 @@ namespace MinecraftClient.Protocol.Handlers
else
{
slotData.Add(1); // Item is present
slotData.AddRange(GetVarInt((int)item.Type));
slotData.AddRange(GetVarInt(itemPalette.ToId(item.Type)));
slotData.Add((byte)item.Count);
slotData.AddRange(GetNbt(item.NBT));
}
@ -978,7 +980,7 @@ namespace MinecraftClient.Protocol.Handlers
slotData.AddRange(GetShort(-1));
else
{
slotData.AddRange(GetShort((short)item.Type));
slotData.AddRange(GetShort((short)itemPalette.ToId(item.Type)));
slotData.Add((byte)item.Count);
slotData.AddRange(GetNbt(item.NBT));
}

View file

@ -15,6 +15,7 @@ using MinecraftClient.Inventory;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Diagnostics;
using MinecraftClient.Inventory.ItemPalettes;
namespace MinecraftClient.Protocol.Handlers
{
@ -43,6 +44,7 @@ namespace MinecraftClient.Protocol.Handlers
internal const int MC1152Version = 578;
internal const int MC116Version = 735;
internal const int MC1161Version = 736;
internal const int MC1162Version = 751;
private int compression_treshold = 0;
private bool autocomplete_received = false;
@ -57,6 +59,7 @@ namespace MinecraftClient.Protocol.Handlers
Protocol18Terrain pTerrain;
IMinecraftComHandler handler;
EntityPalette entityPalette;
ItemPalette itemPalette;
SocketWrapper socketWrapper;
DataTypes dataTypes;
Thread netRead;
@ -78,18 +81,19 @@ namespace MinecraftClient.Protocol.Handlers
handler.SetTerrainEnabled(false);
}
if (handler.GetInventoryEnabled() && (protocolversion < MC110Version || protocolversion > MC1152Version))
if (handler.GetInventoryEnabled() && (protocolversion < MC110Version || protocolversion > MC1162Version))
{
ConsoleIO.WriteLineFormatted("§8Inventories are currently not handled for that MC version.");
handler.SetInventoryEnabled(false);
}
if (handler.GetEntityHandlingEnabled() && (protocolversion < MC110Version || protocolversion > MC1161Version))
if (handler.GetEntityHandlingEnabled() && (protocolversion < MC110Version || protocolversion > MC1162Version))
{
ConsoleIO.WriteLineFormatted("§8Entities are currently not handled for that MC version.");
handler.SetEntityHandlingEnabled(false);
}
// Block palette
if (protocolversion >= MC113Version)
{
if (protocolVersion > MC1152Version && handler.GetTerrainEnabled())
@ -102,12 +106,15 @@ namespace MinecraftClient.Protocol.Handlers
}
else Block.Palette = new Palette112();
// Entity palette
if (protocolversion >= MC113Version)
{
if (protocolversion > MC1161Version && handler.GetEntityHandlingEnabled())
if (protocolversion > MC1162Version && handler.GetEntityHandlingEnabled())
throw new NotImplementedException("Please update entity types handling for this Minecraft version. See EntityType.cs");
if (protocolversion >= MC116Version)
entityPalette = new EntityPalette116();
if (protocolversion >= MC1162Version)
entityPalette = new EntityPalette1162();
else if (protocolversion >= MC116Version)
entityPalette = new EntityPalette1161();
else if (protocolversion >= MC115Version)
entityPalette = new EntityPalette115();
else if (protocolVersion >= MC114Version)
@ -115,6 +122,17 @@ namespace MinecraftClient.Protocol.Handlers
else entityPalette = new EntityPalette113();
}
else entityPalette = new EntityPalette112();
// Item palette
if (protocolversion >= MC116Version)
{
if (protocolversion > MC1162Version && handler.GetInventoryEnabled())
throw new NotImplementedException("Please update item types handling for this Minecraft version. See ItemType.cs");
if (protocolversion >= MC1162Version)
itemPalette = new ItemPalette1162();
else itemPalette = new ItemPalette1161();
}
else itemPalette = new ItemPalette115();
}
/// <summary>
@ -233,6 +251,10 @@ namespace MinecraftClient.Protocol.Handlers
handler.OnGameJoined();
int playerEntityID = dataTypes.ReadNextInt(packetData);
handler.OnReceivePlayerEntityID(playerEntityID);
if (protocolversion >= MC1162Version)
dataTypes.ReadNextBool(packetData); // Is hardcore - 1.16.2 and above
handler.OnGamemodeUpdate(Guid.Empty, dataTypes.ReadNextByte(packetData));
if (protocolversion >= MC116Version)
@ -247,8 +269,11 @@ namespace MinecraftClient.Protocol.Handlers
//Current dimension - String identifier in 1.16, varInt below 1.16, byte below 1.9.1
if (protocolversion >= MC116Version)
{
if (protocolversion >= MC1162Version)
dataTypes.ReadNextNbt(packetData);
else
dataTypes.ReadNextString(packetData);
// TODO handle dimensions for 1.16+, needed for terrain handling
dataTypes.ReadNextString(packetData);
this.currentDimension = 0;
}
else if (protocolversion >= MC191Version)
@ -263,7 +288,10 @@ namespace MinecraftClient.Protocol.Handlers
if (protocolversion >= MC115Version)
dataTypes.ReadNextLong(packetData); // Hashed world seed - 1.15 and above
dataTypes.ReadNextByte(packetData); // Max Players
if (protocolversion >= MC1162Version)
dataTypes.ReadNextVarInt(packetData); // Max Players - 1.16.2 and above
else
dataTypes.ReadNextByte(packetData); // Max Players - 1.16.1 and below
if (protocolversion < MC116Version)
dataTypes.ReadNextString(packetData); // Level Type - 1.15 and below
@ -687,7 +715,7 @@ namespace MinecraftClient.Protocol.Handlers
Dictionary<int, Item> inventorySlots = new Dictionary<int, Item>();
for (short slotId = 0; slotId < elements; slotId++)
{
Item item = dataTypes.ReadNextItemSlot(packetData);
Item item = dataTypes.ReadNextItemSlot(packetData, itemPalette);
if (item != null)
inventorySlots[slotId] = item;
}
@ -699,7 +727,7 @@ namespace MinecraftClient.Protocol.Handlers
{
byte windowID = dataTypes.ReadNextByte(packetData);
short slotID = dataTypes.ReadNextShort(packetData);
Item item = dataTypes.ReadNextItemSlot(packetData);
Item item = dataTypes.ReadNextItemSlot(packetData, itemPalette);
handler.OnSetSlot(windowID, slotID, item);
}
break;
@ -739,9 +767,25 @@ namespace MinecraftClient.Protocol.Handlers
if (handler.GetEntityHandlingEnabled())
{
int entityid = dataTypes.ReadNextVarInt(packetData);
int slot2 = dataTypes.ReadNextVarInt(packetData);
Item item = dataTypes.ReadNextItemSlot(packetData);
handler.OnEntityEquipment(entityid, slot2, item);
if (protocolversion >= MC116Version)
{
bool hasNext;
do
{
byte bitsData = dataTypes.ReadNextByte(packetData);
// Top bit set if another entry follows, and otherwise unset if this is the last item in the array
hasNext = (bitsData >> 7) == 1 ? true : false;
int slot2 = bitsData >> 1;
Item item = dataTypes.ReadNextItemSlot(packetData, itemPalette);
handler.OnEntityEquipment(entityid, slot2, item);
} while (hasNext);
}
else
{
int slot2 = dataTypes.ReadNextVarInt(packetData);
Item item = dataTypes.ReadNextItemSlot(packetData, itemPalette);
handler.OnEntityEquipment(entityid, slot2, item);
}
}
break;
case PacketIncomingType.SpawnLivingEntity:
@ -865,7 +909,7 @@ namespace MinecraftClient.Protocol.Handlers
if (handler.GetEntityHandlingEnabled())
{
int EntityID = dataTypes.ReadNextVarInt(packetData);
Dictionary<int, object> metadata = dataTypes.ReadNextMetadata(packetData);
Dictionary<int, object> metadata = dataTypes.ReadNextMetadata(packetData, itemPalette); // need itemPalette because metadata need to read slot item
int healthField = protocolversion >= MC114Version ? 8 : 7; // Health is field no. 7 in 1.10+ and 8 in 1.14+
if (metadata.ContainsKey(healthField) && metadata[healthField].GetType() == typeof(float))
handler.OnEntityHealth(EntityID, (float)metadata[healthField]);
@ -1641,18 +1685,18 @@ namespace MinecraftClient.Protocol.Handlers
case WindowActionType.LeftClick: button = 0; break;
case WindowActionType.RightClick: button = 1; break;
case WindowActionType.MiddleClick: button = 2; mode = 3; break;
case WindowActionType.ShiftClick: button = 0; mode = 1; item = new Item(-1, 0, null); break;
case WindowActionType.DropItem: button = 0; mode = 4; item = new Item(-1, 0, null); break;
case WindowActionType.DropItemStack: button = 1; mode = 4; item = new Item(-1, 0, null); break;
case WindowActionType.StartDragLeft: button = 0; mode = 5; item = new Item(-1, 0, null); slotId = -999; break;
case WindowActionType.StartDragRight: button = 4; mode = 5; item = new Item(-1, 0, null); slotId = -999; break;
case WindowActionType.StartDragMiddle: button = 8; mode = 5; item = new Item(-1, 0, null); slotId = -999; break;
case WindowActionType.EndDragLeft: button = 2; mode = 5; item = new Item(-1, 0, null); slotId = -999; break;
case WindowActionType.EndDragRight: button = 6; mode = 5; item = new Item(-1, 0, null); slotId = -999; break;
case WindowActionType.EndDragMiddle: button = 10; mode = 5; item = new Item(-1, 0, null); slotId = -999; break;
case WindowActionType.AddDragLeft: button = 1; mode = 5; item = new Item(-1, 0, null); break;
case WindowActionType.AddDragRight: button = 5; mode = 5; item = new Item(-1, 0, null); break;
case WindowActionType.AddDragMiddle: button = 9; mode = 5; item = new Item(-1, 0, null); break;
case WindowActionType.ShiftClick: button = 0; mode = 1; item = new Item(ItemType.Null, 0, null); break;
case WindowActionType.DropItem: button = 0; mode = 4; item = new Item(ItemType.Null, 0, null); break;
case WindowActionType.DropItemStack: button = 1; mode = 4; item = new Item(ItemType.Null, 0, null); break;
case WindowActionType.StartDragLeft: button = 0; mode = 5; item = new Item(ItemType.Null, 0, null); slotId = -999; break;
case WindowActionType.StartDragRight: button = 4; mode = 5; item = new Item(ItemType.Null, 0, null); slotId = -999; break;
case WindowActionType.StartDragMiddle: button = 8; mode = 5; item = new Item(ItemType.Null, 0, null); slotId = -999; break;
case WindowActionType.EndDragLeft: button = 2; mode = 5; item = new Item(ItemType.Null, 0, null); slotId = -999; break;
case WindowActionType.EndDragRight: button = 6; mode = 5; item = new Item(ItemType.Null, 0, null); slotId = -999; break;
case WindowActionType.EndDragMiddle: button = 10; mode = 5; item = new Item(ItemType.Null, 0, null); slotId = -999; break;
case WindowActionType.AddDragLeft: button = 1; mode = 5; item = new Item(ItemType.Null, 0, null); break;
case WindowActionType.AddDragRight: button = 5; mode = 5; item = new Item(ItemType.Null, 0, null); break;
case WindowActionType.AddDragMiddle: button = 9; mode = 5; item = new Item(ItemType.Null, 0, null); break;
}
List<byte> packet = new List<byte>();
@ -1665,7 +1709,7 @@ namespace MinecraftClient.Protocol.Handlers
packet.AddRange(dataTypes.GetVarInt(mode));
else packet.Add(mode);
packet.AddRange(dataTypes.GetItemSlot(item));
packet.AddRange(dataTypes.GetItemSlot(item, itemPalette));
SendPacket(PacketOutgoingType.ClickWindow, packet);
return true;
@ -1681,7 +1725,7 @@ namespace MinecraftClient.Protocol.Handlers
{
List<byte> packet = new List<byte>();
packet.AddRange(dataTypes.GetShort((short)slot));
packet.AddRange(dataTypes.GetItemSlot(new Item((int)itemType, count, nbt)));
packet.AddRange(dataTypes.GetItemSlot(new Item(itemType, count, nbt), itemPalette));
SendPacket(PacketOutgoingType.CreativeInventoryAction, packet);
return true;
}

View file

@ -366,8 +366,10 @@ namespace MinecraftClient.Protocol.Handlers
case 0x4D: return PacketIncomingType.UpdateScore;
}
} else {
switch (packetID) // MC 1.16+
}
else if (protocol <= Protocol18Handler.MC1161Version)
{
switch (packetID) // MC 1.16 and 1.16.1
{
case 0x20: return PacketIncomingType.KeepAlive;
case 0x25: return PacketIncomingType.JoinGame;
@ -413,6 +415,55 @@ namespace MinecraftClient.Protocol.Handlers
case 0x4A: return PacketIncomingType.ScoreboardObjective;
case 0x4D: return PacketIncomingType.UpdateScore;
}
}
else
{
switch (packetID) // MC 1.16.2
{
case 0x1F: return PacketIncomingType.KeepAlive;
case 0x24: return PacketIncomingType.JoinGame;
case 0x0E: return PacketIncomingType.ChatMessage;
case 0x39: return PacketIncomingType.Respawn;
case 0x35: return PacketIncomingType.PlayerPositionAndLook;
case 0x20: return PacketIncomingType.ChunkData;
case 0x3B: return PacketIncomingType.MultiBlockChange;
case 0x0B: return PacketIncomingType.BlockChange;
// MapChunkBulk does not exist since 1.9
case 0x1C: return PacketIncomingType.UnloadChunk;
case 0x32: return PacketIncomingType.PlayerListUpdate;
case 0x0F: return PacketIncomingType.TabCompleteResult;
case 0x17: return PacketIncomingType.PluginMessage;
case 0x19: return PacketIncomingType.KickPacket;
// NetworkCompressionTreshold does not exist since 1.9
case 0x38: return PacketIncomingType.ResourcePackSend;
case 0x12: return PacketIncomingType.CloseWindow;
case 0x2D: return PacketIncomingType.OpenWindow;
case 0x13: return PacketIncomingType.WindowItems;
case 0x11: return PacketIncomingType.WindowConfirmation;
case 0x15: return PacketIncomingType.SetSlot;
case 0x00: return PacketIncomingType.SpawnEntity;
case 0x02: return PacketIncomingType.SpawnLivingEntity;
case 0x04: return PacketIncomingType.SpawnPlayer;
case 0x36: return PacketIncomingType.DestroyEntities;
case 0x16: return PacketIncomingType.SetCooldown;
case 0x27: return PacketIncomingType.EntityPosition;
case 0x28: return PacketIncomingType.EntityPositionAndRotation;
case 0x58: return PacketIncomingType.EntityProperties;
case 0x56: return PacketIncomingType.EntityTeleport;
case 0x46: return PacketIncomingType.EntityVelocity;
case 0x47: return PacketIncomingType.EntityEquipment;
case 0x59: return PacketIncomingType.EntityEffect;
case 0x44: return PacketIncomingType.EntityMetadata;
case 0x4E: return PacketIncomingType.TimeUpdate;
case 0x49: return PacketIncomingType.UpdateHealth;
case 0x48: return PacketIncomingType.SetExperience;
case 0x3F: return PacketIncomingType.HeldItemChange;
case 0x1B: return PacketIncomingType.Explosion;
case 0x25: return PacketIncomingType.MapData;
case 0x4F: return PacketIncomingType.Title;
case 0x4A: return PacketIncomingType.ScoreboardObjective;
case 0x4D: return PacketIncomingType.UpdateScore;
}
}
return PacketIncomingType.UnknownPacket;
@ -606,7 +657,7 @@ namespace MinecraftClient.Protocol.Handlers
case PacketOutgoingType.UpdateCommandBlock: return 0x24;
}
}
else
else if (protocol <= Protocol18Handler.MC1161Version) // MC 1.16 and 1.16.1
{
switch (packet)
{
@ -635,6 +686,35 @@ namespace MinecraftClient.Protocol.Handlers
case PacketOutgoingType.UpdateCommandBlock: return 0x25;
}
}
else
{
switch (packet)
{
case PacketOutgoingType.KeepAlive: return 0x10;
case PacketOutgoingType.ResourcePackStatus: return 0x21;
case PacketOutgoingType.ChatMessage: return 0x03;
case PacketOutgoingType.ClientStatus: return 0x04;
case PacketOutgoingType.ClientSettings: return 0x05;
case PacketOutgoingType.PluginMessage: return 0x0B;
case PacketOutgoingType.TabComplete: return 0x06;
case PacketOutgoingType.EntityAction: return 0x1C;
case PacketOutgoingType.PlayerPosition: return 0x12;
case PacketOutgoingType.PlayerPositionAndLook: return 0x13;
case PacketOutgoingType.TeleportConfirm: return 0x00;
case PacketOutgoingType.HeldItemChange: return 0x25;
case PacketOutgoingType.InteractEntity: return 0x0E;
case PacketOutgoingType.UseItem: return 0x2F;
case PacketOutgoingType.ClickWindow: return 0x09;
case PacketOutgoingType.CloseWindow: return 0x0A;
case PacketOutgoingType.WindowConfirmation: return 0x07;
case PacketOutgoingType.PlayerBlockPlacement: return 0x2E;
case PacketOutgoingType.CreativeInventoryAction: return 0x28;
case PacketOutgoingType.Animation: return 0x2C;
case PacketOutgoingType.PlayerDigging: return 0x1B;
case PacketOutgoingType.UpdateSign: return 0x2B;
case PacketOutgoingType.UpdateCommandBlock: return 0x26;
}
}
throw new System.ComponentModel.InvalidEnumArgumentException("Unknown PacketOutgoingType (protocol=" + protocol + ")", (int)packet, typeof(PacketOutgoingType));
}

View file

@ -126,7 +126,7 @@ namespace MinecraftClient.Protocol
int[] supportedVersions_Protocol16 = { 51, 60, 61, 72, 73, 74, 78 };
if (Array.IndexOf(supportedVersions_Protocol16, ProtocolVersion) > -1)
return new Protocol16Handler(Client, ProtocolVersion, Handler);
int[] supportedVersions_Protocol18 = { 4, 5, 47, 107, 108, 109, 110, 210, 315, 316, 335, 338, 340, 393, 401, 404, 477, 480, 485, 490, 498, 573, 575, 578, 735, 736};
int[] supportedVersions_Protocol18 = { 4, 5, 47, 107, 108, 109, 110, 210, 315, 316, 335, 338, 340, 393, 401, 404, 477, 480, 485, 490, 498, 573, 575, 578, 735, 736, 751 };
if (Array.IndexOf(supportedVersions_Protocol18, ProtocolVersion) > -1)
return new Protocol18Handler(Client, ProtocolVersion, Handler, forgeInfo);
throw new NotSupportedException("The protocol version no." + ProtocolVersion + " is not supported.");
@ -238,6 +238,8 @@ namespace MinecraftClient.Protocol
return 735;
case "1.16.1":
return 736;
case "1.16.2":
return 751;
default:
return 0;
}