diff --git a/build.gradle.kts b/build.gradle.kts index 388c37cc5..25838c89e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -27,7 +27,7 @@ subprojects { expand(rootProject.properties) } - filesMatching(arrayListOf("commands.yml", "config.yml", "*/*.yml", "ignite.mod.json")) { + filesMatching(arrayListOf("commands.yml", "config.yml")) { expand( Pair("project_version", rootProject.properties["project_version"]), Pair("config_version", rootProject.properties["config_version"]), diff --git a/common-files/src/main/resources/translations/en.yml b/common-files/src/main/resources/translations/en.yml index 826d2c3a5..31f407aea 100644 --- a/common-files/src/main/resources/translations/en.yml +++ b/common-files/src/main/resources/translations/en.yml @@ -64,6 +64,7 @@ command.upload.on_progress: "Started uploading progress. Check the consol command.send_resource_pack.success.single: "Sent resource pack to ." command.send_resource_pack.success.multiple: "Send resource packs to players." warning.config.pack.duplicated_files: "Duplicated files Found. Please resolve them through config.yml 'resource-pack.duplicated-files-handler' section." +warning.config.yaml.duplicated_key: "Issue found in file - Found duplicated key '' at line ." warning.config.type.int: "Issue found in file - Failed to load '': Cannot cast '' to integer type for option ''." warning.config.type.float: "Issue found in file - Failed to load '': Cannot cast '' to float type for option ''." warning.config.type.double: "Issue found in file - Failed to load '': Cannot cast '' to double type for option ''." diff --git a/common-files/src/main/resources/translations/zh_cn.yml b/common-files/src/main/resources/translations/zh_cn.yml index db490963c..ec97100fd 100644 --- a/common-files/src/main/resources/translations/zh_cn.yml +++ b/common-files/src/main/resources/translations/zh_cn.yml @@ -64,6 +64,7 @@ command.upload.on_progress: "已开始上传进程. 检查控制台以获 command.send_resource_pack.success.single: "发送资源包给 " command.send_resource_pack.success.multiple: "发送资源包给 个玩家" warning.config.pack.duplicated_files: "发现重复文件 请通过 config.yml 的 'resource-pack.duplicated-files-handler' 部分解决" +warning.config.yaml.duplicated_key: "在文件 发现问题 - 在第行发现重复的键 ''." warning.config.type.int: "在文件 发现问题 - 无法加载 '': 无法将 '' 转换为整数类型 (选项 '')" warning.config.type.float: "在文件 发现问题 - 无法加载 '': 无法将 '' 转换为浮点数类型 (选项 '')" warning.config.type.double: "在文件 发现问题 - 无法加载 '': 无法将 '' 转换为双精度类型 (选项 '')" diff --git a/core/src/main/java/net/momirealms/craftengine/core/pack/AbstractPackManager.java b/core/src/main/java/net/momirealms/craftengine/core/pack/AbstractPackManager.java index 819a3cbf7..ff08121cd 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/pack/AbstractPackManager.java +++ b/core/src/main/java/net/momirealms/craftengine/core/pack/AbstractPackManager.java @@ -439,7 +439,6 @@ public abstract class AbstractPackManager implements PackManager { TreeMap> cachedConfigs = new TreeMap<>(); Map previousFiles = this.cachedConfigFiles; this.cachedConfigFiles = new Object2ObjectOpenHashMap<>(32); - Yaml yaml = new Yaml(new StringKeyConstructor(new LoaderOptions())); for (Pack pack : loadedPacks()) { if (!pack.enabled()) continue; Path configurationFolderPath = pack.configurationFolder(); @@ -456,6 +455,7 @@ public abstract class AbstractPackManager implements PackManager { AbstractPackManager.this.cachedConfigFiles.put(path, cachedFile); } else { try (InputStreamReader inputStream = new InputStreamReader(Files.newInputStream(path), StandardCharsets.UTF_8)) { + Yaml yaml = new Yaml(new StringKeyConstructor(path, new LoaderOptions())); Map data = yaml.load(inputStream); if (data == null) return FileVisitResult.CONTINUE; cachedFile = new CachedConfigFile(data, pack, lastModifiedTime, size); @@ -463,6 +463,10 @@ public abstract class AbstractPackManager implements PackManager { } catch (IOException e) { AbstractPackManager.this.plugin.logger().severe("Error while reading config file: " + path, e); return FileVisitResult.CONTINUE; + } catch (LocalizedException e) { + e.setArgument(0, path.toString()); + TranslationManager.instance().log(e.node(), e.arguments()); + return FileVisitResult.CONTINUE; } } for (Map.Entry entry : cachedFile.config().entrySet()) { @@ -516,7 +520,7 @@ public abstract class AbstractPackManager implements PackManager { TranslationManager.instance().log(e.node(), e.arguments()); this.plugin.debug(e::node); } catch (Exception e) { - this.plugin.logger().warn("Unexpected error loading file " + cached.filePath() + " - '" + parser.sectionId()[0] + "." + key + "'. Please find the cause according to the stacktrace or seek developer help.", e); + this.plugin.logger().warn("Unexpected error loading file " + cached.filePath() + " - '" + parser.sectionId()[0] + "." + key + "'. Please find the cause according to the stacktrace or seek developer help. Additional info: " + GsonHelper.get().toJson(configEntry.getValue()), e); } } } 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 8db8ae617..6adf87772 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 @@ -1,5 +1,6 @@ package net.momirealms.craftengine.core.plugin.config; +import net.momirealms.craftengine.core.plugin.locale.TranslationManager; import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.constructor.SafeConstructor; import org.yaml.snakeyaml.nodes.MappingNode; @@ -7,13 +8,16 @@ import org.yaml.snakeyaml.nodes.Node; import org.yaml.snakeyaml.nodes.NodeTuple; import org.yaml.snakeyaml.nodes.ScalarNode; +import java.nio.file.Path; import java.util.LinkedHashMap; import java.util.Map; public class StringKeyConstructor extends SafeConstructor { + private final Path path; - public StringKeyConstructor(LoaderOptions loaderOptions) { + public StringKeyConstructor(Path path, LoaderOptions loaderOptions) { super(loaderOptions); + this.path = path; } @Override @@ -24,7 +28,10 @@ public class StringKeyConstructor extends SafeConstructor { Node valueNode = tuple.getValueNode(); String key = constructScalar((ScalarNode) keyNode); Object value = constructObject(valueNode); - map.put(key, value); + Object previous = map.put(key, value); + if (previous != null) { + TranslationManager.instance().log("warning.config.yaml.duplicated_key", this.path.toString(), key, String.valueOf(node.getStartMark().getLine() + 1)); + } } return map; } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/locale/TranslationManagerImpl.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/locale/TranslationManagerImpl.java index ce70f2b48..6a494b950 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/locale/TranslationManagerImpl.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/locale/TranslationManagerImpl.java @@ -213,7 +213,7 @@ public class TranslationManagerImpl implements TranslationManager { options.setIndent(2); options.setSplitLines(false); options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED); - Yaml yaml = new Yaml(new StringKeyConstructor(new LoaderOptions()), new Representer(options), options); + Yaml yaml = new Yaml(new StringKeyConstructor(translationFile, new LoaderOptions()), new Representer(options), options); LinkedHashMap newFileContents = new LinkedHashMap<>(); try (InputStream is = this.plugin.resourceStream("translations/" + translationFile.getFileName())) { Map newMap = yaml.load(is);