Solved Multiple arguments with same result?

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

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

    Tirco

    Hi there helpfull bukkiters, i just got into some simple Java programming and made my first plugin.
    But as any "proper" plugin-developer should i am looking for ways to improve my code so i have a question for you.

    Currently i have a command that does /pokeinfo (Pokemon name) and it returns information about that pokèmon. It looks something like this:
    Show Spoiler

    Code:java
    1.  
    2. if(args[0].equalsIgnoreCase("bulbasaur")) {
    3. player.sendMessage(ChatColor.GREEN + "#001" + ChatColor.YELLOW + " Bulbasaur");
    4. player.sendMessage(ChatColor.GREEN + "Evolves:" + ChatColor.YELLOW + " Level 16 - #002 Ivysaur");
    5. } else if(args[0].equalsIgnoreCase("Ivysaur")) {
    6. player.sendMessage(ChatColor.GREEN + "#002" + ChatColor.YELLOW + " Ivysaur");
    7. player.sendMessage(ChatColor.GREEN + "Evolves:" + ChatColor.YELLOW + " Level 32 - #003
    8. Venusaur");
    9. //code continues with about 500 pokemon.
    10. } else {
    11. player.sendMessage(ChatColor.RED + "No such pokemon.");
    12. return false;


    Is there a way i can turn the "If" part for bulbasaur and venasaur so it checks for multiple arguments that will return the same code? As an example i would like the player to be able to do /pokeinfo 001 and get the same result as he would with /pokeinfo bulbasaur? (something like an "or" line instead of if, or is there something inside the "if" line that allows it to check for several different arguments?)

    Another question; when one of the arguments is a number, is there a way to make it so it doesn't tell the difference between "1" and "001"?


    I hope my question was clear enough :)
    Thanks, and have a nice day!
     
  2. Offline

    SnipsRevival

    Are you looking for something like if(condition1 || condition2) where the || translates to "or"?
     
    Tirco likes this.
  3. Offline

    Tirco

    That sounds about right, yes.
    Is it that simple? o.o
     
  4. Offline

    GyllieGyllie

    Use this

    Code:java
    1. if(args[0].equalsIgnoreCase("bulbasaur") || args[0].equalsIgnoreCase("001")) {
    2. player.sendMessage(ChatColor.GREEN + "#001" + ChatColor.YELLOW + " Bulbasaur");
    3. player.sendMessage(ChatColor.GREEN + "Evolves:" + ChatColor.YELLOW + " Level 16 - #002 Ivysaur");
    4. } else if(args[0].equalsIgnoreCase("Ivysaur") || args[0].equalsIgnoreCase("002")) {
    5. player.sendMessage(ChatColor.GREEN + "#002" + ChatColor.YELLOW + " Ivysaur");
    6. player.sendMessage(ChatColor.GREEN + "Evolves:" + ChatColor.YELLOW + " Level 32 - #003
    7. Venusaur");
    8. //code continues with about 500 pokemon.
    9. } else {
    10. player.sendMessage(ChatColor.RED + "No such pokemon.");
    11. return false;
     
    Tirco likes this.
  5. Offline

    Tirco


    Thank you very much!
    Any response to the second part of my question?
    "when one of the arguments is a number, is there a way to make it so it doesn't tell the difference between "1" and "001"? "
     
  6. Offline

    GyllieGyllie

    i'm not sure but i think it will see the 001 as 1 if you change it to an integer
     
    Tirco likes this.
  7. Offline

    SnipsRevival

    I think if(Integer.parseInt(args[0]) == 1) should work. Just make sure args[0] is actually a number or you will get a NumberFormatException.
     
    Tirco likes this.
  8. Offline

    Necrodoom

    If( check1 || check2)
    {

    }
    Look up logical operators on http://www.tutorialspoint.com/java/java_basic_operators.htm

    For the second question, could cast to int, or you could loop over the string and make a substring from the first non 0 character.

    Edit: typing with tablet is annoyingly slow. Heh.
     
    Tirco likes this.
  9. Offline

    Tirco

    Simple proof that Bukkit people are awesome. Thank you very much for helping out a learner :)
     
Thread Status:
Not open for further replies.

Share This Page