Item Mappings for 1.8 - 1.12 + Crash Fix

Item Mappings for 1.8 - 1.12 + Crash Fix
This commit is contained in:
Anon 2024-06-08 21:34:33 +00:00 committed by GitHub
commit 8270a2d9a3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 3277 additions and 65 deletions

View file

@ -420,38 +420,36 @@ namespace MinecraftClient.Protocol.Handlers
public Item? ReadNextItemSlot(Queue<byte> cache, ItemPalette itemPalette)
{
// MC 1.13.2 and greater
if (protocolversion > Protocol18Handler.MC_1_13_Version)
if (protocolversion >= Protocol18Handler.MC_1_13_Version)
{
bool itemPresent = ReadNextBool(cache);
if (itemPresent)
{
int itemID = ReadNextVarInt(cache);
var itemPresent = ReadNextBool(cache);
if (itemID == -1)
return null;
if (!itemPresent)
return null;
ItemType type = itemPalette.FromId(itemID);
byte itemCount = ReadNextByte(cache);
Dictionary<string, object> nbt = ReadNextNbt(cache);
return new Item(type, itemCount, nbt);
}
else return null;
var itemId = ReadNextVarInt(cache);
if (itemId == -1)
return null;
var type = itemPalette.FromId(itemId);
var itemCount = ReadNextByte(cache);
var nbt = ReadNextNbt(cache);
return new Item(type, itemCount, nbt);
}
else
{
// MC 1.13 and lower
short itemID = ReadNextShort(cache);
var itemId = ReadNextShort(cache);
if (itemID == -1)
if (itemId == -1)
return null;
byte itemCount = ReadNextByte(cache);
var itemCount = ReadNextByte(cache);
var data = ReadNextShort(cache);
var nbt = ReadNextNbt(cache);
if (protocolversion < Protocol18Handler.MC_1_13_Version)
ReadNextShort(cache);
Dictionary<string, object> nbt = ReadNextNbt(cache);
return new Item(itemPalette.FromId(itemID), itemCount, nbt);
// For 1.8 - 1.12.2 we combine Item Id and Item Data/Damage to a single value using: (id << 16) | data
return new Item(itemPalette.FromId((itemId << 16) | (ushort)data), itemCount, data, nbt);
}
}
@ -561,13 +559,14 @@ namespace MinecraftClient.Protocol.Handlers
cache.Dequeue();
return nbtData;
}
var nextId = cache.Dequeue();
if (protocolversion < Protocol18Handler.MC_1_20_2_Version)
{
if (nextId is not 10) // TAG_Compound
throw new System.IO.InvalidDataException("Failed to decode NBT: Does not start with TAG_Compound");
throw new System.IO.InvalidDataException(
"Failed to decode NBT: Does not start with TAG_Compound");
// NBT root name
var rootName = Encoding.ASCII.GetString(ReadData(ReadNextUShort(cache), cache));
@ -579,14 +578,15 @@ namespace MinecraftClient.Protocol.Handlers
else
{
if (nextId is not (10 or 8)) // TAG_Compound or TAG_String
throw new System.IO.InvalidDataException("Failed to decode NBT: Does not start with TAG_Compound or TAG_String");
throw new System.IO.InvalidDataException(
"Failed to decode NBT: Does not start with TAG_Compound or TAG_String");
// Read TAG_String
if(nextId is 8)
if (nextId is 8)
{
var byteArrayLength = ReadNextUShort(cache);
var result = Encoding.UTF8.GetString(ReadData(byteArrayLength, cache));
return new Dictionary<string, object>()
{
{ "", result }
@ -900,7 +900,8 @@ namespace MinecraftClient.Protocol.Handlers
break;
case 14:
// 1.15 - 1.16.5 and 1.18 - 1.19.4
if (protocolversion is >= Protocol18Handler.MC_1_15_Version and < Protocol18Handler.MC_1_17_Version or > Protocol18Handler.MC_1_17_1_Version)
if (protocolversion is >= Protocol18Handler.MC_1_15_Version and < Protocol18Handler.MC_1_17_Version
or > Protocol18Handler.MC_1_17_1_Version)
ReadDustParticle(cache);
break;
case 15:
@ -926,12 +927,14 @@ namespace MinecraftClient.Protocol.Handlers
break;
case 24:
// 1.18 - 1.19.2 onwards
if (protocolversion is > Protocol18Handler.MC_1_17_1_Version and < Protocol18Handler.MC_1_19_3_Version)
if (protocolversion is > Protocol18Handler.MC_1_17_1_Version
and < 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 is Protocol18Handler.MC_1_17_Version or Protocol18Handler.MC_1_17_1_Version or >= Protocol18Handler.MC_1_19_3_Version)
if (protocolversion is Protocol18Handler.MC_1_17_Version or Protocol18Handler.MC_1_17_1_Version
or >= Protocol18Handler.MC_1_19_3_Version)
ReadNextVarInt(cache); // Block State (minecraft:falling_dust)
break;
case 27:
@ -1442,8 +1445,11 @@ namespace MinecraftClient.Protocol.Handlers
slotData.AddRange(GetShort(-1));
else
{
slotData.AddRange(GetShort((short)itemPalette.ToId(item.Type)));
// For 1.8 - 1.12.2 we combine Item Id and Item Data to a single value using: (id << 16) | data
// Thus to get an ID we do a right shift by 16 bits
slotData.AddRange(GetShort((short)(itemPalette.ToId(item.Type) >> 16)));
slotData.Add((byte)item.Count);
slotData.Add((byte)item.Data);
slotData.AddRange(GetNbt(item.NBT));
}
}

View file

@ -128,7 +128,7 @@ namespace MinecraftClient.Protocol.Handlers
}
if (handler.GetInventoryEnabled() &&
protocolVersion is < MC_1_9_Version or > MC_1_20_4_Version)
protocolVersion is < MC_1_8_Version or > MC_1_20_4_Version)
{
log.Error($"§c{Translations.extra_inventory_disabled}");
handler.SetInventoryEnabled(false);
@ -195,7 +195,12 @@ namespace MinecraftClient.Protocol.Handlers
>= MC_1_17_Version => new ItemPalette117(),
>= MC_1_16_2_Version => new ItemPalette1162(),
>= MC_1_16_1_Version => new ItemPalette1161(),
_ => new ItemPalette115()
>= MC_1_15_Version => new ItemPalette115(),
>= MC_1_12_Version => new ItemPalette112(),
>= MC_1_11_Version => new ItemPalette111(),
>= MC_1_10_Version => new ItemPalette110(),
>= MC_1_9_Version => new ItemPalette19(),
_ => new ItemPalette18()
};
ChatParser.ChatId2Type = this.protocolVersion switch
@ -531,7 +536,6 @@ namespace MinecraftClient.Protocol.Handlers
break;
case PacketTypesIn.JoinGame:
{
// Temporary fix
log.Debug("Receive JoinGame");
@ -542,7 +546,7 @@ namespace MinecraftClient.Protocol.Handlers
lastReceivedMessage = null;
lastSeenMessagesCollector = protocolVersion >= MC_1_19_3_Version ? new(20) : new(5);
}
handler.OnGameJoined(isOnlineMode);
var playerEntityId = dataTypes.ReadNextInt(packetData);
@ -710,7 +714,6 @@ namespace MinecraftClient.Protocol.Handlers
dataTypes.ReadNextVarInt(packetData); // Portal Cooldown
}
break;
case PacketTypesIn.SpawnPainting: // Just skip, no need for this
return true;
@ -3915,41 +3918,54 @@ namespace MinecraftClient.Protocol.Handlers
public bool SendPlayerBlockPlacement(int hand, Location location, Direction face, int sequenceId)
{
if (protocolVersion < MC_1_14_Version)
{
var playerInventory = handler.GetInventory(0);
if (playerInventory == null)
return false;
var packet = new List<byte>();
packet.AddRange(dataTypes.GetLocation(location));
packet.Add(dataTypes.GetBlockFace(face));
var item = playerInventory.Items[((McClient)handler).GetCurrentSlot()];
packet.AddRange(dataTypes.GetItemSlot(item, itemPalette));
packet.Add(0); // cursorX
packet.Add(0); // cursorY
packet.Add(0); // cursorZ
SendPacket(PacketTypesOut.PlayerBlockPlacement, packet);
return true;
}
try
{
var packet = new List<byte>();
packet.AddRange(DataTypes.GetVarInt(hand));
packet.AddRange(dataTypes.GetLocation(location));
packet.AddRange(DataTypes.GetVarInt(dataTypes.GetBlockFace(face)));
switch (protocolVersion)
{
case < MC_1_9_Version:
packet.AddRange(dataTypes.GetLocation(location));
packet.Add(dataTypes.GetBlockFace(face));
var playerInventory = handler.GetInventory(0);
if (playerInventory?.Items is null)
return false;
var slotWindowIds = new int[]{ 36, 37, 38, 39, 40, 41, 42, 43, 44 };
var currentSlot = ((McClient)handler).GetCurrentSlot();
playerInventory.Items.TryGetValue(slotWindowIds[currentSlot], out var item);
packet.AddRange(dataTypes.GetItemSlot(item, itemPalette));
packet.Add(0); // cursorX
packet.Add(0); // cursorY
packet.Add(0); // cursorZ
return true;
case < MC_1_14_Version:
packet.AddRange(dataTypes.GetLocation(location));
packet.AddRange(DataTypes.GetVarInt(dataTypes.GetBlockFace(face)));
packet.AddRange(DataTypes.GetVarInt(hand));
break;
default:
packet.AddRange(DataTypes.GetVarInt(hand));
packet.AddRange(dataTypes.GetLocation(location));
packet.AddRange(DataTypes.GetVarInt(dataTypes.GetBlockFace(face)));
break;
}
packet.AddRange(dataTypes.GetFloat(0.5f)); // cursorX
packet.AddRange(dataTypes.GetFloat(0.5f)); // cursorY
packet.AddRange(dataTypes.GetFloat(0.5f)); // cursorZ
packet.Add(0); // insideBlock = false;
if(protocolVersion >= MC_1_14_Version)
packet.Add(0); // insideBlock = false
if (protocolVersion >= MC_1_19_Version)
packet.AddRange(DataTypes.GetVarInt(sequenceId));
SendPacket(PacketTypesOut.PlayerBlockPlacement, packet);
return true;
}