Minecraft-Console-Client/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/TrimComponent.cs
BruceChen a36ba23ba6 fix: StructuredComponents batch 1 audit — TrimComponent, ProfileComponent, WrittenBookContent, and NBT serialization
Audited all 8 high-complexity structured components against official 1.20.6
decompiled STREAM_CODEC definitions. Found and fixed bugs in 3 components
plus a systemic NBT serialization issue:

TrimComponent (ID 35):
- Serialize had TrimPatternType and ShowInTooltip incorrectly nested inside
  the TrimMaterialType==0 branch; moved them outside to match Parse logic
- Description fields (TrimMaterial.description, TrimPattern.description) were
  read/written as String but official codec uses ComponentSerialization
  (NBT Tag format); changed to ReadNextNbt/GetNbt

ProfileComponent (ID 46):
- Serialize was missing the HasUniqueId Bool prefix before UUID
- Serialize only wrote properties when count > 0 but omitted the VarInt count
  prefix entirely when empty; now always writes VarInt count

WrittenBookContentComponent (ID 34):
- Page content uses Filterable<Component> where Component is NBT-encoded via
  ComponentSerialization.STREAM_CODEC, not plain String; changed Parse to use
  ReadNextNbt and Serialize to use GetNbt
- Added RawContentNbt/FilteredContentNbt fields to BookPage record for
  round-trip NBT preservation
- Removed unnecessary ChatParser.ParseText on title (it's a plain string)

DataTypes.GetNbt:
- Added TAG_String root support for 1.20.4+ (chat components like "Page 1"
  are encoded as TAG_String, not TAG_Compound)
- Fixed root name handling: versions >= 1.20.2 omit the root compound name,
  but GetNbt was unconditionally writing it

Components confirmed correct (no changes needed):
- FoodComponentComponent (ID 20), ToolComponent (ID 22),
  InstrumentComponent (ID 40), PotionContentsComponent (ID 31),
  AttributeModifiersComponent (ID 12)

Made-with: Cursor
2026-03-20 00:09:05 +08:00

111 lines
No EOL
4.5 KiB
C#

using System;
using System.Collections.Generic;
using MinecraftClient.Inventory;
using MinecraftClient.Inventory.ItemPalettes;
using MinecraftClient.Protocol.Handlers.StructuredComponents.Core;
using MinecraftClient.Protocol.Message;
namespace MinecraftClient.Protocol.Handlers.StructuredComponents.Components._1_20_6;
public class TrimComponent(DataTypes dataTypes, ItemPalette itemPalette, SubComponentRegistry subComponentRegistry) : StructuredComponent(dataTypes, itemPalette, subComponentRegistry)
{
public int TrimMaterialType { get; set; }
public string AssetName { get; set; } = null!;
public int Ingredient { get; set; }
public float ItemModelIndex { get; set; }
public int NumberOfOverrides { get; set; }
public List<TrimAssetOverride>? Overrides { get; set; }
public Dictionary<string, object>? DescriptionNbt { get; set; }
public string Description { get; set; } = null!;
public int TrimPatternType { get; set; }
public string TrimPatternTypeAssetName { get; set; } = null!;
public int TemplateItem { get; set; }
public Dictionary<string, object>? TrimPatternTypeDescriptionNbt { get; set; }
public string TrimPatternTypeDescription { get; set; } = null!;
public bool Decal { get; set; }
public bool ShowInTooltip { get; set; }
public override void Parse(Queue<byte> data)
{
TrimMaterialType = dataTypes.ReadNextVarInt(data);
if (TrimMaterialType == 0)
{
AssetName = dataTypes.ReadNextString(data);
Ingredient = dataTypes.ReadNextVarInt(data);
ItemModelIndex = dataTypes.ReadNextFloat(data);
NumberOfOverrides = dataTypes.ReadNextVarInt(data);
if (NumberOfOverrides > 0)
{
Overrides = [];
for (var i = 0; i < NumberOfOverrides; i++)
Overrides.Add(new TrimAssetOverride(dataTypes.ReadNextVarInt(data),
dataTypes.ReadNextString(data)));
}
DescriptionNbt = dataTypes.ReadNextNbt(data);
Description = ChatParser.ParseText(DescriptionNbt);
}
TrimPatternType = dataTypes.ReadNextVarInt(data);
if (TrimPatternType == 0)
{
TrimPatternTypeAssetName = dataTypes.ReadNextString(data);
TemplateItem = dataTypes.ReadNextVarInt(data);
TrimPatternTypeDescriptionNbt = dataTypes.ReadNextNbt(data);
TrimPatternTypeDescription = ChatParser.ParseText(TrimPatternTypeDescriptionNbt);
Decal = dataTypes.ReadNextBool(data);
}
ShowInTooltip = dataTypes.ReadNextBool(data);
}
public override Queue<byte> Serialize()
{
var data = new List<byte>();
data.AddRange(DataTypes.GetVarInt(TrimMaterialType));
if (TrimMaterialType == 0)
{
if (string.IsNullOrEmpty(AssetName))
throw new NullReferenceException("Can't serialize the TrimComponent because the Asset Name is null!");
data.AddRange(DataTypes.GetString(AssetName));
data.AddRange(DataTypes.GetVarInt(Ingredient));
data.AddRange(DataTypes.GetFloat(ItemModelIndex));
data.AddRange(DataTypes.GetVarInt(NumberOfOverrides));
if (NumberOfOverrides > 0)
{
if(NumberOfOverrides != Overrides?.Count)
throw new NullReferenceException("Can't serialize the TrimComponent because value of NumberOfOverrides and the size of Overrides don't match!");
foreach (var (armorMaterialType, assetName) in Overrides)
{
data.AddRange(DataTypes.GetVarInt(armorMaterialType));
data.AddRange(DataTypes.GetString(assetName));
}
}
data.AddRange(DataTypes.GetNbt(DescriptionNbt));
}
data.AddRange(DataTypes.GetVarInt(TrimPatternType));
if (TrimPatternType == 0)
{
if (string.IsNullOrEmpty(TrimPatternTypeAssetName))
throw new NullReferenceException("Can't serialize the TrimComponent because the TrimPatternTypeAssetName is null!");
data.AddRange(DataTypes.GetString(TrimPatternTypeAssetName));
data.AddRange(DataTypes.GetVarInt(TemplateItem));
data.AddRange(DataTypes.GetNbt(TrimPatternTypeDescriptionNbt));
data.AddRange(DataTypes.GetBool(Decal));
}
data.AddRange(DataTypes.GetBool(ShowInTooltip));
return new Queue<byte>(data);
}
}