Need help, onCommand input string as value? (if else)

Discussion in 'Plugin Development' started by Vaupell, Jul 23, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Vaupell

    I know java on a beginner level, and can work our most uses for websites etc.

    But im am new at using the bukkit api.
    i got a few plugins working and running just fine on my "test enviroment" and tested
    on my live server worked fine aswell :D so i got the very basics down..

    Now i am trying to use player commands with arguments.
    and for this i am trying to make it list <x> items i got.

    Here are a few code snippets of my problem..

    First of i am using onCommand to get playerchat, after watching dinnerbones tut.
    Code:
             public boolean onCommand(CommandSender cs, Command cmnd, String string, String[] args) {
    
    and i get it to respond and work fine, with a command like /elist
    but then i want to add the argument of a number int. like this /elist 5
    and if the user dosent enter a number just /elist it will automatic select 6 for him.

    But i am having problems using the number value,, i have tryid using args[1] but java
    says it is a string.

    Code:
                        int tempval;
                        if (args[1] > 0) {
                            tempval = args[1];
                        } else {
                            tempval = 6;
                        }
    
    I hope you can understand my logic here.
    Check if there is a value above 0 and then assign that value to tempval
    if the value is 0 or less then use 6 as tempval..

    The error i get is "Bad operand type for binary operator" on the line with the if(args[1] > 0){

    any suggestions ?
     
  2. Arguments are passed as strings and not as integers, floats or doubles.
    Arrays start counting at 0, so the first argument is args[0] and not args[1].
    Check if the player typed an argument or not first via args.length > 0

    To get a number out of the argument, use Integer.parseInt(String). It will throw a NumberFormatException if the given String could not be parsed to a number (use try/catch)

    This should be enough information for you to figure out your problem.
     
    Vaupell and Streammz like this.
  3. Example of what @Bone008 said with the features you need:
    Code:
    int tempval = 6;
    try {
        if (args.length > 0) {
            tempval = Integer.parseInt(args[0]);
        }
    } catch (NumberFormatException e) { /* Continue, You want an default of 6. */  }
    
    It will make an default value for 6, TRIES to replace it with args[0] as integer, but if it couldn't parse it, it will keep it 6 and continue
     
    Vaupell likes this.
  4. Offline

    Vaupell

    <3 <3 works, thank you!!
     
Thread Status:
Not open for further replies.

Share This Page