From 8eb1b38d217085a805d7fce94306d53062193497 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Fri, 4 Dec 2015 23:47:48 +0100 Subject: [PATCH] Slim down CommandParts API - Remove all methods that aren't constructors / fancy get() on list; almost possible to remove class entirely --- .../java/fr/xephi/authme/ConsoleLogger.java | 13 ++--- .../authme/command/CommandDescription.java | 3 +- .../xephi/authme/command/CommandHandler.java | 17 ++++--- .../fr/xephi/authme/command/CommandParts.java | 49 +------------------ .../authme/SwitchAntiBotCommand.java | 17 ++++--- .../authme/command/help/HelpProvider.java | 21 +++++--- .../authme/command/help/HelpSyntaxHelper.java | 6 ++- .../fr/xephi/authme/util/StringUtils.java | 2 - 8 files changed, 47 insertions(+), 81 deletions(-) diff --git a/src/main/java/fr/xephi/authme/ConsoleLogger.java b/src/main/java/fr/xephi/authme/ConsoleLogger.java index 890318046..fee14296d 100644 --- a/src/main/java/fr/xephi/authme/ConsoleLogger.java +++ b/src/main/java/fr/xephi/authme/ConsoleLogger.java @@ -2,7 +2,6 @@ package fr.xephi.authme; import com.google.common.base.Throwables; import fr.xephi.authme.settings.Settings; -import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.Wrapper; import java.io.IOException; @@ -17,8 +16,10 @@ import java.util.Date; */ public final class ConsoleLogger { + private static final String NEW_LINE = System.getProperty("line.separator"); + private static final DateFormat DATE_FORMAT = new SimpleDateFormat("[MM-dd HH:mm:ss]"); + private static Wrapper wrapper = Wrapper.getInstance(); - private static final DateFormat df = new SimpleDateFormat("[MM-dd HH:mm:ss]"); private ConsoleLogger() { // Service class @@ -57,11 +58,11 @@ public final class ConsoleLogger { */ private static void writeLog(String message) { String dateTime; - synchronized (df) { - dateTime = df.format(new Date()); + synchronized (DATE_FORMAT) { + dateTime = DATE_FORMAT.format(new Date()); } try { - Files.write(Settings.LOG_FILE.toPath(), (dateTime + ": " + message + StringUtils.newline).getBytes(), + Files.write(Settings.LOG_FILE.toPath(), (dateTime + ": " + message + NEW_LINE).getBytes(), StandardOpenOption.APPEND, StandardOpenOption.CREATE); } catch (IOException ignored) { @@ -77,6 +78,6 @@ public final class ConsoleLogger { if (!Settings.useLogging) { return; } - writeLog("" + Throwables.getStackTraceAsString(ex)); + writeLog(Throwables.getStackTraceAsString(ex)); } } diff --git a/src/main/java/fr/xephi/authme/command/CommandDescription.java b/src/main/java/fr/xephi/authme/command/CommandDescription.java index 2911055de..4623ddd6c 100644 --- a/src/main/java/fr/xephi/authme/command/CommandDescription.java +++ b/src/main/java/fr/xephi/authme/command/CommandDescription.java @@ -211,8 +211,9 @@ public class CommandDescription { List referenceList = new ArrayList<>(); // Check whether this command has a parent, if so, add the absolute parent command - if (getParent() != null) + if (getParent() != null) { referenceList.addAll(getParent().getCommandReference(reference).getList()); + } // Get the current label referenceList.add(getLabel(reference)); diff --git a/src/main/java/fr/xephi/authme/command/CommandHandler.java b/src/main/java/fr/xephi/authme/command/CommandHandler.java index bb0b1298c..67a4fbe64 100644 --- a/src/main/java/fr/xephi/authme/command/CommandHandler.java +++ b/src/main/java/fr/xephi/authme/command/CommandHandler.java @@ -2,6 +2,7 @@ package fr.xephi.authme.command; import fr.xephi.authme.AuthMe; import fr.xephi.authme.command.help.HelpProvider; +import fr.xephi.authme.util.CollectionUtils; import fr.xephi.authme.util.StringUtils; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; @@ -209,28 +210,28 @@ public class CommandHandler { private static void sendImproperArgumentsMessage(CommandSender sender, FoundCommandResult result, CommandParts commandReference, String baseCommand) { // Get the command and the suggested command reference - CommandParts suggestedCommandReference = - new CommandParts(result.getCommandDescription().getCommandReference(commandReference)); - CommandParts helpCommandReference = new CommandParts(suggestedCommandReference.getRange(1)); + List suggestedCommandReference = + result.getCommandDescription().getCommandReference(commandReference).getList(); + List helpCommandReference = CollectionUtils.getRange(suggestedCommandReference, 1); // Show the invalid arguments warning sender.sendMessage(ChatColor.DARK_RED + "Incorrect command arguments!"); // Show the command argument help - HelpProvider.showHelp(sender, commandReference, suggestedCommandReference, + HelpProvider.showHelp(sender, commandReference, new CommandParts(suggestedCommandReference), true, false, true, false, false, false); // Show the command to use for detailed help sender.sendMessage(ChatColor.GOLD + "Detailed help: " + ChatColor.WHITE + "/" + baseCommand - + " help " + helpCommandReference); + + " help " + CommandUtils.labelsToString(helpCommandReference)); } private static void sendCommandAssumptionMessage(CommandSender sender, FoundCommandResult result, CommandParts commandReference) { - CommandParts assumedCommandParts = - new CommandParts(result.getCommandDescription().getCommandReference(commandReference)); + List assumedCommandParts = + result.getCommandDescription().getCommandReference(commandReference).getList(); sender.sendMessage(ChatColor.DARK_RED + "Unknown command, assuming " + ChatColor.GOLD + "/" - + assumedCommandParts + ChatColor.DARK_RED + "!"); + + CommandUtils.labelsToString(assumedCommandParts) + ChatColor.DARK_RED + "!"); } } diff --git a/src/main/java/fr/xephi/authme/command/CommandParts.java b/src/main/java/fr/xephi/authme/command/CommandParts.java index 58df26011..eb6299db5 100644 --- a/src/main/java/fr/xephi/authme/command/CommandParts.java +++ b/src/main/java/fr/xephi/authme/command/CommandParts.java @@ -23,15 +23,6 @@ public class CommandParts { this.parts.add(part); } - /** - * Constructor. - * - * @param commandParts The command parts instance. - */ - public CommandParts(CommandParts commandParts) { - this.parts.addAll(commandParts.getList()); - } - /** * Constructor. * @@ -60,7 +51,7 @@ public class CommandParts { } /** - * Get a part by it's index. + * Get a part by its index. * * @param i Part index. * @@ -75,44 +66,6 @@ public class CommandParts { return this.parts.get(i); } - /** - * Get a range of the parts starting at the specified index up to the end of the range. - * - * @param start The starting index. - * - * @return The parts range. Arguments that were out of bound are not included. - */ - public List getRange(int start) { - return getRange(start, getCount() - start); - } - - /** - * Get a range of the parts. - * - * @param start The starting index. - * @param count The number of parts to get. - * - * @return The parts range. Parts that were out of bound are not included. - */ - @Deprecated - public List getRange(int start, int count) { - // Create a new list to put the range into - List elements = new ArrayList<>(); - - // Get the range - for (int i = start; i < start + count; i++) { - // Get the part and add it if it's valid - String element = get(i); - if (element != null) - elements.add(element); - } - - // Return the list of parts - return elements; - } - - - /** * Convert the parts to a string. * 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 fb844c607..3c0478676 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 @@ -2,11 +2,15 @@ package fr.xephi.authme.command.executable.authme; import fr.xephi.authme.AntiBot; import fr.xephi.authme.command.CommandParts; +import fr.xephi.authme.command.CommandUtils; import fr.xephi.authme.command.ExecutableCommand; import fr.xephi.authme.command.help.HelpProvider; +import fr.xephi.authme.util.CollectionUtils; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; +import java.util.List; + /** */ public class SwitchAntiBotCommand extends ExecutableCommand { @@ -32,16 +36,16 @@ public class SwitchAntiBotCommand extends ExecutableCommand { } // Enable the mod - if (newState.equalsIgnoreCase("ON")) { + if ("ON".equalsIgnoreCase(newState)) { AntiBot.overrideAntiBotStatus(true); - sender.sendMessage("[AuthMe] AntiBot Manual Ovverride: enabled!"); + sender.sendMessage("[AuthMe] AntiBot Manual Override: enabled!"); return true; } // Disable the mod - if (newState.equalsIgnoreCase("OFF")) { + if ("OFF".equalsIgnoreCase(newState)) { AntiBot.overrideAntiBotStatus(false); - sender.sendMessage("[AuthMe] AntiBotMod Manual Ovverride: disabled!"); + sender.sendMessage("[AuthMe] AntiBotMod Manual Override: disabled!"); return true; } @@ -52,8 +56,9 @@ public class SwitchAntiBotCommand extends ExecutableCommand { HelpProvider.showHelp(sender, commandReference, commandReference, true, false, true, false, false, false); // Show the command to use for detailed help - CommandParts helpCommandReference = new CommandParts(commandReference.getRange(1)); - sender.sendMessage(ChatColor.GOLD + "Detailed help: " + ChatColor.WHITE + "/" + commandReference.get(0) + " help " + helpCommandReference); + List helpCommandReference = CollectionUtils.getRange(commandReference.getList(), 1); + sender.sendMessage(ChatColor.GOLD + "Detailed help: " + ChatColor.WHITE + "/" + + commandReference.get(0) + " help " + CommandUtils.labelsToString(helpCommandReference)); return true; } } diff --git a/src/main/java/fr/xephi/authme/command/help/HelpProvider.java b/src/main/java/fr/xephi/authme/command/help/HelpProvider.java index a2f3d1f2c..7f088acf3 100644 --- a/src/main/java/fr/xephi/authme/command/help/HelpProvider.java +++ b/src/main/java/fr/xephi/authme/command/help/HelpProvider.java @@ -3,9 +3,11 @@ package fr.xephi.authme.command.help; import fr.xephi.authme.AuthMe; import fr.xephi.authme.command.CommandDescription; import fr.xephi.authme.command.CommandParts; +import fr.xephi.authme.command.CommandUtils; import fr.xephi.authme.command.FoundCommandResult; import fr.xephi.authme.settings.Settings; +import fr.xephi.authme.util.CollectionUtils; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; @@ -86,13 +88,14 @@ public class HelpProvider { // Show the unknown command warning sender.sendMessage(ChatColor.DARK_RED + "No help found for '" + helpQuery + "'!"); - // Get the suggested command - CommandParts suggestedCommandParts = new CommandParts(result.getCommandDescription().getCommandReference(commandReference).getRange(1)); - // Show a command suggestion if available and the difference isn't too big - if (commandDifference < 0.75) - if (result.getCommandDescription() != null) - sender.sendMessage(ChatColor.YELLOW + "Did you mean " + ChatColor.GOLD + "/" + baseCommand + " help " + suggestedCommandParts + ChatColor.YELLOW + "?"); + if (commandDifference < 0.75 && result.getCommandDescription() != null) { + // Get the suggested command + List suggestedCommandParts = CollectionUtils.getRange( + result.getCommandDescription().getCommandReference(commandReference).getList(), 1); + sender.sendMessage(ChatColor.YELLOW + "Did you mean " + ChatColor.GOLD + "/" + baseCommand + + " help " + CommandUtils.labelsToString(suggestedCommandParts) + ChatColor.YELLOW + "?"); + } // Show the help command sender.sendMessage(ChatColor.YELLOW + "Use the command " + ChatColor.GOLD + "/" + baseCommand + " help" + ChatColor.YELLOW + " to view help."); @@ -102,10 +105,12 @@ public class HelpProvider { // Show a message when the command handler is assuming a command if (commandDifference > 0) { // Get the suggested command - CommandParts suggestedCommandParts = new CommandParts(result.getCommandDescription().getCommandReference(commandReference).getRange(1)); + List suggestedCommandParts = CollectionUtils.getRange( + result.getCommandDescription().getCommandReference(commandReference).getList(), 1); // Show the suggested command - sender.sendMessage(ChatColor.DARK_RED + "No help found, assuming '" + ChatColor.GOLD + suggestedCommandParts + ChatColor.DARK_RED + "'!"); + sender.sendMessage(ChatColor.DARK_RED + "No help found, assuming '" + ChatColor.GOLD + + CommandUtils.labelsToString(suggestedCommandParts) + ChatColor.DARK_RED + "'!"); } // Print the help header diff --git a/src/main/java/fr/xephi/authme/command/help/HelpSyntaxHelper.java b/src/main/java/fr/xephi/authme/command/help/HelpSyntaxHelper.java index 023e4085b..5fa3f5e06 100644 --- a/src/main/java/fr/xephi/authme/command/help/HelpSyntaxHelper.java +++ b/src/main/java/fr/xephi/authme/command/help/HelpSyntaxHelper.java @@ -3,6 +3,8 @@ package fr.xephi.authme.command.help; import fr.xephi.authme.command.CommandArgumentDescription; import fr.xephi.authme.command.CommandDescription; import fr.xephi.authme.command.CommandParts; +import fr.xephi.authme.command.CommandUtils; +import fr.xephi.authme.util.CollectionUtils; import fr.xephi.authme.util.StringUtils; import org.bukkit.ChatColor; @@ -35,8 +37,8 @@ public final class HelpSyntaxHelper { // Get the help command reference, and the command label CommandParts helpCommandReference = commandDescription.getCommandReference(commandReference); - final String parentCommand = new CommandParts( - helpCommandReference.getRange(0, helpCommandReference.getCount() - 1)).toString(); + final String parentCommand = CommandUtils.labelsToString( + CollectionUtils.getRange(helpCommandReference.getList(), 0, helpCommandReference.getCount() - 1)); // Check whether the alternative label should be used String commandLabel; diff --git a/src/main/java/fr/xephi/authme/util/StringUtils.java b/src/main/java/fr/xephi/authme/util/StringUtils.java index 2e37bfce0..1751461d8 100644 --- a/src/main/java/fr/xephi/authme/util/StringUtils.java +++ b/src/main/java/fr/xephi/authme/util/StringUtils.java @@ -13,8 +13,6 @@ import java.util.Arrays; */ public final class StringUtils { - public static final String newline = System.getProperty("line.separator"); - private StringUtils() { // Utility class }