使用此代码给我一个内部错误。怎么修?

时间:2014-08-09 13:23:50

标签: java minecraft bukkit

这段代码给了我一个错误,我不知道为什么。我尝试了很多修复工具,但没有一个可以工作:/继承我的代码:

public class Main extends JavaPlugin {



public void onEnable() {
    this.getCommand("bounty").setExecutor(this);
    Bukkit.getServer().getLogger().info("[Bounty] Enabled");
    Bukkit.getServer().getLogger().info("[Bounty] Developed by ITaco_v2");

}

public void onDisable() {
    Bukkit.getServer().getLogger().info("[Bounty] Disabled");
}



public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    final int b = Integer.parseInt(args[1]);
    //Integer to find the bounty amount
    if (!(sender instanceof Player)) {
        sender.sendMessage(ChatColor.RED + "[" + ChatColor.GREEN + "Bounty" + ChatColor.RED + "] " + ChatColor.GOLD + "In game use only!");
        return true;
    }

    if (cmd.getName().equalsIgnoreCase("bounty")){
        if (sender.hasPermission("bounty.setbounty"));
        //Command and Permissions


            if (args.length == 0) {
                sender.sendMessage(ChatColor.RED + "Please specify a Player and a bounty amount.");
                sender.sendMessage(ChatColor.GREEN + "Like this: /bounty <playername> <amount>");
                return true;
            //If player doesn't show enough arguments

            }

                if (b > 10000){
                    sender.sendMessage(ChatColor.GRAY + "[" + ChatColor.RED + "Bounty" + ChatColor.GRAY + "] " + ChatColor.BLUE + "The maximum bounty is 10000");
                    return true;
                //Check for maximum bounty

        }






        final Player target = Bukkit.getServer().getPlayer(args[0]);
        //Bounty target variable

        if (b <= 10000 && target != null) {
            Bukkit.getServer().broadcastMessage(ChatColor.GRAY + "[" + ChatColor.RED + "Bounty" + ChatColor.GRAY + "] " + ChatColor.DARK_GREEN + ChatColor.RED + sender.getName() + "" + " Has set a bounty of " + ChatColor.GRAY + b + ChatColor.DARK_GREEN + " on " + ChatColor.RED + target.getName() + "");
            return true;
        }

        if (target == null) {
            sender.sendMessage(ChatColor.RED + "Could not find player!");
            return true;
        //Checks if target isnt online or cannot be found    


        }

        if (target != null) {
            sender.sendMessage(ChatColor.RED + "Please specify a bounty amount.");
            sender.sendMessage(ChatColor.GREEN + "Like this: /bounty " + args[0] + " <amount>");
            return true;
        //If target is online but not enough arguments  

        }



    }




    return true;
    }
}

我不知道修复应该是什么,但是,这是我的错误。如果有人能帮助我,我会非常高兴:)

org.bukkit.command.CommandException: Unhandled exception executing command 'bounty' in plugin Bounty v1.0
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit-1.7.10-R0.1-20140804.183445-7.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[craftbukkit-1.7.10-R0.1-20140804.183445-7.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:740) ~[craftbukkit-1.7.10-R0.1-20140804.183445-7.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerConnection.java:957) [craftbukkit-1.7.10-R0.1-20140804.183445-7.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:818) [craftbukkit-1.7.10-R0.1-20140804.183445-7.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) [craftbukkit-1.7.10-R0.1-20140804.183445-7.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat.java:47) [craftbukkit-1.7.10-R0.1-20140804.183445-7.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:157) [craftbukkit-1.7.10-R0.1-20140804.183445-7.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    at net.minecraft.server.v1_7_R4.ServerConnection.c(SourceFile:134) [craftbukkit-1.7.10-R0.1-20140804.183445-7.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:667) [craftbukkit-1.7.10-R0.1-20140804.183445-7.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:258) [craftbukkit-1.7.10-R0.1-20140804.183445-7.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:558) [craftbukkit-1.7.10-R0.1-20140804.183445-7.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:469) [craftbukkit-1.7.10-R0.1-20140804.183445-7.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [craftbukkit-1.7.10-R0.1-20140804.183445-7.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
    at com.itaco.bounty.Main.onCommand(Main.java:29) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.7.10-R0.1-20140804.183445-7.jar:git-Bukkit-1.7.9-R0.2-18-ga04b586-b3107jnks]
    ... 13 more

1 个答案:

答案 0 :(得分:4)

在获取索引1处的元素之前,您需要检查数组是否至少包含2个元素。

public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    if (args.length < 2) { // Handle too few arguments
        // sender.sendMessage("Too few arguments...");
        // return true;
    }
    final int b = Integer.parseInt(args[1]);
    //Integer to find the bounty amount
    if (!(sender instanceof Player)) {
        sender.sendMessage(ChatColor.RED + "[" + ChatColor.GREEN + "Bounty" + ChatColor.RED + "] " + ChatColor.GOLD + "In game use only!");
        return true;
    }
    ...