Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class OrenCommandHandler implements CommandExecutor {
- /**
- * All the commands registered and past on
- */
- private ArrayList<OrenCommand> commands = new ArrayList<>();
- @Override
- public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
- if (args.length == 0) {
- sender.sendMessage("§8§lOrenMC Commands:");
- sender.sendMessage(help(0));
- } else if (NumberUtils.isNumber(args[0])) {
- sender.sendMessage("§8§lOrenMC Commands:");
- sender.sendMessage(help(Integer.parseInt(args[0])));
- } else {
- OrenCommand oc = getCommand(args[0]);
- if (oc == null) {
- sender.sendMessage("§8§lOrenMC Commands:" + help(0));
- sender.sendMessage(help(0));
- } else {
- oc.getExecutor().onCommand(sender, cmd, args[0], args.length >= 2 ? Arrays.copyOfRange(args, 1, args.length) : new String[0]);
- }
- }
- return true;
- }
- /**
- * Get a string of all the commands registered
- *
- * @return The commands separated by a comma
- */
- private String help(int start) {
- StringBuilder sb = new StringBuilder();
- OrenCommand oc;
- for (int i = start * 9; i < commands.size() && i < (start + 1) * 9; i++) {
- oc = commands.get(i);
- sb.append("§a" + StringUtils.capitalize(oc.getName()) + ": §7" + oc.getDescription() + "\n");
- }
- return sb.toString().trim();
- }
- /**
- * Add a command to be handled by OrenCore.
- *
- * @param name
- * The name of the command
- * @param executor
- * The executor to handle the command
- * @param aliases
- * Any aliases of the command
- */
- public void hookCommand(String name, CommandExecutor executor, String description, String... aliases) {
- commands.add(new OrenCommand(name, executor, description, aliases));
- }
- /**
- * Get the command based on the given alias or label
- *
- * @param label
- * The alias or label
- * @return the corresponding command
- */
- private OrenCommand getCommand(String label) {
- for (OrenCommand oc : commands) {
- if (oc.isAlias(label)) {
- return oc;
- }
- }
- return null;
- }
- private class OrenCommand {
- private String name;
- private CommandExecutor executor;
- private String description;
- private String[] aliases;
- public OrenCommand(String name, CommandExecutor executor, String description, String... aliases) {
- this.name = name;
- this.executor = executor;
- this.description = description;
- this.aliases = aliases;
- }
- public CommandExecutor getExecutor() {
- return executor;
- }
- public String getName() {
- return name;
- }
- public String getDescription() {
- return description;
- }
- public boolean isAlias(String label) {
- if (label.equalsIgnoreCase(name)) {
- return true;
- }
- for (String alias : aliases) {
- if (alias.equalsIgnoreCase(label)) {
- return true;
- }
- }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement