diff --git a/pom.xml b/pom.xml index 5183140..19e7a97 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ uk.co.angrybee.joe discord-whitelister - 1.4.8 + 1.4.9 discord-whitelister https://github.com/JoeShimell/DiscordWhitelisterSpigot @@ -49,7 +49,7 @@ org.spigotmc spigot-api - 1.17-R0.1-SNAPSHOT + 1.17.1-R0.1-SNAPSHOT provided diff --git a/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java b/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java index a138955..66131a5 100755 --- a/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java +++ b/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java @@ -25,12 +25,12 @@ import java.util.List; import java.util.logging.Logger; import java.util.stream.Stream; -public class DiscordWhitelister extends JavaPlugin -{ +public class DiscordWhitelister extends JavaPlugin { public static String botToken; private static boolean configCreated = false; + public static boolean initialized = false; public static boolean useCustomMessages = false; public static boolean useIdForRoles = false; public static boolean useCustomPrefixes = false; @@ -70,8 +70,7 @@ public class DiscordWhitelister extends JavaPlugin @Override - public void onEnable() - { + public void onEnable() { thisPlugin = this; thisServer = thisPlugin.getServer(); pluginLogger = thisPlugin.getLogger(); @@ -84,19 +83,20 @@ public class DiscordWhitelister extends JavaPlugin int initSuccess = InitBot(true); - if(initSuccess == 0) - { + if (initSuccess == 0) { pluginLogger.info("Successfully initialized Discord client"); - } - else if(initSuccess == 1) - { + initialized = true; + } else if (initSuccess == 1) { pluginLogger.severe("Discord Client failed to initialize, please check if your config file is valid"); + initialized = false; return; } // Check for leavers if enabled - DiscordClient.ServerLeaveStartupCheck(); - DiscordClient.RequiredRoleStartupCheck(); + if (botEnabled) { + DiscordClient.ServerLeaveStartupCheck(); + DiscordClient.RequiredRoleStartupCheck(); + } this.getCommand("discordwhitelister").setExecutor(new CommandStatus()); this.getCommand("discordwhitelisterabout").setExecutor(new CommandAbout()); @@ -104,33 +104,46 @@ public class DiscordWhitelister extends JavaPlugin } @Override - public void onDisable() - { - DiscordClient.javaDiscordAPI.shutdownNow(); + public void onDisable() { + if (initialized ) { + DiscordClient.javaDiscordAPI.shutdownNow(); + } } - public static JavaPlugin getPlugin() - { + public static JavaPlugin getPlugin() { return thisPlugin; } - public static FileConfiguration getCustomMessagesConfig() { return customMessagesConfig.getFileConfiguration(); } + public static FileConfiguration getCustomMessagesConfig() { + return customMessagesConfig.getFileConfiguration(); + } - public static Logger getPluginLogger() { return pluginLogger; } + public static Logger getPluginLogger() { + return pluginLogger; + } - public static boolean getUseCustomPrefixes() { return useCustomPrefixes; } + public static boolean getUseCustomPrefixes() { + return useCustomPrefixes; + } - public static void addVanishedPlayer() { vanishedPlayersCount++; } + public static void addVanishedPlayer() { + vanishedPlayersCount++; + } - public static void removeVanishedPlayer() { vanishedPlayersCount--; } + public static void removeVanishedPlayer() { + vanishedPlayersCount--; + } - public static int getOnlineUsers() { return thisPlugin.getServer().getOnlinePlayers().size() - vanishedPlayersCount; } + public static int getOnlineUsers() { + return thisPlugin.getServer().getOnlinePlayers().size() - vanishedPlayersCount; + } - public static int getMaximumAllowedPlayers() { return thisPlugin.getServer().getMaxPlayers(); } + public static int getMaximumAllowedPlayers() { + return thisPlugin.getServer().getMaxPlayers(); + } - public static int InitBot(boolean firstInit) - { - if(firstInit) + public static int InitBot(boolean firstInit) { + if (firstInit) vanishedPlayersCount = 0; ConfigSetup(); @@ -146,59 +159,47 @@ public class DiscordWhitelister extends JavaPlugin removeMessageWaitTime = mainConfig.getFileConfiguration().getInt("seconds-to-remove-message-from-whitelist-channel"); useOnWhitelistCommands = mainConfig.getFileConfiguration().getBoolean("use-on-whitelist-commands"); - // Check for LuckPerms first - if(mainConfig.getFileConfiguration().getBoolean("assign-perms-with-luck-perms")) - { - if(getPlugin().getServer().getPluginManager().getPlugin("LuckPerms") != null) - { - useLuckPerms = true; - getPluginLogger().info("LuckPerms found!"); - } - else - { - getPluginLogger().warning("LuckPerms was not found but is enabled in the config. Doing nothing..."); - useLuckPerms = false; - } - } - if(mainConfig.getFileConfiguration().getBoolean("assign-perms-with-ultra-perms")) - { - if(getPlugin().getServer().getPluginManager().getPlugin("UltraPermissions") != null) - { - useUltraPerms = true; - getPluginLogger().info("Ultra Permissions found!"); - } - else - { - getPluginLogger().warning("Ultra Permissions was not found but is enabled in the config. Doing nothing..."); - useUltraPerms = false; - } - } - - // TODO: remove in favour of split versions - DiscordClient.customWhitelistAddPrefix = customPrefixConfig.getFileConfiguration().getString("whitelist-add-prefix").toLowerCase(); - DiscordClient.customWhitelistRemovePrefix = customPrefixConfig.getFileConfiguration().getString("whitelist-remove-prefix").toLowerCase(); - DiscordClient.customClearNamePrefix = customPrefixConfig.getFileConfiguration().getString("clear-name-prefix").toLowerCase(); - DiscordClient.customLimitedWhitelistClearPrefix = customPrefixConfig.getFileConfiguration().getString("limited-whitelist-clear-prefix").toLowerCase(); - DiscordClient.customClearBanPrefix = customPrefixConfig.getFileConfiguration().getString("clear-ban-prefix").toLowerCase(); - - // Split versions - DiscordClient.customWhitelistAddPrefixSplit = customPrefixConfig.getFileConfiguration().getString("whitelist-add-prefix").toLowerCase().trim().split(" "); - DiscordClient.customWhitelistRemovePrefixSplit = customPrefixConfig.getFileConfiguration().getString("whitelist-remove-prefix").toLowerCase().trim().split(" "); - DiscordClient.customClearNamePrefixSplit = customPrefixConfig.getFileConfiguration().getString("clear-name-prefix").toLowerCase().trim().split(" "); - DiscordClient.customLimitedWhitelistClearPrefixSplit = customPrefixConfig.getFileConfiguration().getString("limited-whitelist-clear-prefix").toLowerCase().trim().split(" "); - DiscordClient.customClearBanPrefixSplit = customPrefixConfig.getFileConfiguration().getString("clear-ban-prefix").toLowerCase().trim().split(" "); - DiscordClient.customWhoIsPrefix = customPrefixConfig.getFileConfiguration().getString("whitelist-whois-prefix").toLowerCase().trim().split(" "); - - if(!botEnabled) - { + if (!botEnabled) { pluginLogger.info("Bot is disabled as per the config, doing nothing"); - } - else if(configCreated) - { - pluginLogger.info("Config newly created, please paste your bot token into the config file, doing nothing until next server start"); - } - else - { + } else if (configCreated || botToken.equals(MainConfig.default_token)) { + pluginLogger.warning("Config newly created. Please paste your bot token into the config file, doing nothing until next server start"); + } else { + // Check for LuckPerms first + if (mainConfig.getFileConfiguration().getBoolean("assign-perms-with-luck-perms")) { + if (getPlugin().getServer().getPluginManager().getPlugin("LuckPerms") != null) { + useLuckPerms = true; + getPluginLogger().info("LuckPerms found!"); + } else { + getPluginLogger().warning("LuckPerms was not found but is enabled in the config. Doing nothing..."); + useLuckPerms = false; + } + } + if (mainConfig.getFileConfiguration().getBoolean("assign-perms-with-ultra-perms")) { + if (getPlugin().getServer().getPluginManager().getPlugin("UltraPermissions") != null) { + useUltraPerms = true; + getPluginLogger().info("Ultra Permissions found!"); + } else { + getPluginLogger().warning("Ultra Permissions was not found but is enabled in the config. Doing nothing..."); + useUltraPerms = false; + } + } + + // TODO: remove in favour of split versions + DiscordClient.customWhitelistAddPrefix = customPrefixConfig.getFileConfiguration().getString("whitelist-add-prefix").toLowerCase(); + DiscordClient.customWhitelistRemovePrefix = customPrefixConfig.getFileConfiguration().getString("whitelist-remove-prefix").toLowerCase(); + DiscordClient.customClearNamePrefix = customPrefixConfig.getFileConfiguration().getString("clear-name-prefix").toLowerCase(); + DiscordClient.customLimitedWhitelistClearPrefix = customPrefixConfig.getFileConfiguration().getString("limited-whitelist-clear-prefix").toLowerCase(); + DiscordClient.customClearBanPrefix = customPrefixConfig.getFileConfiguration().getString("clear-ban-prefix").toLowerCase(); + + // Split versions + DiscordClient.customWhitelistAddPrefixSplit = customPrefixConfig.getFileConfiguration().getString("whitelist-add-prefix").toLowerCase().trim().split(" "); + DiscordClient.customWhitelistRemovePrefixSplit = customPrefixConfig.getFileConfiguration().getString("whitelist-remove-prefix").toLowerCase().trim().split(" "); + DiscordClient.customClearNamePrefixSplit = customPrefixConfig.getFileConfiguration().getString("clear-name-prefix").toLowerCase().trim().split(" "); + DiscordClient.customLimitedWhitelistClearPrefixSplit = customPrefixConfig.getFileConfiguration().getString("limited-whitelist-clear-prefix").toLowerCase().trim().split(" "); + DiscordClient.customClearBanPrefixSplit = customPrefixConfig.getFileConfiguration().getString("clear-ban-prefix").toLowerCase().trim().split(" "); + DiscordClient.customWhoIsPrefix = customPrefixConfig.getFileConfiguration().getString("whitelist-whois-prefix").toLowerCase().trim().split(" "); + + pluginLogger.info("Initializing Discord client..."); // TODO: below role section could be moved to DiscordClient class @@ -206,40 +207,34 @@ public class DiscordWhitelister extends JavaPlugin // set add & remove roles DiscordClient.allowedToAddRemoveRoles = new String[mainConfig.getFileConfiguration().getList("add-remove-roles").size()]; - for(int roles = 0; roles < DiscordClient.allowedToAddRemoveRoles.length; ++roles) - { + for (int roles = 0; roles < DiscordClient.allowedToAddRemoveRoles.length; ++roles) { DiscordClient.allowedToAddRemoveRoles[roles] = mainConfig.getFileConfiguration().getList("add-remove-roles").get(roles).toString(); } // set add roles DiscordClient.allowedToAddRoles = new String[mainConfig.getFileConfiguration().getList("add-roles").size()]; - for(int roles = 0; roles < DiscordClient.allowedToAddRoles.length; ++roles) - { + for (int roles = 0; roles < DiscordClient.allowedToAddRoles.length; ++roles) { DiscordClient.allowedToAddRoles[roles] = mainConfig.getFileConfiguration().getList("add-roles").get(roles).toString(); } // set limited add roles DiscordClient.allowedToAddLimitedRoles = new String[mainConfig.getFileConfiguration().getList("limited-add-roles").size()]; - for(int roles = 0; roles < DiscordClient.allowedToAddLimitedRoles.length; ++roles) - { + for (int roles = 0; roles < DiscordClient.allowedToAddLimitedRoles.length; ++roles) { DiscordClient.allowedToAddLimitedRoles[roles] = mainConfig.getFileConfiguration().getList("limited-add-roles").get(roles).toString(); } // Get banned roles - if(useOnBanEvents) - { + if (useOnBanEvents) { List tempBannedRoles = mainConfig.getFileConfiguration().getStringList("banned-roles"); bannedRoles = new String[tempBannedRoles.size()]; - for(int i = 0; i < tempBannedRoles.size(); i++) - { + for (int i = 0; i < tempBannedRoles.size(); i++) { bannedRoles[i] = tempBannedRoles.get(i); } } // Allowed to clear name roles DiscordClient.allowedToClearNamesRoles = new String[mainConfig.getFileConfiguration().getStringList("clear-command-roles").size()]; - for(int roles = 0; roles < DiscordClient.allowedToClearNamesRoles.length; roles++) - { + for (int roles = 0; roles < DiscordClient.allowedToClearNamesRoles.length; roles++) { DiscordClient.allowedToClearNamesRoles[roles] = mainConfig.getFileConfiguration().getStringList("clear-command-roles").get(roles); } @@ -254,15 +249,14 @@ public class DiscordWhitelister extends JavaPlugin int initSuccess = DiscordClient.InitializeClient(botToken); - if(initSuccess == 1) + if (initSuccess == 1) return 1; // No need for an if here statement anymore as this code will not run if the client has not been initialized // Only attempt to set player count if the bot successfully initialized - if(mainConfig.getFileConfiguration().getBoolean("show-player-count")) - { - if(firstInit) { + if (mainConfig.getFileConfiguration().getBoolean("show-player-count")) { + if (firstInit) { // Register events if enabled thisServer.getPluginManager().registerEvents(new JoinLeaveEvents(), thisPlugin); //pluginLogger.info("Registered join/leave events!"); @@ -270,7 +264,7 @@ public class DiscordWhitelister extends JavaPlugin thisServer.getPluginManager().registerEvents(new SuperVanishEvents(), thisPlugin); //pluginLogger.info("Registered SuperVanish events!"); } - if(vanishNoPacketPlugin != null) { + if (vanishNoPacketPlugin != null) { thisServer.getPluginManager().registerEvents(new VanishNoPacketEvents(), thisPlugin); //pluginLogger.info("Registered VanishNoPacket events!"); } @@ -285,21 +279,20 @@ public class DiscordWhitelister extends JavaPlugin } // Register whitelist events if enabled - if(useInGameAddRemoves) + if (useInGameAddRemoves) thisServer.getPluginManager().registerEvents(new OnWhitelistEvents(), thisPlugin); // Register ban events if enabled - if(useOnBanEvents) + if (useOnBanEvents) thisServer.getPluginManager().registerEvents(new OnBanEvent(), thisPlugin); return 0; } - return 0; + return 1; } - public static void ConfigSetup() - { + public static void ConfigSetup() { mainConfig = new MainConfig(); customPrefixConfig = new CustomPrefixConfig(); customMessagesConfig = new CustomMessagesConfig(); diff --git a/src/main/java/uk/co/angrybee/joe/commands/minecraft/CommandReload.java b/src/main/java/uk/co/angrybee/joe/commands/minecraft/CommandReload.java index 87c3bee..e5b3e9b 100644 --- a/src/main/java/uk/co/angrybee/joe/commands/minecraft/CommandReload.java +++ b/src/main/java/uk/co/angrybee/joe/commands/minecraft/CommandReload.java @@ -39,8 +39,10 @@ public class CommandReload implements CommandExecutor { pluginLogger.info("Failed to re-initialize client"); sender.sendMessage("[DW] Failed to reload Discord client (Reason: Failed to re-initialize client)"); + DiscordWhitelister.initialized = false; return false; } + DiscordWhitelister.initialized = true; if(!DiscordWhitelister.botEnabled) { diff --git a/src/main/java/uk/co/angrybee/joe/configs/MainConfig.java b/src/main/java/uk/co/angrybee/joe/configs/MainConfig.java index d5163a5..12eaf11 100644 --- a/src/main/java/uk/co/angrybee/joe/configs/MainConfig.java +++ b/src/main/java/uk/co/angrybee/joe/configs/MainConfig.java @@ -9,8 +9,10 @@ import java.io.IOException; import java.util.Arrays; import java.util.Collections; + // discord-whitelister.yml public class MainConfig extends Config { + public static String default_token = "Discord bot token goes here, you can find it here: https://discordapp.com/developers/applications/"; public MainConfig() { fileName = "discord-whitelister.yml"; file = new File(DiscordWhitelister.getPlugin().getDataFolder(), fileName); @@ -32,7 +34,7 @@ public class MainConfig extends Config { if (!file.exists()) { CreateConfig(); - DiscordWhitelister.getPluginLogger().info("Configuration file created at: " + file.getPath() + + DiscordWhitelister.getPluginLogger().warning("Configuration file created at: " + file.getPath() + ", please edit this else the plugin will not work!"); } LoadConfigFile(); @@ -45,7 +47,7 @@ public class MainConfig extends Config { CheckEntry("bot-enabled", true); CheckEntry("discord-bot-token", - "Discord bot token goes here, you can find it here: https://discordapp.com/developers/applications/"); + default_token); CheckEntry("use-id-for-roles", false);