#305 Prepare to remove CommandParts - replace getCount() to List#size()

- Preparations to remove the CommandParts as it's a "decorated list".
This commit is contained in:
ljacqu 2015-12-05 12:06:44 +01:00
parent 9d39fc1127
commit 61875f26fa
13 changed files with 60 additions and 51 deletions

View File

@ -459,11 +459,13 @@ public class CommandDescription {
*/
public FoundCommandResult findCommand(final CommandParts queryReference) {
// Make sure the command reference is valid
if (queryReference.getCount() <= 0)
List<String> queryRef = queryReference.getList();
if (queryRef.isEmpty()) {
return null;
}
// Check whether this description is for the last element in the command reference, if so return the current command
if (queryReference.getCount() <= getParentCount() + 1) {
if (queryRef.size() <= getParentCount() + 1) {
return new FoundCommandResult(
this,
getCommandReference(queryReference),
@ -523,12 +525,13 @@ public class CommandDescription {
*/
public int getSuitableArgumentsDifference(CommandParts commandReference) {
// Make sure the command reference is valid
if (commandReference.getCount() <= 0) {
List<String> labels = commandReference.getList();
if (labels.isEmpty()) {
return -1;
}
// Get the remaining command reference element count
int remainingElementCount = commandReference.getCount() - getParentCount() - 1;
int remainingElementCount = labels.size() - getParentCount() - 1;
// Check if there are too few arguments
int minArguments = CommandUtils.getMinNumberOfArguments(this);

View File

@ -126,8 +126,10 @@ public class CommandHandler {
*/
public FoundCommandResult findCommand(CommandParts queryReference) {
// Make sure the command reference is valid
if (queryReference.getCount() <= 0)
List<String> labels = queryReference.getList();
if (labels.isEmpty()) {
return null;
}
for (CommandDescription commandDescription : commands) {
// Check whether there's a command description available for the

View File

@ -41,15 +41,6 @@ public class CommandParts {
return this.parts;
}
/**
* Get the number of parts.
*
* @return Part count.
*/
public int getCount() {
return this.parts.size();
}
/**
* Get a part by its index.
*
@ -59,8 +50,9 @@ public class CommandParts {
*/
public String get(int i) {
// Make sure the index is in-bound
if (i < 0 || i >= getCount())
if (i < 0 || i >= parts.size()) {
return null;
}
// Get and return the argument
return this.parts.get(i);

View File

@ -5,6 +5,8 @@ import fr.xephi.authme.command.ExecutableCommand;
import fr.xephi.authme.command.help.HelpProvider;
import org.bukkit.command.CommandSender;
import java.util.List;
/**
*/
public class HelpCommand extends ExecutableCommand {
@ -12,10 +14,10 @@ public class HelpCommand extends ExecutableCommand {
@Override
public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
// Check whether quick help should be shown
boolean quickHelp = commandArguments.getCount() == 0;
List<String> arguments = commandArguments.getList();
// Set the proper command arguments for the quick help and show it
if (quickHelp) {
if (arguments.isEmpty()) {
commandArguments = new CommandParts(commandReference.get(0));
HelpProvider.showHelp(sender, commandReference, commandArguments, false, false, false, false, false, true);
} else {

View File

@ -18,15 +18,14 @@ public class AccountsCommand extends ExecutableCommand {
@Override
public boolean executeCommand(final CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
// AuthMe plugin instance
final AuthMe plugin = AuthMe.getInstance();
// Messages instance
final Messages m = plugin.getMessages();
List<String> arguments = commandArguments.getList();
// Get the player query
String playerQuery = sender.getName();
if (commandArguments.getCount() >= 1)
if (arguments.size() >= 1)
playerQuery = commandArguments.get(0);
final String playerQueryFinal = playerQuery;

View File

@ -8,6 +8,8 @@ import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.List;
/**
*/
public class ForceLoginCommand extends ExecutableCommand {
@ -19,7 +21,8 @@ public class ForceLoginCommand extends ExecutableCommand {
// Get the player query
String playerName = sender.getName();
if (commandArguments.getCount() >= 1)
List<String> arguments = commandArguments.getList();
if (arguments.size() >= 1)
playerName = commandArguments.get(0);
// Command logic

View File

@ -8,6 +8,8 @@ import fr.xephi.authme.output.MessageKey;
import fr.xephi.authme.output.Messages;
import org.bukkit.command.CommandSender;
import java.util.List;
/**
*/
public class GetEmailCommand extends ExecutableCommand {
@ -24,8 +26,9 @@ public class GetEmailCommand extends ExecutableCommand {
@Override
public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
// Get the player name
List<String> arguments = commandArguments.getList();
String playerName = sender.getName();
if (commandArguments.getCount() >= 1)
if (arguments.size() >= 1)
playerName = commandArguments.get(0);
// Get the authenticated user

View File

@ -7,21 +7,20 @@ import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.List;
/**
*/
public class GetIpCommand extends ExecutableCommand {
@Override
public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
// AuthMe plugin instance
final AuthMe plugin = AuthMe.getInstance();
List<String> arguments = commandArguments.getList();
// Get the player query
String playerName = sender.getName();
if (commandArguments.getCount() >= 1)
playerName = commandArguments.get(0);
String playerName = (arguments.size() >= 1) ? arguments.get(0) : sender.getName();
@SuppressWarnings("deprecation")
Player player = Bukkit.getPlayer(playerName);
if (player == null) {
sender.sendMessage("This player is not actually online");

View File

@ -9,6 +9,7 @@ import fr.xephi.authme.output.Messages;
import org.bukkit.command.CommandSender;
import java.util.Date;
import java.util.List;
/**
*/
@ -17,9 +18,8 @@ public class LastLoginCommand extends ExecutableCommand {
@Override
public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
// Get the player
String playerName = sender.getName();
if (commandArguments.getCount() >= 1)
playerName = commandArguments.get(0);
List<String> arguments = commandArguments.getList();
String playerName = (arguments.size() >= 1) ? arguments.get(0) : sender.getName();
// Validate the player
AuthMe plugin = AuthMe.getInstance();

View File

@ -10,6 +10,8 @@ import fr.xephi.authme.output.Messages;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.List;
/**
*/
public class PurgeLastPositionCommand extends ExecutableCommand {
@ -25,16 +27,13 @@ public class PurgeLastPositionCommand extends ExecutableCommand {
*/
@Override
public boolean executeCommand(final CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
// AuthMe plugin instance
final AuthMe plugin = AuthMe.getInstance();
// Messages instance
final Messages m = plugin.getMessages();
List<String> arguments = commandArguments.getList();
String playerName = arguments.isEmpty() ? sender.getName() : arguments.get(0);
// Get the player
String playerName = sender.getName();
if (commandArguments.getCount() >= 1)
playerName = commandArguments.get(0);
String playerNameLowerCase = playerName.toLowerCase();
// Purge the last position of the player

View File

@ -28,9 +28,11 @@ public class SwitchAntiBotCommand extends ExecutableCommand {
public boolean executeCommand(final CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
// Get the new state
String newState = null;
if (commandArguments.getCount() == 1) {
List<String> arguments = commandArguments.getList();
if (arguments.size() == 1) {
newState = commandArguments.get(0);
} else if(commandArguments.getCount() == 0) {
} else if (arguments.size() == 0) {
sender.sendMessage("[AuthMe] AntiBot status: " + AntiBot.getAntiBotStatus().name());
return true;
}

View File

@ -12,39 +12,40 @@ import fr.xephi.authme.util.Wrapper;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.List;
/**
*/
public class RegisterCommand extends ExecutableCommand {
@Override
public boolean executeCommand(CommandSender sender, CommandParts commandReference, CommandParts commandArguments) {
// Make sure the sender is a player
if (!(sender instanceof Player)) {
sender.sendMessage("Player Only! Use 'authme register <playername> <password>' instead");
return true;
}
List<String> arguments = commandArguments.getList();
final Wrapper wrapper = Wrapper.getInstance();
final AuthMe plugin = wrapper.getAuthMe();
final Messages m = wrapper.getMessages();
// Make sure the command arguments are valid
final Player player = (Player) sender;
if (commandArguments.getCount() == 0 || (Settings.getEnablePasswordVerifier && commandArguments.getCount() < 2)) {
if (arguments.isEmpty() || (Settings.getEnablePasswordVerifier && arguments.size() < 2)) {
m.send(player, MessageKey.USAGE_REGISTER);
return true;
}
final Management management = plugin.getManagement();
if (Settings.emailRegistration && !Settings.getmailAccount.isEmpty()) {
if (Settings.doubleEmailCheck) {
if (commandArguments.getCount() < 2 || !commandArguments.get(0).equals(commandArguments.get(1))) {
m.send(player, MessageKey.USAGE_REGISTER);
return true;
}
if (Settings.doubleEmailCheck && arguments.size() < 2 || !arguments.get(0).equals(arguments.get(1))) {
m.send(player, MessageKey.USAGE_REGISTER);
return true;
}
final String email = commandArguments.get(0);
final String email = arguments.get(0);
if (!Settings.isEmailCorrect(email)) {
m.send(player, MessageKey.INVALID_EMAIL);
return true;
@ -53,8 +54,8 @@ public class RegisterCommand extends ExecutableCommand {
management.performRegister(player, thePass, email);
return true;
}
if (commandArguments.getCount() > 1 && Settings.getEnablePasswordVerifier) {
if (!commandArguments.get(0).equals(commandArguments.get(1))) {
if (arguments.size() > 1 && Settings.getEnablePasswordVerifier) {
if (!arguments.get(0).equals(commandArguments.get(1))) {
m.send(player, MessageKey.PASSWORD_MATCH_ERROR);
return true;
}

View File

@ -8,6 +8,8 @@ import fr.xephi.authme.util.CollectionUtils;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.ChatColor;
import java.util.List;
/**
* Helper class for formatting a command's structure (name and arguments)
* for a Minecraft user.
@ -37,13 +39,15 @@ public final class HelpSyntaxHelper {
// Get the help command reference, and the command label
CommandParts helpCommandReference = commandDescription.getCommandReference(commandReference);
List<String> helpLabels = helpCommandReference.getList();
final String parentCommand = CommandUtils.labelsToString(
CollectionUtils.getRange(helpCommandReference.getList(), 0, helpCommandReference.getCount() - 1));
CollectionUtils.getRange(helpCommandReference.getList(), 0, helpLabels.size() - 1));
// Check whether the alternative label should be used
String commandLabel;
if (StringUtils.isEmpty(alternativeLabel)) {
commandLabel = helpCommandReference.get(helpCommandReference.getCount() - 1);
commandLabel = helpLabels.get(helpLabels.size() - 1);
} else {
commandLabel = alternativeLabel;
}