From cc8401e841b7f3bb0fff0fb872fc9c8a300f3b72 Mon Sep 17 00:00:00 2001 From: Gabriele C Date: Tue, 24 Nov 2015 18:50:56 +0100 Subject: [PATCH] Move the antibot management --- src/main/java/fr/xephi/authme/AntiBot.java | 101 ++++++++++++++++++ src/main/java/fr/xephi/authme/AuthMe.java | 46 +++----- .../authme/SwitchAntiBotCommand.java | 21 ++-- .../authme/listener/AuthMePlayerListener.java | 45 +------- 4 files changed, 127 insertions(+), 86 deletions(-) create mode 100644 src/main/java/fr/xephi/authme/AntiBot.java diff --git a/src/main/java/fr/xephi/authme/AntiBot.java b/src/main/java/fr/xephi/authme/AntiBot.java new file mode 100644 index 000000000..381acfac2 --- /dev/null +++ b/src/main/java/fr/xephi/authme/AntiBot.java @@ -0,0 +1,101 @@ +package fr.xephi.authme; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +import fr.xephi.authme.settings.Messages; +import fr.xephi.authme.settings.Settings; + +/** + * The AntiBot Service Management class. + */ +public class AntiBot { + + public enum AntiBotStatus { + ARMED, + DISARMED, + DELAYED, + ACTIVATED + } + + private static AntiBotStatus antiBotStatus = AntiBotStatus.DISARMED; + private static final AuthMe plugin = AuthMe.getInstance(); + private static final Messages messages = plugin.getMessages(); + private static final List antibotPlayers = new ArrayList<>(); + + public static void setupAntiBotService() { + if (!Settings.enableAntiBot) { + return; + } + Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { + @Override + public void run() { + antiBotStatus = AntiBotStatus.ARMED; + } + }, 2400); + } + + public static void switchAntiBotStatus(boolean activated) { + if(antiBotStatus == AntiBotStatus.DISARMED || antiBotStatus == AntiBotStatus.DELAYED) { + return; + } + if(activated) { + antiBotStatus = AntiBotStatus.ACTIVATED; + } else { + antiBotStatus = AntiBotStatus.ARMED; + } + } + + public static AntiBotStatus getAntiBotStatus() { + return antiBotStatus; + } + + public static void activateAntiBot() { + antiBotStatus = AntiBotStatus.ACTIVATED; + for (String s : messages.send("antibot_auto_enabled")) { + Bukkit.broadcastMessage(s); + } + + Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { + @Override + public void run() { + if (antiBotStatus == AntiBotStatus.ACTIVATED) { + antiBotStatus = AntiBotStatus.ARMED; + antibotPlayers.clear(); + for (String s : messages.send("antibot_auto_disabled")) + Bukkit.broadcastMessage(s.replace("%m", "" + Settings.antiBotDuration)); + } + } + }, Settings.antiBotDuration * 1200); + } + + /** + * Method checkAntiBotMod. + * + * @param player Player + */ + public static void checkAntiBot(final Player player) { + if (antiBotStatus == AntiBotStatus.ACTIVATED || antiBotStatus == AntiBotStatus.DISARMED) { + return; + } + if (plugin.getPermissionsManager().hasPermission(player, "authme.bypassantibot")) { + return; + } + + antibotPlayers.add(player.getName().toLowerCase()); + if (antibotPlayers.size() > Settings.antiBotSensibility) { + activateAntiBot(); + return; + } + Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { + @Override + public void run() { + antibotPlayers.remove(player.getName().toLowerCase()); + } + }, 300); + } + +} diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 2fd5d9802..d5aa5e2a6 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -24,6 +24,7 @@ import fr.xephi.authme.settings.Messages; import fr.xephi.authme.settings.OtherAccounts; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Spawn; +import fr.xephi.authme.AntiBot; import fr.xephi.authme.util.GeoLiteAPI; import fr.xephi.authme.util.Utils; import net.minelink.ctplus.CombatTagPlus; @@ -82,30 +83,30 @@ public class AuthMe extends JavaPlugin { public DataSource database; public OtherAccounts otherAccounts; public Location essentialsSpawn; - // Hooks TODO: move into modules + + // Hooks TODO: Move into modules public Essentials ess; public MultiverseCore multiverse; public CombatTagPlus combatTagPlus; public AuthMeInventoryPacketAdapter inventoryProtector; - // Random data maps and stuff - // TODO: Create Manager for this + + // Data maps and stuff + // TODO: Move into a manager public final ConcurrentHashMap sessions = new ConcurrentHashMap<>(); public final ConcurrentHashMap captcha = new ConcurrentHashMap<>(); public final ConcurrentHashMap cap = new ConcurrentHashMap<>(); public final ConcurrentHashMap realIp = new ConcurrentHashMap<>(); - // AntiBot Status - // TODO: Create Manager for this - public boolean antiBotMod = false; - public boolean delayedAntiBot = true; + + // If cache is enabled, prevent any connection before the players data caching is completed. + // TODO: Move somewhere + private boolean canConnect = true; + private CommandHandler commandHandler = null; private PermissionsManager permsMan = null; private Settings settings; private Messages messages; private JsonCache playerBackup; private ModuleManager moduleManager; - // If cache is enabled, prevent any connection before the players data caching is completed. - // TODO: Move somewhere - private boolean canConnect = true; /** * Returns the plugin's instance. @@ -229,7 +230,7 @@ public class AuthMe extends JavaPlugin { setupConsoleFilter(); // AntiBot delay - setupAntiBotDelay(); + AntiBot.setupAntiBotService(); // Download and load GeoIp.dat file if absent GeoLiteAPI.isDataAvailable(); @@ -443,20 +444,6 @@ public class AuthMe extends JavaPlugin { return false; } - /** - * Set up the AntiBot delay. - */ - private void setupAntiBotDelay() { - if (Settings.enableAntiBot) { - Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() { - @Override - public void run() { - delayedAntiBot = false; - } - }, 2400); - } - } - /** * Set up the console filter. */ @@ -836,15 +823,6 @@ public class AuthMe extends JavaPlugin { return player.getWorld().getSpawnLocation(); } - public void switchAntiBotMod(boolean mode) { - this.antiBotMod = mode; - Settings.switchAntiBotMod(mode); - } - - public boolean getAntiBotModMode() { - return this.antiBotMod; - } - private void recallEmail() { if (!Settings.recallEmail) return; diff --git a/src/main/java/fr/xephi/authme/command/executable/authme/SwitchAntiBotCommand.java b/src/main/java/fr/xephi/authme/command/executable/authme/SwitchAntiBotCommand.java index bbbf89990..b8b836050 100644 --- a/src/main/java/fr/xephi/authme/command/executable/authme/SwitchAntiBotCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/authme/SwitchAntiBotCommand.java @@ -1,6 +1,6 @@ package fr.xephi.authme.command.executable.authme; -import fr.xephi.authme.AuthMe; +import fr.xephi.authme.AntiBot; import fr.xephi.authme.command.CommandParts; import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.command.help.HelpProvider; @@ -22,25 +22,26 @@ public class SwitchAntiBotCommand extends ExecutableCommand { */ @Override public boolean executeCommand(final CommandSender sender, CommandParts commandReference, CommandParts commandArguments) { - // AuthMe plugin instance - final AuthMe plugin = AuthMe.getInstance(); - // Get the new state - String newState = plugin.getAntiBotModMode() ? "OFF" : "ON"; - if (commandArguments.getCount() >= 1) + String newState = null; + if (commandArguments.getCount() == 1) { newState = commandArguments.get(0); + } else if(commandArguments.getCount() == 0) { + sender.sendMessage("[AuthMe] AntiBot status: " + AntiBot.getAntiBotStatus().name()); + return true; + } // Enable the mod if (newState.equalsIgnoreCase("ON")) { - plugin.switchAntiBotMod(true); - sender.sendMessage("[AuthMe] AntiBotMod enabled"); + AntiBot.switchAntiBotStatus(true); + sender.sendMessage("[AuthMe] AntiBot Manual Ovverride: enabled!"); return true; } // Disable the mod if (newState.equalsIgnoreCase("OFF")) { - plugin.switchAntiBotMod(false); - sender.sendMessage("[AuthMe] AntiBotMod disabled"); + AntiBot.switchAntiBotStatus(false); + sender.sendMessage("[AuthMe] AntiBotMod Manual Ovverride: disabled!"); return true; } diff --git a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java index d5ddd7479..4a5e705a5 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java @@ -2,6 +2,8 @@ package fr.xephi.authme.listener; import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteStreams; + +import fr.xephi.authme.AntiBot; import fr.xephi.authme.AuthMe; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.cache.auth.PlayerAuth; @@ -27,9 +29,7 @@ import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryOpenEvent; import org.bukkit.event.player.*; -import java.util.ArrayList; import java.util.Arrays; -import java.util.List; import java.util.concurrent.ConcurrentHashMap; /** @@ -41,7 +41,6 @@ public class AuthMePlayerListener implements Listener { public static final ConcurrentHashMap causeByAuthMe = new ConcurrentHashMap<>(); public final AuthMe plugin; private final Messages m = Messages.getInstance(); - private final List antibot = new ArrayList<>(); /** * Constructor for AuthMePlayerListener. @@ -206,44 +205,6 @@ public class AuthMePlayerListener implements Listener { } } - /** - * Method checkAntiBotMod. - * - * @param player Player - */ - private void checkAntiBotMod(final Player player) { - if (plugin.delayedAntiBot || plugin.antiBotMod) - return; - if (plugin.getPermissionsManager().hasPermission(player, "authme.bypassantibot")) - return; - if (antibot.size() > Settings.antiBotSensibility) { - plugin.switchAntiBotMod(true); - for (String s : m.send("antibot_auto_enabled")) - Bukkit.broadcastMessage(s); - Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { - - @Override - public void run() { - if (plugin.antiBotMod) { - plugin.switchAntiBotMod(false); - antibot.clear(); - for (String s : m.send("antibot_auto_disabled")) - Bukkit.broadcastMessage(s.replace("%m", "" + Settings.antiBotDuration)); - } - } - }, Settings.antiBotDuration * 1200); - return; - } - antibot.add(player.getName().toLowerCase()); - Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { - - @Override - public void run() { - antibot.remove(player.getName().toLowerCase()); - } - }, 300); - } - /** * Method onPlayerJoin. * @@ -402,7 +363,7 @@ public class AuthMePlayerListener implements Listener { return; } - checkAntiBotMod(player); + AntiBot.checkAntiBot(player); if (Settings.bungee) { ByteArrayDataOutput out = ByteStreams.newDataOutput();