Fix default values for different implementations

This commit is contained in:
Blue (Lukas Rieger) 2019-11-23 21:24:17 +01:00
parent 5858142eb1
commit 27d7f37e38

View File

@ -37,6 +37,7 @@
import de.bluecolored.bluemap.core.BlueMap;
import de.bluecolored.bluemap.core.resourcepack.ResourcePack;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
import ninja.leaping.configurate.loader.ConfigurationLoader;
@ -55,18 +56,35 @@ public class ConfigurationFile {
private File configFile;
private Configuration config;
private ConfigurationFile(File configFile) throws IOException {
private ConfigurationFile(File configFile, File defaultValuesFile) throws IOException {
this.configFile = configFile;
ConfigurationLoader<CommentedConfigurationNode> configLoader = HoconConfigurationLoader.builder()
.setFile(configFile)
.build();
CommentedConfigurationNode rootNode = configLoader.load();
// fill defaults
ConfigurationLoader<CommentedConfigurationNode> defaultConfigLoader = HoconConfigurationLoader.builder()
.setFile(defaultValuesFile)
.build();
CommentedConfigurationNode defaultRootNode = defaultConfigLoader.load();
fillDefaults(rootNode, defaultRootNode);
this.config = new Configuration(rootNode);
}
private void fillDefaults(ConfigurationNode rootNode, ConfigurationNode defaultRootNode) {
setDefault(rootNode.getNode("metrics"), defaultRootNode.getNode("metrics").getValue());
setDefault(rootNode.getNode("data"), defaultRootNode.getNode("data").getValue());
setDefault(rootNode.getNode("renderThreadCount"), defaultRootNode.getNode("renderThreadCount").getValue());
rootNode.getNode("web").mergeValuesFrom(defaultRootNode.getNode("web"));
}
private void setDefault(ConfigurationNode node, Object value) {
if (node.isVirtual()) node.setValue(value);
}
public File getFile() {
return configFile;
}
@ -80,20 +98,22 @@ public static ConfigurationFile loadOrCreate(File configFile) throws IOException
}
public static ConfigurationFile loadOrCreate(File configFile, URL defaultConfig) throws IOException {
File tempDefaultFile = File.createTempFile("bluemap-defaults", ".conf");
FileUtils.copyURLToFile(defaultConfig, tempDefaultFile, 10000, 10000);
if (!configFile.exists()) {
configFile.getParentFile().mkdirs();
FileUtils.copyURLToFile(defaultConfig, configFile, 10000, 10000);
//replace placeholder
String content = new String(Files.readAllBytes(configFile.toPath()), StandardCharsets.UTF_8);
String content = new String(Files.readAllBytes(tempDefaultFile.toPath()), StandardCharsets.UTF_8);
for (Placeholder placeholder : CONFIG_PLACEHOLDERS) {
content = placeholder.apply(content);
}
Files.write(configFile.toPath(), content.getBytes(StandardCharsets.UTF_8));
}
return new ConfigurationFile(configFile);
return new ConfigurationFile(configFile, tempDefaultFile);
}
public static void registerPlaceholder(Placeholder placeholder) {