From e4d46ae9b9a5359e1c53ead08d05c331308b9b61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Vis=C3=A9e?= Date: Sun, 1 Nov 2015 18:30:29 +0100 Subject: [PATCH] Implemented the unregister command --- .../xephi/authme/command/CommandManager.java | 15 +++ .../executable/authme/UnregisterCommand.java | 98 +++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 src/main/java/fr/xephi/authme/command/executable/authme/UnregisterCommand.java diff --git a/src/main/java/fr/xephi/authme/command/CommandManager.java b/src/main/java/fr/xephi/authme/command/CommandManager.java index aaae7e6a1..b03e9e5f9 100644 --- a/src/main/java/fr/xephi/authme/command/CommandManager.java +++ b/src/main/java/fr/xephi/authme/command/CommandManager.java @@ -138,6 +138,21 @@ public class CommandManager { registerCommand.addArgument(new CommandArgumentDescription("player", "Player name", false)); registerCommand.addArgument(new CommandArgumentDescription("password", "Password", false)); + // Register the unregister command + CommandDescription unregisterCommand = new CommandDescription( + new UnregisterCommand(), + new ArrayList() {{ + add("unregister"); + add("unreg"); + add("delete"); + add("del"); + }}, + "Unregister a player", + "Unregister the specified player.", + authMeCommand); + unregisterCommand.setCommandPermissions("authme.admin.unregister", CommandPermissions.DefaultPermission.OP_ONLY); + unregisterCommand.addArgument(new CommandArgumentDescription("player", "Player name", false)); + // Register the changepassword command CommandDescription changePasswordCommand = new CommandDescription( new RegisterCommand(), diff --git a/src/main/java/fr/xephi/authme/command/executable/authme/UnregisterCommand.java b/src/main/java/fr/xephi/authme/command/executable/authme/UnregisterCommand.java new file mode 100644 index 000000000..e04b88896 --- /dev/null +++ b/src/main/java/fr/xephi/authme/command/executable/authme/UnregisterCommand.java @@ -0,0 +1,98 @@ +package fr.xephi.authme.command.executable.authme; + +import fr.xephi.authme.AuthMe; +import fr.xephi.authme.ConsoleLogger; +import fr.xephi.authme.Utils; +import fr.xephi.authme.cache.auth.PlayerCache; +import fr.xephi.authme.cache.limbo.LimboCache; +import fr.xephi.authme.command.CommandParts; +import fr.xephi.authme.command.ExecutableCommand; +import fr.xephi.authme.events.SpawnTeleportEvent; +import fr.xephi.authme.settings.Messages; +import fr.xephi.authme.settings.Settings; +import fr.xephi.authme.task.MessageTask; +import fr.xephi.authme.task.TimeoutTask; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; +import org.bukkit.scheduler.BukkitScheduler; +import org.bukkit.scheduler.BukkitTask; + +public class UnregisterCommand extends ExecutableCommand { + + /** + * Execute the command. + * + * @param sender The command sender. + * @param commandReference The command reference. + * @param commandArguments The command arguments. + * + * @return True if the command was executed successfully, false otherwise. + */ + @Override + public boolean executeCommand(final CommandSender sender, CommandParts commandReference, CommandParts commandArguments) { + // AuthMe plugin instance + final AuthMe plugin = AuthMe.getInstance(); + + // Messages instance + final Messages m = Messages.getInstance(); + + // Get the player name + String playerName = commandArguments.get(0); + String playerNameLowerCase = playerName.toLowerCase(); + + // Make sure the user is valid + if (!plugin.database.isAuthAvailable(playerNameLowerCase)) { + m.send(sender, "user_unknown"); + return true; + } + + // Remove the player + if (!plugin.database.removeAuth(playerNameLowerCase)) { + m.send(sender, "error"); + return true; + } + + // Unregister the player + @SuppressWarnings("deprecation") + Player target = Bukkit.getPlayer(playerNameLowerCase); + PlayerCache.getInstance().removePlayer(playerNameLowerCase); + Utils.setGroup(target, Utils.GroupType.UNREGISTERED); + if (target != null) { + if (target.isOnline()) { + if (Settings.isTeleportToSpawnEnabled && !Settings.noTeleport) { + Location spawn = plugin.getSpawnLocation(target); + SpawnTeleportEvent tpEvent = new SpawnTeleportEvent(target, target.getLocation(), spawn, false); + plugin.getServer().getPluginManager().callEvent(tpEvent); + if (!tpEvent.isCancelled()) { + target.teleport(tpEvent.getTo()); + } + } + LimboCache.getInstance().addLimboPlayer(target); + int delay = Settings.getRegistrationTimeout * 20; + int interval = Settings.getWarnMessageInterval; + BukkitScheduler scheduler = sender.getServer().getScheduler(); + if (delay != 0) { + BukkitTask id = scheduler.runTaskLaterAsynchronously(plugin, new TimeoutTask(plugin, playerNameLowerCase, target), delay); + LimboCache.getInstance().getLimboPlayer(playerNameLowerCase).setTimeoutTaskId(id); + } + LimboCache.getInstance().getLimboPlayer(playerNameLowerCase).setMessageTaskId(scheduler.runTaskAsynchronously(plugin, new MessageTask(plugin, playerNameLowerCase, m.send("reg_msg"), interval))); + if (Settings.applyBlindEffect) + target.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, Settings.getRegistrationTimeout * 20, 2)); + if (!Settings.isMovementAllowed && Settings.isRemoveSpeedEnabled) { + target.setWalkSpeed(0.0f); + target.setFlySpeed(0.0f); + } + m.send(target, "unregistered"); + } + } + + // Show a status message + m.send(sender, "unregistered"); + ConsoleLogger.info(playerName + " unregistered"); + return true; + } +}