- public boolean onCommand(CommandSender sender, Command arg1, String label, String[] args) {
- if(label.equalsIgnoreCase("xxx")){
- //判断是否是玩家
- if(!(sender instanceof Player)){
- sender.sendMessage("只能在游戏里执行此命令!");
- return true;
- }
- //判断是否有权限
- if(!sender.hasPermission("xxx.use")){
- sender.sendMessage("你没有权限。");
- return true;
- }
- //强转对象
- Player p=(Player) sender;
- //
- //无参数
- if(args.length==0){
- p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6==========[&b 帮助&6 ]=========="));
- for(Method method : this.getClass().getDeclaredMethods()){
- if(!method.isAnnotationPresent(SubCommand.class)){
- continue;
- }
- SubCommand sub=method.getAnnotation(SubCommand.class);
- p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6/xxx &b"+sub.cmd()+" &3"+sub.arg()+"&6-&a "+sub.des()));
- }
- return true;
- }
- for(Method method:this.getClass().getDeclaredMethods()){
- if(!method.isAnnotationPresent(SubCommand.class)){
- continue;
- }
- SubCommand sub=method.getAnnotation(SubCommand.class);
- if(!sub.cmd().equalsIgnoreCase(args[0])){
- continue;
- }
-
- try {
- method.invoke(this, p,args);
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- }
- return true;
- }
- p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&a未找到此子命令:&c"+args[0]));
- return true;
- }
- return false;
- }
复制代码 |