feat: Added missing enchantments for new versions

This commit is contained in:
Anon 2026-03-25 20:01:43 +01:00 committed by GitHub
commit 966b787a4c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 119 additions and 31 deletions

View file

@ -138,8 +138,8 @@ namespace MinecraftClient.Inventory
{ 38, Enchantments.VanishingCurse }
};
// 1.20.6+
private static Dictionary<short, Enchantments> enchantmentMappings = new()
// 1.20.6 - 1.21.10
private static Dictionary<short, Enchantments> enchantmentMappings1206 = new()
{
//id type
{ 0, Enchantments.Protection },
@ -185,20 +185,63 @@ namespace MinecraftClient.Inventory
{ 40, Enchantments.Mending },
{ 41, Enchantments.VanishingCurse }
};
// 1.21.11+
private static Dictionary<short, Enchantments> enchantmentMappings12111 = new()
{
//id type
{ 0, Enchantments.Protection },
{ 1, Enchantments.FireProtection },
{ 2, Enchantments.FeatherFalling },
{ 3, Enchantments.BlastProtection },
{ 4, Enchantments.ProjectileProtection },
{ 5, Enchantments.Respiration },
{ 6, Enchantments.AquaAffinity },
{ 7, Enchantments.Thorns },
{ 8, Enchantments.DepthStrider },
{ 9, Enchantments.FrostWalker },
{ 10, Enchantments.BindingCurse },
{ 11, Enchantments.SoulSpeed },
{ 12, Enchantments.SwiftSneak },
{ 13, Enchantments.Sharpness },
{ 14, Enchantments.Smite },
{ 15, Enchantments.BaneOfArthropods },
{ 16, Enchantments.Knockback },
{ 17, Enchantments.FireAspect },
{ 18, Enchantments.Looting },
{ 19, Enchantments.Sweeping },
{ 20, Enchantments.Efficiency },
{ 21, Enchantments.SilkTouch },
{ 22, Enchantments.Unbreaking },
{ 23, Enchantments.Fortune },
{ 24, Enchantments.Power },
{ 25, Enchantments.Punch },
{ 26, Enchantments.Flame },
{ 27, Enchantments.Infinity },
{ 28, Enchantments.LuckOfTheSea },
{ 29, Enchantments.Lure },
{ 30, Enchantments.Loyalty },
{ 31, Enchantments.Impaling },
{ 32, Enchantments.Riptide },
{ 33, Enchantments.Channeling },
{ 34, Enchantments.Multishot },
{ 35, Enchantments.QuickCharge },
{ 36, Enchantments.Piercing },
{ 37, Enchantments.Density },
{ 38, Enchantments.Breach },
{ 39, Enchantments.WindBurst },
{ 40, Enchantments.Lunge },
{ 41, Enchantments.Mending },
{ 42, Enchantments.VanishingCurse }
};
#pragma warning restore format // @formatter:on
public static Enchantments GetEnchantmentById(int protocolVersion, short id)
{
if (protocolVersion < Protocol18Handler.MC_1_14_Version)
throw new Exception("Enchantments mappings are not implemented bellow 1.14");
throw new Exception("Enchantments mappings are not implemented below 1.14");
var map = protocolVersion switch
{
>= Protocol18Handler.MC_1_14_Version and < Protocol18Handler.MC_1_16_Version => enchantmentMappings114,
>= Protocol18Handler.MC_1_16_Version and < Protocol18Handler.MC_1_19_Version => enchantmentMappings116,
>= Protocol18Handler.MC_1_19_Version and < Protocol18Handler.MC_1_21_Version => enchantmentMappings119,
_ => enchantmentMappings
};
var map = GetMapForProtocolVersion(protocolVersion);
if (!map.TryGetValue(id, out var value))
throw new Exception($"Got an Unknown Enchantment ID {id}, please update the Mappings!");
@ -206,7 +249,12 @@ namespace MinecraftClient.Inventory
return value;
}
private static Dictionary<Enchantments, short>? reverseEnchantmentMappings;
private static Dictionary<Enchantments, short>? reverseDynamicEnchantmentMappings;
private static readonly Dictionary<Enchantments, short> reverseEnchantmentMappings114 = CreateReverseMap(enchantmentMappings114);
private static readonly Dictionary<Enchantments, short> reverseEnchantmentMappings116 = CreateReverseMap(enchantmentMappings116);
private static readonly Dictionary<Enchantments, short> reverseEnchantmentMappings119 = CreateReverseMap(enchantmentMappings119);
private static readonly Dictionary<Enchantments, short> reverseEnchantmentMappings1206 = CreateReverseMap(enchantmentMappings1206);
private static readonly Dictionary<Enchantments, short> reverseEnchantmentMappings12111 = CreateReverseMap(enchantmentMappings12111);
private static Dictionary<int, Enchantments>? dynamicEnchantmentIdMap;
private static readonly Dictionary<string, Enchantments> nameToEnchantment = new()
@ -242,6 +290,7 @@ namespace MinecraftClient.Inventory
{ "luck_of_the_sea", Enchantments.LuckOfTheSea },
{ "lure", Enchantments.Lure },
{ "loyalty", Enchantments.Loyalty },
{ "lunge", Enchantments.Lunge },
{ "impaling", Enchantments.Impaling },
{ "riptide", Enchantments.Riptide },
{ "channeling", Enchantments.Channeling },
@ -268,35 +317,34 @@ namespace MinecraftClient.Inventory
if (nameToEnchantment.TryGetValue(name, out var enchantment))
dynamicEnchantmentIdMap[kvp.Key] = enchantment;
}
reverseEnchantmentMappings = null;
reverseDynamicEnchantmentMappings = null;
}
public static Enchantments GetEnchantmentByRegistryId1206(int id)
public static Enchantments GetEnchantmentByRegistryId1206(int protocolVersion, int id)
{
if (dynamicEnchantmentIdMap is not null && dynamicEnchantmentIdMap.TryGetValue(id, out var dynValue))
return dynValue;
if (enchantmentMappings.TryGetValue((short)id, out var value))
if (GetMapForProtocolVersion(protocolVersion).TryGetValue((short)id, out var value))
return value;
return (Enchantments)(-1);
}
public static int GetRegistryId1206ByEnchantment(Enchantments enchantment)
public static int GetRegistryId1206ByEnchantment(int protocolVersion, Enchantments enchantment)
{
if (reverseEnchantmentMappings is null)
if (dynamicEnchantmentIdMap is not null)
{
reverseEnchantmentMappings = new();
if (dynamicEnchantmentIdMap is not null)
if (reverseDynamicEnchantmentMappings is null)
{
reverseDynamicEnchantmentMappings = new();
foreach (var kvp in dynamicEnchantmentIdMap)
reverseEnchantmentMappings[kvp.Value] = (short)kvp.Key;
}
else
{
foreach (var kvp in enchantmentMappings)
reverseEnchantmentMappings[kvp.Value] = kvp.Key;
reverseDynamicEnchantmentMappings[kvp.Value] = (short)kvp.Key;
}
return reverseDynamicEnchantmentMappings.TryGetValue(enchantment, out var dynamicId) ? dynamicId : -1;
}
return reverseEnchantmentMappings.TryGetValue(enchantment, out var id) ? id : -1;
var reverseMap = GetReverseMapForProtocolVersion(protocolVersion);
return reverseMap.TryGetValue(enchantment, out var id) ? id : -1;
}
public static string GetEnchantmentName(Enchantments enchantment)
@ -333,5 +381,40 @@ namespace MinecraftClient.Inventory
return result;
}
private static Dictionary<short, Enchantments> GetMapForProtocolVersion(int protocolVersion)
{
return protocolVersion switch
{
>= Protocol18Handler.MC_1_14_Version and < Protocol18Handler.MC_1_16_Version => enchantmentMappings114,
>= Protocol18Handler.MC_1_16_Version and < Protocol18Handler.MC_1_19_Version => enchantmentMappings116,
>= Protocol18Handler.MC_1_19_Version and < Protocol18Handler.MC_1_20_6_Version => enchantmentMappings119,
>= Protocol18Handler.MC_1_20_6_Version and < Protocol18Handler.MC_1_21_11_Version => enchantmentMappings1206,
>= Protocol18Handler.MC_1_21_11_Version => enchantmentMappings12111,
_ => throw new Exception("Enchantments mappings are not implemented below 1.14")
};
}
private static Dictionary<Enchantments, short> GetReverseMapForProtocolVersion(int protocolVersion)
{
return protocolVersion switch
{
>= Protocol18Handler.MC_1_14_Version and < Protocol18Handler.MC_1_16_Version => reverseEnchantmentMappings114,
>= Protocol18Handler.MC_1_16_Version and < Protocol18Handler.MC_1_19_Version => reverseEnchantmentMappings116,
>= Protocol18Handler.MC_1_19_Version and < Protocol18Handler.MC_1_20_6_Version => reverseEnchantmentMappings119,
>= Protocol18Handler.MC_1_20_6_Version and < Protocol18Handler.MC_1_21_11_Version => reverseEnchantmentMappings1206,
>= Protocol18Handler.MC_1_21_11_Version => reverseEnchantmentMappings12111,
_ => throw new Exception("Enchantments mappings are not implemented below 1.14")
};
}
private static Dictionary<Enchantments, short> CreateReverseMap(Dictionary<short, Enchantments> map)
{
Dictionary<Enchantments, short> reverseMap = new();
foreach (var kvp in map)
reverseMap[kvp.Value] = kvp.Key;
return reverseMap;
}
}
}

