尝试编写Minecraft插件的代码,当我在游戏中运行/ fakeop时,出现外部错误。 / fakeop(玩家名)虽然有效

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错误]:空
  org.bukkit.command.CommandException:未处理的异常执行
  CortexTroll v1.0插件中的命令'fakeop'
          在org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
  〜[spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
          在org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)
  〜[spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
          在org.bukkit.craftbukkit.v1_10_R1.CraftServer.dispatchCommand(CraftServer.java:646)
  〜[spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
          在net.minecraft.server.v1_10_R1.PlayerConnection.handleCommand(PlayerConnection.java:1351)
  [spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
          在net.minecraft.server.v1_10_R1.PlayerConnection.a(PlayerConnection.java:1186)
  [spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
          在net.minecraft.server.v1_10_R1.PacketPlayInChat.a(PacketPlayInChat.java:45)
  [spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
          在net.minecraft.server.v1_10_R1.PacketPlayInChat.a(PacketPlayInChat.java:1)
  [spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
          在net.minecraft.server.v1_10_R1.PlayerConnectionUtils $ 1.run(SourceFile:13)
  [spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
          在java.util.concurrent.Executors $ RunnableAdapter.call(未知来源)[?: 1.8.0_91]
          在java.util.concurrent.FutureTask.run(未知来源)[?: 1.8.0_91]
          在net.minecraft.server.v1_10_R1.SystemUtils.a(SourceFile:45)[spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
          在net.minecraft.server.v1_10_R1.MinecraftServer.D(MinecraftServer.java:733)
  [spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
          在net.minecraft.server.v1_10_R1.DedicatedServer.D(DedicatedServer.java:399)
  [spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
          在net.minecraft.server.v1_10_R1.MinecraftServer.C(MinecraftServer.java:672)
  [spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
          在net.minecraft.server.v1_10_R1.MinecraftServer.run(MinecraftServer.java:571)
  [spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]
          在java.lang.Thread.run(未知来源)[?: 1.8.0_91]原因:java.lang.ArrayIndexOutOfBoundsException:0
          在我们那里.thecortex.cortextroll.CortexTroll.onCommand(CortexTroll.java:29)
  〜[?:?]
          在org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
  〜[spigot-1.10.2.jar:git-Spigot-5391d73-00359a1]

最佳答案

由于im不允许评论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。

10-04 18:10