diff --git a/api/src/main/java/com/alttd/chat/ChatAPI.java b/api/src/main/java/com/alttd/chat/ChatAPI.java index 9cb583b..507d125 100755 --- a/api/src/main/java/com/alttd/chat/ChatAPI.java +++ b/api/src/main/java/com/alttd/chat/ChatAPI.java @@ -3,7 +3,9 @@ package com.alttd.chat; import com.alttd.chat.database.DatabaseConnection; import net.luckperms.api.LuckPerms; -public interface ChatAPI { +import java.util.HashMap; + +public abstract interface ChatAPI { static ChatAPI get() { return ChatImplementation.get(); @@ -17,4 +19,6 @@ public interface ChatAPI { void ReloadChatFilters(); + HashMap getPrefixes(); + } diff --git a/api/src/main/java/com/alttd/chat/ChatImplementation.java b/api/src/main/java/com/alttd/chat/ChatImplementation.java index 79f82fa..36d5a15 100755 --- a/api/src/main/java/com/alttd/chat/ChatImplementation.java +++ b/api/src/main/java/com/alttd/chat/ChatImplementation.java @@ -1,13 +1,19 @@ package com.alttd.chat; import com.alttd.chat.config.Config; +import com.alttd.chat.config.PrefixConfig; import com.alttd.chat.database.DatabaseConnection; import com.alttd.chat.database.Queries; import com.alttd.chat.managers.ChatUserManager; import com.alttd.chat.managers.PartyManager; import com.alttd.chat.managers.RegexManager; +import com.alttd.chat.util.ALogger; import net.luckperms.api.LuckPerms; import net.luckperms.api.LuckPermsProvider; +import net.luckperms.api.model.group.Group; + +import java.util.HashMap; +import java.util.Map; public class ChatImplementation implements ChatAPI{ @@ -16,6 +22,8 @@ public class ChatImplementation implements ChatAPI{ private LuckPerms luckPerms; private DatabaseConnection databaseConnection; + private HashMap prefixes; + public ChatImplementation() { instance = this; ReloadConfig(); @@ -52,6 +60,7 @@ public class ChatImplementation implements ChatAPI{ @Override public void ReloadConfig() { Config.init(); + loadPrefixes(); } @Override @@ -59,4 +68,18 @@ public class ChatImplementation implements ChatAPI{ RegexManager.initialize(); } + @Override + public HashMap getPrefixes() { + return prefixes; + } + + private void loadPrefixes() { + prefixes = new HashMap<>(); + getLuckPerms().getGroupManager().getLoadedGroups().stream() + .map(Group::getName).forEach(groupName -> prefixes.put(groupName, new PrefixConfig(groupName).PREFIXFORMAT)); + for (Map.Entry entry : prefixes.entrySet()) { + ALogger.info("prefix: " + entry.getKey() + " format: " + entry.getValue()); + } + } + } diff --git a/api/src/main/java/com/alttd/chat/config/Config.java b/api/src/main/java/com/alttd/chat/config/Config.java index 51e6b10..1541b33 100755 --- a/api/src/main/java/com/alttd/chat/config/Config.java +++ b/api/src/main/java/com/alttd/chat/config/Config.java @@ -32,7 +32,7 @@ public final class Config { public static File CONFIGPATH; public static void init() { // todo setup share for the config CONFIGPATH = new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "ChatPlugin"); - CONFIG_FILE = new File(CONFIGPATH, "config.yml");; + CONFIG_FILE = new File(CONFIGPATH, "config.yml"); configLoader = YAMLConfigurationLoader.builder() .setFile(CONFIG_FILE) .setFlowStyle(DumperOptions.FlowStyle.BLOCK) @@ -68,6 +68,7 @@ public final class Config { e.printStackTrace(); } } + public static void readConfig(Class clazz, Object instance) { for (Method method : clazz.getDeclaredMethods()) { if (Modifier.isPrivate(method.getModifiers())) { @@ -155,7 +156,6 @@ public final class Config { return config.getNode(splitPath(path)); } - /** ONLY EDIT ANYTHING BELOW THIS LINE **/ public static List PREFIXGROUPS = new ArrayList<>(); public static List CONFLICTINGPREFIXGROUPS = new ArrayList<>(); @@ -168,7 +168,6 @@ public final class Config { Lists.newArrayList("discord", "socialmedia", "eventteam", "eventleader", "youtube", "twitch", "developer")); CONFLICTINGPREFIXGROUPS = getList("settings.prefix-conflicts-groups", Lists.newArrayList("eventteam", "eventleader")); - STAFFGROUPS = getList("settings.staff-groups", Lists.newArrayList("trainee", "moderator", "headmod", "admin", "manager", "owner")); CONSOLENAME = getString("settings.console-name", CONSOLENAME); diff --git a/api/src/main/java/com/alttd/chat/config/PrefixConfig.java b/api/src/main/java/com/alttd/chat/config/PrefixConfig.java new file mode 100755 index 0000000..e46ed1a --- /dev/null +++ b/api/src/main/java/com/alttd/chat/config/PrefixConfig.java @@ -0,0 +1,152 @@ +package com.alttd.chat.config; + +import com.google.common.base.Throwables; +import com.google.common.reflect.TypeToken; +import ninja.leaping.configurate.ConfigurationNode; +import ninja.leaping.configurate.ConfigurationOptions; +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.regex.Pattern; + +public final class PrefixConfig { + private static final Pattern PATH_PATTERN = Pattern.compile("\\."); + private static final String HEADER = + "This file is used to store all of the prefix settings used in the Altitude Chat plugin.\n" + + "Legacy and MiniMessage formatting can be applied to these settings.\n"; + + private static File CONFIG_FILE; + public static ConfigurationNode config; + public static YAMLConfigurationLoader configLoader; + + private static String prefixName; + private static String configPath; + private static String defaultPath; + + public PrefixConfig(String prefix) { + prefixName = prefix; + configPath = "prefix-settings." + prefixName + "."; + defaultPath = "prefix-settings.default."; + init(); + } + + public static File CONFIGPATH; + public void init() { + CONFIGPATH = new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "ChatPlugin"); + CONFIG_FILE = new File(CONFIGPATH, "prefix.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(); + } + + readConfig(PrefixConfig.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()); + } + } + + public 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(defaultPath + path, def); + return config.getNode(splitPath(configPath + path)).getBoolean( + config.getNode(splitPath(defaultPath + path)).getBoolean(def)); + } + + private static double getDouble(String path, double def) { + set(defaultPath + path, def); + return config.getNode(splitPath(configPath + path)).getDouble( + config.getNode(splitPath(defaultPath + path)).getDouble(def)); + } + + private static int getInt(String path, int def) { + set(defaultPath + path, def); + return config.getNode(splitPath(configPath + path)).getInt( + config.getNode(splitPath(defaultPath + path)).getInt(def)); + } + + private static String getString(String path, String def) { + set(defaultPath + path, def); + return config.getNode(splitPath(configPath + path)).getString( + config.getNode(splitPath(defaultPath + path)).getString(def)); + } + + /** ONLY EDIT ANYTHING BELOW THIS LINE **/ + public static String PREFIXFORMAT = ""; + private static void PrefixSettings() { + PREFIXFORMAT = getString("format", PREFIXFORMAT); + } + +} diff --git a/api/src/main/java/com/alttd/chat/managers/ChatUserManager.java b/api/src/main/java/com/alttd/chat/managers/ChatUserManager.java index 1024a17..fef889e 100755 --- a/api/src/main/java/com/alttd/chat/managers/ChatUserManager.java +++ b/api/src/main/java/com/alttd/chat/managers/ChatUserManager.java @@ -4,10 +4,7 @@ import com.alttd.chat.database.Queries; import com.alttd.chat.objects.ChatUser; import com.alttd.chat.objects.Mail; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.UUID; +import java.util.*; import java.util.stream.Collectors; public final class ChatUserManager { diff --git a/api/src/main/java/com/alttd/chat/util/Utility.java b/api/src/main/java/com/alttd/chat/util/Utility.java index 8ef795d..75c0490 100755 --- a/api/src/main/java/com/alttd/chat/util/Utility.java +++ b/api/src/main/java/com/alttd/chat/util/Utility.java @@ -18,6 +18,7 @@ import org.jetbrains.annotations.NotNull; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; public class Utility { @@ -63,11 +64,12 @@ public class Utility { LuckPerms luckPerms = ChatAPI.get().getLuckPerms(); User user = luckPerms.getUserManager().getUser(uuid); List prefixGroups = Config.PREFIXGROUPS; - if(prefixGroups.containsAll(Config.CONFLICTINGPREFIXGROUPS)) - prefixGroups.remove("eventteam"); // hardcoded for now, new prefix system would load this from config if(user == null) return Component.empty(); if(!single) { Collection inheritedGroups = user.getInheritedGroups(user.getQueryOptions()); + if(inheritedGroups.stream().map(Group::getName).collect(Collectors.toList()).contains("eventleader")) { + prefixGroups.remove("eventteam"); // hardcoded for now, new prefix system would load this from config + } inheritedGroups.stream() .sorted(Comparator.comparingInt(o -> o.getWeight().orElse(0))) .forEach(group -> { @@ -95,22 +97,23 @@ public class Utility { } public static String getGroupPrefix(Group group) { - switch (group.getName()) { // hardcoded for now, new prefix system would load this from config - case "discord": - return "" + group.getCachedData().getMetaData().getPrefix() + ""; - case "socialmedia": - return "" + group.getCachedData().getMetaData().getPrefix() + ""; - case "eventteam": - return "" + group.getCachedData().getMetaData().getPrefix() + ""; - case "eventleader": - return "" + group.getCachedData().getMetaData().getPrefix() + ""; - case "youtube": - return "" + group.getCachedData().getMetaData().getPrefix() + ""; - case "twitch": - return "" + group.getCachedData().getMetaData().getPrefix() + ""; - default: - return group.getCachedData().getMetaData().getPrefix(); - } + return ChatAPI.get().getPrefixes().get(group.getName()).replace("", group.getCachedData().getMetaData().getPrefix()); +// switch (group.getName()) { // hardcoded for now, new prefix system would load this from config +// case "discord": +// return "" + group.getCachedData().getMetaData().getPrefix() + ""; +// case "socialmedia": +// return "" + group.getCachedData().getMetaData().getPrefix() + ""; +// case "eventteam": +// return "" + group.getCachedData().getMetaData().getPrefix() + ""; +// case "eventleader": +// return "" + group.getCachedData().getMetaData().getPrefix() + ""; +// case "youtube": +// return "" + group.getCachedData().getMetaData().getPrefix() + ""; +// case "twitch": +// return "" + group.getCachedData().getMetaData().getPrefix() + ""; +// default: +// return group.getCachedData().getMetaData().getPrefix(); +// } } public static String getDisplayName(UUID uuid, String playerName) { diff --git a/galaxy/src/main/java/com/alttd/chat/listeners/PluginMessage.java b/galaxy/src/main/java/com/alttd/chat/listeners/PluginMessage.java index 7df9290..71020e5 100755 --- a/galaxy/src/main/java/com/alttd/chat/listeners/PluginMessage.java +++ b/galaxy/src/main/java/com/alttd/chat/listeners/PluginMessage.java @@ -183,45 +183,4 @@ public class PluginMessage implements PluginMessageListener { }.runTaskAsynchronously(ChatPlugin.getInstance()); } -} -/* // todo implement AdvancedChatFilter for this like this - // send pluginmessage to backend server and return a shatteredcomponent to be reparsed by proxy - // Start - move these to util - public Component itemComponent(ItemStack item) { - Component component = Component.text("[i]"); - if(item.getType().equals(Material.AIR)) - return component; - boolean dname = item.hasItemMeta() && item.getItemMeta().hasDisplayName(); - if(dname) { - component = component.append(item.getItemMeta().displayName()); - } else { - component = Component.text(materialToName(item.getType())); - } - component = component.hoverEvent(item.asHoverEvent()); - return component; - } - - private static String materialToName(Material m) { - if (m.equals(Material.TNT)) { - return "TNT"; - } - String orig = m.toString().toLowerCase(); - String[] splits = orig.split("_"); - StringBuilder sb = new StringBuilder(orig.length()); - int pos = 0; - for (String split : splits) { - sb.append(split); - int loc = sb.lastIndexOf(split); - char charLoc = sb.charAt(loc); - if (!(split.equalsIgnoreCase("of") || split.equalsIgnoreCase("and") || - split.equalsIgnoreCase("with") || split.equalsIgnoreCase("on"))) - sb.setCharAt(loc, Character.toUpperCase(charLoc)); - if (pos != splits.length - 1) - sb.append(' '); - ++pos; - } - - return sb.toString(); - } - // end - move these to util -}*/ +} \ No newline at end of file