View file

@ -24,6 +24,7 @@ namespace MinecraftClient.Inventory
Looting,
LuckOfTheSea,
Loyalty,
Lunge,
Lure,
Mending,
Multishot,

View file

@ -20,7 +20,7 @@ public class EnchantmentsComponent(DataTypes dataTypes, ItemPalette itemPalette,
{
var registryId = DataTypes.ReadNextVarInt(data);
var level = DataTypes.ReadNextVarInt(data);
Enchantments.Add(new Enchantment(EnchantmentMapping.GetEnchantmentByRegistryId1206(registryId), level));
Enchantments.Add(new Enchantment(EnchantmentMapping.GetEnchantmentByRegistryId1206(DataTypes.ProtocolVersion, registryId), level));
}
ShowTooltip = DataTypes.ReadNextBool(data);
@ -32,10 +32,10 @@ public class EnchantmentsComponent(DataTypes dataTypes, ItemPalette itemPalette,
data.AddRange(DataTypes.GetVarInt(Enchantments.Count));
foreach (var enchantment in Enchantments)
{
data.AddRange(DataTypes.GetVarInt(EnchantmentMapping.GetRegistryId1206ByEnchantment(enchantment.Type)));
data.AddRange(DataTypes.GetVarInt(EnchantmentMapping.GetRegistryId1206ByEnchantment(DataTypes.ProtocolVersion, enchantment.Type)));
data.AddRange(DataTypes.GetVarInt(enchantment.Level));
}
data.AddRange(DataTypes.GetBool(ShowTooltip));
return new Queue<byte>(data);
}
}
}

