本文介绍了Java问题,为什么我在使用invoke命令时遇到了一个非法的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在为在我的服务器上播放的朋友写一个Spigot Dr Who Tardis插件。 当我执行命令/ tardis help时,命令执行成功。但是当我执行命令/ tardis createcamouflage 735 66 -1352 739 68 -1356时,我得到一个IllegalArgumentException:参数类型不匹配。 最大的问题.. 。为什么第79行的测试返回真的?????? 我在第79和84行标注了评论。 以下是代码: public class CommandHandler { 插件插件; TardisListener tardisListener; public CommandHandler(插件实例){ 此 .plugin =实例; 此 .tardisListener = new TardisListener( this .plugin); } 字符串 [] commands1 = { help, create , save }; 字符串 [] methods1 = { giveHelp, createBox, saveBoxes }; 字符串 [] commands2 = { createcamouflage}; String [] methods2 = { createTemplate }; HashMap< String,Method> actions = new HashMap< String,Method>(); HashMap< String,Method> actions1 = new HashMap< String,Method>(); HashMap< String,Method> actions2 = new HashMap< String,Method>(); private HashMap< String,Method> dictionary( String []命令, String [] methods){ for ( int i = 0; i< commands.length; i ++){ Method m [] = 此 .getClass()getDeclaredMethods(); 尝试 { for ( int c = 0 ; c< m.length; c ++){ if (m [c] .getName()。equalsIgnoreCase(methods [i])){ this .actions.put(commands [i],米并[c]); } } } catch (SecurityException e){ / / TODO自动生成的捕获块 e.printStackTrace(); } } 返回行动; } public boolean executeCommand(CommandSender sender,Command cmd ,字符串标签,字符串 [] args){ if (sender instanceof Player){ Player player =(Player)sender; player.sendMessage( 收到的命令); } else { System.out.println( Tardis:Command Received); } if (cmd.getName()。equalsIgnoreCase( tardis)){ // 如果播放器键入/ tardis然后执行以下操作... 如果(args.length!= 0 ){ String arg = args [ 0 ]。 toLowerCase(); System.out.println( Tardis:Building Dictionary); actions1 = dictionary(commands1,methods1); actions2 = dictionary(commands2,methods2); System.out.println( Tardis:启动第一阶段多态性); 方法方法; 尝试 { for ( String key:actions1.keySet()){ if (key.equalsIgnoreCase(arg)){ // 第79行,为什么要返回true?! System.out.println( Tardis:第一阶段多态执行); CommandHandler handler = new CommandHandler(plugin); method = actions1.get(arg); Object [] obj = {sender}; method.invoke(handler,obj); // 第84行,为什么要调用它? 返回 true; } } System.out.println( Tardis:开始第二阶段多态性); for ( String key:actions2.keySet()){ if (key.equalsIgnoreCase(arg)){ CommandHandler handler = new CommandHandler(plugin); method = actions2.get(arg); Object [] obj = {args}; System.out.println( Tardis:调用createTemplate函数); method.invoke(handler,obj); 返回 true; } } } catch (IllegalAccessException e){ / / TODO自动生成的捕获块 e.printStackTrace(); } catch (IllegalArgumentException e){ // TODO自动生成的捕获块 e.printStackTrace(); } catch (InvocationTargetException e){ // TODO自动生成的捕获块 e.printStackTrace(); } catch (SecurityException e){ // TODO自动生成的捕获块 e.printStackTrace(); } } 其他 { 字符串 comError = 未定义的设置类型。; sendError(sender,comError); } } return false; } 这是错误: [14:39:46] [服务器主题/信息]:Tardis:收到的命令 [14:39:46] [服务器主题/信息]:Tardis:建筑词典 [14:39:46] [服务器线程/信息]:Tardis:启动第一阶段多态性 [14:39:46] [服务器线程/信息]:Tardis:第一阶段多态执行 [14:39:46] [服务器线程/ WARN]:java.lang.IllegalArgumentException:参数类型不匹配 [14: 39:46] [服务器线程/警告]:at sun.reflect.NativeMethodAccessorImpl.invoke0(原生方法) [14:39:46] [服务器线程/警告]:在sun.reflect。 NativeMethodAccessorImpl.invoke(未知来源) [14:39:46] [服务器线程/警告]:at sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源) [ 14:39:46] [服务器线程/警告]:在java.lang.reflect.Method.invoke(未知来源) [14:39:46] [服务器线程/警告]:在au.com.mshcraft.tardis.CommandHan dler.executeCommand(CommandHandler.java:84) [14:39:46] [服务器线程/警告]:at au.com.mshcraft.tardis.Tardis.onCommand(Tardis.java:28 ) [14:39:46] [服务器线程/警告]:at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) [14 :39:46] [服务器线程/ WARN]:at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [14:39:46] [服务器线程/ WARN] :at net.minecraft.server.v1_9_R2.CommandBlockListenerAbstract.executeCommand(CommandBlockListenerAbstract.java:219) [14:39:46] [服务器线程/警告]:at net.minecraft.server.v1_9_R2 .CommandBlockListenerAbstract.a(CommandBlockListenerAbstract.java:104) [14:39:46] [服务器线程/警告]:在net.minecraft.server.v1_9_R2.BlockCommand.b(BlockCommand.java: 77) [14:39:46] [服务器线程/警告]:在net.minecraft.server.v1_9_R2.WorldServer.a(WorldServer.java:737) [14:39:46] [服务器线程/警告]:在net.minecraft.server.v1_9_R2.WorldServ er.doTick(WorldServer.java:249) [14:39:46] [服务器线程/警告]:在net.minecraft.server.v1_9_R2.MinecraftServer.D(MinecraftServer.java:781) ) [14:39:46] [服务器线程/警告]:在net.minecraft.server.v1_9_R2.DedicatedServer.D(DedicatedServer.java:399) [14:39:46] [服务器线程/警告]:在net.minecraft.server.v1_9_R2.MinecraftServer.C(MinecraftServer.java:665) [14:39:46] [服务器thread / WARN]:在net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:564) [14:39:46] [服务器线程/警告]:在java.lang .Thread.run(未知来源) 我的尝试: 尝试更改测试: if (actions1。 containsKey(arg))) 到目前的状态。解决方案 不要问我这个程序是如何反向运行的,但我修复了它: private HashMap< String,Method> dictionary( String []命令, String [] methods){ actions = null; actions = new HashMap< String,Method>(); 当我在字典函数的第二次调用中明确地将键createcamouflage分配给动作HashMap时,为什么在字典方法中HashMap的动作返回时使用键createcamouflage第一次调用它。这非常令人费解,它看起来好像2个调用语句在编译时被反转。 ie: actions1 = dictionary(commands1,methods1); actions2 =字典(commands2,methods2); 似乎正在运行: actions2 = dictionary(commands2,methods2); actions1 =字典(commands1,methods1); 有没有人有答案这个还是我读错了/我错过了我做错的事吗? I am writing a Spigot Dr Who Tardis plugin for a friend who plays on my server.When I execute the command "/tardis help", the command executes successfully. However when I execute the command "/tardis createcamouflage 735 66 -1352 739 68 -1356", I get an IllegalArgumentException: argument type mismatch.The big question...Why is the test at line 79 returning true?????? I have marked lines 79 and 84 with comments.Here is the code:public class CommandHandler {Plugin plugin;TardisListener tardisListener;public CommandHandler(Plugin instance) {this.plugin = instance;this.tardisListener = new TardisListener(this.plugin);}String[] commands1 = {"help","create","save"};String[] methods1 = {"giveHelp", "createBox","saveBoxes"};String[] commands2 = {"createcamouflage"};String[] methods2 = {"createTemplate"};HashMap<String, Method> actions = new HashMap<String, Method>();HashMap<String, Method> actions1 = new HashMap<String, Method>();HashMap<String, Method> actions2 = new HashMap<String, Method>();private HashMap<String, Method> dictionary(String[] commands, String[] methods) {for (int i=0; i<commands.length; i++) {Method m[] = this.getClass().getDeclaredMethods();try {for (int c = 0; c < m.length; c++) {if (m[c].getName().equalsIgnoreCase(methods[i])) { this.actions.put(commands[i], m[c]);}}} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return actions;}public boolean executeCommand(CommandSender sender, Command cmd, String label, String[] args) {if (sender instanceof Player) {Player player = (Player) sender;player.sendMessage("Command Received");} else {System.out.println("Tardis: Command Received");}if (cmd.getName().equalsIgnoreCase("tardis")) { // If the player typed /tardis then do the following...if (args.length != 0) {String arg = args[0].toLowerCase();System.out.println("Tardis: Building Dictionary");actions1 = dictionary(commands1, methods1);actions2 = dictionary(commands2, methods2);System.out.println("Tardis: Start 1st stage polymorphism");Method method;try {for (String key : actions1.keySet()) {if (key.equalsIgnoreCase(arg)) { // line 79, Why are you returning true?!System.out.println("Tardis: 1st stage polymorphism executing");CommandHandler handler = new CommandHandler(plugin);method = actions1.get(arg);Object[] obj = {sender};method.invoke(handler, obj); // line 84, why are you being invoked?return true; }}System.out.println("Tardis: Start 2nd stage polymorphism");for (String key : actions2.keySet()) {if (key.equalsIgnoreCase(arg)) {CommandHandler handler = new CommandHandler(plugin);method = actions2.get(arg);Object[] obj = {args};System.out.println("Tardis: invoking createTemplate function");method.invoke(handler, obj);return true;}}} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvocationTargetException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SecurityException e) {// TODO Auto-generated catch blocke.printStackTrace();}} else {String comError = "Undefined Setting Type."; sendError(sender, comError);}}return false;}And here is the error:[14:39:46] [Server thread/INFO]: Tardis: Command Received[14:39:46] [Server thread/INFO]: Tardis: Building Dictionary[14:39:46] [Server thread/INFO]: Tardis: Start 1st stage polymorphism[14:39:46] [Server thread/INFO]: Tardis: 1st stage polymorphism executing[14:39:46] [Server thread/WARN]: java.lang.IllegalArgumentException: argument type mismatch[14:39:46] [Server thread/WARN]: at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[14:39:46] [Server thread/WARN]: at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)[14:39:46] [Server thread/WARN]: at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)[14:39:46] [Server thread/WARN]: at java.lang.reflect.Method.invoke(Unknown Source)[14:39:46] [Server thread/WARN]: at au.com.mshcraft.tardis.CommandHandler.executeCommand(CommandHandler.java:84)[14:39:46] [Server thread/WARN]: at au.com.mshcraft.tardis.Tardis.onCommand(Tardis.java:28)[14:39:46] [Server thread/WARN]: at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)[14:39:46] [Server thread/WARN]: at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)[14:39:46] [Server thread/WARN]: at net.minecraft.server.v1_9_R2.CommandBlockListenerAbstract.executeCommand(CommandBlockListenerAbstract.java:219)[14:39:46] [Server thread/WARN]: at net.minecraft.server.v1_9_R2.CommandBlockListenerAbstract.a(CommandBlockListenerAbstract.java:104)[14:39:46] [Server thread/WARN]: at net.minecraft.server.v1_9_R2.BlockCommand.b(BlockCommand.java:77)[14:39:46] [Server thread/WARN]: at net.minecraft.server.v1_9_R2.WorldServer.a(WorldServer.java:737)[14:39:46] [Server thread/WARN]: at net.minecraft.server.v1_9_R2.WorldServer.doTick(WorldServer.java:249)[14:39:46] [Server thread/WARN]: at net.minecraft.server.v1_9_R2.MinecraftServer.D(MinecraftServer.java:781)[14:39:46] [Server thread/WARN]: at net.minecraft.server.v1_9_R2.DedicatedServer.D(DedicatedServer.java:399)[14:39:46] [Server thread/WARN]: at net.minecraft.server.v1_9_R2.MinecraftServer.C(MinecraftServer.java:665)[14:39:46] [Server thread/WARN]: at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:564)[14:39:46] [Server thread/WARN]: at java.lang.Thread.run(Unknown Source)What I have tried:tried changing the tests from:if (actions1.containsKey(arg)))to what they currently are. 解决方案 Don't ask me how this program is running in reverse but I fixed it with:private HashMap<String, Method> dictionary(String[] commands, String[] methods) { actions = null; actions = new HashMap<String, Method>();Why is the actions HashMap in the dictionary method returning with the key "createcamouflage" the first time its invoked when I am clearly assigning the key "createcamouflage" to the actions HashMap in the 2nd invokation of the dictionary function. This is very puzzling, its appearing as if the 2 invoking statements are inverted at compile time.ie:actions1 = dictionary(commands1, methods1);actions2 = dictionary(commands2, methods2);Appears to be running as:actions2 = dictionary(commands2, methods2);actions1 = dictionary(commands1, methods1);Does anyone have an answer to this or am I reading it incorrectly / did I miss something I did wrong? 这篇关于Java问题,为什么我在使用invoke命令时遇到了一个非法的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 07:59