Enhance container handling for Minecraft protocol updates

- Updated the Container class to include a protocol version parameter for accurate container type mapping.
- Modified the GetContainerType method to account for changes in container types introduced in Minecraft 1.20.4.
- Added new container types, including Crafter, to the ContainerType enum.
- Adjusted ContainerTypeExtensions to reflect the new container mappings and ensure compatibility with the updated protocol.
This commit is contained in:
BruceChen 2026-03-28 02:16:31 +08:00
parent 22f46d14a9
commit ef133f3d6d
4 changed files with 69 additions and 19 deletions

View file

@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
namespace MinecraftClient.Inventory
{
@ -91,10 +91,11 @@ namespace MinecraftClient.Inventory
/// <param name="id">Container ID</param>
/// <param name="typeID">Container Type</param>
/// <param name="title">Container Title</param>
public Container(int id, int typeID, string title)
/// <param name="protocolVersion">Protocol version for version-specific mapping</param>
public Container(int id, int typeID, string title, int protocolVersion = 0)
{
ID = id;
Type = GetContainerType(typeID);
Type = GetContainerType(typeID, protocolVersion);
Title = title;
Items = new();
Properties = new();
@ -131,22 +132,62 @@ namespace MinecraftClient.Inventory
/// Get container type from Type ID
/// </summary>
/// <param name="typeID">Container Type ID</param>
/// <param name="protocolVersion">Protocol version (menu registry changed across versions)</param>
/// <returns>Container Type</returns>
public static ContainerType GetContainerType(int typeID)
public static ContainerType GetContainerType(int typeID, int protocolVersion = 0)
{
// https://wiki.vg/Inventory didn't state the inventory ID, assume that list start with 0
// MC 1.20.4 (protocol 765) added crafter_3x3 at index 7, shifting all subsequent IDs by +1.
// Registry order from decompiled MenuType.java:
// 1.14-1.20.2: generic_9x1..generic_3x3(6), anvil(7), beacon(8), ... stonecutter(22)
// 1.20.4+: generic_9x1..generic_3x3(6), crafter_3x3(7), anvil(8), beacon(9), ... stonecutter(24)
if (protocolVersion >= 765)
{
return typeID switch
{
#pragma warning disable format // @formatter:off
0 => ContainerType.Generic_9x1,
1 => ContainerType.Generic_9x2,
2 => ContainerType.Generic_9x3,
3 => ContainerType.Generic_9x4,
4 => ContainerType.Generic_9x5,
5 => ContainerType.Generic_9x6,
6 => ContainerType.Generic_3x3,
7 => ContainerType.Crafter,
8 => ContainerType.Anvil,
9 => ContainerType.Beacon,
10 => ContainerType.BlastFurnace,
11 => ContainerType.BrewingStand,
12 => ContainerType.Crafting,
13 => ContainerType.Enchantment,
14 => ContainerType.Furnace,
15 => ContainerType.Grindstone,
16 => ContainerType.Hopper,
17 => ContainerType.Lectern,
18 => ContainerType.Loom,
19 => ContainerType.Merchant,
20 => ContainerType.ShulkerBox,
21 => ContainerType.SmightingTable,
22 => ContainerType.Smoker,
23 => ContainerType.Cartography,
24 => ContainerType.Stonecutter,
_ => ContainerType.Unknown,
#pragma warning restore format // @formatter:on
};
}
return typeID switch
{
0 => ContainerType.Generic_9x1,
1 => ContainerType.Generic_9x2,
2 => ContainerType.Generic_9x3,
3 => ContainerType.Generic_9x4,
4 => ContainerType.Generic_9x5,
5 => ContainerType.Generic_9x6,
6 => ContainerType.Generic_3x3,
7 => ContainerType.Anvil,
8 => ContainerType.Beacon,
9 => ContainerType.BlastFurnace,
#pragma warning disable format // @formatter:off
0 => ContainerType.Generic_9x1,
1 => ContainerType.Generic_9x2,
2 => ContainerType.Generic_9x3,
3 => ContainerType.Generic_9x4,
4 => ContainerType.Generic_9x5,
5 => ContainerType.Generic_9x6,
6 => ContainerType.Generic_3x3,
7 => ContainerType.Anvil,
8 => ContainerType.Beacon,
9 => ContainerType.BlastFurnace,
10 => ContainerType.BrewingStand,
11 => ContainerType.Crafting,
12 => ContainerType.Enchantment,
@ -160,7 +201,8 @@ namespace MinecraftClient.Inventory
20 => ContainerType.Smoker,
21 => ContainerType.Cartography,
22 => ContainerType.Stonecutter,
_ => ContainerType.Unknown,
_ => ContainerType.Unknown,
#pragma warning restore format // @formatter:on
};
}

View file

@ -1,4 +1,4 @@
namespace MinecraftClient.Inventory
namespace MinecraftClient.Inventory
{
// For MC 1.14 after ONLY
public enum ContainerType
@ -10,6 +10,7 @@
Generic_9x5,
Generic_9x6,
Generic_3x3,
Crafter,
Anvil,
Beacon,
BlastFurnace,

View file

@ -1,4 +1,4 @@
namespace MinecraftClient.Inventory
namespace MinecraftClient.Inventory
{
public static class ContainerTypeExtensions
{
@ -13,9 +13,14 @@
{
#pragma warning disable format // @formatter:off
ContainerType.PlayerInventory => 46,
ContainerType.Generic_9x1 => 45,
ContainerType.Generic_9x2 => 54,
ContainerType.Generic_9x3 => 63,
ContainerType.Generic_9x4 => 72,
ContainerType.Generic_9x5 => 81,
ContainerType.Generic_9x6 => 90,
ContainerType.Generic_3x3 => 45,
ContainerType.Crafter => 45,
ContainerType.Crafting => 46,
ContainerType.BlastFurnace => 39,
ContainerType.Furnace => 39,
@ -27,6 +32,7 @@
ContainerType.Anvil => 39,
ContainerType.Hopper => 41,
ContainerType.ShulkerBox => 63,
ContainerType.SmightingTable => 39,
ContainerType.Loom => 40,
ContainerType.Stonecutter => 38,
ContainerType.Lectern => 37,
@ -52,6 +58,7 @@
ContainerType.Generic_9x3 => AsciiArt.Container_Generic_9x3,
ContainerType.Generic_9x6 => AsciiArt.Container_Generic_9x6,
ContainerType.Generic_3x3 => AsciiArt.Container_Generic_3x3,
ContainerType.Crafter => AsciiArt.Container_Generic_3x3,
ContainerType.Crafting => AsciiArt.Container_Crafting,
ContainerType.BlastFurnace => AsciiArt.Container_Furnace,
ContainerType.Furnace => AsciiArt.Container_Furnace,

View file

@ -2319,7 +2319,7 @@ namespace MinecraftClient.Protocol.Handlers
var windowId = dataTypes.ReadNextVarInt(packetData);
var windowType = dataTypes.ReadNextVarInt(packetData);
var title = dataTypes.ReadNextChat(packetData);
Container inventory = new(windowId, windowType, ChatParser.ParseText(title));
Container inventory = new(windowId, windowType, ChatParser.ParseText(title), protocolVersion);
handler.OnInventoryOpen(windowId, inventory);
}
}