Minecraft-Console-Client/MinecraftClient/Protocol/Handlers/StructuredComponents/Components/1_21_5/EnchantmentsComponent1215.cs
BruceChen c0c4c078c0 fix: use HashedStack for container_click and fix enchantments parsing for 1.21.5+
Two bugs fixed:

1. EnchantmentsComponent was reading a trailing ShowTooltip boolean that
   was removed from the wire format in MC 1.21.5. Created
   EnchantmentsComponent1215 and StoredEnchantmentsComponent1215 that
   omit the boolean. Used by StructuredComponentsRegistry1215 and 12111.

2. MC 1.21.5+ changed ServerboundContainerClickPacket to use HashedStack
   (item holder id + count + hashed component patch map) instead of full
   ItemStack for changed slots and carried item. Added GetHashedItemSlot()
   in DataTypes.cs and gated SendWindowAction in Protocol18.cs to use it
   for 1.21.5+. Since MCC doesn't track component hashes, an empty
   HashedPatchMap is sent; the server detects stateId mismatch and resyncs.

Tested: AutoFishing bot successfully catches fish on MC 1.21.11 with
enchanted fishing rods (Lure III + Luck of the Sea III).

Made-with: Cursor
2026-03-22 02:18:01 +08:00

39 lines
1.6 KiB
C#

using System.Collections.Generic;
using MinecraftClient.Inventory;
using MinecraftClient.Inventory.ItemPalettes;
using MinecraftClient.Protocol.Handlers.StructuredComponents.Components._1_20_6;
using MinecraftClient.Protocol.Handlers.StructuredComponents.Core;
namespace MinecraftClient.Protocol.Handlers.StructuredComponents.Components._1_21_5;
/// <summary>
/// 1.21.5+ enchantments: showInTooltip removed from wire format (moved to tooltip_display component).
/// Wire: VarInt count, then (VarInt holder_id + VarInt level) per entry. No trailing boolean.
/// </summary>
public class EnchantmentsComponent1215(DataTypes dataTypes, ItemPalette itemPalette, SubComponentRegistry subComponentRegistry)
: EnchantmentsComponent(dataTypes, itemPalette, subComponentRegistry)
{
public override void Parse(Queue<byte> data)
{
NumberOfEnchantments = dataTypes.ReadNextVarInt(data);
for (var i = 0; i < NumberOfEnchantments; i++)
{
var registryId = dataTypes.ReadNextVarInt(data);
var level = dataTypes.ReadNextVarInt(data);
Enchantments.Add(new Enchantment(EnchantmentMapping.GetEnchantmentByRegistryId1206(registryId), level));
}
}
public override Queue<byte> Serialize()
{
var data = new List<byte>();
data.AddRange(DataTypes.GetVarInt(Enchantments.Count));
foreach (var enchantment in Enchantments)
{
data.AddRange(DataTypes.GetVarInt(EnchantmentMapping.GetRegistryId1206ByEnchantment(enchantment.Type)));
data.AddRange(DataTypes.GetVarInt(enchantment.Level));
}
return new Queue<byte>(data);
}
}