mirror of
https://github.com/filoghost/ChestCommands.git
synced 2025-02-19 21:11:42 +01:00
Refactor command framework
This commit is contained in:
parent
97af919551
commit
298662f019
@ -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;
|
||||
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.gmail.filoghost.chestcommands.command.framework;
|
||||
|
||||
public class CommandException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public CommandException(String msg) {
|
||||
super(msg);
|
||||
}
|
||||
|
||||
}
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user