From 4c536a52043da84a606f2fc75fe8cd327beb6a1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Vis=C3=A9e?= Date: Sun, 1 Nov 2015 21:20:08 +0100 Subject: [PATCH] Implemented the email add command --- src/main/java/fr/xephi/authme/AuthMe.java | 2 +- .../xephi/authme/command/CommandManager.java | 50 ++++++++++- .../executable/email/AddEmailCommand.java | 90 +++++++++++++++++++ 3 files changed, 137 insertions(+), 5 deletions(-) create mode 100644 src/main/java/fr/xephi/authme/command/executable/email/AddEmailCommand.java diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 50743503..92db8257 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -352,7 +352,7 @@ public class AuthMe extends JavaPlugin { //getCommand("changepassword").setExecutor(new ChangePasswordCommand(this)); //getCommand("logout").setExecutor(new LogoutCommand(this)); //getCommand("unregister").setExecutor(new UnregisterCommand(this)); - getCommand("email").setExecutor(new EmailCommand(this)); + //getCommand("email").setExecutor(new EmailCommand(this)); getCommand("captcha").setExecutor(new CaptchaCommand(this)); getCommand("converter").setExecutor(new ConverterCommand(this)); diff --git a/src/main/java/fr/xephi/authme/command/CommandManager.java b/src/main/java/fr/xephi/authme/command/CommandManager.java index 864d098b..28b11b6d 100644 --- a/src/main/java/fr/xephi/authme/command/CommandManager.java +++ b/src/main/java/fr/xephi/authme/command/CommandManager.java @@ -3,6 +3,7 @@ package fr.xephi.authme.command; import fr.xephi.authme.command.executable.*; import fr.xephi.authme.command.executable.authme.*; import fr.xephi.authme.command.executable.changepassword.ChangePasswordCommand; +import fr.xephi.authme.command.executable.email.AddEmailCommand; import fr.xephi.authme.command.executable.login.LoginCommand; import fr.xephi.authme.command.executable.logout.LogoutCommand; @@ -31,7 +32,7 @@ public class CommandManager { */ @SuppressWarnings("SpellCheckingInspection") public void registerCommands() { - // Register the base Dungeon Maze command + // Register the base AuthMe Reloaded command CommandDescription authMeBaseCommand = new CommandDescription( new AuthMeCommand(), new ArrayList() {{ @@ -378,7 +379,7 @@ public class CommandManager { add("?"); }}, "View help", - "View detailed help pages about AuthMeReloaded register commands.", + "View detailed help pages about AuthMeReloaded logout commands.", logoutBaseCommand); logoutHelpCommand.addArgument(new CommandArgumentDescription("query", "The command or query to view help for.", true)); logoutHelpCommand.setMaximumArguments(false); @@ -437,7 +438,7 @@ public class CommandManager { add("?"); }}, "View help", - "View detailed help pages about AuthMeReloaded register commands.", + "View detailed help pages about AuthMeReloaded unregister commands.", unregisterBaseCommand); unregisterHelpCommand.addArgument(new CommandArgumentDescription("query", "The command or query to view help for.", true)); unregisterHelpCommand.setMaximumArguments(false); @@ -467,11 +468,51 @@ public class CommandManager { add("?"); }}, "View help", - "View detailed help pages about AuthMeReloaded register commands.", + "View detailed help pages about AuthMeReloaded change password commands.", changePasswordBaseCommand); changePasswordHelpCommand.addArgument(new CommandArgumentDescription("query", "The command or query to view help for.", true)); changePasswordHelpCommand.setMaximumArguments(false); + // Register the base Dungeon Maze command + CommandDescription emailBaseCommand = new CommandDescription( + new HelpCommand(), + new ArrayList() {{ + add("email"); + add("mail"); + }}, + "E-mail command", + "The AuthMe Reloaded E-mail command. The root for all E-mail commands.", null); + + // Register the help command + CommandDescription addEmailCommand = new CommandDescription( + new AddEmailCommand(), + new ArrayList() {{ + add("add"); + add("addemail"); + add("addmail"); + }}, + "Add E-mail", + "Add a new E-Mail address to your account.", + emailBaseCommand); + addEmailCommand.addArgument(new CommandArgumentDescription("email", "Email address", false)); + addEmailCommand.addArgument(new CommandArgumentDescription("verifyEmail", "Email address verification", false)); + + // Register the help command + CommandDescription emailHelpCommand = new CommandDescription( + new HelpCommand(), + new ArrayList() {{ + add("help"); + add("hlp"); + add("h"); + add("sos"); + add("?"); + }}, + "View help", + "View detailed help pages about AuthMeReloaded help commands.", + emailBaseCommand); + emailHelpCommand.addArgument(new CommandArgumentDescription("query", "The command or query to view help for.", true)); + emailHelpCommand.setMaximumArguments(false); + // Add the base commands to the commands array this.commandDescriptions.add(authMeBaseCommand); this.commandDescriptions.add(loginBaseCommand); @@ -479,6 +520,7 @@ public class CommandManager { this.commandDescriptions.add(registerBaseCommand); this.commandDescriptions.add(unregisterBaseCommand); this.commandDescriptions.add(changePasswordBaseCommand); + this.commandDescriptions.add(emailBaseCommand); } /** diff --git a/src/main/java/fr/xephi/authme/command/executable/email/AddEmailCommand.java b/src/main/java/fr/xephi/authme/command/executable/email/AddEmailCommand.java new file mode 100644 index 00000000..3fa8e0f2 --- /dev/null +++ b/src/main/java/fr/xephi/authme/command/executable/email/AddEmailCommand.java @@ -0,0 +1,90 @@ +package fr.xephi.authme.command.executable.email; + +import fr.xephi.authme.AuthMe; +import fr.xephi.authme.cache.auth.PlayerAuth; +import fr.xephi.authme.cache.auth.PlayerCache; +import fr.xephi.authme.command.CommandParts; +import fr.xephi.authme.command.ExecutableCommand; +import fr.xephi.authme.settings.Messages; +import fr.xephi.authme.settings.Settings; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class AddEmailCommand 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(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) { + // AuthMe plugin instance + final AuthMe plugin = AuthMe.getInstance(); + + // Messages instance + final Messages m = Messages.getInstance(); + + // Get the parameter values + String playerMail = commandArguments.get(0); + String playerMailVerify = commandArguments.get(1); + + // Make sure the current command executor is a player + if(!(sender instanceof Player)) { + return true; + } + + // Get the player instance and name + final Player player = (Player) sender; + final String playerName = player.getName(); + + // Command logic + if (Settings.getmaxRegPerEmail > 0) { + if (!plugin.authmePermissible(sender, "authme.allow2accounts") && plugin.database.getAllAuthsByEmail(playerMail).size() >= Settings.getmaxRegPerEmail) { + m.send(player, "max_reg"); + return true; + } + } + + if (playerMail.equals(playerMailVerify) && PlayerCache.getInstance().isAuthenticated(playerName)) { + PlayerAuth auth = PlayerCache.getInstance().getAuth(playerName); + + if (auth.getEmail() == null || (!auth.getEmail().equals("your@email.com") && !auth.getEmail().isEmpty())) { + m.send(player, "usage_email_change"); + return true; + } + + if (!Settings.isEmailCorrect(playerMail)) { + m.send(player, "email_invalid"); + return true; + } + + auth.setEmail(playerMail); + + if (!plugin.database.updateEmail(auth)) { + m.send(player, "error"); + return true; + } + + PlayerCache.getInstance().updatePlayer(auth); + m.send(player, "email_added"); + player.sendMessage(auth.getEmail()); + + } else if (PlayerCache.getInstance().isAuthenticated(playerName)) { + m.send(player, "email_confirm"); + + } else { + if (!plugin.database.isAuthAvailable(playerName)) { + m.send(player, "login_msg"); + } else { + m.send(player, "reg_email_msg"); + } + } + + return true; + } +}