Minecraft-Console-Client/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_20_6/WritableBlookContentComponent.cs
BruceChen cc10f4effa refactor: StructuredComponents batch 5 audit — remove redundant count fields
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
2026-03-20 00:45:50 +08:00

52 lines
No EOL
1.8 KiB
C#

using System;
using System.Collections.Generic;
using MinecraftClient.Inventory;
using MinecraftClient.Inventory.ItemPalettes;
using MinecraftClient.Protocol.Handlers.StructuredComponents.Core;
namespace MinecraftClient.Protocol.Handlers.StructuredComponents.Components._1_20_6;
public class WritableBlookContentComponent(DataTypes dataTypes, ItemPalette itemPalette, SubComponentRegistry subComponentRegistry) : StructuredComponent(dataTypes, itemPalette, subComponentRegistry)
{
public List<BookPage> Pages { get; set; } = [];
public override void Parse(Queue<byte> data)
{
var count = dataTypes.ReadNextVarInt(data);
for (var i = 0; i < count; i++)
{
var rawContent = dataTypes.ReadNextString(data);
var hasFilteredContent = dataTypes.ReadNextBool(data);
var filteredContent = null as string;
if(hasFilteredContent)
filteredContent = dataTypes.ReadNextString(data);
Pages.Add(new BookPage(rawContent, hasFilteredContent, filteredContent));
}
}
public override Queue<byte> Serialize()
{
var data = new List<byte>();
data.AddRange(DataTypes.GetVarInt(Pages.Count));
foreach (var page in Pages)
{
data.AddRange(DataTypes.GetString(page.RawContent));
data.AddRange(DataTypes.GetBool(page.HasFilteredContent));
if (page.HasFilteredContent)
{
if(page.FilteredContent is null)
throw new InvalidOperationException("Can not serialize WritableBlookContentComponent because page.HasFilteredContent = true, but FilteredContent is null!");
data.AddRange(DataTypes.GetString(page.FilteredContent));
}
}
return new Queue<byte>(data);
}
}