From cc10f4effab476f872b984c353187ee040e0da23 Mon Sep 17 00:00:00 2001 From: BruceChen Date: Fri, 20 Mar 2026 00:45:50 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20StructuredComponents=20batch=205=20?= =?UTF-8?q?audit=20=E2=80=94=20remove=20redundant=20count=20fields?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Audited batch 5 components (ChargedProjectiles, BundleContents, Container, WritableBookContent, BlockState, PotDecorations) against official 1.20.6 decompiled STREAM_CODEC definitions. All network encodings were correct. Removed redundant NumberOfItems/NumberOfPages/NumberOfProperties fields from ContainerComponent, WritableBlookContentComponent, BlockStateComponent, and PotDecorationsComponent. Serialize now uses the actual collection .Count instead of a potentially stale cached value, matching the pattern already used by ContainerComponent's Serialize and other components. Also modernized loop style (foreach with deconstruction where applicable) and fixed a typo in an exception message ("setialize" -> "serialize"). Made-with: Cursor --- .../Components/1_20_6/BlockStateComponent.cs | 13 ++++++------- .../Components/1_20_6/ContainerComponent.cs | 5 ++--- .../Components/1_20_6/PotDecorationsComponent.cs | 11 +++++------ .../1_20_6/WritableBlookContentComponent.cs | 12 ++++-------- 4 files changed, 17 insertions(+), 24 deletions(-) diff --git a/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/BlockStateComponent.cs b/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/BlockStateComponent.cs index be3950fe..8037d07c 100644 --- a/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/BlockStateComponent.cs +++ b/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/BlockStateComponent.cs @@ -7,24 +7,23 @@ namespace MinecraftClient.Protocol.Handlers.StructuredComponents.Components._1_2 public class BlockStateComponent(DataTypes dataTypes, ItemPalette itemPalette, SubComponentRegistry subComponentRegistry) : StructuredComponent(dataTypes, itemPalette, subComponentRegistry) { - public int NumberOfProperties { get; set; } public List<(string, string)> Properties { get; set; } = []; public override void Parse(Queue data) { - NumberOfProperties = dataTypes.ReadNextVarInt(data); - for(var i = 0; i < NumberOfProperties; i++) + var count = dataTypes.ReadNextVarInt(data); + for(var i = 0; i < count; i++) Properties.Add((dataTypes.ReadNextString(data), dataTypes.ReadNextString(data))); } public override Queue Serialize() { var data = new List(); - data.AddRange(DataTypes.GetVarInt(NumberOfProperties)); - for (var i = 0; i < NumberOfProperties; i++) + data.AddRange(DataTypes.GetVarInt(Properties.Count)); + foreach (var (key, value) in Properties) { - data.AddRange(DataTypes.GetString(Properties[i].Item1)); - data.AddRange(DataTypes.GetString(Properties[i].Item2)); + data.AddRange(DataTypes.GetString(key)); + data.AddRange(DataTypes.GetString(value)); } return new Queue(data); diff --git a/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/ContainerComponent.cs b/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/ContainerComponent.cs index 053fc918..c132e06f 100644 --- a/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/ContainerComponent.cs +++ b/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/ContainerComponent.cs @@ -8,13 +8,12 @@ namespace MinecraftClient.Protocol.Handlers.StructuredComponents.Components._1_2 public class ContainerComponent(DataTypes dataTypes, ItemPalette itemPalette, SubComponentRegistry subComponentRegistry) : StructuredComponent(dataTypes, itemPalette, subComponentRegistry) { - public int NumberOfItems { get; set; } public List Items { get; set; } = []; public override void Parse(Queue data) { - NumberOfItems = dataTypes.ReadNextVarInt(data); - for (var i = 0; i < NumberOfItems; i++) + var count = dataTypes.ReadNextVarInt(data); + for (var i = 0; i < count; i++) Items.Add(dataTypes.ReadNextItemSlot(data, ItemPalette)); } diff --git a/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/PotDecorationsComponent.cs b/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/PotDecorationsComponent.cs index 74607228..0acc10e6 100644 --- a/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/PotDecorationsComponent.cs +++ b/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/PotDecorationsComponent.cs @@ -7,22 +7,21 @@ namespace MinecraftClient.Protocol.Handlers.StructuredComponents.Components._1_2 public class PotDecorationsComponent(DataTypes dataTypes, ItemPalette itemPalette, SubComponentRegistry subComponentRegistry) : StructuredComponent(dataTypes, itemPalette, subComponentRegistry) { - public int NumberOfItems { get; set; } public List Items { get; set; } = []; public override void Parse(Queue data) { - NumberOfItems = dataTypes.ReadNextVarInt(data); - for(var i = 0; i < NumberOfItems; i++) + var count = dataTypes.ReadNextVarInt(data); + for(var i = 0; i < count; i++) Items.Add(dataTypes.ReadNextVarInt(data)); } public override Queue Serialize() { var data = new List(); - data.AddRange(DataTypes.GetVarInt(NumberOfItems)); - for(var i = 0; i < NumberOfItems; i++) - data.AddRange(DataTypes.GetVarInt(Items[i])); + data.AddRange(DataTypes.GetVarInt(Items.Count)); + foreach (var item in Items) + data.AddRange(DataTypes.GetVarInt(item)); return new Queue(data); } } \ No newline at end of file diff --git a/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/WritableBlookContentComponent.cs b/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/WritableBlookContentComponent.cs index 9c782d55..e22c714a 100644 --- a/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/WritableBlookContentComponent.cs +++ b/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/WritableBlookContentComponent.cs @@ -8,14 +8,13 @@ namespace MinecraftClient.Protocol.Handlers.StructuredComponents.Components._1_2 public class WritableBlookContentComponent(DataTypes dataTypes, ItemPalette itemPalette, SubComponentRegistry subComponentRegistry) : StructuredComponent(dataTypes, itemPalette, subComponentRegistry) { - public int NumberOfPages { get; set; } public List Pages { get; set; } = []; public override void Parse(Queue data) { - NumberOfPages = dataTypes.ReadNextVarInt(data); + var count = dataTypes.ReadNextVarInt(data); - for (var i = 0; i < NumberOfPages; i++) + for (var i = 0; i < count; i++) { var rawContent = dataTypes.ReadNextString(data); var hasFilteredContent = dataTypes.ReadNextBool(data); @@ -32,10 +31,7 @@ public class WritableBlookContentComponent(DataTypes dataTypes, ItemPalette item { var data = new List(); - data.AddRange(DataTypes.GetVarInt(NumberOfPages)); - - if (NumberOfPages != Pages.Count) - throw new InvalidOperationException("Can not setialize WritableBlookContentComponent1206 because NumberOfPages != Pages.Count!"); + data.AddRange(DataTypes.GetVarInt(Pages.Count)); foreach (var page in Pages) { @@ -45,7 +41,7 @@ public class WritableBlookContentComponent(DataTypes dataTypes, ItemPalette item if (page.HasFilteredContent) { if(page.FilteredContent is null) - throw new InvalidOperationException("Can not setialize WritableBlookContentComponent1206 because page.HasFilteredContent = true, but FilteredContent is null!"); + throw new InvalidOperationException("Can not serialize WritableBlookContentComponent because page.HasFilteredContent = true, but FilteredContent is null!"); data.AddRange(DataTypes.GetString(page.FilteredContent)); }