Add support for ItemStackTemplate in DataTypes and Protocol18

- Implemented ReadNextItemStackTemplate method to read ItemStackTemplate data with item-first encoding.
- Updated Protocol18 to utilize ReadItemStackTemplateLabel for improved item display handling.
- Enhanced item component parsing to accommodate new structured components.
This commit is contained in:
BruceChen 2026-03-31 01:27:27 +08:00
parent eae96a8fbc
commit 35dd3c4f06
2 changed files with 41 additions and 1 deletions

View file

@ -411,6 +411,37 @@ namespace MinecraftClient.Protocol.Handlers
return ReadNextNbt(cache, true);
}
/// <summary>
/// Read an ItemStackTemplate (26.1+) from a cache of bytes.
/// Unlike ItemStack, this uses item-first encoding: item_id, count, DataComponentPatch.
/// ItemStackTemplate is always non-empty (no count=0 sentinel).
/// </summary>
public Item ReadNextItemStackTemplate(Queue<byte> cache, ItemPalette itemPalette)
{
var itemId = ReadNextVarInt(cache);
var itemCount = ReadNextVarInt(cache);
var item = new Item(itemPalette.FromId(itemId), itemCount, null);
var numberOfComponentsToAdd = ReadNextVarInt(cache);
var numberofComponentsToRemove = ReadNextVarInt(cache);
var structuredComponentHandler = new StructuredComponentsHandler(protocolversion, this, itemPalette);
var strcturedComponentsToAdd = new List<StructuredComponent>(numberOfComponentsToAdd);
for (var i = 0; i < numberOfComponentsToAdd; i++)
{
var componentTypeId = ReadNextVarInt(cache);
strcturedComponentsToAdd.Add(structuredComponentHandler.Parse(componentTypeId, cache));
}
for (var i = 0; i < numberofComponentsToRemove; i++)
ReadNextVarInt(cache);
if (strcturedComponentsToAdd.Count > 0)
item.Components = strcturedComponentsToAdd;
return item;
}
/// <summary>
/// Read a single item slot from a cache of bytes and remove it from the cache
/// </summary>

View file

@ -3530,7 +3530,7 @@ namespace MinecraftClient.Protocol.Handlers
2 => ReadWithAnyPotionSlotDisplayLabel(packetData),
3 => ReadOnlyWithComponentSlotDisplayLabel(packetData),
4 => Item.GetTypeString(itemPalette.FromId(dataTypes.ReadNextVarInt(packetData))),
5 => dataTypes.ReadNextItemSlot(packetData, itemPalette)?.GetTypeString() ?? "Empty",
5 => ReadItemStackTemplateLabel(packetData),
6 => "#" + dataTypes.ReadNextString(packetData),
7 => ReadDyedSlotDisplayLabel(packetData),
8 => ReadSmithingTrimSlotDisplayLabel(packetData),
@ -3612,6 +3612,15 @@ namespace MinecraftClient.Protocol.Handlers
return label;
}
/// <summary>
/// Read an ItemStackTemplate (26.1+) which encodes fields in a different order
/// than ItemStack: item_id (VarInt), count (VarInt), DataComponentPatch.
/// </summary>
private string ReadItemStackTemplateLabel(Queue<byte> packetData)
{
return dataTypes.ReadNextItemStackTemplate(packetData, itemPalette).GetTypeString();
}
private void SkipOptionalCraftingRequirements(Queue<byte> packetData)
{
if (!dataTypes.ReadNextBool(packetData))