Getting Command args into a variable

Discussion in 'Spigot Plugin Development' started by FlailoftheLord, Aug 3, 2018.

  1. Ok, so im new to spigot developing... ive been doing java for a bit longer tho, but the basic code for this doesnt seem to work and only returns errors, what im trying to do is when a player types a command for example:
    /speed (any number here)
    i want to take that first arg: arg[0] as in the speed number... and put that into a variable...
    Ive tried doing this:
    Code (Text):

    String amount = args[0];          
     
    but that doesnt work... can someone help? thankss alot!
    im still new to this so thanks
     
  2. The final parameter in the onCommand method contains a list of character strings, but you want a number of sorts. I'll assume that in integer will do. There is a function in Java to parse a String to an integer: Integer#parseInt. It works as follows.
    Code (Text):
    int amount = Integer.parseInt(args[0]);
    It may throw an exception which you should catch. Parse the integer, then use it for whatever it is you are trying to do. :)
     
  3. read about parsing int or double
    check if args[0] is a double or integer
    check if args[0] is null
    check if args length complies with your required number of arguments
     
  4. Yeah, so trying that gives practically the same errors...
    Code (Text):

    public boolean onCommand(CommandSender sender, Command cmd, String label, String args) {
         
            Player player = (Player) sender;
         
            if (sender instanceof Player) {
             
                String lowerCmd = cmd.getName().toLowerCase();
                Inventory inv = player.getEnderChest();
             
                switch (lowerCmd) {
               
                   case "giveme":
                     
                       int length = args.length();
                     
                       switch (length) {
                     
                       int amount = Integer.parseInt(args[0]);
                       
                         case 1:
                            ItemStack item = new ItemMaker().giveItem(amount);
                            inv.addItem(item);
                            return true;
                         case 0:
                             ItemStack item = new ItemMaker().giveItem(1);
                            inv.addItem(item);
                            return true;
                        default:
                            player.sendMessage(ChatColor.RED + "Your Command was not recognized");
                            return true;
                           
                        }
                    default:
                       player.sendMessage(ChatColor.RED + "Your Command was not recognized");
                       return true;
                }
     
    Essentially im trying to put some items into the player's enderchest when they type the command /giveme (number)
     
  5. I can tell you to start looking at JAVA REGEX to check if a string only contains a number.
     
  6. I spot two mistakes in your code. The signature of onCommand has a String array as its last argument, not just a String. You can tell your compiler to warn you of this if you use the @ Override annotation.
    Code (Text):
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    Next, the parsing of the first argument goes after the "case 1". After all, you have to make sure the array contains an element 0 before you access it.
    Code (Text):
    switch (length) {
    case 1:
      int amount = Integer.parseInt(args[0]);
    And about checking if your String can be parsed to int, the correct way to go about it is catching the exception. The Javadoc I linked in the post above tells you which exception(s) the function may throw. You then put the call in a try-catch-block and handle the exception when it occurs, like so.
    Code (Text):
    int amount;
    try {
      amount = Integer.parseInt(args[0]);
    } catch (NumberFormatException nfe) {
      sender.sendMessage("Number expected: " + args[0]);
      return true;
    }
    Edit: Sorry to user "Override" for accidentally pinging you. xD
     
    • Useful Useful x 1
  7. As I try to avoid as much as possible inducing an exception I will try to provide a simple check it is still up to your coding style and preference what path you will take.

    Code (Text):
    if (args == null || !arg[0].matches("[0-9]+") ) {
        //message player that arg[0] is not an integer
       //do code here or just return.
    }
     
     

  8. THnaks so much!!
    worked perfectly
    im sorry im so new to this so thanks for being patient!
     
  9. thats a little more compact than the try catch....
    hm cool