9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-31 04:46:37 +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

@@ -439,7 +439,6 @@ public abstract class AbstractPackManager implements PackManager {
TreeMap<ConfigParser, List<CachedConfigSection>> cachedConfigs = new TreeMap<>();
Map<Path, CachedConfigFile> 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<String, Object> 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<String, Object> 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);
}
}
}

View File

@@ -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;
}

View File

@@ -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<String, String> newFileContents = new LinkedHashMap<>();
try (InputStream is = this.plugin.resourceStream("translations/" + translationFile.getFileName())) {
Map<String, String> newMap = yaml.load(is);