From 1b3454300f25d6c73afb6d6cc934aca63bf12109 Mon Sep 17 00:00:00 2001 From: Joe Shimell <49585339+JoeShimell@users.noreply.github.com> Date: Wed, 8 Jul 2020 01:50:24 +0100 Subject: [PATCH] Custom command prefix support & ability to hide the players' skin image on whitelist --- .../joe/Configs/CustomPrefixConfig.java | 107 ++++++++++++++++++ .../uk/co/angrybee/joe/DiscordClient.java | 38 ++++++- .../co/angrybee/joe/DiscordWhitelister.java | 35 +++++- 3 files changed, 174 insertions(+), 6 deletions(-) create mode 100644 src/main/java/uk/co/angrybee/joe/Configs/CustomPrefixConfig.java diff --git a/src/main/java/uk/co/angrybee/joe/Configs/CustomPrefixConfig.java b/src/main/java/uk/co/angrybee/joe/Configs/CustomPrefixConfig.java new file mode 100644 index 0000000..224d5ac --- /dev/null +++ b/src/main/java/uk/co/angrybee/joe/Configs/CustomPrefixConfig.java @@ -0,0 +1,107 @@ +package uk.co.angrybee.joe.Configs; + +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import uk.co.angrybee.joe.DiscordWhitelister; + +import java.io.File; +import java.io.IOException; + +public class CustomPrefixConfig +{ + static File customPrefixesFile; + static FileConfiguration customPrefixesConfig; + + static boolean customPrefixesFileCreated = false; + + public static String whitelistAddPrefix; + public static String whitelistRemovePrefix; + + public static void ConfigSetup() + { + customPrefixesFile = new File(DiscordWhitelister.getPlugin().getDataFolder(), "custom-prefixes.yml"); + customPrefixesConfig = new YamlConfiguration(); + + if(!customPrefixesFile.exists()) + { + CreateConfig(); + } + + LoadConfig(); + CheckEntries(); + SaveConfig(); + AssignStrings(); + } + + private static void CreateConfig() + { + try + { + customPrefixesFile.createNewFile(); + } + catch (IOException e) + { + e.printStackTrace(); + } + + DiscordWhitelister.getPluginLogger().info("Custom Prefixes file created at: " + customPrefixesFile.getPath()); + customPrefixesFileCreated = true; + } + + private static void LoadConfig() + { + try + { + customPrefixesConfig.load(customPrefixesFile); + } + catch (IOException | InvalidConfigurationException e) + { + e.printStackTrace(); + } + } + + private static void CheckEntries() + { + if(customPrefixesFile.exists()) + { + if(customPrefixesConfig.getString("whitelist-add-prefix") == null) + { + customPrefixesConfig.set("whitelist-add-prefix", "!whitelist add"); + + if(!customPrefixesFileCreated) + { + DiscordWhitelister.getPluginLogger().warning("Entry 'whitelist-add-prefix' was not found, adding it to the config..."); + } + } + + if(customPrefixesConfig.getString("whitelist-remove-prefix") == null) + { + customPrefixesConfig.set("whitelist-remove-prefix", "!whitelist remove"); + + if(!customPrefixesFileCreated) + { + DiscordWhitelister.getPluginLogger().warning("Entry 'whitelist-remove-prefix' was not found, adding it to the config..."); + } + } + } + } + + private static void SaveConfig() + { + try + { + customPrefixesConfig.save(customPrefixesFile.getPath()); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + + private static void AssignStrings() + { + whitelistAddPrefix = customPrefixesConfig.getString("whitelist-add-prefix"); + whitelistRemovePrefix = customPrefixesConfig.getString("whitelist-remove-prefix"); + } +} diff --git a/src/main/java/uk/co/angrybee/joe/DiscordClient.java b/src/main/java/uk/co/angrybee/joe/DiscordClient.java index c6d2d4d..87ce245 100644 --- a/src/main/java/uk/co/angrybee/joe/DiscordClient.java +++ b/src/main/java/uk/co/angrybee/joe/DiscordClient.java @@ -8,6 +8,7 @@ import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.events.guild.member.GuildMemberLeaveEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; +import net.dv8tion.jda.internal.requests.Route; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; @@ -16,6 +17,7 @@ import org.json.simple.JSONObject; import org.json.simple.JSONValue; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; +import uk.co.angrybee.joe.Configs.CustomPrefixConfig; import javax.annotation.Nonnull; import javax.security.auth.login.LoginException; @@ -191,7 +193,8 @@ public class DiscordClient extends ListenerAdapter } // Add Command - if (messageContents.toLowerCase().startsWith("!whitelist add")) + if (messageContents.toLowerCase().startsWith("!whitelist add") && !DiscordWhitelister.getUseCustomPrefixes() + || DiscordWhitelister.getUseCustomPrefixes() && messageContents.toLowerCase().startsWith(CustomPrefixConfig.whitelistAddPrefix)) { // Permission check if (!(authorPermissions.isUserCanAddRemove() || authorPermissions.isUserCanAdd() || limitedWhitelistEnabled && authorPermissions.isUserHasLimitedAdd())) @@ -253,7 +256,17 @@ public class DiscordClient extends ListenerAdapter if (authorPermissions.isUserCanAddRemove() || authorPermissions.isUserCanAdd() || limitedWhitelistEnabled && authorPermissions.isUserHasLimitedAdd()) { messageContents = messageContents.toLowerCase(); - String messageContentsAfterCommand = messageContents.substring("!whitelist add".length() + 1); // get everything after !whitelist add[space] + + String messageContentsAfterCommand = ""; + if(!DiscordWhitelister.getUseCustomPrefixes()) + { + messageContentsAfterCommand = messageContents.substring("!whitelist add".length() + 1); // get everything after !whitelist add[space] + } + else + { + messageContentsAfterCommand = messageContents.substring(CustomPrefixConfig.whitelistAddPrefix.length() + 1); // get everything after whitelistAddPrefix[space] + } + final String finalNameToAdd = messageContentsAfterCommand.replaceAll(" .*", ""); // The name is everything up to the first space final char[] finalNameToWhitelistChar = finalNameToAdd.toCharArray(); @@ -515,7 +528,10 @@ public class DiscordClient extends ListenerAdapter this will run even if the message is never sent, but is a good trade off */ EmbedBuilder embedBuilderWhitelistSuccess = new EmbedBuilder(); embedBuilderWhitelistSuccess.setColor(new Color(46, 204, 113)); - embedBuilderWhitelistSuccess.setThumbnail("https://minotar.net/bust/" + playerUUID + "/100.png"); + if(DiscordWhitelister.showPlayerSkin) + { + embedBuilderWhitelistSuccess.setThumbnail("https://minotar.net/bust/" + playerUUID + "/100.png"); + } if(!DiscordWhitelister.useCustomMessages) { @@ -694,10 +710,22 @@ public class DiscordClient extends ListenerAdapter } } - if (messageContents.toLowerCase().startsWith("!whitelist remove")) { + if (messageContents.toLowerCase().startsWith("!whitelist remove") && !DiscordWhitelister.getUseCustomPrefixes() + || DiscordWhitelister.getUseCustomPrefixes() && messageContents.toLowerCase().startsWith(CustomPrefixConfig.whitelistRemovePrefix)) + { if (authorPermissions.isUserCanAddRemove()) { messageContents = messageContents.toLowerCase(); - String messageContentsAfterCommand = messageContents.substring("!whitelist remove".length() + 1); // get everything after !whitelist add[space] + + String messageContentsAfterCommand = ""; + if(!DiscordWhitelister.useCustomPrefixes) + { + messageContentsAfterCommand = messageContents.substring("!whitelist remove".length() + 1); // get everything after !whitelist remove[space] + } + else + { + messageContentsAfterCommand = messageContents.substring(CustomPrefixConfig.whitelistRemovePrefix.length() + 1); // get everything after whitelistRemovePrefix[space] + } + final String finalNameToRemove = messageContentsAfterCommand.replaceAll(" .*", ""); // The name is everything up to the first space if (finalNameToRemove.isEmpty()) { diff --git a/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java b/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java index fcc6075..65408b5 100755 --- a/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java +++ b/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java @@ -9,6 +9,7 @@ import org.bukkit.plugin.java.JavaPlugin; import uk.co.angrybee.joe.Commands.CommandAbout; import uk.co.angrybee.joe.Commands.CommandReload; import uk.co.angrybee.joe.Commands.CommandStatus; +import uk.co.angrybee.joe.Configs.CustomPrefixConfig; import uk.co.angrybee.joe.Events.JoinLeaveEvents; import java.io.File; @@ -23,11 +24,13 @@ public class DiscordWhitelister extends JavaPlugin private static File removedListFile; private static File customMessagesFile; + private static FileConfiguration whitelisterBotConfig; private static FileConfiguration userList; private static FileConfiguration removedList; private static FileConfiguration customMessagesConfig; + // easy whitelist public static Plugin easyWhitelist; @@ -41,6 +44,8 @@ public class DiscordWhitelister extends JavaPlugin public static boolean useEasyWhitelist = false; public static boolean useCustomMessages = false; public static boolean useIdForRoles = false; + public static boolean useCustomPrefixes = false; + public static boolean showPlayerSkin = true; public static boolean botEnabled; @@ -101,7 +106,9 @@ public class DiscordWhitelister extends JavaPlugin return removedListFile; } - public static FileConfiguration getCustomMessagesConfig() { return customMessagesConfig; } + public static FileConfiguration getCustomMessagesConfig() { return customMessagesConfig; } + + public static Logger getPluginLogger() { return pluginLogger; } public static List getRegisteredUsers(String userId) { return userList.getList(userId); } @@ -113,6 +120,8 @@ public class DiscordWhitelister extends JavaPlugin } } + public static boolean getUseCustomPrefixes() { return useCustomPrefixes; } + public static void addRegisteredUser(String userId, String userToAdd) throws IOException { List x = getRegisteredUsers(userId); List newList = new ArrayList<>(); @@ -151,6 +160,7 @@ public class DiscordWhitelister extends JavaPlugin botToken = getWhitelisterBotConfig().getString("discord-bot-token"); botEnabled = getWhitelisterBotConfig().getBoolean("bot-enabled"); + showPlayerSkin = getWhitelisterBotConfig().getBoolean("show-player-skin-on-whitelist"); if(!botEnabled) { @@ -206,6 +216,7 @@ public class DiscordWhitelister extends JavaPlugin // Custom messages check useCustomMessages = getWhitelisterBotConfig().getBoolean("use-custom-messages"); + useCustomPrefixes = getWhitelisterBotConfig().getBoolean("use-custom-prefixes"); int initSuccess = DiscordClient.InitializeClient(botToken); @@ -527,6 +538,26 @@ public class DiscordWhitelister extends JavaPlugin } } + if(getWhitelisterBotConfig().get("use-custom-prefixes") == null) + { + getWhitelisterBotConfig().set("use-custom-prefixes", false); + + if(!configCreated) + { + getPlugin().getLogger().warning("Entry 'use-custom-prefixes' was not found, adding it to the config..."); + } + } + + if(getWhitelisterBotConfig().get("show-player-skin-on-whitelist") == null) + { + getWhitelisterBotConfig().set("show-player-skin-on-whitelist", true); + + if(!configCreated) + { + getPlugin().getLogger().warning("Entry 'show-player-skin-on-whitelist' was not found, adding it to the config..."); + } + } + try { getWhitelisterBotConfig().save((whitelisterBotConfigFile.getPath())); @@ -795,6 +826,8 @@ public class DiscordWhitelister extends JavaPlugin } } + CustomPrefixConfig.ConfigSetup(); + if(userListCreated) { try