From e4c4b0e7cd691620ecd091c799044e5d474c3231 Mon Sep 17 00:00:00 2001 From: Catnies Date: Wed, 25 Jun 2025 23:19:02 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E9=BD=90=E6=B3=A8=E9=87=8A,=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=B0=E7=9A=84=E8=AF=AD=E8=A8=80key.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/translations/zh_cn.yml | 1 + .../plugin/config/StringKeyConstructor.java | 23 +++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/common-files/src/main/resources/translations/zh_cn.yml b/common-files/src/main/resources/translations/zh_cn.yml index 2edc1009c..fcfa0a54f 100644 --- a/common-files/src/main/resources/translations/zh_cn.yml +++ b/common-files/src/main/resources/translations/zh_cn.yml @@ -65,6 +65,7 @@ command.send_resource_pack.success.single: "发送资源包给 发送资源包给 个玩家" warning.config.pack.duplicated_files: "发现重复文件 请通过 config.yml 的 'resource-pack.duplicated-files-handler' 部分解决" warning.config.yaml.duplicated_key: "在文件 发现问题 - 在第行发现重复的键 '', 这可能会导致一些意料之外的问题." +warning.config.yaml.key_path_conflict: "在文件 发现问题 - 在第行发现重复且值类型不同的键 '', 这可能会导致一些意料之外的问题." warning.config.type.int: "在文件 发现问题 - 无法加载 '': 无法将 '' 转换为整数类型 (选项 '')" warning.config.type.float: "在文件 发现问题 - 无法加载 '': 无法将 '' 转换为浮点数类型 (选项 '')" warning.config.type.boolean: "在文件 发现问题 - 无法加载 '': 无法将 '' 转换为布尔类型 (选项 '')" diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/config/StringKeyConstructor.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/config/StringKeyConstructor.java index c744d82cf..a54f63239 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/config/StringKeyConstructor.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/config/StringKeyConstructor.java @@ -102,27 +102,32 @@ public class StringKeyConstructor extends SafeConstructor { // 处理深层键 private void processDeepKey(Map rootMap, String fullKey, Node valueNode, Node keyNode) { + // 分割出不同的层级 String[] keyParts = fullKey.split(DEEP_KEY_SEPARATOR); Map currentMap = rootMap; - // 创建必要的的中间层级 + // 创建必要的的中间层级(最后一个key不应遍历, 如aa::bb::cc, 只应创建aa和bb.) for (int i = 0; i < keyParts.length - 1; i++) { String keyPart = keyParts[i]; Object existingValue = currentMap.get(keyPart); + // 路径中的值 if (existingValue instanceof Map) { currentMap = (Map) existingValue; - } else { - if (existingValue != null) { - logWarning("key_path_conflict", keyPart, keyNode); - } - Map newMap = new LinkedHashMap<>(); - currentMap.put(keyPart, newMap); - currentMap = newMap; + continue; } + + // 如果路径中存在一个非map的值, 这意味着 + // 当存在了 {aa: bb}, 又想要写入 {aa::bb::c: value} 时, 会触发这个警告, 然后会覆盖之前的. + if (existingValue != null) logWarning("key_path_conflict", keyPart, keyNode); + + // 创建层级 + Map newMap = new LinkedHashMap<>(); + currentMap.put(keyPart, newMap); + currentMap = newMap; } - // 处理最终键 + // 这里再处理最后的 cc key. String finalKey = keyParts[keyParts.length - 1]; Object newValue = constructObject(valueNode); String keyPath = String.join(DEEP_KEY_SEPARATOR, keyParts); // 构建完整的键路径字符串