Initial commit
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
package com.alttd.config;
|
||||
|
||||
import com.alttd.AltitudeBot;
|
||||
import com.alttd.util.Logger;
|
||||
import io.leangen.geantyref.TypeToken;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.spongepowered.configurate.ConfigurationNode;
|
||||
import org.spongepowered.configurate.ConfigurationOptions;
|
||||
import org.spongepowered.configurate.serialize.SerializationException;
|
||||
import org.spongepowered.configurate.yaml.NodeStyle;
|
||||
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
|
||||
|
||||
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.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@SuppressWarnings({"unused", "SameParameterValue"})
|
||||
abstract class AbstractConfig {
|
||||
private static final Pattern PATH_PATTERN = Pattern.compile("\\.");
|
||||
private static final String HEADER = "";
|
||||
|
||||
private YamlConfigurationLoader configLoader;
|
||||
private ConfigurationNode config;
|
||||
|
||||
AbstractConfig(String filename) {
|
||||
init(new File(AltitudeBot.getInstance().getDataFolder(), filename), filename);
|
||||
}
|
||||
|
||||
private void init(File file, String filename) {
|
||||
configLoader = YamlConfigurationLoader.builder()
|
||||
.file(file)
|
||||
.nodeStyle(NodeStyle.BLOCK)
|
||||
.build();
|
||||
if (!file.getParentFile().exists()) {
|
||||
if(!file.getParentFile().mkdirs()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
if(!file.createNewFile()) {
|
||||
return;
|
||||
}
|
||||
} catch (IOException error) {
|
||||
error.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
config = configLoader.load(ConfigurationOptions.defaults().header(HEADER).shouldCopyDefaults(false));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
void readConfig(Class<?> clazz, Object instance) {
|
||||
for (Method method : clazz.getDeclaredMethods()) {
|
||||
if (Modifier.isPrivate(method.getModifiers())) {
|
||||
if (method.getParameterTypes().length == 0 && method.getReturnType() == Void.TYPE) {
|
||||
try {
|
||||
method.setAccessible(true);
|
||||
method.invoke(instance);
|
||||
} catch (InvocationTargetException ex) {
|
||||
throw new RuntimeException(ex.getCause());
|
||||
} catch (Exception ex) {
|
||||
Logger.severe("Error invoking %.", method.toString());
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
save();
|
||||
}
|
||||
|
||||
private void save() {
|
||||
try {
|
||||
configLoader.save(config);
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static Object[] splitPath(String key) {
|
||||
return PATH_PATTERN.split(key);
|
||||
}
|
||||
|
||||
protected void set(String path, Object def) {
|
||||
if(config.node(splitPath(path)).virtual()) {
|
||||
try {
|
||||
config.node(splitPath(path)).set(def);
|
||||
} catch (SerializationException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void setString(String path, String def) {
|
||||
try {
|
||||
if(config.node(splitPath(path)).virtual())
|
||||
config.node(splitPath(path)).set(io.leangen.geantyref.TypeToken.get(String.class), def);
|
||||
} catch(SerializationException ex) {
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean getBoolean(String path, boolean def) {
|
||||
set(path, def);
|
||||
return config.node(splitPath(path)).getBoolean(def);
|
||||
}
|
||||
|
||||
protected double getDouble(String path, double def) {
|
||||
set(path, def);
|
||||
return config.node(splitPath(path)).getDouble(def);
|
||||
}
|
||||
|
||||
protected int getInt(String path, int def) {
|
||||
set(path, def);
|
||||
return config.node(splitPath(path)).getInt(def);
|
||||
}
|
||||
|
||||
protected String getString(String path, String def) {
|
||||
setString(path, def);
|
||||
return config.node(splitPath(path)).getString(def);
|
||||
}
|
||||
|
||||
protected Long getLong(String path, Long def) {
|
||||
set(path, def);
|
||||
return config.node(splitPath(path)).getLong(def);
|
||||
}
|
||||
|
||||
protected <T> List<String> getList(String path, T def) {
|
||||
try {
|
||||
set(path, def);
|
||||
return config.node(splitPath(path)).getList(TypeToken.get(String.class));
|
||||
} catch(SerializationException ex) {
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
protected ConfigurationNode getNode(String path) {
|
||||
if(config.node(splitPath(path)).virtual()) {
|
||||
//new RegexConfig("Dummy");
|
||||
}
|
||||
config.childrenMap();
|
||||
return config.node(splitPath(path));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.alttd.config;
|
||||
|
||||
public class MessagesConfig extends AbstractConfig {
|
||||
|
||||
static MessagesConfig messagesConfig;
|
||||
|
||||
public MessagesConfig() {
|
||||
super("messages.yml");
|
||||
}
|
||||
|
||||
public static void reload() {
|
||||
messagesConfig = new MessagesConfig();
|
||||
|
||||
messagesConfig.readConfig(MessagesConfig.class, null);
|
||||
}
|
||||
|
||||
public static String HELP_HELP = "`<prefix>help`: Shows help menu";
|
||||
public static String HELP_MESSAGE_TEMPLATE = "<commands>";
|
||||
private static void loadHelp() {
|
||||
HELP_HELP = messagesConfig.getString("help.help", HELP_HELP);
|
||||
HELP_MESSAGE_TEMPLATE = messagesConfig.getString("help.message-template", HELP_MESSAGE_TEMPLATE);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.alttd.config;
|
||||
|
||||
public class SettingsConfig extends AbstractConfig {
|
||||
|
||||
static SettingsConfig settingsConfig;
|
||||
|
||||
public SettingsConfig() {
|
||||
super("settings.yml");
|
||||
}
|
||||
|
||||
public static void reload() {
|
||||
settingsConfig = new SettingsConfig();
|
||||
|
||||
settingsConfig.readConfig(SettingsConfig.class, null);
|
||||
}
|
||||
|
||||
public static String TOKEN = "token";
|
||||
private void loadSettings() {
|
||||
TOKEN = settingsConfig.getString("settings.token", TOKEN);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user