Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandExecutor;
- import org.bukkit.command.CommandSender;
- import org.bukkit.entity.Player;
- public class SCmd implements CommandExecutor {
- public static enum Message {
- DISABLED_PLAYERS,
- DISABLED_CONSOLE,
- NO_PERM,
- NO_ACTION,
- ARG_NOT_FOUND;
- }
- private static class Messages {
- public static String DISABLED_PLAYERS = "That is disabled for players.";
- public static String DISABLED_CONSOLE = "That is disabled for the console.";
- public static String NO_PERM = "You do not have the permission to do that.";
- public static String NO_ACTION = "There is no action defined for that.";
- public static String ARG_NOT_FOUND = "Argument not found.";
- public static void edit(Message m, String msg) {
- switch(m) {
- case DISABLED_PLAYERS:
- DISABLED_PLAYERS = msg;
- break;
- case DISABLED_CONSOLE:
- DISABLED_CONSOLE = msg;
- break;
- case NO_PERM:
- NO_PERM = msg;
- break;
- case NO_ACTION:
- NO_ACTION = msg;
- break;
- case ARG_NOT_FOUND:
- ARG_NOT_FOUND = msg;
- break;
- default:
- break;
- }
- }
- }
- public interface CmdAction {
- public void run(CommandSender sender);
- }
- public interface ArgAction {
- public void run(CommandSender sender, String[] args);
- }
- public static class Subcommand {
- private ArrayList<Subcommand> subs = new ArrayList<Subcommand>();
- private ArrayList<Arguments> arglist = new ArrayList<Arguments>();
- private CmdAction action;
- private String cmd, perm;
- private String[] help;
- private boolean players = true, console = true, noac = true, endhere = false;
- public Subcommand(String cmd) {
- this.cmd = cmd;
- }
- public Subcommand(String cmd, String perm) {
- this.cmd = cmd;
- this.perm = perm;
- }
- public String getName() {
- return cmd;
- }
- public boolean check(String input) {
- return input.equalsIgnoreCase(cmd);
- }
- public Subcommand endHere(boolean end) {
- this.endhere = end;
- return this;
- }
- public Subcommand setHelpMSG(String[] msg) {
- this.help = msg;
- return this;
- }
- public Subcommand msgNoAction(boolean b) {
- this.noac = b;
- return this;
- }
- public Subcommand allowPlayers(boolean allow) {
- players = allow;
- if (!allow) console = true;
- return this;
- }
- public Subcommand allowConsole(boolean allow) {
- console = allow;
- if (!allow) players = true;
- return this;
- }
- public Subcommand addSubcommand(Subcommand sub) throws IllegalArgumentException {
- for (Subcommand s : subs) {
- if (s.getName().equalsIgnoreCase(sub.getName())) throw new IllegalArgumentException("Subcommand already exists.");
- }
- subs.add(sub);
- return this;
- }
- public Subcommand addArguments(Arguments args) throws IllegalArgumentException {
- for (Arguments a : arglist) {
- if (a.length() == args.length()) throw new IllegalArgumentException("There are already arguments for that length.");
- }
- arglist.add(args);
- return this;
- }
- public Subcommand setAction(CmdAction ca) {
- action = ca;
- return this;
- }
- public void use(CommandSender sender, String[] args, int level) {
- boolean player = false;
- if (sender instanceof Player) {
- player = true;
- if (!players) {
- sender.sendMessage(Messages.DISABLED_PLAYERS);
- return;
- }
- }
- else {
- if (!console) {
- sender.sendMessage(Messages.DISABLED_CONSOLE);
- return;
- }
- }
- if (player) {
- if (perm != null && !perm.isEmpty()) {
- if (!((Player) sender).hasPermission(perm)) {
- sender.sendMessage(Messages.NO_PERM);
- return;
- }
- }
- }
- if (args.length == level+1 || endhere) {
- if (action != null) {
- action.run(sender);
- }
- else {
- if (help != null && help.length > 0) {
- for (int i = 0; i < help.length; i++) {
- sender.sendMessage(help[i]);
- }
- }
- else {
- if (noac) sender.sendMessage(Messages.NO_ACTION);
- }
- }
- }
- else if (args.length > level+1) {
- boolean f = false;
- if (subs.size() > 0) {
- for (Subcommand sub : subs) {
- if (sub.check(args[level+1])) {
- sub.use(sender, args, level+1);
- f = true;
- break;
- }
- }
- if (!f) {
- for (Arguments a : arglist) {
- if (a.length() == (args.length-(level+1))) {
- a.use(sender, args, level+1);
- f = true;
- break;
- }
- }
- if (!f) sender.sendMessage(Messages.ARG_NOT_FOUND);
- }
- }
- else {
- for (Arguments a : arglist) {
- if (a.length() == (args.length-(level+1))) {
- a.use(sender, args, level+1);
- f = true;
- break;
- }
- }
- if (!f) sender.sendMessage(Messages.ARG_NOT_FOUND);
- }
- }
- }
- }
- public static class Arguments {
- private ArgAction action;
- private int length;
- private String perm;
- private String[] help;
- private boolean players = true, console = true, noac = true;
- public Arguments(int length) {
- this.length = length;
- }
- public Arguments(int length, String perm) {
- this.length = length;
- this.perm = perm;
- }
- public int length() {
- return length;
- }
- public Arguments setHelpMSG(String[] msg) {
- this.help = msg;
- return this;
- }
- public Arguments msgNoAction(boolean b) {
- this.noac = b;
- return this;
- }
- public Arguments allowPlayers(boolean allow) {
- players = allow;
- if (!allow) console = true;
- return this;
- }
- public Arguments allowConsole(boolean allow) {
- console = allow;
- if (!allow) players = true;
- return this;
- }
- public Arguments setAction(ArgAction aa) {
- action = aa;
- return this;
- }
- public void use(CommandSender sender, String[] args, int level) {
- boolean player = false;
- if (sender instanceof Player) {
- player = true;
- if (!players) {
- sender.sendMessage(Messages.DISABLED_PLAYERS);
- return;
- }
- }
- else {
- if (!console) {
- sender.sendMessage(Messages.DISABLED_CONSOLE);
- return;
- }
- }
- if (player) {
- if (perm != null && !perm.isEmpty()) {
- if (!((Player) sender).hasPermission(perm)) {
- sender.sendMessage(Messages.NO_PERM);
- return;
- }
- }
- }
- if (action != null) {
- String[] a = new String[length];
- for (int i = 0; i < length; i++) {
- a[i] = args[level+i];
- }
- action.run(sender, a);
- }
- else {
- if (help != null && help.length > 0) {
- for (int i = 0; i < help.length; i++) {
- sender.sendMessage(help[i]);
- }
- }
- else {
- if (noac) sender.sendMessage(Messages.NO_ACTION);
- }
- }
- }
- }
- public SCmd() {}
- public SCmd(String perm) {
- this.perm = perm;
- }
- private ArrayList<Subcommand> subs = new ArrayList<Subcommand>();
- private ArrayList<Arguments> arglist = new ArrayList<Arguments>();
- private CmdAction action;
- private String perm;
- private String[] help;
- private boolean players = true, console = true, noac = true, endhere = false;
- @Override
- public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
- boolean player = false;
- if (sender instanceof Player) {
- player = true;
- if (!players) {
- sender.sendMessage(Messages.DISABLED_PLAYERS);
- return true;
- }
- }
- else {
- if (!console) {
- sender.sendMessage(Messages.DISABLED_CONSOLE);
- return true;
- }
- }
- if (player) {
- if (perm != null && !perm.isEmpty()) {
- if (!((Player) sender).hasPermission(perm)) {
- sender.sendMessage(Messages.NO_PERM);
- return true;
- }
- }
- }
- if (args.length == 0 || endhere) {
- if (action != null) {
- action.run(sender);
- }
- else {
- if (help != null && help.length > 0) {
- for (int i = 0; i < help.length; i++) {
- sender.sendMessage(help[i]);
- }
- }
- else {
- if (noac) sender.sendMessage(Messages.NO_ACTION);
- }
- }
- }
- else if (args.length > 0) {
- boolean f = false;
- if (subs.size() > 0) {
- for (Subcommand sub : subs) {
- if (sub.check(args[0])) {
- sub.use(sender, args, 0);
- f = true;
- break;
- }
- }
- if (!f) {
- for (Arguments a : arglist) {
- if (a.length() == args.length) {
- a.use(sender, args, 0);
- f = true;
- break;
- }
- }
- if (!f) sender.sendMessage(Messages.ARG_NOT_FOUND);
- }
- }
- else {
- for (Arguments a : arglist) {
- if (a.length() == args.length) {
- a.use(sender, args, 0);
- f = true;
- break;
- }
- }
- if (!f) sender.sendMessage(Messages.ARG_NOT_FOUND);
- }
- }
- return true;
- }
- public SCmd endHere(boolean end) {
- endhere = end;
- return this;
- }
- public SCmd msgNoAction(boolean b) {
- this.noac = b;
- return this;
- }
- public SCmd editMessage(Message m, String msg) {
- Messages.edit(m, msg);
- return this;
- }
- public SCmd setHelpMSG(String[] msg) {
- help = msg;
- return this;
- }
- public SCmd allowPlayers(boolean allow) {
- players = allow;
- if (!allow) console = true;
- return this;
- }
- public SCmd allowConsole(boolean allow) {
- console = allow;
- if (!allow) players = true;
- return this;
- }
- public SCmd addSubcommand(Subcommand sub) throws IllegalArgumentException {
- for (Subcommand s : subs) {
- if (s.getName().equalsIgnoreCase(sub.getName())) throw new IllegalArgumentException("Subcommand already exists.");
- }
- subs.add(sub);
- return this;
- }
- public SCmd addArguments(Arguments args) throws IllegalArgumentException {
- for (Arguments a : arglist) {
- if (a.length() == args.length()) throw new IllegalArgumentException("There are already arguments for that length.");
- }
- arglist.add(args);
- return this;
- }
- public SCmd setAction(CmdAction ca) {
- action = ca;
- return this;
- }
- }
Add Comment
Please, Sign In to add comment