Skip to content

Commit

Permalink
Item Mappings for 1.8 - 1.12 + Crash Fix
Browse files Browse the repository at this point in the history
Item Mappings for 1.8 - 1.12 + Crash Fix
  • Loading branch information
milutinke authored Jun 8, 2024
2 parents fc2373b + 5044ec9 commit 8270a2d
Show file tree
Hide file tree
Showing 9 changed files with 3,273 additions and 61 deletions.
10 changes: 10 additions & 0 deletions MinecraftClient/Inventory/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public class Item
/// </summary>
public int Count;

/// <summary>
/// Item Count
/// </summary>
public int Data;

/// <summary>
/// Item Metadata
/// </summary>
Expand All @@ -39,6 +44,11 @@ public Item(ItemType itemType, int count, Dictionary<string, object>? nbt)
Count = count;
NBT = nbt;
}

public Item(ItemType itemType, int count, int data, Dictionary<string, object>? nbt) : this(itemType, count, nbt)
{
Data = data;
}

/// <summary>
/// Check if the item slot is empty
Expand Down
5 changes: 5 additions & 0 deletions MinecraftClient/Inventory/ItemPalettes/ItemPalette.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ public ItemPalette()
{
// Index reverse mappings for use in ToId()
foreach (KeyValuePair<int, ItemType> entry in GetDict())
{
if (DictReverse.ContainsKey(entry.Value))
continue;

DictReverse.Add(entry.Value, entry.Key);
}

// Hardcoded placeholder types for internal and network use
DictReverse[ItemType.Unknown] = (int)ItemType.Unknown;
Expand Down
613 changes: 613 additions & 0 deletions MinecraftClient/Inventory/ItemPalettes/ItemPalette110.cs

Large diffs are not rendered by default.

646 changes: 646 additions & 0 deletions MinecraftClient/Inventory/ItemPalettes/ItemPalette111.cs

Large diffs are not rendered by default.

669 changes: 669 additions & 0 deletions MinecraftClient/Inventory/ItemPalettes/ItemPalette112.cs

Large diffs are not rendered by default.

640 changes: 640 additions & 0 deletions MinecraftClient/Inventory/ItemPalettes/ItemPalette18.cs

Large diffs are not rendered by default.

607 changes: 607 additions & 0 deletions MinecraftClient/Inventory/ItemPalettes/ItemPalette19.cs

Large diffs are not rendered by default.

72 changes: 39 additions & 33 deletions MinecraftClient/Protocol/Handlers/DataTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,38 +420,36 @@ public Dictionary<string, object> ReadNextNbt(Queue<byte> cache)
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);
}
}

Expand Down Expand Up @@ -561,13 +559,14 @@ private Dictionary<string, object> ReadNextNbt(Queue<byte> cache, bool root)
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));

Expand All @@ -579,14 +578,15 @@ private Dictionary<string, object> ReadNextNbt(Queue<byte> cache, bool root)
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 }
Expand Down Expand Up @@ -900,7 +900,8 @@ public void ReadParticleData(Queue<byte> cache, ItemPalette itemPalette)
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:
Expand All @@ -926,12 +927,14 @@ public void ReadParticleData(Queue<byte> cache, ItemPalette itemPalette)
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:
Expand Down Expand Up @@ -1442,8 +1445,11 @@ public byte[] GetItemSlot(Item? item, ItemPalette itemPalette)
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));
}
}
Expand Down
72 changes: 44 additions & 28 deletions MinecraftClient/Protocol/Handlers/Protocol18.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public Protocol18Handler(TcpClient Client, int protocolVersion, IMinecraftComHan
}

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);
Expand Down Expand Up @@ -195,7 +195,12 @@ public Protocol18Handler(TcpClient Client, int protocolVersion, IMinecraftComHan
>= 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
Expand Down Expand Up @@ -531,7 +536,6 @@ private bool HandlePlayPackets(int packetId, Queue<byte> packetData)
break;

case PacketTypesIn.JoinGame:
{
// Temporary fix
log.Debug("Receive JoinGame");

Expand All @@ -542,7 +546,7 @@ private bool HandlePlayPackets(int packetId, Queue<byte> packetData)

lastReceivedMessage = null;
lastSeenMessagesCollector = protocolVersion >= MC_1_19_3_Version ? new(20) : new(5);
}

handler.OnGameJoined(isOnlineMode);

var playerEntityId = dataTypes.ReadNextInt(packetData);
Expand Down Expand Up @@ -710,7 +714,6 @@ private bool HandlePlayPackets(int packetId, Queue<byte> packetData)

dataTypes.ReadNextVarInt(packetData); // Portal Cooldown
}

break;
case PacketTypesIn.SpawnPainting: // Just skip, no need for this
return true;
Expand Down Expand Up @@ -3915,41 +3918,54 @@ public bool SendPlayerDigging(int status, Location location, Direction face, int

public bool SendPlayerBlockPlacement(int hand, Location location, Direction face, int sequenceId)
{
if (protocolVersion < MC_1_14_Version)
try
{
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));
switch (protocolVersion)
{
case < MC_1_9_Version:
packet.AddRange(dataTypes.GetLocation(location));
packet.Add(dataTypes.GetBlockFace(face));

var item = playerInventory.Items[((McClient)handler).GetCurrentSlot()];
packet.AddRange(dataTypes.GetItemSlot(item, itemPalette));
var playerInventory = handler.GetInventory(0);

packet.Add(0); // cursorX
packet.Add(0); // cursorY
packet.Add(0); // cursorZ
if (playerInventory?.Items is null)
return false;

SendPacket(PacketTypesOut.PlayerBlockPlacement, packet);
return true;
}
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

try
{
var packet = new List<byte>();
packet.AddRange(DataTypes.GetVarInt(hand));
packet.AddRange(dataTypes.GetLocation(location));
packet.AddRange(DataTypes.GetVarInt(dataTypes.GetBlockFace(face)));
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;
}
Expand Down

0 comments on commit 8270a2d

Please sign in to comment.