130 lines
3.1 KiB
Java
130 lines
3.1 KiB
Java
/**
|
|
*
|
|
*/
|
|
package cn.citycraft.CTZServer.commands;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import org.bukkit.command.CommandException;
|
|
import org.bukkit.plugin.Plugin;
|
|
|
|
/**
|
|
* 子命令处理类
|
|
*
|
|
* @author 蒋天蓓 2015年8月22日上午8:29:44
|
|
*/
|
|
public class HandlerSubCommand {
|
|
/**
|
|
* 命令监听类列表
|
|
*/
|
|
private final List<BaseCommand> commandlist = new ArrayList<BaseCommand>();
|
|
|
|
/**
|
|
* 默认命令处理
|
|
*/
|
|
DefaultCommand defaultcommand;
|
|
|
|
/**
|
|
* 插件主类
|
|
*/
|
|
Plugin main;
|
|
|
|
/**
|
|
* 已注册命令列表(包括别名)
|
|
*/
|
|
List<String> RegisterCommandList = new ArrayList<String>();
|
|
|
|
/**
|
|
* 注册子命令
|
|
*
|
|
* @param yum
|
|
* - 插件主类
|
|
*/
|
|
public HandlerSubCommand(final Plugin plugin) {
|
|
this.main = plugin;
|
|
}
|
|
|
|
/**
|
|
* 转移数组
|
|
*
|
|
* @param args
|
|
* - 原数组
|
|
* @param start
|
|
* - 数组开始位置
|
|
* @return 转移后的数组字符串
|
|
*/
|
|
public static String[] moveStrings(final String[] args, final int start) {
|
|
final String[] ret = new String[args.length - start];
|
|
System.arraycopy(args, start, ret, 0, ret.length);
|
|
return ret;
|
|
}
|
|
|
|
/**
|
|
* 获得已注册的命令列表
|
|
*
|
|
* @return - 返回已注册的命令List
|
|
*/
|
|
public List<String> getRegisterCommands() {
|
|
final List<String> cmds = new ArrayList<String>();
|
|
for (final BaseCommand command : commandlist) {
|
|
cmds.addAll(command.getCommandList());
|
|
}
|
|
return cmds;
|
|
}
|
|
|
|
public boolean onCommand(final String label, final String[] args) {
|
|
final String prefix = "§6[§b" + main.getName() + "§6] ";
|
|
final String subcmd = args[0];
|
|
if (subcmd.equalsIgnoreCase("help")) {
|
|
printl("§6=========" + prefix + "插件帮助列表=========");
|
|
for (final BaseCommand command : commandlist) {
|
|
printl(String.format("§6/" + label + " §a%1$s %2$s §6- §b%3$s", command.getName(), command.getPossibleArguments(), command.getDescription()));
|
|
}
|
|
return true;
|
|
}
|
|
final String[] subargs = moveStrings(args, 1);
|
|
for (final BaseCommand command : commandlist) {
|
|
if (command.isValidTrigger(subcmd)) {
|
|
if (subargs.length >= command.getMinimumArguments()) {
|
|
try {
|
|
command.execute(subcmd, subargs);
|
|
return true;
|
|
} catch (final CommandException e) {
|
|
printl(e.getMessage());
|
|
}
|
|
} else {
|
|
printl(prefix + "§c错误的参数 §e使用方法 /" + label + " " + command.getName() + " " + command.getPossibleArguments());
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public void printl(final String msg) {
|
|
System.out.println(msg);
|
|
}
|
|
|
|
/**
|
|
* 注册命令
|
|
*
|
|
* @param command
|
|
* - 被注册的命令类
|
|
*/
|
|
public void registerCommand(final BaseCommand command) {
|
|
commandlist.add(command);
|
|
RegisterCommandList = getRegisterCommands();
|
|
}
|
|
|
|
/**
|
|
* 处理默认命令
|
|
*
|
|
* @param defaultcommand
|
|
* - 默认命令处理类
|
|
*/
|
|
public void setDefaultCommand(final DefaultCommand defaultcommand) {
|
|
this.defaultcommand = defaultcommand;
|
|
}
|
|
|
|
}
|