在Bukkit插件中以整数形式获取赏金参数 [英] Reading arguments as Integer for a Bounty in a Bukkit plugin
问题描述
这只是插件的开始,还会有更多.这就是我想要的:对于/bounty <name> <amount>
,我希望能够读取所读取的内容,以生成类似int a = args[1]
的变量,但是我不知道该怎么做.
This is just the start of the plugin and there will be more. This is what I want to have: For /bounty <name> <amount>
I want to be able to read what is read on the amount to make a variable like int a = args[1]
, but I don't know how to do that.
我已经尝试过,但给了我一些错误.我也想要它,所以它只能是命令上的数字.我正在使用bukkit版本:craftbukkit-1.7.10-R0.1-20140804.183445-7
I have tried and it gave me some errors. I also want it so it can only be a number on the command. I am using bukkit version: 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;
}
}
推荐答案
在进行实际代码编写之前,您应该查看代码中的以下代码片段:
Before the actual codework, you should review the following code snippets from your code:
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) {
// ...
}
我将扩展您现有的代码.首先,请确保有第二个参数:
I'll extend upon your existing code. Firstly, make sure there is a 2nd argument:
{
// }
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()
解析它并使用它!
Then parse it with Integer.parseInt()
and use it!
{
// }
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;
}
了解更多信息
这篇关于在Bukkit插件中以整数形式获取赏金参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!