From 2f798a9e1c00ffc783e82c0268624d38ef67f364 Mon Sep 17 00:00:00 2001 From: len <40720638+destro174@users.noreply.github.com> Date: Mon, 10 May 2021 10:45:43 +0200 Subject: [PATCH] move Config to API --- api/pom.xml | 5 + .../java/com/alttd/chat/config/Config.java | 187 ++++++++++++++++++ velocity/pom.xml | 7 +- 3 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 api/src/main/java/com/alttd/chat/config/Config.java diff --git a/api/pom.xml b/api/pom.xml index e10dc6e..deef1af 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -47,6 +47,11 @@ 5.3 provided + + org.spongepowered + configurate-yaml + 3.7.1 + \ No newline at end of file diff --git a/api/src/main/java/com/alttd/chat/config/Config.java b/api/src/main/java/com/alttd/chat/config/Config.java new file mode 100644 index 0000000..7e23121 --- /dev/null +++ b/api/src/main/java/com/alttd/chat/config/Config.java @@ -0,0 +1,187 @@ +package com.alttd.chat.config; + +import com.google.common.base.Throwables; +import com.google.common.collect.Lists; +import com.google.common.reflect.TypeToken; +import ninja.leaping.configurate.ConfigurationNode; +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.List; +import java.util.regex.Pattern; + +public final class Config { + private static final Pattern PATH_PATTERN = Pattern.compile("\\."); + private static final String HEADER = ""; + + private static File CONFIG_FILE; + public static ConfigurationNode config; + public static YAMLConfigurationLoader configLoader; + + static int version; + static boolean verbose; + + public static void init(File path) { + CONFIG_FILE = new File(path, "config.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(); + } catch (IOException e) { + e.printStackTrace(); + } + + configLoader.getDefaultOptions().setHeader(HEADER); + configLoader.getDefaultOptions().withShouldCopyDefaults(true); + + verbose = getBoolean("verbose", true); + version = getInt("config-version", 1); + + readConfig(Config.class, null); + try { + configLoader.save(config); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static 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 | IllegalAccessException ex) { + throw Throwables.propagate(ex.getCause()); + } + } + } + } + try { + configLoader.save(config); + } catch (IOException ex) { + throw Throwables.propagate(ex.getCause()); + } + } + + public static void saveConfig() { + try { + configLoader.save(config); + } catch (IOException ex) { + throw Throwables.propagate(ex.getCause()); + } + } + + private static Object[] splitPath(String key) { + return PATH_PATTERN.split(key); + } + + private static void set(String path, Object def) { + if(config.getNode(splitPath(path)).isVirtual()) + config.getNode(splitPath(path)).setValue(def); + } + + private static void setString(String path, String def) { + try { + if(config.getNode(splitPath(path)).isVirtual()) + config.getNode(splitPath(path)).setValue(TypeToken.of(String.class), def); + } catch(ObjectMappingException ex) { + } + } + + private static boolean getBoolean(String path, boolean def) { + set(path, def); + return config.getNode(splitPath(path)).getBoolean(def); + } + + private static double getDouble(String path, double def) { + set(path, def); + return config.getNode(splitPath(path)).getDouble(def); + } + + private static int getInt(String path, int def) { + set(path, def); + return config.getNode(splitPath(path)).getInt(def); + } + + private static String getString(String path, String def) { + setString(path, def); + return config.getNode(splitPath(path)).getString(def); + } + + private static Long getLong(String path, Long def) { + set(path, def); + return config.getNode(splitPath(path)).getLong(def); + } + + private static List getList(String path, T def) { + try { + set(path, def); + return config.getNode(splitPath(path)).getList(TypeToken.of(String.class)); + } catch(ObjectMappingException ex) { + } + return new ArrayList<>(); + } + + /** ONLY EDIT ANYTHING BELOW THIS LINE **/ + public static List PREFIXGROUPS = new ArrayList<>(); + private static void settings() { + PREFIXGROUPS = getList("settings.prefix-groups", + Lists.newArrayList("discord", "socialmedia", "eventteam", "eventleader", "youtube", "twitch", "developer")); + } + + public static List MESSAGECOMMANDALIASES = new ArrayList<>(); + public static List REPLYCOMMANDALIASES = new ArrayList<>(); + public static String MESSAGESENDER = " >(Me -> ) "; + public static String MESSAGERECIEVER = " >( on -> Me) "; + private static void messageCommand() { + MESSAGECOMMANDALIASES.clear(); + REPLYCOMMANDALIASES.clear(); + MESSAGECOMMANDALIASES = getList("commands.message.aliases", Lists.newArrayList("msg", "whisper", "tell")); + REPLYCOMMANDALIASES = getList("commands.reply.aliases", Lists.newArrayList("r")); + MESSAGESENDER = getString("commands.message.sender-message", MESSAGESENDER); + MESSAGERECIEVER = getString("commands.message.reciever-message", MESSAGERECIEVER); + } + + public static List GCCOMMANDALIASES = new ArrayList<>(); + public static String GCFORMAT = " >to Global: "; + public static String GCPERMISSION = "proxy.globalchat"; + private static void globalChat() { + MESSAGERECIEVER = getString("commands.globalchat.format", MESSAGERECIEVER); + GCPERMISSION = getString("commands.globalchat.view-chat-permission", GCPERMISSION); + GCCOMMANDALIASES = getList("commands.globalchat.aliases", Lists.newArrayList("gc")); + } + + public static List GACECOMMANDALIASES = new ArrayList<>(); + public static String GACFORMAT = " >( on -> Team) "; + private static void globalAdminChat() { + GACECOMMANDALIASES = getList("commands.globaladminchat.aliases", Lists.newArrayList("acg")); + GACFORMAT = getString("commands.globaladminchat.format", GACFORMAT); + } + +} diff --git a/velocity/pom.xml b/velocity/pom.xml index 04f6295..fcda1c7 100644 --- a/velocity/pom.xml +++ b/velocity/pom.xml @@ -80,7 +80,7 @@ minecraft-libraries https://libraries.minecraft.net/ - + jcenter jcenter-bintray https://jcenter.bintray.com @@ -89,6 +89,11 @@ sonatype-oss-snapshots https://oss.sonatype.org/content/repositories/snapshots/ + + sponge-repo + Sponge Maven Repository + https://repo.spongepowered.org/maven +