Turning Arguments into a Value.

Discussion in 'Plugin Development' started by XDMAN00, Apr 24, 2014.

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

    XDMAN00

    I was just wondering how to to an argument in to a value.
    For example, essentials uses the command /pay <player> <amount>
    I was wandering how do i get the string <amount> an byte value.
    Sorry if this is obvious, I am new the developing plugins.
     
  2. Offline

    adam753

    Code:
    int amount = Integer.parseInt(myString);
     
  3. Offline

    skyrimfan1

    To be more specific:
    Code:
    byte amount = Byte.parseByte(arg1);
     
  4. Offline

    BillyGalbreath

    To elaborate more on the above answer:

    Code:java
    1.  
    2. Integer value = null;
    3. try {
    4. value = Integer.valueOf(args[1]);
    5. // not a valid integer
    6. }
    7.  


    And dont forget to null check the args variable before trying to use it ;)
     
  5. Offline

    Alshain01

    BillyGalbreath

    Integer.valueOf() returns an Integer, while Integer.parseInt() returns and int. You should use primitives unless you specifically need the Integer class.
     
  6. Offline

    BillyGalbreath

    Alshain01 I know how data types work (see my exampke above?) :p there is no rule against using Integer over the primitive. Especially since my example declares it null instead of 0. All about personal preference. ;)
     
  7. Offline

    XDMAN00

    Thanks guys.
    So to sum up.
    Code:java
    1. byte b = Byte.parseByte(args[0]);
    2. int i = Integer.parseInteger(args[0]);
    3. short s = Short.parseShort(args[0]);
    4. long l = Long.parseLong(args[0]);
     
  8. Offline

    coasterman10

    Pretty much.

    As BillyGalbreath suggested though it is a good idea to use a try-catch and catch NumberFormatException in case the player doesn't enter a valid number or value, which allows you to send them feedback instead of just letting the method crash.
     
  9. Offline

    Alshain01

    Sure, no rule against it, but it's inefficient. Why waste processing time creating a new object, the code behind Integer.valueOf() reads like this:

    return new Integer(Integer.parseInt(s));

    so your calling parseInt anyway and creating an object on top of it. Why would you do that? And that doesn't even account for the size of an object vs an 32-bit int.
     
  10. Offline

    Skyost

  11. Offline

    BillyGalbreath

    Like you said, sometimes you need an Integer. I'm not arguing that you are wrong, because you're right.
     
Thread Status:
Not open for further replies.

Share This Page