From a36ba23ba617d9a2e1a3dfeb73d8ad3edecacd15 Mon Sep 17 00:00:00 2001 From: BruceChen Date: Fri, 20 Mar 2026 00:09:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20StructuredComponents=20batch=201=20audit?= =?UTF-8?q?=20=E2=80=94=20TrimComponent,=20ProfileComponent,=20WrittenBook?= =?UTF-8?q?Content,=20and=20NBT=20serialization?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 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 --- MinecraftClient/Inventory/BookPage.cs | 9 +++- .../Protocol/Handlers/DataTypes.cs | 27 ++++++++---- .../Components/1_20_6/ProfileComponent.cs | 26 +++++------- .../Components/1_20_6/TrimComponent.cs | 42 ++++++++++--------- .../1_20_6/WrittenBlookContentComponent.cs | 33 +++++++-------- 5 files changed, 77 insertions(+), 60 deletions(-) diff --git a/MinecraftClient/Inventory/BookPage.cs b/MinecraftClient/Inventory/BookPage.cs index c7ec7e44..9240d77e 100644 --- a/MinecraftClient/Inventory/BookPage.cs +++ b/MinecraftClient/Inventory/BookPage.cs @@ -1,3 +1,10 @@ +using System.Collections.Generic; + namespace MinecraftClient.Inventory; -public record BookPage(string RawContent, bool HasFilteredContent, string? FilteredContent); \ No newline at end of file +public record BookPage( + string RawContent, + bool HasFilteredContent, + string? FilteredContent, + Dictionary? RawContentNbt = null, + Dictionary? FilteredContentNbt = null); \ No newline at end of file diff --git a/MinecraftClient/Protocol/Handlers/DataTypes.cs b/MinecraftClient/Protocol/Handlers/DataTypes.cs index af09909c..d36ab257 100644 --- a/MinecraftClient/Protocol/Handlers/DataTypes.cs +++ b/MinecraftClient/Protocol/Handlers/DataTypes.cs @@ -1231,18 +1231,31 @@ namespace MinecraftClient.Protocol.Handlers if (root) { + if (protocolversion >= Protocol18Handler.MC_1_20_4_Version + && nbt.Count == 1 + && nbt.TryGetValue("", out var rootVal) && rootVal is string rootStr) + { + bytes.Add(8); // TAG_String + var strBytes = Encoding.UTF8.GetBytes(rootStr); + bytes.AddRange(GetUShort((ushort)strBytes.Length)); + bytes.AddRange(strBytes); + return bytes.ToArray(); + } + bytes.Add(10); // TAG_Compound - // NBT root name - string? rootName = null; + if (protocolversion < Protocol18Handler.MC_1_20_2_Version) + { + string? rootName = null; - if (nbt.ContainsKey("")) - rootName = nbt[""] as string; + if (nbt.ContainsKey("")) + rootName = nbt[""] as string; - rootName ??= ""; + rootName ??= ""; - bytes.AddRange(GetUShort((ushort)rootName.Length)); - bytes.AddRange(Encoding.ASCII.GetBytes(rootName)); + bytes.AddRange(GetUShort((ushort)rootName.Length)); + bytes.AddRange(Encoding.ASCII.GetBytes(rootName)); + } } foreach (var item in nbt) diff --git a/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/ProfileComponent.cs b/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/ProfileComponent.cs index fc8dc441..17853fbd 100644 --- a/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/ProfileComponent.cs +++ b/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/ProfileComponent.cs @@ -51,26 +51,22 @@ public class ProfileComponent(DataTypes dataTypes, ItemPalette itemPalette, SubC data.AddRange(DataTypes.GetString(Name)); } + data.AddRange(DataTypes.GetBool(HasUniqueId)); if (HasUniqueId) data.AddRange(DataTypes.GetUUID(Uuid)); - if (NumberOfProperties > 0) + data.AddRange(DataTypes.GetVarInt(ProfileProperties.Count)); + foreach (var profileProperty in ProfileProperties) { - if(NumberOfProperties != ProfileProperties.Count) - throw new Exception("Can't serialize the ProfileComponent because the NumberOfProperties and ProfileProperties.Count differ!"); - - foreach (var profileProperty in ProfileProperties) + data.AddRange(DataTypes.GetString(profileProperty.Name)); + data.AddRange(DataTypes.GetString(profileProperty.Value)); + data.AddRange(DataTypes.GetBool(profileProperty.HasSignature)); + if (profileProperty.HasSignature) { - data.AddRange(DataTypes.GetString(profileProperty.Name)); - data.AddRange(DataTypes.GetString(profileProperty.Value)); - data.AddRange(DataTypes.GetBool(profileProperty.HasSignature)); - if (profileProperty.HasSignature) - { - if(string.IsNullOrEmpty(profileProperty.Signature)) - throw new NullReferenceException("Can't serialize the ProfileComponent because HasSignature is true, but the Signature is null/empty!"); - - data.AddRange(DataTypes.GetString(profileProperty.Signature)); - } + if(string.IsNullOrEmpty(profileProperty.Signature)) + throw new NullReferenceException("Can't serialize the ProfileComponent because HasSignature is true, but the Signature is null/empty!"); + + data.AddRange(DataTypes.GetString(profileProperty.Signature)); } } diff --git a/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/TrimComponent.cs b/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/TrimComponent.cs index 25d30c4a..374f1962 100644 --- a/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/TrimComponent.cs +++ b/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/TrimComponent.cs @@ -15,10 +15,12 @@ public class TrimComponent(DataTypes dataTypes, ItemPalette itemPalette, SubComp public float ItemModelIndex { get; set; } public int NumberOfOverrides { get; set; } public List? Overrides { get; set; } + public Dictionary? 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? TrimPatternTypeDescriptionNbt { get; set; } public string TrimPatternTypeDescription { get; set; } = null!; public bool Decal { get; set; } public bool ShowInTooltip { get; set; } @@ -43,7 +45,8 @@ public class TrimComponent(DataTypes dataTypes, ItemPalette itemPalette, SubComp dataTypes.ReadNextString(data))); } - Description = ChatParser.ParseText(dataTypes.ReadNextString(data)); + DescriptionNbt = dataTypes.ReadNextNbt(data); + Description = ChatParser.ParseText(DescriptionNbt); } TrimPatternType = dataTypes.ReadNextVarInt(data); @@ -52,7 +55,8 @@ public class TrimComponent(DataTypes dataTypes, ItemPalette itemPalette, SubComp { TrimPatternTypeAssetName = dataTypes.ReadNextString(data); TemplateItem = dataTypes.ReadNextVarInt(data); - TrimPatternTypeDescription = dataTypes.ReadNextString(data); + TrimPatternTypeDescriptionNbt = dataTypes.ReadNextNbt(data); + TrimPatternTypeDescription = ChatParser.ParseText(TrimPatternTypeDescriptionNbt); Decal = dataTypes.ReadNextBool(data); } @@ -67,8 +71,8 @@ public class TrimComponent(DataTypes dataTypes, ItemPalette itemPalette, SubComp if (TrimMaterialType == 0) { - if (string.IsNullOrEmpty(AssetName) || string.IsNullOrEmpty(Description)) - throw new NullReferenceException("Can't serialize the TrimComponent because the Asset Name or Description are null!"); + 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)); @@ -85,22 +89,22 @@ public class TrimComponent(DataTypes dataTypes, ItemPalette itemPalette, SubComp data.AddRange(DataTypes.GetString(assetName)); } } - data.AddRange(DataTypes.GetString(Description)); - - data.AddRange(DataTypes.GetVarInt(TrimPatternType)); - if (TrimPatternType == 0) - { - if (string.IsNullOrEmpty(TrimPatternTypeAssetName) || string.IsNullOrEmpty(TrimPatternTypeDescription)) - throw new NullReferenceException("Can't serialize the TrimComponent because the TrimPatternTypeAssetName or TrimPatternTypeDescription are null!"); - - data.AddRange(DataTypes.GetString(TrimPatternTypeAssetName)); - data.AddRange(DataTypes.GetVarInt(TemplateItem)); - data.AddRange(DataTypes.GetString(TrimPatternTypeDescription)); - data.AddRange(DataTypes.GetBool(Decal)); - } - - data.AddRange(DataTypes.GetBool(ShowInTooltip)); + 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(data); } diff --git a/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/WrittenBlookContentComponent.cs b/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/WrittenBlookContentComponent.cs index bf315b3d..1f7cc905 100644 --- a/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/WrittenBlookContentComponent.cs +++ b/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/WrittenBlookContentComponent.cs @@ -20,7 +20,7 @@ public class WrittenBlookContentComponent(DataTypes dataTypes, ItemPalette itemP public override void Parse(Queue data) { - RawTitle = ChatParser.ParseText(dataTypes.ReadNextString(data)); + RawTitle = dataTypes.ReadNextString(data); HasFilteredTitle = dataTypes.ReadNextBool(data); if (HasFilteredTitle) @@ -32,14 +32,19 @@ public class WrittenBlookContentComponent(DataTypes dataTypes, ItemPalette itemP for (var i = 0; i < NumberOfPages; i++) { - var rawContent = ChatParser.ParseText(dataTypes.ReadNextString(data)); + var rawContentNbt = dataTypes.ReadNextNbt(data); + var rawContent = ChatParser.ParseText(rawContentNbt); var hasFilteredContent = dataTypes.ReadNextBool(data); - var filteredContent = null as string; + Dictionary? filteredContentNbt = null; + string? filteredContent = null; - if(hasFilteredContent) - filteredContent = dataTypes.ReadNextString(data); + if (hasFilteredContent) + { + filteredContentNbt = dataTypes.ReadNextNbt(data); + filteredContent = ChatParser.ParseText(filteredContentNbt); + } - Pages.Add(new BookPage(rawContent, hasFilteredContent, filteredContent)); + Pages.Add(new BookPage(rawContent, hasFilteredContent, filteredContent, rawContentNbt, filteredContentNbt)); } Resolved = dataTypes.ReadNextBool(data); @@ -55,30 +60,22 @@ public class WrittenBlookContentComponent(DataTypes dataTypes, ItemPalette itemP if (HasFilteredTitle) { if(FilteredTitle is null) - throw new InvalidOperationException("Can not setialize WrittenBlookContentComponent1206 because HasFilteredTitle is true but FilteredTitle is null!"); + throw new InvalidOperationException("Can not serialize WrittenBookContentComponent because HasFilteredTitle is true but FilteredTitle is null!"); data.AddRange(DataTypes.GetString(FilteredTitle)); } data.AddRange(DataTypes.GetString(Author)); data.AddRange(DataTypes.GetVarInt(Generation)); - data.AddRange(DataTypes.GetVarInt(NumberOfPages)); - - if (NumberOfPages != Pages.Count) - throw new InvalidOperationException("Can not setialize WrittenBlookContentComponent1206 because NumberOfPages != Pages.Count!"); + data.AddRange(DataTypes.GetVarInt(Pages.Count)); foreach (var page in Pages) { - data.AddRange(DataTypes.GetString(page.RawContent)); + data.AddRange(DataTypes.GetNbt(page.RawContentNbt)); data.AddRange(DataTypes.GetBool(page.HasFilteredContent)); if (page.HasFilteredContent) - { - if(page.FilteredContent is null) - throw new InvalidOperationException("Can not setialize WrittenBlookContentComponent1206 because page.HasFilteredContent = true, but FilteredContent is null!"); - - data.AddRange(DataTypes.GetString(page.FilteredContent)); - } + data.AddRange(DataTypes.GetNbt(page.FilteredContentNbt)); } data.AddRange(DataTypes.GetBool(Resolved)); return new Queue(data);