From e71d68f9107edacf8a6c2d6c7d178b4581116da5 Mon Sep 17 00:00:00 2001 From: Joe Shimell <49585339+JoeShimell@users.noreply.github.com> Date: Fri, 10 Jul 2020 00:41:37 +0100 Subject: [PATCH] Some code cleanup --- plugin.yml | 2 +- .../joe/Configs/CustomMessagesConfig.java | 131 ++++ .../joe/Configs/CustomPrefixConfig.java | 48 +- .../co/angrybee/joe/Configs/MainConfig.java | 160 +++++ .../uk/co/angrybee/joe/DiscordClient.java | 11 +- .../co/angrybee/joe/DiscordWhitelister.java | 606 +----------------- .../joe/Events/OnWhitelistEvents.java | 65 ++ .../joe/Stores/RemovedPlayersStore.java | 6 + .../java/uk/co/angrybee/joe/VersionInfo.java | 2 +- 9 files changed, 402 insertions(+), 629 deletions(-) create mode 100644 src/main/java/uk/co/angrybee/joe/Configs/CustomMessagesConfig.java create mode 100644 src/main/java/uk/co/angrybee/joe/Configs/MainConfig.java create mode 100644 src/main/java/uk/co/angrybee/joe/Events/OnWhitelistEvents.java create mode 100644 src/main/java/uk/co/angrybee/joe/Stores/RemovedPlayersStore.java diff --git a/plugin.yml b/plugin.yml index 62ef3e2..09f979b 100755 --- a/plugin.yml +++ b/plugin.yml @@ -1,5 +1,5 @@ name: DiscordWhitelister -version: 1.3.6 +version: 1.3.7 author: Joe Shimell main: uk.co.angrybee.joe.DiscordWhitelister description: Discord whitelister bot. diff --git a/src/main/java/uk/co/angrybee/joe/Configs/CustomMessagesConfig.java b/src/main/java/uk/co/angrybee/joe/Configs/CustomMessagesConfig.java new file mode 100644 index 0000000..b67ab40 --- /dev/null +++ b/src/main/java/uk/co/angrybee/joe/Configs/CustomMessagesConfig.java @@ -0,0 +1,131 @@ +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; + +// custom-messages.yml +public class CustomMessagesConfig +{ + private static File customMessagesFile; + private static FileConfiguration customMessagesConfig; + + public static FileConfiguration getCustomMessagesConfig() { return customMessagesConfig; } + + private static boolean customMessagesFileCreated = false; + + public static void ConfigSetup() + { + customMessagesFile = new File(DiscordWhitelister.getPlugin().getDataFolder(), "custom-messages.yml"); + customMessagesConfig = new YamlConfiguration(); + + if(!customMessagesFile.exists()) + CreateConfig(); + + LoadConfigFile(); + CheckEntries(); + SaveConfig(); + } + + private static void CreateConfig() + { + try + { + customMessagesFile.createNewFile(); + } + catch (IOException e) + { + e.printStackTrace(); + } + + DiscordWhitelister.getPluginLogger().info("Custom messages file created at: " + customMessagesFile.getPath()); + customMessagesFileCreated = true; + } + + private static void LoadConfigFile() + { + try + { + customMessagesConfig.load(customMessagesFile); + } + catch (IOException | InvalidConfigurationException e) + { + e.printStackTrace(); + } + } + + private static void CheckEntries() + { + /* TODO: add a YAML comment (#) explaining the config file params + NOTE: only {params} in the original messages will be evaluated. For example: using {MaxWhitelistAmount} in the "insufficient-permissions" String will not work as it was never in the original message. + {Sender} == author.getAsMention(), {RemainingWhitelists} == (maxWhitelistAmount - timesWhitelisted), {MaxWhitelistAmount} == maxWhitelistAmount, + {MinecraftUsername} == finalNameToAdd/Remove, {StaffMember} == DiscordWhitelister.getRemovedList().get(finalNameToAdd), {AddRemoveRoles} = DiscordWhitelister.getWhitelisterBotConfig().getList("add-remove-roles") + Internal error messages & info messages will remain uneditable. No need to add custom remove failure messages as it should never happen */ + + if(customMessagesFile.exists()) + { + CheckEntry("insufficient-permissions-title", "Insufficient Permissions"); + CheckEntry("insufficient-permissions", "{Sender}, you do not have permission to use this command."); + + CheckEntry("insufficient-permissions-remove-title", "Insufficient Permissions"); + CheckEntry("insufficient-permissions-remove", "{Sender}, you only have permission to add people to the whitelist. To remove people from the whitelist you must be moved to the following roles: {AddRemoveRoles}; or get the owner to move your role to 'add-remove-roles' in the config."); + + CheckEntry("no-whitelists-remaining-title", "No Whitelists Remaining"); + CheckEntry("no-whitelists-remaining", "{Sender}, unable to whitelist. You have **{RemainingWhitelists} out of {MaxWhitelistAmount}** whitelists remaining."); + + CheckEntry("invalid-characters-warning-title", "Invalid Username"); + CheckEntry("invalid-characters-warning", "{Sender}, the username you have specified contains invalid characters. **Only letters, numbers and underscores are allowed**."); + + CheckEntry("invalid-length-warning-title", "Invalid Username"); + CheckEntry("invalid-length-warning", "{Sender}, the username you have specified either contains too few or too many characters. **Usernames can only consist of 3-16 characters**."); + + CheckEntry("already-on-whitelist-title", "User already on the whitelist"); + CheckEntry("already-on-whitelist", "{Sender}, cannot add user as `{MinecraftUsername}` is already on the whitelist!"); + + CheckEntry("user-was-removed-title", "This user was previously removed by a staff member"); + CheckEntry("user-was-removed", "{Sender}, this user was previously removed by a staff member ({StaffMember}). Please ask a user with higher permissions to add this user."); + + CheckEntry("whitelists-remaining-title", "Whitelists Remaining"); + CheckEntry("whitelists-remaining", "You have **{RemainingWhitelists} out of {MaxWhitelistAmount}** whitelists remaining."); + + CheckEntry("whitelist-success-title", "{MinecraftUsername} is now whitelisted!"); + CheckEntry("whitelist-success", "{Sender} has added `{MinecraftUsername}` to the whitelist."); + + CheckEntry("whitelist-failure-title", "Failed to whitelist"); + CheckEntry("whitelist-failure", "{Sender}, failed to add `{MinecraftUsername}` to the whitelist. This is most likely due to an invalid Minecraft username."); + + CheckEntry("user-not-on-whitelist-title", "This user is not on the whitelist"); + CheckEntry("user-not-on-whitelist", "{Sender}, cannot remove user as `{MinecraftUsername}` is not on the whitelist!"); + + CheckEntry("remove-success-title", "{MinecraftUsername} has been removed"); + CheckEntry("remove-success", "{Sender} has removed {MinecraftUsername} from the whitelist"); + } + } + + private static void SaveConfig() + { + try + { + customMessagesConfig.save(customMessagesFile.getPath()); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + + private static void CheckEntry(String entryName, Object passedValue) + { + if(customMessagesConfig.get(entryName) == null) + { + customMessagesConfig.set(entryName, passedValue); + + if(!customMessagesFileCreated) + DiscordWhitelister.getPluginLogger().warning("Entry '" + entryName + "' was not found, adding it to custom-messages.yml..."); + } + } +} diff --git a/src/main/java/uk/co/angrybee/joe/Configs/CustomPrefixConfig.java b/src/main/java/uk/co/angrybee/joe/Configs/CustomPrefixConfig.java index 224d5ac..45f7f3b 100644 --- a/src/main/java/uk/co/angrybee/joe/Configs/CustomPrefixConfig.java +++ b/src/main/java/uk/co/angrybee/joe/Configs/CustomPrefixConfig.java @@ -8,15 +8,15 @@ import uk.co.angrybee.joe.DiscordWhitelister; import java.io.File; import java.io.IOException; +// custom-prefixes.yml public class CustomPrefixConfig { - static File customPrefixesFile; - static FileConfiguration customPrefixesConfig; + private static File customPrefixesFile; + private static FileConfiguration customPrefixesConfig; - static boolean customPrefixesFileCreated = false; + public static FileConfiguration getCustomPrefixesConfig() { return customPrefixesConfig; } - public static String whitelistAddPrefix; - public static String whitelistRemovePrefix; + private static boolean customPrefixesFileCreated = false; public static void ConfigSetup() { @@ -24,14 +24,11 @@ public class CustomPrefixConfig customPrefixesConfig = new YamlConfiguration(); if(!customPrefixesFile.exists()) - { CreateConfig(); - } - LoadConfig(); + LoadConfigFile(); CheckEntries(); SaveConfig(); - AssignStrings(); } private static void CreateConfig() @@ -49,7 +46,7 @@ public class CustomPrefixConfig customPrefixesFileCreated = true; } - private static void LoadConfig() + private static void LoadConfigFile() { try { @@ -65,25 +62,9 @@ public class CustomPrefixConfig { if(customPrefixesFile.exists()) { - if(customPrefixesConfig.getString("whitelist-add-prefix") == null) - { - customPrefixesConfig.set("whitelist-add-prefix", "!whitelist add"); + CheckEntry("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..."); - } - } + CheckEntry("whitelist-remove-prefix", "!whitelist remove"); } } @@ -99,9 +80,14 @@ public class CustomPrefixConfig } } - private static void AssignStrings() + private static void CheckEntry(String entryName, Object passedValue) { - whitelistAddPrefix = customPrefixesConfig.getString("whitelist-add-prefix"); - whitelistRemovePrefix = customPrefixesConfig.getString("whitelist-remove-prefix"); + if(customPrefixesConfig.get(entryName) == null) + { + customPrefixesConfig.set(entryName, passedValue); + + if(!customPrefixesFileCreated) + DiscordWhitelister.getPluginLogger().warning("Entry '" + entryName + "' was not found, adding it to custom-prefixes.yml..."); + } } } diff --git a/src/main/java/uk/co/angrybee/joe/Configs/MainConfig.java b/src/main/java/uk/co/angrybee/joe/Configs/MainConfig.java new file mode 100644 index 0000000..80a44f1 --- /dev/null +++ b/src/main/java/uk/co/angrybee/joe/Configs/MainConfig.java @@ -0,0 +1,160 @@ +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; +import java.util.Arrays; +import java.util.Collections; + +// discord-whitelister.yml +public class MainConfig +{ + private static File whitelisterBotConfigFile; + private static FileConfiguration whitelisterBotConfig; + + public static FileConfiguration getMainConfig() { return whitelisterBotConfig; } + + public static boolean configCreated = false; + + public static void ConfigSetup() + { + whitelisterBotConfigFile = new File(DiscordWhitelister.getPlugin().getDataFolder(), "discord-whitelister.yml"); + whitelisterBotConfig = new YamlConfiguration(); + + // Create root folder for configs if it does not exist + if(!whitelisterBotConfigFile.getParentFile().exists()) + whitelisterBotConfigFile.getParentFile().mkdirs(); + + if(!whitelisterBotConfigFile.exists()) + CreateConfig(); + + LoadConfigFile(); + CheckEntries(); + SaveConfig(); + } + + private static void CreateConfig() + { + try + { + whitelisterBotConfigFile.createNewFile(); + } + catch (IOException e) + { + e.printStackTrace(); + } + + DiscordWhitelister.getPluginLogger().info("Configuration file created at: " + whitelisterBotConfigFile.getPath() + + ", please edit this else the plugin will not work!"); + configCreated = true; + } + + private static void LoadConfigFile() + { + try + { + whitelisterBotConfig.load(whitelisterBotConfigFile); + } + catch (IOException | InvalidConfigurationException e) + { + e.printStackTrace(); + } + } + + private static void CheckEntries() + { + CheckEntry("bot-enabled", true); + + CheckEntry("discord-bot-token", + "Discord bot token goes here, you can find it here: https://discordapp.com/developers/applications/" ); + + CheckEntry("use-id-for-roles", false); + + // Allowed to add and remove from the whitelist + CheckEntry("add-remove-roles", Arrays.asList("Owner", "Admin")); + + // Only allowed to add to the whitelist + CheckEntry("add-roles", Arrays.asList("Mod", "Whitelister")); + + // Roles that are allowed whitelist a limited amount of times + CheckEntry("limited-add-roles", Collections.singletonList("LimitedWhitelister")); + + // The roles to add/remove when whitelisted/removed + CheckEntry("whitelisted-roles", Collections.singletonList("Whitelisted")); + + CheckEntry("target-text-channels", Arrays.asList("000000000000000000", "111111111111111111")); + + // EasyWhitelist support (https://www.spigotmc.org/resources/easywhitelist-name-based-whitelist.65222/) + CheckEntry("use-easy-whitelist", false); + + // If adding the whitelisted role to the discord user is enabled + CheckEntry("whitelisted-role-auto-add", false); + + // If removing the whitelisted role from the discord user is enabled + CheckEntry("whitelisted-role-auto-remove", false); + + // If the limited whitelist feature should be enabled + CheckEntry("limited-whitelist-enabled", true); + + // The amount of times a non-staff user is allowed to whitelist + CheckEntry("max-whitelist-amount", 3); + + CheckEntry("username-validation", true); + + CheckEntry("removed-list-enabled", true); + + CheckEntry("use-custom-messages", false); + + CheckEntry("use-custom-prefixes", false); + + CheckEntry("show-player-skin-on-whitelist", true); + + CheckEntry("show-player-count", true); + + // Remove old role entry if found, move role to new array (for people with v1.3.6 or below) + if(whitelisterBotConfig.get("whitelisted-role") != null) + { + DiscordWhitelister.getPluginLogger().warning("Found whitelisted-role entry, moving over to whitelisted-roles. Please check your config to make sure the change is correct"); + // Get the role from the old entry + String whitelistedRoleTemp = whitelisterBotConfig.getString("whitelisted-role"); + // Assign role from old entry to new entry as a list + whitelisterBotConfig.set("whitelisted-roles", Collections.singletonList(whitelistedRoleTemp)); + + // Remove now un-used entry + whitelisterBotConfig.set("whitelisted-role", null); + + // Note to users that id for roles now affects the new entry + if(whitelisterBotConfig.getBoolean("use-id-for-roles")) + { + DiscordWhitelister.getPluginLogger().severe("You have 'use-id-for-roles' enabled please change the whitelisted-roles to ids as they now follow this setting"); + } + } + } + + private static void SaveConfig() + { + try + { + whitelisterBotConfig.save(whitelisterBotConfigFile.getPath()); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + + private static void CheckEntry(String entryName, Object passedValue) + { + if(whitelisterBotConfig.get(entryName) == null) + { + whitelisterBotConfig.set(entryName, passedValue); + + if(!configCreated) + DiscordWhitelister.getPluginLogger().warning("Entry '" + entryName + "' was not found, adding it to the config..."); + } + } +} diff --git a/src/main/java/uk/co/angrybee/joe/DiscordClient.java b/src/main/java/uk/co/angrybee/joe/DiscordClient.java index 7d03d2c..48b6a54 100644 --- a/src/main/java/uk/co/angrybee/joe/DiscordClient.java +++ b/src/main/java/uk/co/angrybee/joe/DiscordClient.java @@ -34,6 +34,9 @@ public class DiscordClient extends ListenerAdapter private static String[] targetTextChannels; + public static String whitelistAddPrefix; + public static String whitelistRemovePrefix; + private static MessageEmbed botInfo; private static MessageEmbed addCommandInfo; private static MessageEmbed removeCommandInfo; @@ -197,7 +200,7 @@ public class DiscordClient extends ListenerAdapter // Add Command if (messageContents.toLowerCase().startsWith("!whitelist add") && !DiscordWhitelister.getUseCustomPrefixes() - || DiscordWhitelister.getUseCustomPrefixes() && messageContents.toLowerCase().startsWith(CustomPrefixConfig.whitelistAddPrefix)) + || DiscordWhitelister.getUseCustomPrefixes() && messageContents.toLowerCase().startsWith(whitelistAddPrefix)) { // Permission check if (!(authorPermissions.isUserCanAddRemove() || authorPermissions.isUserCanAdd() || limitedWhitelistEnabled && authorPermissions.isUserHasLimitedAdd())) @@ -267,7 +270,7 @@ public class DiscordClient extends ListenerAdapter } else { - messageContentsAfterCommand = messageContents.substring(CustomPrefixConfig.whitelistAddPrefix.length() + 1); // get everything after whitelistAddPrefix[space] + messageContentsAfterCommand = messageContents.substring(whitelistAddPrefix.length() + 1); // get everything after whitelistAddPrefix[space] } final String finalNameToAdd = messageContentsAfterCommand.replaceAll(" .*", ""); // The name is everything up to the first space @@ -760,7 +763,7 @@ public class DiscordClient extends ListenerAdapter } if (messageContents.toLowerCase().startsWith("!whitelist remove") && !DiscordWhitelister.getUseCustomPrefixes() - || DiscordWhitelister.getUseCustomPrefixes() && messageContents.toLowerCase().startsWith(CustomPrefixConfig.whitelistRemovePrefix)) + || DiscordWhitelister.getUseCustomPrefixes() && messageContents.toLowerCase().startsWith(whitelistRemovePrefix)) { if (authorPermissions.isUserCanAddRemove()) { messageContents = messageContents.toLowerCase(); @@ -772,7 +775,7 @@ public class DiscordClient extends ListenerAdapter } else { - messageContentsAfterCommand = messageContents.substring(CustomPrefixConfig.whitelistRemovePrefix.length() + 1); // get everything after whitelistRemovePrefix[space] + messageContentsAfterCommand = messageContents.substring(whitelistRemovePrefix.length() + 1); // get everything after whitelistRemovePrefix[space] } final String finalNameToRemove = messageContentsAfterCommand.replaceAll(" .*", ""); // The name is everything up to the first space diff --git a/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java b/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java index a30c4e8..bcc307c 100755 --- a/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java +++ b/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java @@ -9,7 +9,9 @@ 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.CustomMessagesConfig; import uk.co.angrybee.joe.Configs.CustomPrefixConfig; +import uk.co.angrybee.joe.Configs.MainConfig; import uk.co.angrybee.joe.Events.JoinLeaveEvents; import java.io.File; @@ -19,17 +21,11 @@ import java.util.logging.Logger; public class DiscordWhitelister extends JavaPlugin { - private static File whitelisterBotConfigFile; private static File userListFile; 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; @@ -46,6 +42,7 @@ public class DiscordWhitelister extends JavaPlugin public static boolean useIdForRoles = false; public static boolean useCustomPrefixes = false; public static boolean showPlayerSkin = true; + public static boolean addInGameRemovesToList = true; public static boolean botEnabled; @@ -84,10 +81,7 @@ public class DiscordWhitelister extends JavaPlugin return thisPlugin; } - public static FileConfiguration getWhitelisterBotConfig() - { - return whitelisterBotConfig; - } + public static FileConfiguration getWhitelisterBotConfig() { return MainConfig.getMainConfig(); } public static FileConfiguration getUserList() { @@ -106,7 +100,7 @@ public class DiscordWhitelister extends JavaPlugin return removedListFile; } - public static FileConfiguration getCustomMessagesConfig() { return customMessagesConfig; } + public static FileConfiguration getCustomMessagesConfig() { return CustomMessagesConfig.getCustomMessagesConfig(); } public static Logger getPluginLogger() { return pluginLogger; } @@ -148,10 +142,8 @@ public class DiscordWhitelister extends JavaPlugin public static int InitBot(boolean firstInit) { - whitelisterBotConfig = new YamlConfiguration(); userList = new YamlConfiguration(); removedList = new YamlConfiguration(); - customMessagesConfig = new YamlConfiguration(); if(firstInit) vanishedPlayersCount = 0; @@ -161,6 +153,10 @@ public class DiscordWhitelister extends JavaPlugin botToken = getWhitelisterBotConfig().getString("discord-bot-token"); botEnabled = getWhitelisterBotConfig().getBoolean("bot-enabled"); showPlayerSkin = getWhitelisterBotConfig().getBoolean("show-player-skin-on-whitelist"); + configCreated = MainConfig.configCreated; + + DiscordClient.whitelistAddPrefix = CustomPrefixConfig.getCustomPrefixesConfig().getString("whitelist-add-prefix"); + DiscordClient.whitelistRemovePrefix = CustomPrefixConfig.getCustomPrefixesConfig().getString("whitelist-remove-prefix"); if(!botEnabled) { @@ -245,40 +241,16 @@ public class DiscordWhitelister extends JavaPlugin File dataFolder = thisPlugin.getDataFolder(); Logger pluginLogger = thisPlugin.getLogger(); - whitelisterBotConfigFile = new File(dataFolder, "discord-whitelister.yml"); + // Run this first as it creates the root folder if it does not exist + MainConfig.ConfigSetup(); + CustomPrefixConfig.ConfigSetup(); + CustomMessagesConfig.ConfigSetup(); + userListFile = new File(dataFolder, "user-list.yml"); removedListFile = new File(dataFolder, "removed-list.yml"); - customMessagesFile = new File(dataFolder, "custom-messages.yml"); - if(!whitelisterBotConfigFile.getParentFile().exists()) - { - whitelisterBotConfigFile.getParentFile().mkdirs(); - } - if(!whitelisterBotConfigFile.exists()) - { - try - { - whitelisterBotConfigFile.createNewFile(); - } - catch(IOException e) - { - e.printStackTrace(); - } - pluginLogger.info("Configuration file created at: " + whitelisterBotConfigFile.getPath() + - ", please edit this else the plugin will not work!"); - configCreated = true; - } - - try - { - getWhitelisterBotConfig().load(whitelisterBotConfigFile); - } - catch(IOException | InvalidConfigurationException e) - { - e.printStackTrace(); - } if(!userListFile.exists()) { @@ -328,556 +300,6 @@ public class DiscordWhitelister extends JavaPlugin e.printStackTrace(); } - if(!customMessagesFile.exists()) - { - try - { - customMessagesFile.createNewFile(); - } - catch(IOException e) - { - e.printStackTrace(); - } - - pluginLogger.info("Custom messages file has been created at: " + customMessagesFile.getPath()); - customMessagesCreated = true; - } - - try - { - getCustomMessagesConfig().load(customMessagesFile); - } - catch(IOException | InvalidConfigurationException e) - { - e.printStackTrace(); - } - - // check if entries exist in config file and add them if they are not. only log entry creation if it is not first time set up - if(whitelisterBotConfigFile.exists()) - { - if(getWhitelisterBotConfig().get("discord-bot-token") == null) - { - getWhitelisterBotConfig().set("discord-bot-token", "Discord bot token goes here, you can find it here: " + - "https://discordapp.com/developers/applications/"); - - if(!configCreated) - { - getPlugin().getLogger().warning("Entry 'discord-bot-token' was not found, adding it to the config..."); - } - } - - // allowed to add and remove from the whitelist - if(getWhitelisterBotConfig().get("add-remove-roles") == null) - { - List tempAddRemoveRoles = Arrays.asList("Owner", "Admin"); - getWhitelisterBotConfig().set("add-remove-roles", tempAddRemoveRoles); - - if(!configCreated) - { - getPlugin().getLogger().warning("Entry 'add-remove-roles' was not found, adding it to the config..."); - } - } - - // only allowed to add to the whitelist - if(getWhitelisterBotConfig().get("add-roles") == null) - { - List tempAddRoles = Arrays.asList("Mod", "Whitelister"); - getWhitelisterBotConfig().set("add-roles", tempAddRoles); - - if(!configCreated) - { - getPlugin().getLogger().warning("Entry 'add-roles' was not found, adding it to the config..."); - } - } - - if(getWhitelisterBotConfig().get("username-validation") == null) - { - getWhitelisterBotConfig().set("username-validation", true); - - if(!configCreated) - { - getPlugin().getLogger().warning("Entry 'username-validation' was not found, adding it to the config..."); - } - } - - if(getWhitelisterBotConfig().get("removed-list-enabled") == null) - { - getWhitelisterBotConfig().set("removed-list-enabled", true); - - if(!configCreated) - { - getPlugin().getLogger().warning("Entry 'removed-list-enabled' was not found, adding it to the config..."); - } - } - - // if the limited whitelist feature should be enabled - if(getWhitelisterBotConfig().get("limited-whitelist-enabled") == null) - { - getWhitelisterBotConfig().set("limited-whitelist-enabled", true); - - if(!configCreated) - { - getPlugin().getLogger().warning("Entry 'limited-whitelist-enabled' was not found, adding it to the config..."); - } - } - - // the amount of times a non-staff user is allowed to whitelist - if(getWhitelisterBotConfig().get("max-whitelist-amount") == null) - { - getWhitelisterBotConfig().set("max-whitelist-amount", 3); - - if(!configCreated) - { - getPlugin().getLogger().warning("Entry 'max-whitelist-amount' was not found, adding it to the config..."); - } - } - - // roles that are allowed whitelist a limited amount of times - if(getWhitelisterBotConfig().get("limited-add-roles") == null) - { - List tempLimitedRoles = Arrays.asList("VIP", "LimitedWhitelister"); - getWhitelisterBotConfig().set("limited-add-roles", tempLimitedRoles); - - if(!configCreated) - { - getPlugin().getLogger().warning("Entry 'limited-add-roles' was not found, adding it to the config..."); - } - } - - if(getWhitelisterBotConfig().get("target-text-channels") == null) - { - List tempChannelIds = Arrays.asList("445666834382061569", "488450157881327616"); - getWhitelisterBotConfig().set("target-text-channels", tempChannelIds); - - if(!configCreated) - { - getPlugin().getLogger().warning("Entry 'target-text-channels' was not found, adding it to the config..."); - } - } - - // If adding the whitelisted role to the discord user is enabled - if(getWhitelisterBotConfig().get("whitelisted-role-auto-add") == null) - { - getWhitelisterBotConfig().set("whitelisted-role-auto-add", false); - - if(!configCreated) - { - getPlugin().getLogger().warning("Entry 'whitelisted-role-auto-add' was not found, adding it to the config..."); - } - } - - // If removing the whitelisted role from the discord user is enabled - if(getWhitelisterBotConfig().get("whitelisted-role-auto-remove") == null) - { - getWhitelisterBotConfig().set("whitelisted-role-auto-remove", false); - - if(!configCreated) - { - getPlugin().getLogger().warning("Entry 'whitelisted-role-auto-remove' was not found, adding it to the config..."); - } - } - - // The name of the role to add/remove to the user - if(getWhitelisterBotConfig().get("whitelisted-roles") == null) - { - //getWhitelisterBotConfig().set("whitelisted-role", "Whitelisted"); - // Change to array format to allow multiple whitelisted roles / role ids - List tempLimitedRoles = Collections.singletonList("Whitelisted"); - getWhitelisterBotConfig().set("whitelisted-roles", tempLimitedRoles); - - if(!configCreated) - { - getPlugin().getLogger().warning("Entry 'whitelisted-roles' was not found, adding it to the config..."); - } - - // Save early for check below -// try -// { -// getWhitelisterBotConfig().save((whitelisterBotConfigFile.getPath())); -// } -// catch(IOException e) -// { -// e.printStackTrace(); -// } - } - - // remove old role, add to new array - if(getWhitelisterBotConfig().get("whitelisted-role") != null) - { - /* - ().warning("whitelist-role has changed to whitelist-roles to allow for multiple roles, moving over role..."); - String whitelistedRoleTemp = getWhitelisterBotConfig().getString("whitelisted-role"); - List finalRoles = null; - - if(getWhitelisterBotConfig().getList("whitelisted-roles") != null) - { - getWhitelisterBotConfig().getList("whitelisted-roles").forEach(role -> - { - finalRoles.add(role.toString()); - }); - } - - finalRoles.add(whitelistedRoleTemp); - - getWhitelisterBotConfig().set("whitelisted-roles", finalRoles); - getPluginLogger().info("Role successfully moved to whitelisted-roles, please check the config to make sure it is correct."); - */ - - getPluginLogger().warning("Found whitelisted-role entry, moving over to whitelisted-roles. Please check your config to make sure the change is correct"); - String whitelistedRoleTemp = getWhitelisterBotConfig().getString("whitelisted-role"); - // For now just assign it to whitelisted-roles instead of the default - List tempLimitedRoles = Collections.singletonList(whitelistedRoleTemp); - getWhitelisterBotConfig().set("whitelisted-roles", tempLimitedRoles); - - // Remove now un-used entry - getWhitelisterBotConfig().set("whitelisted-role", null); - - if(getWhitelisterBotConfig().getBoolean("use-id-for-roles")) - { - getPluginLogger().severe("You have 'use-id-for-roles' enabled please change the whitelisted-roles to ids as they now follow this setting"); - } - } - - // easy whitelist support - if(getWhitelisterBotConfig().get("use-easy-whitelist") == null) - { - getWhitelisterBotConfig().set("use-easy-whitelist", false); - - if (!configCreated) { - getPlugin().getLogger().warning("Entry 'use-easy-whitelist' was not found, adding it to the config..."); - } - } - - if(getWhitelisterBotConfig().get("bot-enabled") == null) - { - getWhitelisterBotConfig().set("bot-enabled", true); - - if(!configCreated) - { - getPlugin().getLogger().warning("Entry 'bot-enabled' was not found, adding it to the config..."); - } - } - - if(getWhitelisterBotConfig().get("show-player-count") == null) - { - getWhitelisterBotConfig().set("show-player-count", true); - - if(!configCreated) - { - getPlugin().getLogger().warning("Entry 'show-player-count' was not found, adding it to the config..."); - } - } - - if(getWhitelisterBotConfig().get("use-custom-messages") == null) - { - getWhitelisterBotConfig().set("use-custom-messages", false); - - if(!configCreated) - { - getPlugin().getLogger().warning("Entry 'use-custom-messages' was not found, adding it to the config..."); - } - } - - if(getWhitelisterBotConfig().get("use-id-for-roles") == null) - { - getWhitelisterBotConfig().set("use-id-for-roles", false); - - if(!configCreated) - { - getPlugin().getLogger().warning("Entry 'use-id-for-roles' was not found, adding it to the config..."); - } - } - - 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())); - } - catch(IOException e) - { - e.printStackTrace(); - } - } - - if(customMessagesFile.exists()) - { - /* TODO: add a YAML comment (#) explaining the config file params - NOTE: only {params} in the original messages will be evaluated. For example: using {MaxWhitelistAmount} in the "insufficient-permissions" String will not work as it was never in the original message. - {Sender} == author.getAsMention(), {RemainingWhitelists} == (maxWhitelistAmount - timesWhitelisted), {MaxWhitelistAmount} == maxWhitelistAmount, - {MinecraftUsername} == finalNameToAdd/Remove, {StaffMember} == DiscordWhitelister.getRemovedList().get(finalNameToAdd), {AddRemoveRoles} = DiscordWhitelister.getWhitelisterBotConfig().getList("add-remove-roles") - Internal error messages & info messages will remain uneditable. No need to add custom remove failure messages as it should never happen */ - - if(getCustomMessagesConfig().getString("insufficient-permissions-title") == null) - { - getCustomMessagesConfig().set("insufficient-permissions-title", "Insufficient Permissions"); - - if(!customMessagesCreated) - { - getPlugin().getLogger().warning("Entry 'insufficient-permissions-title' was not found, adding it to the config..."); - } - } - - if(getCustomMessagesConfig().getString("insufficient-permissions") == null) - { - getCustomMessagesConfig().set("insufficient-permissions", "{Sender}, you do not have permission to use this command."); - - if(!customMessagesCreated) - { - getPlugin().getLogger().warning("Entry 'insufficient-permissions' was not found, adding it to the config..."); - } - } - - if(getCustomMessagesConfig().getString("insufficient-permissions-remove-title") == null) - { - getCustomMessagesConfig().set("insufficient-permissions-remove-title", "Insufficient Permissions"); - - if(!customMessagesCreated) - { - getPlugin().getLogger().warning("Entry 'insufficient-permissions-remove-title' was not found, adding it to the config..."); - } - } - - if(getCustomMessagesConfig().getString("insufficient-permissions-remove") == null) - { - getCustomMessagesConfig().set("insufficient-permissions-remove", "{Sender}, you only have permission to add people to the whitelist. To remove people from the whitelist you must be moved to the following roles: {AddRemoveRoles}; or get the owner to move your role to 'add-remove-roles' in the config."); - - if(!customMessagesCreated) - { - getPlugin().getLogger().warning("Entry 'insufficient-permissions-remove' was not found, adding it to the config..."); - } - } - - if(getCustomMessagesConfig().getString("no-whitelists-remaining-title") == null) - { - getCustomMessagesConfig().set("no-whitelists-remaining-title", "No Whitelists Remaining"); - - if(!customMessagesCreated) - { - getPlugin().getLogger().warning("Entry 'no-whitelists-remaining-title' was not found, adding it to the config..."); - } - } - - if(getCustomMessagesConfig().getString("no-whitelists-remaining") == null) - { - getCustomMessagesConfig().set("no-whitelists-remaining", "{Sender}, unable to whitelist. You have **{RemainingWhitelists} out of {MaxWhitelistAmount}** whitelists remaining."); - - if(!customMessagesCreated) - { - getPlugin().getLogger().warning("Entry 'insufficient-permissions' was not found, adding it to the config..."); - } - } - - if(getCustomMessagesConfig().getString("invalid-characters-warning-title") == null) - { - getCustomMessagesConfig().set("invalid-characters-warning-title", "Invalid Username"); - - if(!customMessagesCreated) - { - getPlugin().getLogger().warning("Entry 'invalid-characters-warning-title' was not found, adding it to the config..."); - } - } - - if(getCustomMessagesConfig().getString("invalid-characters-warning") == null) - { - getCustomMessagesConfig().set("invalid-characters-warning", "{Sender}, the username you have specified contains invalid characters. **Only letters, numbers and underscores are allowed**."); - - if(!customMessagesCreated) - { - getPlugin().getLogger().warning("Entry 'invalid-characters-warning' was not found, adding it to the config..."); - } - } - - if(getCustomMessagesConfig().getString("invalid-length-warning-title") == null) - { - getCustomMessagesConfig().set("invalid-length-warning-title", "Invalid Username"); - - if(!customMessagesCreated) - { - getPlugin().getLogger().warning("Entry 'invalid-length-warning-title' was not found, adding it to the config..."); - } - } - - if(getCustomMessagesConfig().getString("invalid-length-warning") == null) - { - getCustomMessagesConfig().set("invalid-length-warning", "{Sender}, the username you have specified either contains too few or too many characters. **Usernames can only consist of 3-16 characters**."); - - if(!customMessagesCreated) - { - getPlugin().getLogger().warning("Entry 'invalid-length-warning' was not found, adding it to the config..."); - } - } - - if(getCustomMessagesConfig().getString("already-on-whitelist-title") == null) - { - getCustomMessagesConfig().set("already-on-whitelist-title", "User already on the whitelist"); - - if(!customMessagesCreated) - { - getPlugin().getLogger().warning("Entry 'already-on-whitelist-title' was not found, adding it to the config..."); - } - } - - if(getCustomMessagesConfig().getString("already-on-whitelist") == null) - { - getCustomMessagesConfig().set("already-on-whitelist", "{Sender}, cannot add user as `{MinecraftUsername}` is already on the whitelist!"); - - if(!customMessagesCreated) - { - getPlugin().getLogger().warning("Entry 'already-on-whitelist' was not found, adding it to the config..."); - } - } - - if(getCustomMessagesConfig().getString("user-was-removed-title") == null) - { - getCustomMessagesConfig().set("user-was-removed-title", "This user was previously removed by a staff member"); - - if(!customMessagesCreated) - { - getPlugin().getLogger().warning("Entry 'user-was-removed-title' was not found, adding it to the config..."); - } - } - - if(getCustomMessagesConfig().getString("user-was-removed") == null) - { - getCustomMessagesConfig().set("user-was-removed", "{Sender}, this user was previously removed by a staff member ({StaffMember}). Please ask a user with higher permissions to add this user."); - - if(!customMessagesCreated) - { - getPlugin().getLogger().warning("Entry 'user-was-removed' was not found, adding it to the config..."); - } - } - - if(getCustomMessagesConfig().getString("whitelists-remaining-title") == null) - { - getCustomMessagesConfig().set("whitelists-remaining-title", "Whitelists Remaining"); - - if(!customMessagesCreated) - { - getPlugin().getLogger().warning("Entry 'whitelists-remaining-title' was not found, adding it to the config..."); - } - } - - if(getCustomMessagesConfig().getString("whitelists-remaining") == null) - { - getCustomMessagesConfig().set("whitelists-remaining", "You have **{RemainingWhitelists} out of {MaxWhitelistAmount}** whitelists remaining."); - - if(!customMessagesCreated) - { - getPlugin().getLogger().warning("Entry 'whitelists-remaining' was not found, adding it to the config..."); - } - } - - if(getCustomMessagesConfig().getString("whitelist-success-title") == null) - { - getCustomMessagesConfig().set("whitelist-success-title", "{MinecraftUsername} is now whitelisted!"); - - if(!customMessagesCreated) - { - getPlugin().getLogger().warning("Entry 'whitelist-success-title' was not found, adding it to the config..."); - } - } - - if(getCustomMessagesConfig().getString("whitelist-success") == null) - { - getCustomMessagesConfig().set("whitelist-success", "{Sender} has added `{MinecraftUsername}` to the whitelist."); - - if(!customMessagesCreated) - { - getPlugin().getLogger().warning("Entry 'whitelist-success' was not found, adding it to the config..."); - } - } - - if(getCustomMessagesConfig().getString("whitelist-failure-title") == null) - { - getCustomMessagesConfig().set("whitelist-failure-title", "Failed to whitelist"); - - if(!customMessagesCreated) - { - getPlugin().getLogger().warning("Entry 'whitelist-failure-title' was not found, adding it to the config..."); - } - } - - if(getCustomMessagesConfig().getString("whitelist-failure") == null) - { - getCustomMessagesConfig().set("whitelist-failure", "{Sender}, failed to add `{MinecraftUsername}` to the whitelist. This is most likely due to an invalid Minecraft username."); - - if(!customMessagesCreated) - { - getPlugin().getLogger().warning("Entry 'whitelist-failure' was not found, adding it to the config..."); - } - } - - if(getCustomMessagesConfig().getString("user-not-on-whitelist-title") == null) - { - getCustomMessagesConfig().set("user-not-on-whitelist-title", "This user is not on the whitelist"); - - if(!customMessagesCreated) - { - getPlugin().getLogger().warning("Entry 'user-not-on-whitelist-title' was not found, adding it to the config..."); - } - } - - if(getCustomMessagesConfig().getString("user-not-on-whitelist") == null) - { - getCustomMessagesConfig().set("user-not-on-whitelist", "{Sender}, cannot remove user as `{MinecraftUsername}` is not on the whitelist!"); - - if(!customMessagesCreated) - { - getPlugin().getLogger().warning("Entry 'user-not-on-whitelist' was not found, adding it to the config..."); - } - } - - if(getCustomMessagesConfig().getString("remove-success-title") == null) - { - getCustomMessagesConfig().set("remove-success-title", "{MinecraftUsername} has been removed"); - - if(!customMessagesCreated) - { - getPlugin().getLogger().warning("Entry 'remove-success-title' was not found, adding it to the config..."); - } - } - - if(getCustomMessagesConfig().getString("remove-success") == null) - { - getCustomMessagesConfig().set("remove-success", "{Sender} has removed {MinecraftUsername} from the whitelist"); - - if(!customMessagesCreated) - { - getPlugin().getLogger().warning("Entry 'remove-success' was not found, adding it to the config..."); - } - } - - try - { - getCustomMessagesConfig().save((customMessagesFile.getPath())); - } - catch(IOException e) - { - e.printStackTrace(); - } - } - - CustomPrefixConfig.ConfigSetup(); - if(userListCreated) { try diff --git a/src/main/java/uk/co/angrybee/joe/Events/OnWhitelistEvents.java b/src/main/java/uk/co/angrybee/joe/Events/OnWhitelistEvents.java new file mode 100644 index 0000000..c898313 --- /dev/null +++ b/src/main/java/uk/co/angrybee/joe/Events/OnWhitelistEvents.java @@ -0,0 +1,65 @@ +package uk.co.angrybee.joe.Events; + +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import uk.co.angrybee.joe.DiscordWhitelister; + +// Checks for whitelist removes in-game, so player's cannot use the bot to add them back without an admin/staff member with higher permissions +public class OnWhitelistEvents implements Listener +{ + private enum CommandContext { VANILLA_ADD, EASYWL_ADD, VANILLA_REMOVE, EASYWL_REMOVE } + + @EventHandler + public void PlayerCommandPreprocessEvent(Player commandCaller, String message) + { + // Initial check + CommandContext commandContext; + + if(message.startsWith("!whitelist add")) + commandContext = CommandContext.VANILLA_ADD; + else if(message.startsWith("!easywl add")) + commandContext = CommandContext.EASYWL_ADD; + else if(message.startsWith("!whitelist remove")) + commandContext = CommandContext.VANILLA_REMOVE; + else if(message.startsWith("!easywl remove")) + commandContext = CommandContext.EASYWL_REMOVE; + else + return; + + // Don't proceed to run if the player does not have permission + if(commandContext.equals(CommandContext.VANILLA_ADD) && !commandCaller.hasPermission("bukkit.command.whitelist.add")) + return; + else if(commandContext.equals(CommandContext.EASYWL_ADD) && !commandCaller.hasPermission("easywhitelist.admin")) + return; + else if(commandContext.equals(CommandContext.VANILLA_REMOVE) && !commandCaller.hasPermission("bukkit.command.whitelist.remove")) + return; + else if(commandContext.equals(CommandContext.EASYWL_REMOVE) && !commandCaller.hasPermission("easywhitelist.admin")) + return; + + // Determine what command to check + + // Check for adds to remove player's off the removed list (if they are on it) + if(commandContext.equals(CommandContext.VANILLA_ADD) && !DiscordWhitelister.useEasyWhitelist) + { + // TODO + // Check removed-list.yml, remove username from there if it exists + // Check in-game removed list when created and remove from there if it exists + // Log removal of name from list if it existed + } + else if(commandContext.equals(CommandContext.EASYWL_ADD) && DiscordWhitelister.useEasyWhitelist) + { + + } + else if(commandContext.equals(CommandContext.VANILLA_REMOVE) && !DiscordWhitelister.useEasyWhitelist) + { + // TODO + // Check if the player is in the whitelist as this runs before the command is executed + // Add player to in-game removed list and the players' name that removed it + } + else if(commandContext.equals(CommandContext.EASYWL_REMOVE) && DiscordWhitelister.useEasyWhitelist) + { + + } + } +} diff --git a/src/main/java/uk/co/angrybee/joe/Stores/RemovedPlayersStore.java b/src/main/java/uk/co/angrybee/joe/Stores/RemovedPlayersStore.java new file mode 100644 index 0000000..367474e --- /dev/null +++ b/src/main/java/uk/co/angrybee/joe/Stores/RemovedPlayersStore.java @@ -0,0 +1,6 @@ +package uk.co.angrybee.joe.Stores; + +// Stores removed players that were removed in-game +public class RemovedPlayersStore +{ +} diff --git a/src/main/java/uk/co/angrybee/joe/VersionInfo.java b/src/main/java/uk/co/angrybee/joe/VersionInfo.java index 4391e42..9e71fff 100644 --- a/src/main/java/uk/co/angrybee/joe/VersionInfo.java +++ b/src/main/java/uk/co/angrybee/joe/VersionInfo.java @@ -10,5 +10,5 @@ public class VersionInfo return "v." + getVersion(); } - private static String version = "1.3.6"; + private static String version = "1.3.7"; }