Command refactor - remove unused fields, reduce variable "scope"

Minor refactorings in the command section for familiarization.

1. Removed suppressWarning("Deprecated") - the method is deprecated for a reason and we should be made aware of that.
2. Removed same javadoc on ExecutableCommand implementation that just had the same as the interface (this is just clutter; @Override signals that it's an implementing class and a developer can view the superclass javadoc)
3. In places where the AuthMe instance was retrieved at the top but used at the very bottom, moved it to the bottom to reduce its "scope"
(cherry picked from commit 45a50f3)
This commit is contained in:
ljacqu 2015-11-21 09:07:12 +01:00
parent 504106f835
commit 3934d67330
18 changed files with 36 additions and 213 deletions

View File

@ -39,8 +39,8 @@ public class CommandArgumentDescription {
/**
* Get the argument label.
*
* @return Argument label. */
* @return Argument label.
*/
public String getLabel() {
return this.label;
}
@ -57,8 +57,8 @@ public class CommandArgumentDescription {
/**
* Get the argument description.
*
* @return Argument description. */
* @return Argument description.
*/
public String getDescription() {
return description;
}
@ -75,8 +75,8 @@ public class CommandArgumentDescription {
/**
* Check whether the argument is optional.
*
* @return True if the argument is optional, false otherwise. */
* @return True if the argument is optional, false otherwise.
*/
public boolean isOptional() {
return optional;
}

View File

@ -3,6 +3,7 @@ package fr.xephi.authme.command;
import org.bukkit.command.CommandSender;
/**
* Base class for AuthMe commands that can be executed.
*/
public abstract class ExecutableCommand {
@ -13,7 +14,7 @@ public abstract class ExecutableCommand {
* @param commandReference The command reference.
* @param commandArguments The command arguments.
*
* @return True if the command was executed successfully, false otherwise. */
* @return True if the command was executed successfully, false otherwise.
*/
public abstract boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments);
}

View File

@ -10,31 +10,19 @@ import fr.xephi.authme.command.help.HelpProvider;
*/
public class HelpCommand 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) {
// Check whether quick help should be shown
boolean quickHelp = commandArguments.getCount() == 0;
// Set the proper command arguments for the quick help
if(quickHelp)
// Set the proper command arguments for the quick help and show it
if (quickHelp) {
commandArguments = new CommandParts(commandReference.get(0));
// Show the new help
if(quickHelp)
HelpProvider.showHelp(sender, commandReference, commandArguments, false, false, false, false, false, true);
else
} else {
HelpProvider.showHelp(sender, commandReference, commandArguments);
}
// Return the result
return true;
}
}

View File

@ -16,15 +16,6 @@ import fr.xephi.authme.settings.Messages;
*/
public class AccountsCommand 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

View File

@ -11,15 +11,6 @@ import fr.xephi.authme.command.ExecutableCommand;
*/
public class AuthMeCommand 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) {
// Show some version info

View File

@ -19,20 +19,8 @@ import fr.xephi.authme.settings.Settings;
*/
public class ChangePasswordCommand 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();
@ -62,6 +50,7 @@ public class ChangePasswordCommand extends ExecutableCommand {
}
// Set the password
final AuthMe plugin = AuthMe.getInstance();
final String playerNameLowerCase = playerName.toLowerCase();
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {

View File

@ -12,15 +12,6 @@ import fr.xephi.authme.settings.Spawn;
*/
public class FirstSpawnCommand 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) {
// Make sure the command executor is a player
@ -33,6 +24,7 @@ public class FirstSpawnCommand extends ExecutableCommand {
sender.sendMessage("[AuthMe] Please use that command in game");
}
} catch (NullPointerException ex) {
// TODO ljacqu 20151119: Catching NullPointerException is never a good idea. Find what can cause one instead
ConsoleLogger.showError(ex.getMessage());
}
return true;

View File

@ -12,15 +12,6 @@ import fr.xephi.authme.command.ExecutableCommand;
*/
public class ForceLoginCommand 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
@ -33,7 +24,6 @@ public class ForceLoginCommand extends ExecutableCommand {
// Command logic
try {
@SuppressWarnings("deprecation")
Player player = Bukkit.getPlayer(playerName);
if (player == null || !player.isOnline()) {
sender.sendMessage("Player needs to be online!");

View File

@ -23,18 +23,14 @@ public class GetEmailCommand extends ExecutableCommand {
* @return True if the command was executed successfully, false otherwise. */
@Override
public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
// AuthMe plugin instance
AuthMe plugin = AuthMe.getInstance();
// Messages instance
Messages m = Messages.getInstance();
// Get the player name
String playerName = sender.getName();
if(commandArguments.getCount() >= 1)
playerName = commandArguments.get(0);
// Get the authenticated user
AuthMe plugin = AuthMe.getInstance();
Messages m = Messages.getInstance();
PlayerAuth auth = plugin.database.getAuth(playerName.toLowerCase());
if (auth == null) {
m.send(sender, "unknown_user");

View File

@ -12,15 +12,6 @@ import fr.xephi.authme.command.ExecutableCommand;
*/
public class GetIpCommand 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
@ -31,7 +22,6 @@ public class GetIpCommand extends ExecutableCommand {
if(commandArguments.getCount() >= 1)
playerName = commandArguments.get(0);
@SuppressWarnings("deprecation")
Player player = Bukkit.getPlayer(playerName);
if (player == null) {
sender.sendMessage("This player is not actually online");

View File

@ -14,29 +14,17 @@ import fr.xephi.authme.settings.Messages;
*/
public class LastLoginCommand 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
AuthMe plugin = AuthMe.getInstance();
// Messages instance
Messages m = Messages.getInstance();
// Get the player
String playerName = sender.getName();
if(commandArguments.getCount() >= 1)
playerName = commandArguments.get(0);
// Validate the player
AuthMe plugin = AuthMe.getInstance();
Messages m = Messages.getInstance();
PlayerAuth auth;
try {
auth = plugin.database.getAuth(playerName.toLowerCase());

View File

@ -15,21 +15,8 @@ import fr.xephi.authme.task.ChangePasswordTask;
*/
public class ChangePasswordCommand 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 passwords
@ -71,6 +58,7 @@ public class ChangePasswordCommand extends ExecutableCommand {
}
// Set the password
final AuthMe plugin = AuthMe.getInstance();
plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new ChangePasswordTask(plugin, player, playerPass, playerPassVerify));
return true;
}

View File

@ -12,22 +12,8 @@ import fr.xephi.authme.settings.Messages;
*/
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);
@ -37,11 +23,9 @@ public class AddEmailCommand extends ExecutableCommand {
return true;
}
// Get the player instance and name
// Get the player and perform email addition
final AuthMe plugin = AuthMe.getInstance();
final Player player = (Player) sender;
final String playerName = player.getName().toLowerCase();
// Command logic
plugin.management.performAddEmail(player, playerMail, playerMailVerify);
return true;
}

View File

@ -6,28 +6,13 @@ import org.bukkit.entity.Player;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.command.CommandParts;
import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.settings.Messages;
/**
*/
public class ChangeEmailCommand 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 playerMailOld = commandArguments.get(0);
String playerMailNew = commandArguments.get(1);
@ -37,11 +22,9 @@ public class ChangeEmailCommand extends ExecutableCommand {
return true;
}
// Get the player instance and name
// Get the player instance and execute action
final AuthMe plugin = AuthMe.getInstance();
final Player player = (Player) sender;
final String playerName = player.getName();
// Command logic
plugin.management.performChangeEmail(player, playerMailOld, playerMailNew);
return true;
}

View File

@ -20,23 +20,8 @@ import fr.xephi.authme.settings.Settings;
*/
public class RecoverEmailCommand 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);
@ -50,6 +35,9 @@ public class RecoverEmailCommand extends ExecutableCommand {
final String playerName = player.getName();
// Command logic
final AuthMe plugin = AuthMe.getInstance();
final Messages m = Messages.getInstance();
if (plugin.mail == null) {
m.send(player, "error");
return true;

View File

@ -11,32 +11,19 @@ import fr.xephi.authme.command.ExecutableCommand;
*/
public class LoginCommand 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();
// Make sure the current command executor is a player
if(!(sender instanceof Player)) {
if (!(sender instanceof Player)) {
return true;
}
// Get the player instance
// Get the necessary objects
final AuthMe plugin = AuthMe.getInstance();
final Player player = (Player) sender;
final String playerPass = commandArguments.get(0);
// Get the password
String playerPass = commandArguments.get(0);
// Login the player
// Log the player in
plugin.management.performLogin(player, playerPass, false);
return true;
}

View File

@ -11,26 +11,15 @@ import fr.xephi.authme.command.ExecutableCommand;
*/
public class LogoutCommand 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();
// Make sure the current command executor is a player
if(!(sender instanceof Player)) {
if (!(sender instanceof Player)) {
return true;
}
// Get the player instance
final AuthMe plugin = AuthMe.getInstance();
final Player player = (Player) sender;
// Logout the player

View File

@ -14,21 +14,8 @@ import fr.xephi.authme.settings.Settings;
*/
public class RegisterCommand 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();
// Make sure the sender is a player
@ -44,6 +31,7 @@ public class RegisterCommand extends ExecutableCommand {
return true;
}
final AuthMe plugin = AuthMe.getInstance();
if (Settings.emailRegistration && !Settings.getmailAccount.isEmpty()) {
if (Settings.doubleEmailCheck) {
if (commandArguments.getCount() < 2 || !commandArguments.get(0).equals(commandArguments.get(1))) {