Reading arguments as Integer for a Bounty in a Bukkit plugin

我们两清 提交于 2019-12-24 17:00:01

问题


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.

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

Here is my code:

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

}

回答1:


You can parse integer from string using Integer.parseInt(String).

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



回答2:


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

Then validate if it is an 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;
}

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

Read more:

  • Minecraft Forge Making Custom Player Command Issues


来源:https://stackoverflow.com/questions/25193058/reading-arguments-as-integer-for-a-bounty-in-a-bukkit-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!