fix config headers

This commit is contained in:
len 2021-05-25 14:49:15 +02:00
parent 595bff765a
commit 2adf114b65
2 changed files with 138 additions and 41 deletions

View File

@ -4,6 +4,7 @@ import com.google.common.base.Throwables;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.reflect.TypeToken; import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.ConfigurationNode; import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.ConfigurationOptions;
import ninja.leaping.configurate.objectmapping.ObjectMappingException; import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.yaml.YAMLConfigurationLoader; import ninja.leaping.configurate.yaml.YAMLConfigurationLoader;
import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.DumperOptions;
@ -52,14 +53,11 @@ public final class Config {
} }
try { try {
config = configLoader.load(); config = configLoader.load(ConfigurationOptions.defaults().setHeader(HEADER));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
configLoader.getDefaultOptions().setHeader(HEADER);
configLoader.getDefaultOptions().withShouldCopyDefaults(true);
verbose = getBoolean("verbose", true); verbose = getBoolean("verbose", true);
version = getInt("config-version", 1); version = getInt("config-version", 1);
@ -152,8 +150,9 @@ public final class Config {
private static ConfigurationNode getNode(String path) { private static ConfigurationNode getNode(String path) {
if(config.getNode(splitPath(path)).isVirtual()) { if(config.getNode(splitPath(path)).isVirtual()) {
new RegexConfig("Dummy"); //new RegexConfig("Dummy");
} }
config.getChildrenMap();
return config.getNode(splitPath(path)); return config.getNode(splitPath(path));
} }

View File

@ -1,73 +1,171 @@
package com.alttd.chat.config; package com.alttd.chat.config;
import com.google.common.base.Throwables;
import com.google.common.reflect.TypeToken; import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.ConfigurationOptions;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
import ninja.leaping.configurate.objectmapping.ObjectMappingException; import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.yaml.YAMLConfigurationLoader;
import org.yaml.snakeyaml.DumperOptions;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public final class RegexConfig { public final class RegexConfig {
private static final Pattern PATH_PATTERN = Pattern.compile("\\."); private static final Pattern PATH_PATTERN = Pattern.compile("\\.");
private static final String HEADER =
"This file is used to store all of the chatfilters used in the Altitude Chat plugin.\n"
+ "Nodes must be build in this format.\n"
+ "TODO update this format\n";
private final String regexName; private static File CONFIG_FILE;
private final String configPath; public static ConfigurationNode config;
// TODO move this into regex.yml? public static YAMLConfigurationLoader configLoader;
public RegexConfig(String regexName) {
this.regexName = regexName; static int version;
this.configPath = "regex-settings." + this.regexName + "."; static boolean verbose;
init();
public static void init(File path) {
CONFIG_FILE = new File(path, "filters.yml");;
configLoader = YAMLConfigurationLoader.builder()
.setFile(CONFIG_FILE)
.setFlowStyle(DumperOptions.FlowStyle.BLOCK)
.build();
if (!CONFIG_FILE.getParentFile().exists()) {
if(!CONFIG_FILE.getParentFile().mkdirs()) {
return;
}
}
if (!CONFIG_FILE.exists()) {
try {
if(!CONFIG_FILE.createNewFile()) {
return;
}
} catch (IOException error) {
error.printStackTrace();
}
}
try {
config = configLoader.load(ConfigurationOptions.defaults().setHeader(HEADER));
} catch (IOException e) {
e.printStackTrace();
}
verbose = getBoolean("verbose", true);
version = getInt("config-version", 1);
readConfig(Config.class, null);
try {
configLoader.save(config);
} catch (IOException e) {
e.printStackTrace();
}
} }
public void init() { public static void readConfig(Class<?> clazz, Object instance) {
Config.readConfig(RegexConfig.class, this); for (Method method : clazz.getDeclaredMethods()) {
Config.saveConfig(); if (Modifier.isPrivate(method.getModifiers())) {
if (method.getParameterTypes().length == 0 && method.getReturnType() == Void.TYPE) {
try {
method.setAccessible(true);
method.invoke(instance);
} catch (InvocationTargetException | IllegalAccessException ex) {
throw Throwables.propagate(ex.getCause());
}
}
}
}
try {
configLoader.save(config);
} catch (IOException ex) {
throw Throwables.propagate(ex.getCause());
}
} }
public static Object[] splitPath(String key) { private static Object[] splitPath(String key) {
return PATH_PATTERN.split(key); return PATH_PATTERN.split(key);
} }
private static void set(String path, Object def) { private static void set(String path, Object def) {
if(Config.config.getNode(splitPath(path)).isVirtual()) { if(config.getNode(splitPath(path)).isVirtual())
Config.config.getNode(splitPath(path)).setValue(def); config.getNode(splitPath(path)).setValue(def);
}
} }
private static void setString(String path, String def) { private static void setString(String path, String def) {
try { try {
if(Config.config.getNode(splitPath(path)).isVirtual()) if(config.getNode(splitPath(path)).isVirtual())
Config.config.getNode(splitPath(path)).setValue(TypeToken.of(String.class), def); config.getNode(splitPath(path)).setValue(TypeToken.of(String.class), def);
} catch(ObjectMappingException ex) { } catch(ObjectMappingException ex) {
} }
} }
private boolean getBoolean(String path, boolean def) { private static boolean getBoolean(String path, boolean def) {
set(configPath + path, def); set(path, def);
return Config.config.getNode(splitPath(configPath+path)).getBoolean(def); return config.getNode(splitPath(path)).getBoolean(def);
} }
private double getDouble(String path, double def) { private static double getDouble(String path, double def) {
set(configPath +path, def); set(path, def);
return Config.config.getNode(splitPath(configPath+path)).getDouble(def); return config.getNode(splitPath(path)).getDouble(def);
} }
private int getInt(String path, int def) { private static int getInt(String path, int def) {
set(configPath +path, def); set(path, def);
return Config.config.getNode(splitPath(configPath+path)).getInt(def); return config.getNode(splitPath(path)).getInt(def);
} }
private String getString(String path, String def) { private static String getString(String path, String def) {
set(configPath +path, def); setString(path, def);
return Config.config.getNode(splitPath(configPath+path)).getString(def); return config.getNode(splitPath(path)).getString(def);
} }
/** DO NOT EDIT ANYTHING ABOVE **/ private static Long getLong(String path, Long def) {
set(path, def);
return config.getNode(splitPath(path)).getLong(def);
}
public String REGEX = "REGEX"; private static <T> List<String> getList(String path, T def) {
public String TYPE = "TYPE"; try {
public String REPLACEMENT = "REPLACEMENT"; set(path, def);
private void ServerSettings() { return config.getNode(splitPath(path)).getList(TypeToken.of(String.class));
REGEX = getString("regex", REGEX); } catch(ObjectMappingException ex) {
TYPE = getString("type", TYPE); }
REPLACEMENT = getString("replacement", REPLACEMENT); return new ArrayList<>();
}
// public String REGEX = "REGEX";
// public String TYPE = "TYPE";
// public String REPLACEMENT = "REPLACEMENT";
// private void ServerSettings() {
// REGEX = getString("regex", REGEX);
// TYPE = getString("type", TYPE);
// REPLACEMENT = getString("replacement", REPLACEMENT);
// }
private void loadChatFilters() {
Map<String, Object> chProps = new HashMap<>();
config.getChildrenMap().forEach((key, value) -> {
if (value.hasMapChildren()) {
for (Map.Entry<Object, ? extends ConfigurationNode> vl : value.getChildrenMap().entrySet()) {
chProps.put( vl.getKey().toString(), vl.getValue().getValue());
}
} else {
chProps.put(key.toString(), value.getValue());
}
});
//UCChannel ch = new UCChannel(chProps);
//addChannel(ch);
} }
} }