View file

@ -21,7 +21,7 @@ public class EnchantmentsComponent1215(DataTypes dataTypes, ItemPalette itemPale
{
var registryId = DataTypes.ReadNextVarInt(data);
var level = DataTypes.ReadNextVarInt(data);
Enchantments.Add(new Enchantment(EnchantmentMapping.GetEnchantmentByRegistryId1206(registryId), level));
Enchantments.Add(new Enchantment(EnchantmentMapping.GetEnchantmentByRegistryId1206(DataTypes.ProtocolVersion, registryId), level));
}
}
@ -31,7 +31,7 @@ public class EnchantmentsComponent1215(DataTypes dataTypes, ItemPalette itemPale
data.AddRange(DataTypes.GetVarInt(Enchantments.Count));
foreach (var enchantment in Enchantments)
{
data.AddRange(DataTypes.GetVarInt(EnchantmentMapping.GetRegistryId1206ByEnchantment(enchantment.Type)));
data.AddRange(DataTypes.GetVarInt(EnchantmentMapping.GetRegistryId1206ByEnchantment(DataTypes.ProtocolVersion, enchantment.Type)));
data.AddRange(DataTypes.GetVarInt(enchantment.Level));
}
return new Queue<byte>(data);

View file

@ -3153,8 +3153,10 @@
"enchantment.minecraft.bane_of_arthropods": "Bane of Arthropods",
"enchantment.minecraft.binding_curse": "Curse of Binding",
"enchantment.minecraft.blast_protection": "Blast Protection",
"enchantment.minecraft.breach": "Breach",
"enchantment.minecraft.channeling": "Channeling",
"enchantment.minecraft.depth_strider": "Depth Strider",
"enchantment.minecraft.density": "Density",
"enchantment.minecraft.efficiency": "Efficiency",
"enchantment.minecraft.feather_falling": "Feather Falling",
"enchantment.minecraft.fire_aspect": "Fire Aspect",
@ -3168,6 +3170,7 @@
"enchantment.minecraft.looting": "Looting",
"enchantment.minecraft.loyalty": "Loyalty",
"enchantment.minecraft.luck_of_the_sea": "Luck of the Sea",
"enchantment.minecraft.lunge": "Lunge",
"enchantment.minecraft.lure": "Lure",
"enchantment.minecraft.mending": "Mending",
"enchantment.minecraft.multishot": "Multishot",
@ -3188,6 +3191,7 @@
"enchantment.minecraft.thorns": "Thorns",
"enchantment.minecraft.unbreaking": "Unbreaking",
"enchantment.minecraft.vanishing_curse": "Curse of Vanishing",
"enchantment.minecraft.wind_burst": "Wind Burst",
"entity.minecraft.allay": "Allay",
"entity.minecraft.area_effect_cloud": "Area Effect Cloud",
"entity.minecraft.armor_stand": "Armor Stand",
@ -6539,4 +6543,4 @@
"tutorial.socialInteractions.description": "Press %s to open",
"tutorial.socialInteractions.title": "Social Interactions",
"upgrade.minecraft.netherite_upgrade": "Netherite Upgrade"
}
}