Minecraft插件外部错误

时间:2016-06-29 04:32:29

标签: java plugins minecraft

尝试编写Minecraft插件代码,当我在游戏中运行/ fakeop时出现外部错误。 / fakeop(playername)可以工作

public class CortexTroll extends JavaPlugin {

     @Override
     public void onEnable() {}

     @Override
     public void onDisable() {}

     public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
          if (cmd.getName().equalsIgnoreCase("fakeop")) {
               Player player = (Player) sender;
               Player target = Bukkit.getServer().getPlayer(args[0]);
               if (args.length == 0) {
                    player.sendMessage("Specify a player to Op. /fakeop <target>");
                    return true;
               }
               player.sendMessage("Fake Opped " + args[0]);
               Bukkit.broadcastMessage(ChatColor.GRAY + "[" + player.getName() + ": Opped " + args[0] + "]");
               target.sendMessage(ChatColor.YELLOW + "You are now op!");
          }
          return true;
     }
 }

错误:

  

Illuminatiiiiii发出服务器命令:/ fakeop [21:33:10 ERROR]:null   org.bukkit.command.CommandException:未处理的异常执行   在插件CortexTroll v1.0中命令'fakeop'           在org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)   〜[插口1.10.2.jar:GIT-插口5391d73-00359a1]           在org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)   〜[插口1.10.2.jar:GIT-插口5391d73-00359a1]           在org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchCommand(CraftServer.java:646)   〜[插口1.10.2.jar:GIT-插口5391d73-00359a1]           at net.minecraft.server.v1_10_R1.PlayerConnection.handleCommand(PlayerConnection.java:1351)   [插口1.10.2.jar:GIT-插口5391d73-00359a1]           at net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:1186)   [插口1.10.2.jar:GIT-插口5391d73-00359a1]           at net.minecraft.server.v1_10_R1.PacketPlayInChat.a(PacketPlayInChat.java:45)   [插口1.10.2.jar:GIT-插口5391d73-00359a1]           at net.minecraft.server.v1_10_R1.PacketPlayInChat.a(PacketPlayInChat.java:1)   [插口1.10.2.jar:GIT-插口5391d73-00359a1]           at net.minecraft.server.v1_10_R1.PlayerConnectionUtils $ 1.run(SourceFile:13)   [插口1.10.2.jar:GIT-插口5391d73-00359a1]           at java.util.concurrent.Executors $ RunnableAdapter.call(Unknown Source)[?:1.8.0_91]           at java.util.concurrent.FutureTask.run(Unknown Source)[?:1.8.0_91]           at net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45)[spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]           at net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:733)   [插口1.10.2.jar:GIT-插口5391d73-00359a1]           at net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:399)   [插口1.10.2.jar:GIT-插口5391d73-00359a1]           at net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:672)   [插口1.10.2.jar:GIT-插口5391d73-00359a1]           at net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:571)   [插口1.10.2.jar:GIT-插口5391d73-00359a1]           at java.lang.Thread.run(Unknown Source)[?:1.8.0_91]引起:java.lang.ArrayIndexOutOfBoundsException:0           at us.thecortex.cortextroll.CortexTroll.onCommand(CortexTroll.java:29)   〜[:?]           在org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)   〜[插口1.10.2.jar:GIT-插口5391d73-00359a1]

1 个答案:

答案 0 :(得分:2)

因为我不允许评论jet,所以你可以尝试:在检查args.length是否为0的if语句之后移动下面的行

public Class CortexTroll extends JavaPlugin{

     @Override
     public void onEnable() {}

     @Override
     public void onDisable() {}

     public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
          if (cmd.getName().equalsIgnoreCase("fakeop")) {
               Player player = (Player) sender;
               if (args.length == 0) {
                    player.sendMessage("Specify a player to Op. /fakeop <target>");
                    return true;
               }
               Player target = Bukkit.getServer().getPlayer(args[0]); // Is now below the if-statement
               player.sendMessage("Fake Opped " + args[0]);
               Bukkit.broadcastMessage(ChatColor.GRAY + "[" + player.getName() + ": Opped " + args[0] + "]");
               target.sendMessage(ChatColor.YELLOW + "You are now op!");
          }
          return true;
     }
 }

为什么要修复此错误?

如果执行/fakeop而没有任何参数,则args的长度为0.如果您尝试访问args[0],则会得到ArrayIndexOutOfBoundsException,因为它需要一个长度为1的数组

编辑:感谢您指出它不是NullPointer。