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.BlueMap;
import de.bluecolored.bluemap.core.resourcepack.ResourcePack; import de.bluecolored.bluemap.core.resourcepack.ResourcePack;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.commented.CommentedConfigurationNode; import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.hocon.HoconConfigurationLoader; import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
import ninja.leaping.configurate.loader.ConfigurationLoader; import ninja.leaping.configurate.loader.ConfigurationLoader;
@ -55,18 +56,35 @@ public class ConfigurationFile {
private File configFile; private File configFile;
private Configuration config; private Configuration config;
private ConfigurationFile(File configFile) throws IOException { private ConfigurationFile(File configFile, File defaultValuesFile) throws IOException {
this.configFile = configFile; this.configFile = configFile;
ConfigurationLoader<CommentedConfigurationNode> configLoader = HoconConfigurationLoader.builder() ConfigurationLoader<CommentedConfigurationNode> configLoader = HoconConfigurationLoader.builder()
.setFile(configFile) .setFile(configFile)
.build(); .build();
CommentedConfigurationNode rootNode = configLoader.load(); 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); 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() { public File getFile() {
return configFile; return configFile;
} }
@ -80,20 +98,22 @@ public static ConfigurationFile loadOrCreate(File configFile) throws IOException
} }
public static ConfigurationFile loadOrCreate(File configFile, URL defaultConfig) 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()) { if (!configFile.exists()) {
configFile.getParentFile().mkdirs(); configFile.getParentFile().mkdirs();
FileUtils.copyURLToFile(defaultConfig, configFile, 10000, 10000);
//replace placeholder //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) { for (Placeholder placeholder : CONFIG_PLACEHOLDERS) {
content = placeholder.apply(content); content = placeholder.apply(content);
} }
Files.write(configFile.toPath(), content.getBytes(StandardCharsets.UTF_8)); Files.write(configFile.toPath(), content.getBytes(StandardCharsets.UTF_8));
} }
return new ConfigurationFile(configFile); return new ConfigurationFile(configFile, tempDefaultFile);
} }
public static void registerPlaceholder(Placeholder placeholder) { public static void registerPlaceholder(Placeholder placeholder) {