mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2026-08-15 13:04:36 +00:00
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
This commit is contained in:
parent
5e416d6b72
commit
cc10f4effa
4 changed files with 17 additions and 24 deletions
|
|
@ -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<byte> 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<byte> Serialize()
|
||||
{
|
||||
var data = new List<byte>();
|
||||
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<byte>(data);
|
||||
|
|
|
|||
|
|
@ -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<Item?> Items { get; set; } = [];
|
||||
|
||||
public override void Parse(Queue<byte> 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));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<int> Items { get; set; } = [];
|
||||
|
||||
public override void Parse(Queue<byte> 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<byte> Serialize()
|
||||
{
|
||||
var data = new List<byte>();
|
||||
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<byte>(data);
|
||||
}
|
||||
}
|
||||
|
|
@ -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<BookPage> Pages { get; set; } = [];
|
||||
|
||||
public override void Parse(Queue<byte> 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<byte>();
|
||||
|
||||
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));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue