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());
这样是如何实现的
我想在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());
这样是如何实现的
你想要两个插件使用同一个指令?只能通过提供API吧...
那为什么不写在一起
那为什么不写在一起
cnYeqi 发表于 2022-5-3 10:54
你想要两个插件使用同一个指令?只能通过提供API吧...
那为什么不写在一起 ...
看着太难受了
这里小雨. 发表于 2022-5-3 10:54
看着太难受了
分包分模块按照软件工程的规范写的话不会乱,如果想要两个插件用一个指令我能想到的就是其中一个插件给另一个提供API接口
"一起使用"是什么意思?
执行指令的时候,要么调用 A 的 onCommand 要么调用 B 的 onCommand ,不可能同时调用
仔细看一下 PluginCommand 类的代码:
复制代码
这说明一个指令同时就只能有一个 CommandExecutor,只有这一个 onCommand 方法能执行,你这个需求不可能实现。必须调整架构,比如说在一个 onCommand 方法里面同时调用 A 和 B 类的方法。
执行指令的时候,要么调用 A 的 onCommand 要么调用 B 的 onCommand ,不可能同时调用
仔细看一下 PluginCommand 类的代码:
 
-   private CommandExecutor executor;
 
-   
 
-   ......
 
 
-   public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
 
-     boolean success = false;
 
-     if (!this.owningPlugin.isEnabled())
 
-       throw new CommandException("Cannot execute command '" + commandLabel + "' in plugin " + this.owningPlugin.getDescription().getFullName() + " - plugin is disabled."); 
 
-     if (!testPermission(sender))
 
-       return true; 
 
-     try {
 
-       success = this.executor.onCommand(sender, this, commandLabel, args);
 
-     } catch (Throwable ex) {
 
-       throw new CommandException("Unhandled exception executing command '" + commandLabel + "' in plugin " + this.owningPlugin.getDescription().getFullName(), ex);
 
-     } 
 
-     if (!success && this.usageMessage.length() > 0) {
 
-       byte b;
 
-       int i;
 
-       String[] arrayOfString;
 
-       for (i = (arrayOfString = this.usageMessage.replace("<command>", commandLabel).split("\n")).length, b = 0; b < i; ) {
 
-         String line = arrayOfString[b];
 
-         sender.sendMessage(line);
 
-         b++;
 
-       } 
 
-     } 
 
-     return success;
 
- }
这说明一个指令同时就只能有一个 CommandExecutor,只有这一个 onCommand 方法能执行,你这个需求不可能实现。必须调整架构,比如说在一个 onCommand 方法里面同时调用 A 和 B 类的方法。
William_Shi 发表于 2022-5-3 11:22
"一起使用"是什么意思?
执行指令的时候,要么调用 A 的 onCommand 要么调用 B 的 onCommand ,不可能同时 ...
我之前好像看到一个cmd指令集来实现此功能的
不知道有没有类似的实例?