9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-19 15:09:15 +00:00

添加冲突键警告

This commit is contained in:
XiaoMoMi
2025-06-09 21:31:20 +08:00
parent a48164fca3
commit 6a71ef957d
6 changed files with 19 additions and 6 deletions

View File

@@ -27,7 +27,7 @@ subprojects {
expand(rootProject.properties) expand(rootProject.properties)
} }
filesMatching(arrayListOf("commands.yml", "config.yml", "*/*.yml", "ignite.mod.json")) { filesMatching(arrayListOf("commands.yml", "config.yml")) {
expand( expand(
Pair("project_version", rootProject.properties["project_version"]), Pair("project_version", rootProject.properties["project_version"]),
Pair("config_version", rootProject.properties["config_version"]), Pair("config_version", rootProject.properties["config_version"]),

View File

@@ -64,6 +64,7 @@ command.upload.on_progress: "<white>Started uploading progress. Check the consol
command.send_resource_pack.success.single: "<white>Sent resource pack to <arg:0>.</white>" command.send_resource_pack.success.single: "<white>Sent resource pack to <arg:0>.</white>"
command.send_resource_pack.success.multiple: "<white>Send resource packs to <arg:0> players.</white>" command.send_resource_pack.success.multiple: "<white>Send resource packs to <arg:0> players.</white>"
warning.config.pack.duplicated_files: "</red>Duplicated files Found. Please resolve them through config.yml 'resource-pack.duplicated-files-handler' section.</red>" warning.config.pack.duplicated_files: "</red>Duplicated files Found. Please resolve them through config.yml 'resource-pack.duplicated-files-handler' section.</red>"
warning.config.yaml.duplicated_key: "<yellow>Issue found in file <arg:0> - Found duplicated key '<arg:1>' at line <arg:2>.</yellow>"
warning.config.type.int: "<yellow>Issue found in file <arg:0> - Failed to load '<arg:1>': Cannot cast '<arg:2>' to integer type for option '<arg:3>'.</yellow>" warning.config.type.int: "<yellow>Issue found in file <arg:0> - Failed to load '<arg:1>': Cannot cast '<arg:2>' to integer type for option '<arg:3>'.</yellow>"
warning.config.type.float: "<yellow>Issue found in file <arg:0> - Failed to load '<arg:1>': Cannot cast '<arg:2>' to float type for option '<arg:3>'.</yellow>" warning.config.type.float: "<yellow>Issue found in file <arg:0> - Failed to load '<arg:1>': Cannot cast '<arg:2>' to float type for option '<arg:3>'.</yellow>"
warning.config.type.double: "<yellow>Issue found in file <arg:0> - Failed to load '<arg:1>': Cannot cast '<arg:2>' to double type for option '<arg:3>'.</yellow>" warning.config.type.double: "<yellow>Issue found in file <arg:0> - Failed to load '<arg:1>': Cannot cast '<arg:2>' to double type for option '<arg:3>'.</yellow>"

View File

@@ -64,6 +64,7 @@ command.upload.on_progress: "<white>已开始上传进程. 检查控制台以获
command.send_resource_pack.success.single: "<white>发送资源包给 <arg:0></white>" command.send_resource_pack.success.single: "<white>发送资源包给 <arg:0></white>"
command.send_resource_pack.success.multiple: "<white>发送资源包给 <arg:0> 个玩家</white>" command.send_resource_pack.success.multiple: "<white>发送资源包给 <arg:0> 个玩家</white>"
warning.config.pack.duplicated_files: "</red>发现重复文件 请通过 config.yml 的 'resource-pack.duplicated-files-handler' 部分解决</red>" warning.config.pack.duplicated_files: "</red>发现重复文件 请通过 config.yml 的 'resource-pack.duplicated-files-handler' 部分解决</red>"
warning.config.yaml.duplicated_key: "<yellow>在文件 <arg:0> 发现问题 - 在第<arg:2>行发现重复的键 '<arg:1>'.</yellow>"
warning.config.type.int: "<yellow>在文件 <arg:0> 发现问题 - 无法加载 '<arg:1>': 无法将 '<arg:2>' 转换为整数类型 (选项 '<arg:3>')</yellow>" warning.config.type.int: "<yellow>在文件 <arg:0> 发现问题 - 无法加载 '<arg:1>': 无法将 '<arg:2>' 转换为整数类型 (选项 '<arg:3>')</yellow>"
warning.config.type.float: "<yellow>在文件 <arg:0> 发现问题 - 无法加载 '<arg:1>': 无法将 '<arg:2>' 转换为浮点数类型 (选项 '<arg:3>')</yellow>" warning.config.type.float: "<yellow>在文件 <arg:0> 发现问题 - 无法加载 '<arg:1>': 无法将 '<arg:2>' 转换为浮点数类型 (选项 '<arg:3>')</yellow>"
warning.config.type.double: "<yellow>在文件 <arg:0> 发现问题 - 无法加载 '<arg:1>': 无法将 '<arg:2>' 转换为双精度类型 (选项 '<arg:3>')</yellow>" warning.config.type.double: "<yellow>在文件 <arg:0> 发现问题 - 无法加载 '<arg:1>': 无法将 '<arg:2>' 转换为双精度类型 (选项 '<arg:3>')</yellow>"

View File

@@ -439,7 +439,6 @@ public abstract class AbstractPackManager implements PackManager {
TreeMap<ConfigParser, List<CachedConfigSection>> cachedConfigs = new TreeMap<>(); TreeMap<ConfigParser, List<CachedConfigSection>> cachedConfigs = new TreeMap<>();
Map<Path, CachedConfigFile> previousFiles = this.cachedConfigFiles; Map<Path, CachedConfigFile> previousFiles = this.cachedConfigFiles;
this.cachedConfigFiles = new Object2ObjectOpenHashMap<>(32); this.cachedConfigFiles = new Object2ObjectOpenHashMap<>(32);
Yaml yaml = new Yaml(new StringKeyConstructor(new LoaderOptions()));
for (Pack pack : loadedPacks()) { for (Pack pack : loadedPacks()) {
if (!pack.enabled()) continue; if (!pack.enabled()) continue;
Path configurationFolderPath = pack.configurationFolder(); Path configurationFolderPath = pack.configurationFolder();
@@ -456,6 +455,7 @@ public abstract class AbstractPackManager implements PackManager {
AbstractPackManager.this.cachedConfigFiles.put(path, cachedFile); AbstractPackManager.this.cachedConfigFiles.put(path, cachedFile);
} else { } else {
try (InputStreamReader inputStream = new InputStreamReader(Files.newInputStream(path), StandardCharsets.UTF_8)) { try (InputStreamReader inputStream = new InputStreamReader(Files.newInputStream(path), StandardCharsets.UTF_8)) {
Yaml yaml = new Yaml(new StringKeyConstructor(path, new LoaderOptions()));
Map<String, Object> data = yaml.load(inputStream); Map<String, Object> data = yaml.load(inputStream);
if (data == null) return FileVisitResult.CONTINUE; if (data == null) return FileVisitResult.CONTINUE;
cachedFile = new CachedConfigFile(data, pack, lastModifiedTime, size); cachedFile = new CachedConfigFile(data, pack, lastModifiedTime, size);
@@ -463,6 +463,10 @@ public abstract class AbstractPackManager implements PackManager {
} catch (IOException e) { } catch (IOException e) {
AbstractPackManager.this.plugin.logger().severe("Error while reading config file: " + path, e); AbstractPackManager.this.plugin.logger().severe("Error while reading config file: " + path, e);
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} catch (LocalizedException e) {
e.setArgument(0, path.toString());
TranslationManager.instance().log(e.node(), e.arguments());
return FileVisitResult.CONTINUE;
} }
} }
for (Map.Entry<String, Object> entry : cachedFile.config().entrySet()) { for (Map.Entry<String, Object> entry : cachedFile.config().entrySet()) {
@@ -516,7 +520,7 @@ public abstract class AbstractPackManager implements PackManager {
TranslationManager.instance().log(e.node(), e.arguments()); TranslationManager.instance().log(e.node(), e.arguments());
this.plugin.debug(e::node); this.plugin.debug(e::node);
} catch (Exception e) { } 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);
} }
} }
} }

View File

@@ -1,5 +1,6 @@
package net.momirealms.craftengine.core.plugin.config; package net.momirealms.craftengine.core.plugin.config;
import net.momirealms.craftengine.core.plugin.locale.TranslationManager;
import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.constructor.SafeConstructor; import org.yaml.snakeyaml.constructor.SafeConstructor;
import org.yaml.snakeyaml.nodes.MappingNode; 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.NodeTuple;
import org.yaml.snakeyaml.nodes.ScalarNode; import org.yaml.snakeyaml.nodes.ScalarNode;
import java.nio.file.Path;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
public class StringKeyConstructor extends SafeConstructor { public class StringKeyConstructor extends SafeConstructor {
private final Path path;
public StringKeyConstructor(LoaderOptions loaderOptions) { public StringKeyConstructor(Path path, LoaderOptions loaderOptions) {
super(loaderOptions); super(loaderOptions);
this.path = path;
} }
@Override @Override
@@ -24,7 +28,10 @@ public class StringKeyConstructor extends SafeConstructor {
Node valueNode = tuple.getValueNode(); Node valueNode = tuple.getValueNode();
String key = constructScalar((ScalarNode) keyNode); String key = constructScalar((ScalarNode) keyNode);
Object value = constructObject(valueNode); 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; return map;
} }

View File

@@ -213,7 +213,7 @@ public class TranslationManagerImpl implements TranslationManager {
options.setIndent(2); options.setIndent(2);
options.setSplitLines(false); options.setSplitLines(false);
options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED); 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<String, String> newFileContents = new LinkedHashMap<>(); LinkedHashMap<String, String> newFileContents = new LinkedHashMap<>();
try (InputStream is = this.plugin.resourceStream("translations/" + translationFile.getFileName())) { try (InputStream is = this.plugin.resourceStream("translations/" + translationFile.getFileName())) {
Map<String, String> newMap = yaml.load(is); Map<String, String> newMap = yaml.load(is);