这里小雨.
RT

我想在A.java B.java中实现类似于
this.getCommand("指令1").setExecutor(new A());

this.getCommand("指令1").setExecutor(new B());

并且可以一起使用

而不是
this.getCommand("指令1").setExecutor(new A());

this.getCommand("指令2").setExecutor(new B());


这样是如何实现的

cnYeqi
你想要两个插件使用同一个指令?只能通过提供API吧...
那为什么不写在一起

这里小雨.
cnYeqi 发表于 2022-5-3 10:54
你想要两个插件使用同一个指令?只能通过提供API吧...
那为什么不写在一起 ...

看着太难受了

cnYeqi

分包分模块按照软件工程的规范写的话不会乱,如果想要两个插件用一个指令我能想到的就是其中一个插件给另一个提供API接口

William_Shi
"一起使用"是什么意思?
执行指令的时候,要么调用 A 的 onCommand 要么调用 B 的 onCommand ,不可能同时调用
仔细看一下 PluginCommand 类的代码:


  1.   private CommandExecutor executor;
  2.   
  3.   ......

  4.   public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
  5.     boolean success = false;
  6.     if (!this.owningPlugin.isEnabled())
  7.       throw new CommandException("Cannot execute command '" + commandLabel + "' in plugin " + this.owningPlugin.getDescription().getFullName() + " - plugin is disabled.");
  8.     if (!testPermission(sender))
  9.       return true;
  10.     try {
  11.       success = this.executor.onCommand(sender, this, commandLabel, args);
  12.     } catch (Throwable ex) {
  13.       throw new CommandException("Unhandled exception executing command '" + commandLabel + "' in plugin " + this.owningPlugin.getDescription().getFullName(), ex);
  14.     }
  15.     if (!success && this.usageMessage.length() > 0) {
  16.       byte b;
  17.       int i;
  18.       String[] arrayOfString;
  19.       for (i = (arrayOfString = this.usageMessage.replace("<command>", commandLabel).split("\n")).length, b = 0; b < i; ) {
  20.         String line = arrayOfString[b];
  21.         sender.sendMessage(line);
  22.         b++;
  23.       }
  24.     }
  25.     return success;
  26.   }
复制代码


这说明一个指令同时就只能有一个 CommandExecutor,只有这一个 onCommand 方法能执行,你这个需求不可能实现。必须调整架构,比如说在一个 onCommand 方法里面同时调用 A 和 B 类的方法。

第一页 上一页 下一页 最后一页