From 298662f019d653bb3efc3cccbb0004fe5f238ea0 Mon Sep 17 00:00:00 2001 From: filoghost Date: Wed, 28 Nov 2018 17:00:36 +0100 Subject: [PATCH] Refactor command framework --- .../chestcommands/ChestCommands.java | 2 +- .../command/CommandFramework.java | 165 ------------------ .../chestcommands/command/CommandHandler.java | 2 + .../command/framework/CommandException.java | 25 +++ .../command/framework/CommandFramework.java | 73 ++++++++ .../command/framework/CommandValidate.java | 85 +++++++++ 6 files changed, 186 insertions(+), 166 deletions(-) delete mode 100644 Plugin/src/main/java/com/gmail/filoghost/chestcommands/command/CommandFramework.java create mode 100644 Plugin/src/main/java/com/gmail/filoghost/chestcommands/command/framework/CommandException.java create mode 100644 Plugin/src/main/java/com/gmail/filoghost/chestcommands/command/framework/CommandFramework.java create mode 100644 Plugin/src/main/java/com/gmail/filoghost/chestcommands/command/framework/CommandValidate.java diff --git a/Plugin/src/main/java/com/gmail/filoghost/chestcommands/ChestCommands.java b/Plugin/src/main/java/com/gmail/filoghost/chestcommands/ChestCommands.java index 79aa22e..9cf4067 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/chestcommands/ChestCommands.java +++ b/Plugin/src/main/java/com/gmail/filoghost/chestcommands/ChestCommands.java @@ -31,8 +31,8 @@ import org.bukkit.plugin.java.JavaPlugin; import com.gmail.filoghost.chestcommands.SimpleUpdater.ResponseHandler; import com.gmail.filoghost.chestcommands.bridge.BarAPIBridge; import com.gmail.filoghost.chestcommands.bridge.EconomyBridge; -import com.gmail.filoghost.chestcommands.command.CommandFramework; import com.gmail.filoghost.chestcommands.command.CommandHandler; +import com.gmail.filoghost.chestcommands.command.framework.CommandFramework; import com.gmail.filoghost.chestcommands.config.AsciiPlaceholders; import com.gmail.filoghost.chestcommands.config.Lang; import com.gmail.filoghost.chestcommands.config.Settings; diff --git a/Plugin/src/main/java/com/gmail/filoghost/chestcommands/command/CommandFramework.java b/Plugin/src/main/java/com/gmail/filoghost/chestcommands/command/CommandFramework.java deleted file mode 100644 index f133969..0000000 --- a/Plugin/src/main/java/com/gmail/filoghost/chestcommands/command/CommandFramework.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package com.gmail.filoghost.chestcommands.command; - -import org.bukkit.ChatColor; -import org.bukkit.command.Command; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.command.PluginCommand; -import org.bukkit.plugin.java.JavaPlugin; - -/** - * Wrapper for the default command executor. - */ -public abstract class CommandFramework implements CommandExecutor { - - /*************************************************** - * - * STATIC REGISTER METHOD - * - ***************************************************/ - public static boolean register(JavaPlugin plugin, CommandFramework command) { - PluginCommand pluginCommand = plugin.getCommand(command.label); - - if (pluginCommand == null) { - return false; - } - - pluginCommand.setExecutor(command); - return true; - } - - /*************************************************** - * - * COMMAND FRAMEWORK CLASS - * - ***************************************************/ - private String label; - - public CommandFramework(String label) { - this.label = label; - } - - @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - try { - - execute(sender, label, args); - - } catch (CommandException ex) { - - if (ex.getMessage() != null && !ex.getMessage().isEmpty()) { - // Use RED by default - sender.sendMessage(ChatColor.RED + ex.getMessage()); - } - } - - return true; - } - - public abstract void execute(CommandSender sender, String label, String[] args); - - - /*************************************************** - * - * COMMAND EXCEPTION - * - ***************************************************/ - public static class CommandException extends RuntimeException { - - private static final long serialVersionUID = 1L; - - public CommandException(String msg) { - super(msg); - } - - } - - - /*************************************************** - * - * VALIDATE CLASS - * - ***************************************************/ - public static class CommandValidate { - - public static void notNull(Object o, String msg) { - if (o == null) { - throw new CommandException(msg); - } - } - - public static void isTrue(boolean b, String msg) { - if (!b) { - throw new CommandException(msg); - } - } - - public static int getPositiveInteger(String input) { - try { - int i = Integer.parseInt(input); - if (i < 0) { - throw new CommandException("The number must be 0 or positive."); - } - return i; - } catch (NumberFormatException e) { - throw new CommandException("Invalid number \"" + input + "\"."); - } - } - - public static int getPositiveIntegerNotZero(String input) { - try { - int i = Integer.parseInt(input); - if (i <= 0) { - throw new CommandException("The number must be positive."); - } - return i; - } catch (NumberFormatException e) { - throw new CommandException("Invalid number \"" + input + "\"."); - } - } - - public static double getPositiveDouble(String input) { - try { - double d = Double.parseDouble(input); - if (d < 0) { - throw new CommandException("The number must be 0 or positive."); - } - return d; - } catch (NumberFormatException e) { - throw new CommandException("Invalid number \"" + input + "\"."); - } - } - - public static double getPositiveDoubleNotZero(String input) { - try { - double d = Integer.parseInt(input); - if (d <= 0) { - throw new CommandException("The number must be positive."); - } - return d; - } catch (NumberFormatException e) { - throw new CommandException("Invalid number \"" + input + "\"."); - } - } - - public static void minLength(Object[] array, int minLength, String msg) { - if (array.length < minLength) { - throw new CommandException(msg); - } - } - } -} diff --git a/Plugin/src/main/java/com/gmail/filoghost/chestcommands/command/CommandHandler.java b/Plugin/src/main/java/com/gmail/filoghost/chestcommands/command/CommandHandler.java index 564ab05..70462af 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/chestcommands/command/CommandHandler.java +++ b/Plugin/src/main/java/com/gmail/filoghost/chestcommands/command/CommandHandler.java @@ -22,6 +22,8 @@ import org.bukkit.entity.Player; import com.gmail.filoghost.chestcommands.ChestCommands; import com.gmail.filoghost.chestcommands.Permissions; +import com.gmail.filoghost.chestcommands.command.framework.CommandFramework; +import com.gmail.filoghost.chestcommands.command.framework.CommandValidate; import com.gmail.filoghost.chestcommands.internal.ExtendedIconMenu; import com.gmail.filoghost.chestcommands.task.ErrorLoggerTask; import com.gmail.filoghost.chestcommands.util.ErrorLogger; diff --git a/Plugin/src/main/java/com/gmail/filoghost/chestcommands/command/framework/CommandException.java b/Plugin/src/main/java/com/gmail/filoghost/chestcommands/command/framework/CommandException.java new file mode 100644 index 0000000..5f802ce --- /dev/null +++ b/Plugin/src/main/java/com/gmail/filoghost/chestcommands/command/framework/CommandException.java @@ -0,0 +1,25 @@ +/* + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.gmail.filoghost.chestcommands.command.framework; + +public class CommandException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + public CommandException(String msg) { + super(msg); + } + +} \ No newline at end of file diff --git a/Plugin/src/main/java/com/gmail/filoghost/chestcommands/command/framework/CommandFramework.java b/Plugin/src/main/java/com/gmail/filoghost/chestcommands/command/framework/CommandFramework.java new file mode 100644 index 0000000..4dbe024 --- /dev/null +++ b/Plugin/src/main/java/com/gmail/filoghost/chestcommands/command/framework/CommandFramework.java @@ -0,0 +1,73 @@ +/* + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.gmail.filoghost.chestcommands.command.framework; + +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.PluginCommand; +import org.bukkit.plugin.java.JavaPlugin; + +/** + * Wrapper for the default command executor. + */ +public abstract class CommandFramework implements CommandExecutor { + + private String label; + + + public CommandFramework(String label) { + this.label = label; + } + + + public abstract void execute(CommandSender sender, String label, String[] args); + + + /** + * Default implementation of Bukkit's command executor. + */ + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + try { + execute(sender, label, args); + + } catch (CommandException ex) { + if (ex.getMessage() != null && !ex.getMessage().isEmpty()) { + // Use RED by default + sender.sendMessage(ChatColor.RED + ex.getMessage()); + } + } + + return true; + } + + + /** + * Register a command through the framework. + */ + public static boolean register(JavaPlugin plugin, CommandFramework command) { + PluginCommand pluginCommand = plugin.getCommand(command.label); + + if (pluginCommand == null) { + return false; + } + + pluginCommand.setExecutor(command); + return true; + } + +} diff --git a/Plugin/src/main/java/com/gmail/filoghost/chestcommands/command/framework/CommandValidate.java b/Plugin/src/main/java/com/gmail/filoghost/chestcommands/command/framework/CommandValidate.java new file mode 100644 index 0000000..732a7a7 --- /dev/null +++ b/Plugin/src/main/java/com/gmail/filoghost/chestcommands/command/framework/CommandValidate.java @@ -0,0 +1,85 @@ +/* + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.gmail.filoghost.chestcommands.command.framework; + +public class CommandValidate { + + public static void notNull(Object o, String msg) { + if (o == null) { + throw new CommandException(msg); + } + } + + public static void isTrue(boolean b, String msg) { + if (!b) { + throw new CommandException(msg); + } + } + + public static int getPositiveInteger(String input) { + try { + int i = Integer.parseInt(input); + if (i < 0) { + throw new CommandException("The number must be 0 or positive."); + } + return i; + } catch (NumberFormatException e) { + throw new CommandException("Invalid number \"" + input + "\"."); + } + } + + public static int getPositiveIntegerNotZero(String input) { + try { + int i = Integer.parseInt(input); + if (i <= 0) { + throw new CommandException("The number must be positive."); + } + return i; + } catch (NumberFormatException e) { + throw new CommandException("Invalid number \"" + input + "\"."); + } + } + + public static double getPositiveDouble(String input) { + try { + double d = Double.parseDouble(input); + if (d < 0) { + throw new CommandException("The number must be 0 or positive."); + } + return d; + } catch (NumberFormatException e) { + throw new CommandException("Invalid number \"" + input + "\"."); + } + } + + public static double getPositiveDoubleNotZero(String input) { + try { + double d = Integer.parseInt(input); + if (d <= 0) { + throw new CommandException("The number must be positive."); + } + return d; + } catch (NumberFormatException e) { + throw new CommandException("Invalid number \"" + input + "\"."); + } + } + + public static void minLength(Object[] array, int minLength, String msg) { + if (array.length < minLength) { + throw new CommandException(msg); + } + } + +} \ No newline at end of file