From 71c01c739a7841145d9ecb1f064f2962e10a8480 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 25 Mar 2026 18:02:12 +0000 Subject: [PATCH] refactor: simplify fallback enchantment reverse maps Co-authored-by: milutinke <441903+milutinke@users.noreply.github.com> Agent-Logs-Url: https://github.com/MCCTeam/Minecraft-Console-Client/sessions/16e21ef0-6f09-4280-b539-8d704ddc3b67 --- .../Inventory/EnchantmentMapping.cs | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/MinecraftClient/Inventory/EnchantmentMapping.cs b/MinecraftClient/Inventory/EnchantmentMapping.cs index 74855fef..e8f94128 100644 --- a/MinecraftClient/Inventory/EnchantmentMapping.cs +++ b/MinecraftClient/Inventory/EnchantmentMapping.cs @@ -250,7 +250,11 @@ namespace MinecraftClient.Inventory } private static Dictionary? reverseDynamicEnchantmentMappings; - private static readonly Dictionary> reverseFallbackEnchantmentMappings = new(); + private static readonly Dictionary reverseEnchantmentMappings114 = CreateReverseMap(enchantmentMappings114); + private static readonly Dictionary reverseEnchantmentMappings116 = CreateReverseMap(enchantmentMappings116); + private static readonly Dictionary reverseEnchantmentMappings119 = CreateReverseMap(enchantmentMappings119); + private static readonly Dictionary reverseEnchantmentMappings1206 = CreateReverseMap(enchantmentMappings1206); + private static readonly Dictionary reverseEnchantmentMappings12111 = CreateReverseMap(enchantmentMappings12111); private static Dictionary? dynamicEnchantmentIdMap; private static readonly Dictionary nameToEnchantment = new() @@ -339,7 +343,7 @@ namespace MinecraftClient.Inventory return reverseDynamicEnchantmentMappings.TryGetValue(enchantment, out var dynamicId) ? dynamicId : -1; } - var reverseMap = GetReverseFallbackMapForProtocolVersion(protocolVersion); + var reverseMap = GetReverseMapForProtocolVersion(protocolVersion); return reverseMap.TryGetValue(enchantment, out var id) ? id : -1; } @@ -390,16 +394,24 @@ namespace MinecraftClient.Inventory }; } - private static Dictionary GetReverseFallbackMapForProtocolVersion(int protocolVersion) + private static Dictionary GetReverseMapForProtocolVersion(int protocolVersion) { - if (reverseFallbackEnchantmentMappings.TryGetValue(protocolVersion, out var reverseMap)) - return reverseMap; + 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_21_11_Version => reverseEnchantmentMappings12111, + _ => reverseEnchantmentMappings1206 + }; + } - reverseMap = new(); - foreach (var kvp in GetMapForProtocolVersion(protocolVersion)) + private static Dictionary CreateReverseMap(Dictionary map) + { + Dictionary reverseMap = new(); + foreach (var kvp in map) reverseMap[kvp.Value] = kvp.Key; - reverseFallbackEnchantmentMappings[protocolVersion] = reverseMap; return reverseMap; } }