Fix PotionContentsComponent and InstrumentComponent serialization for 1.20.6

Both components had incorrect Parse/Serialize implementations that would
cause packet deserialization misalignment when encountered in-game.

PotionContentsComponent (3 bugs):
- Serialize unconditionally wrote VarInt(PotionId) and Int(CustomColor)
  even when HasPotionId/HasCustomColor was false. The official format
  (PotionContents.STREAM_CODEC) uses Optional encoding: Bool(hasValue)
  followed by the value only when true. The extra bytes caused all
  subsequent fields in the packet to be read at wrong offsets.
- Serialize omitted the VarInt(count) prefix for the custom effects list.
  The official codec uses ByteBufCodecs.list() which always writes a
  VarInt count header before the list elements.
- Also fixed typo: PotiononId -> PotionId.

InstrumentComponent (3 bugs):
- The official Instrument.STREAM_CODEC uses ByteBufCodecs.holder() which
  encodes as VarInt(holderId): 0 = inline data, N>0 = registry ref (N-1).
  The SoundEvent field inside uses the same holder pattern. The old code
  unconditionally read SoundName (ResourceLocation) and HasFixedRange/
  FixedRange even when SoundEventHolderId != 0 (registry reference case
  has no inline data).
- UseDuration was read/written as Float, but the official codec uses
  ByteBufCodecs.VAR_INT. This caused a 4-byte vs variable-length
  mismatch that would shift all subsequent data.
- HasFixedRange was read unconditionally when SoundEventHolderId == 0,
  but FixedRange was also read unconditionally. The official SoundEvent
  DIRECT_STREAM_CODEC uses Optional<Float> encoding: Bool(hasValue)
  followed by Float only when true.

These components are used for potion items and goat horns respectively.
Verified against official 1.20.6 decompiled source:
- net.minecraft.world.item.alchemy.PotionContents (STREAM_CODEC)
- net.minecraft.world.item.Instrument (STREAM_CODEC/DIRECT_STREAM_CODEC)
- net.minecraft.sounds.SoundEvent (STREAM_CODEC/DIRECT_STREAM_CODEC)
- net.minecraft.network.codec.ByteBufCodecs (holder/optional/list)

Made-with: Cursor
This commit is contained in:
BruceChen 2026-03-19 01:44:23 +08:00
parent 56f2426c1f
commit 1e2b853b14
2 changed files with 48 additions and 48 deletions

View file

@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using MinecraftClient.Inventory.ItemPalettes;
using MinecraftClient.Protocol.Handlers.StructuredComponents.Components.Subcomponents;
@ -11,21 +10,23 @@ public class PotionContentsComponent(DataTypes dataTypes, ItemPalette itemPalett
: StructuredComponent(dataTypes, itemPalette, subComponentRegistry)
{
public bool HasPotionId { get; set; }
public int PotiononId { get; set; }
public int PotionId { get; set; }
public bool HasCustomColor { get; set; }
public int CustomColor { get; set; }
public int NumberOfCustomEffects { get; set; }
public List<PotionEffectSubComponent> Effects { get; set; } = new();
public override void Parse(Queue<byte> data)
{
HasPotionId = dataTypes.ReadNextBool(data);
PotiononId = HasPotionId ? dataTypes.ReadNextVarInt(data) : 0; // TODO: Find from the registry
if (HasPotionId)
PotionId = dataTypes.ReadNextVarInt(data);
HasCustomColor = dataTypes.ReadNextBool(data);
CustomColor = HasCustomColor ? dataTypes.ReadNextInt(data) : 0; // TODO: Find from the registry
NumberOfCustomEffects = dataTypes.ReadNextVarInt(data);
for(var i = 0; i < NumberOfCustomEffects; i++)
if (HasCustomColor)
CustomColor = dataTypes.ReadNextInt(data);
var numberOfEffects = dataTypes.ReadNextVarInt(data);
for (var i = 0; i < numberOfEffects; i++)
Effects.Add((PotionEffectSubComponent)subComponentRegistry.ParseSubComponent(SubComponents.PotionEffect, data));
}
@ -33,19 +34,17 @@ public class PotionContentsComponent(DataTypes dataTypes, ItemPalette itemPalett
{
var data = new List<byte>();
data.AddRange(DataTypes.GetBool(HasPotionId));
data.AddRange(DataTypes.GetVarInt(PotiononId));
data.AddRange(DataTypes.GetBool(HasCustomColor));
data.AddRange(DataTypes.GetInt(CustomColor));
if (HasPotionId)
data.AddRange(DataTypes.GetVarInt(PotionId));
if (NumberOfCustomEffects > 0)
{
if(Effects.Count != NumberOfCustomEffects)
throw new ArgumentNullException($"Can not serialize PotionContentsComponentComponent1206 due to NumberOfCustomEffects being different from the count of elements in the Effects list!");
foreach(var effect in Effects)
data.AddRange(effect.Serialize());
}
data.AddRange(DataTypes.GetBool(HasCustomColor));
if (HasCustomColor)
data.AddRange(DataTypes.GetInt(CustomColor));
data.AddRange(DataTypes.GetVarInt(Effects.Count));
foreach (var effect in Effects)
data.AddRange(effect.Serialize());
return new Queue<byte>(data);
}
}
}