【问题标题】:Reading arguments as Integer for a Bounty in a Bukkit plugin在 Bukkit 插件中将参数作为 Bounty 的整数读取
【发布时间】:2014-08-07 22:06:12
【问题描述】:

这只是插件的开始,以后还会有更多。这就是我想要的:对于/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;
    }

}

【问题讨论】:

  • 随机指针:你能存储对Bukkit.getServer().getLogger()的引用吗?可能会使您的日志记录过程更容易。 :)
  • 另外,不要抑制弃用警告。不推荐使用 API 是有原因的。并且没有必要将Player 变量设为final。一旦方法返回并有资格进行垃圾收集,它就会失去作用域,因此您只是无缘无故地强制 JVM 在 permgen 中分配空间。
  • its given me some errors 请在您的帖子中包含它们,您使用的是什么版本的 Bukkit?
  • @MrLore 该代码没有给我任何错误
  • 您不需要第 26 行的 if 语句。

标签: java minecraft bukkit


【解决方案1】:

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

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

【讨论】:

  • 请注意,这会抛出NumberFormatException
【解决方案2】:

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

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;
}

了解更多:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多