在Bukkit插件中为Bounty读取参数为Integer

时间:2014-08-07 22:06:12

标签: java minecraft bukkit

这只是插件的开始,而且会有更多。这就是我想要的:对于/bounty <name> <amount>我希望能够阅读有关变量的内容,例如int a = args[1],但我不知道如何做到这一点

我试过了,它给了我一些错误。我也想要它所以它只能是命令上的数字。我使用的是bukkit版本:craftbukkit-1.7.10-R0.1-20140804.183445-7

这是我的代码:

public class Main extends JavaPlugin {

    public void onEnable() {
        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) {

        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"));

            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;

            }

            Player target = Bukkit.getServer().getPlayer(args[0]);

            if (target == null) {
                sender.sendMessage(ChatColor.RED + "Could not find player!");
                return true;
            }

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

            }

        }
        return false;
    }

}

2 个答案:

答案 0 :(得分:1)

您可以使用Integer.parseInt(String)从字符串中解析整数。

int bounty = Integer.parseInt(args[1]);

答案 1 :(得分:1)

在实际的代码工作之前,您应该从代码中查看以下代码片段:

if (sender.hasPermission("bounty.setbounty"));
    // This code does nothing, perhaps you meant to return if not true?
if ( !sender.hasPermission("bounty.setbounty"))
    return true;

if (target == null) {
    // ...
}
/* This should be changed to "else"?
 * Or you should actually remove this (if statement),
 * it will never fail as target == null block terminates with "return true;"
 */
if (target != null) {
    // ...
}

我会扩展您现有的代码。首先,确保有第二个参数:

{
    //  }
        if (args.length == 1) sender.sendMessage(NOT_ENOUGH_ARGUMENTS);
        // ...
    }
    return false;
}

然后验证它是否为Integer

{
    //  }
        if (args.length == 1)
            sender.sendMessage(NOT_ENOUGH_ARGUMENTS);
        else if ( !args[1].matches("-?\\d+"))
            // ** To not allow negative integers, remove '-?' **
            sender.sendMessage(NOT_INTEGER);
    }
    return false;
}

然后用Integer.parseInt()解析并使用它!

{
    //  }
        if (args.length == 1)
            sender.sendMessage(NOT_ENOUGH_ARGUMENTS);
        else if ( !args[1].matches("-?\\d+"))
            // ** To not allow negative integers, remove '-?' **
            sender.sendMessage(NOT_INTEGER);
        else {
            int amount = Integer.parseInt(args[1]);
            // The rest is your job to finish...
        }
    }
    return false;
}

了解详情: