use of com.electronwill.nightconfig.core.CommentedConfig in project Mohist by MohistMC.
the class ForgeConfigSpec method correct.
private int correct(UnmodifiableConfig spec, CommentedConfig config, LinkedList<String> parentPath, List<String> parentPathUnmodifiable, CorrectionListener listener, CorrectionListener commentListener, boolean dryRun) {
int count = 0;
Map<String, Object> specMap = spec.valueMap();
Map<String, Object> configMap = config.valueMap();
for (Map.Entry<String, Object> specEntry : specMap.entrySet()) {
final String key = specEntry.getKey();
final Object specValue = specEntry.getValue();
final Object configValue = configMap.get(key);
final CorrectionAction action = configValue == null ? ADD : REPLACE;
parentPath.addLast(key);
if (specValue instanceof Config) {
if (configValue instanceof CommentedConfig) {
count += correct((Config) specValue, (CommentedConfig) configValue, parentPath, parentPathUnmodifiable, listener, commentListener, dryRun);
if (count > 0 && dryRun)
return count;
} else if (dryRun) {
return 1;
} else {
CommentedConfig newValue = config.createSubConfig();
configMap.put(key, newValue);
listener.onCorrect(action, parentPathUnmodifiable, configValue, newValue);
count++;
count += correct((Config) specValue, newValue, parentPath, parentPathUnmodifiable, listener, commentListener, dryRun);
}
String newComment = levelComments.get(parentPath);
String oldComment = config.getComment(key);
if (!stringsMatchIgnoringNewlines(oldComment, newComment)) {
if (commentListener != null)
commentListener.onCorrect(action, parentPathUnmodifiable, oldComment, newComment);
if (dryRun)
return 1;
config.setComment(key, newComment);
}
} else {
ValueSpec valueSpec = (ValueSpec) specValue;
if (!valueSpec.test(configValue)) {
if (dryRun)
return 1;
Object newValue = valueSpec.correct(configValue);
configMap.put(key, newValue);
listener.onCorrect(action, parentPathUnmodifiable, configValue, newValue);
count++;
}
String oldComment = config.getComment(key);
if (!stringsMatchIgnoringNewlines(oldComment, valueSpec.getComment())) {
if (commentListener != null)
commentListener.onCorrect(action, parentPathUnmodifiable, oldComment, valueSpec.getComment());
if (dryRun)
return 1;
config.setComment(key, valueSpec.getComment());
}
}
parentPath.removeLast();
}
// Second step: removes the unspecified values
for (Iterator<Map.Entry<String, Object>> ittr = configMap.entrySet().iterator(); ittr.hasNext(); ) {
Map.Entry<String, Object> entry = ittr.next();
if (!specMap.containsKey(entry.getKey())) {
if (dryRun)
return 1;
ittr.remove();
parentPath.addLast(entry.getKey());
listener.onCorrect(REMOVE, parentPathUnmodifiable, entry.getValue(), null);
parentPath.removeLast();
count++;
}
}
return count;
}
use of com.electronwill.nightconfig.core.CommentedConfig in project LoliServer by Loli-Server.
the class ConfigTracker method loadDefaultServerConfigs.
public void loadDefaultServerConfigs() {
configSets.get(ModConfig.Type.SERVER).forEach(modConfig -> {
final CommentedConfig commentedConfig = CommentedConfig.inMemory();
modConfig.getSpec().correct(commentedConfig);
modConfig.setConfigData(commentedConfig);
modConfig.fireEvent(new ModConfig.Loading(modConfig));
});
}
use of com.electronwill.nightconfig.core.CommentedConfig in project Magma-1.16.x by magmafoundation.
the class ForgeConfigSpec method correct.
private int correct(UnmodifiableConfig spec, CommentedConfig config, LinkedList<String> parentPath, List<String> parentPathUnmodifiable, CorrectionListener listener, CorrectionListener commentListener, boolean dryRun) {
int count = 0;
Map<String, Object> specMap = spec.valueMap();
Map<String, Object> configMap = config.valueMap();
for (Map.Entry<String, Object> specEntry : specMap.entrySet()) {
final String key = specEntry.getKey();
final Object specValue = specEntry.getValue();
final Object configValue = configMap.get(key);
final CorrectionAction action = configValue == null ? ADD : REPLACE;
parentPath.addLast(key);
if (specValue instanceof Config) {
if (configValue instanceof CommentedConfig) {
count += correct((Config) specValue, (CommentedConfig) configValue, parentPath, parentPathUnmodifiable, listener, commentListener, dryRun);
if (count > 0 && dryRun)
return count;
} else if (dryRun) {
return 1;
} else {
CommentedConfig newValue = config.createSubConfig();
configMap.put(key, newValue);
listener.onCorrect(action, parentPathUnmodifiable, configValue, newValue);
count++;
count += correct((Config) specValue, newValue, parentPath, parentPathUnmodifiable, listener, commentListener, dryRun);
}
String newComment = levelComments.get(parentPath);
String oldComment = config.getComment(key);
if (!stringsMatchIgnoringNewlines(oldComment, newComment)) {
if (commentListener != null)
commentListener.onCorrect(action, parentPathUnmodifiable, oldComment, newComment);
if (dryRun)
return 1;
config.setComment(key, newComment);
}
} else {
ValueSpec valueSpec = (ValueSpec) specValue;
if (!valueSpec.test(configValue)) {
if (dryRun)
return 1;
Object newValue = valueSpec.correct(configValue);
configMap.put(key, newValue);
listener.onCorrect(action, parentPathUnmodifiable, configValue, newValue);
count++;
}
String oldComment = config.getComment(key);
if (!stringsMatchIgnoringNewlines(oldComment, valueSpec.getComment())) {
if (commentListener != null)
commentListener.onCorrect(action, parentPathUnmodifiable, oldComment, valueSpec.getComment());
if (dryRun)
return 1;
config.setComment(key, valueSpec.getComment());
}
}
parentPath.removeLast();
}
// Second step: removes the unspecified values
for (Iterator<Map.Entry<String, Object>> ittr = configMap.entrySet().iterator(); ittr.hasNext(); ) {
Map.Entry<String, Object> entry = ittr.next();
if (!specMap.containsKey(entry.getKey())) {
if (dryRun)
return 1;
ittr.remove();
parentPath.addLast(entry.getKey());
listener.onCorrect(REMOVE, parentPathUnmodifiable, entry.getValue(), null);
parentPath.removeLast();
count++;
}
}
return count;
}
use of com.electronwill.nightconfig.core.CommentedConfig in project Magma-1.16.x by magmafoundation.
the class ConfigTracker method loadDefaultServerConfigs.
public void loadDefaultServerConfigs() {
configSets.get(ModConfig.Type.SERVER).forEach(modConfig -> {
final CommentedConfig commentedConfig = CommentedConfig.inMemory();
modConfig.getSpec().correct(commentedConfig);
modConfig.setConfigData(commentedConfig);
modConfig.fireEvent(new ModConfig.Loading(modConfig));
});
}
use of com.electronwill.nightconfig.core.CommentedConfig in project MinecraftForge by MinecraftForge.
the class ForgeConfigSpec method correct.
private int correct(UnmodifiableConfig spec, CommentedConfig config, LinkedList<String> parentPath, List<String> parentPathUnmodifiable, CorrectionListener listener, CorrectionListener commentListener, boolean dryRun) {
int count = 0;
Map<String, Object> specMap = spec.valueMap();
Map<String, Object> configMap = config.valueMap();
for (Map.Entry<String, Object> specEntry : specMap.entrySet()) {
final String key = specEntry.getKey();
final Object specValue = specEntry.getValue();
final Object configValue = configMap.get(key);
final CorrectionAction action = configValue == null ? ADD : REPLACE;
parentPath.addLast(key);
if (specValue instanceof Config) {
if (configValue instanceof CommentedConfig) {
count += correct((Config) specValue, (CommentedConfig) configValue, parentPath, parentPathUnmodifiable, listener, commentListener, dryRun);
if (count > 0 && dryRun)
return count;
} else if (dryRun) {
return 1;
} else {
CommentedConfig newValue = config.createSubConfig();
configMap.put(key, newValue);
listener.onCorrect(action, parentPathUnmodifiable, configValue, newValue);
count++;
count += correct((Config) specValue, newValue, parentPath, parentPathUnmodifiable, listener, commentListener, dryRun);
}
String newComment = levelComments.get(parentPath);
String oldComment = config.getComment(key);
if (!stringsMatchIgnoringNewlines(oldComment, newComment)) {
if (commentListener != null)
commentListener.onCorrect(action, parentPathUnmodifiable, oldComment, newComment);
if (dryRun)
return 1;
config.setComment(key, newComment);
}
} else {
ValueSpec valueSpec = (ValueSpec) specValue;
if (!valueSpec.test(configValue)) {
if (dryRun)
return 1;
Object newValue = valueSpec.correct(configValue);
configMap.put(key, newValue);
listener.onCorrect(action, parentPathUnmodifiable, configValue, newValue);
count++;
}
String oldComment = config.getComment(key);
if (!stringsMatchIgnoringNewlines(oldComment, valueSpec.getComment())) {
if (commentListener != null)
commentListener.onCorrect(action, parentPathUnmodifiable, oldComment, valueSpec.getComment());
if (dryRun)
return 1;
config.setComment(key, valueSpec.getComment());
}
}
parentPath.removeLast();
}
// Second step: removes the unspecified values
for (Iterator<Map.Entry<String, Object>> ittr = configMap.entrySet().iterator(); ittr.hasNext(); ) {
Map.Entry<String, Object> entry = ittr.next();
if (!specMap.containsKey(entry.getKey())) {
if (dryRun)
return 1;
ittr.remove();
parentPath.addLast(entry.getKey());
listener.onCorrect(REMOVE, parentPathUnmodifiable, entry.getValue(), null);
parentPath.removeLast();
count++;
}
}
return count;
}
Aggregations