Solved How to check what args[2] are in an if?

Discussion in 'Plugin Development' started by kayc01, Oct 4, 2015.

Thread Status:
Not open for further replies.
  1. Hey guys, so i am trying to compress my 32 else if's into one - two if statements that check arguments and change the outcome accordingly.
    So i don't have to repeat myself in the code 100 times.

    Here is one of the 32 else if's i have:

    Code:
    if ((args[0].equals("addleader")) && (playerTarget.getName().equalsIgnoreCase(args[1])) && (args[2].equalsIgnoreCase("gym1") && (p.hasPermission("pixelgym.admin")))) {
                        if (plugin.getConfig().getString("config.pex").equals("True")) {
                         if (plugin.getConfig().getString("config.enablegroup").equals("True")) {
                           Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pex user " + playerTarget.getName().toString() + " group add " + plugin.getConfig().getString("config.globalgroupname"));
                           Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pex user " + playerTarget.getName().toString() + " add pixelgym.gym1");
                         if (plugin.getConfig().getString("config.giveleaderpokemon").equals("True")) {
                           Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke1")) + " lvl" + (plugin.getConfig().getString("config.gym1poke1lvl")));
                          Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke2")) + " lvl" + (plugin.getConfig().getString("config.gym1poke2lvl")));
                          Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke3")) + " lvl" + (plugin.getConfig().getString("config.gym1poke3lvl")));
                          Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke4")) + " lvl" + (plugin.getConfig().getString("config.gym1poke4lvl")));
                          Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke5")) + " lvl" + (plugin.getConfig().getString("config.gym1poke5lvl")));
                          Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke6")) + " lvl" + (plugin.getConfig().getString("config.gym1poke6lvl")));
                         }
                        }
                         else {
                          Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pex user " + playerTarget.getName().toString() + " add pixelgym.leader");
                          Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pex user " + playerTarget.getName().toString() + " add pixelgym.gym1");
                           if (plugin.getConfig().getString("config.giveleaderpokemon").equals("True")) {
                              Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke1")) + " lvl" + (plugin.getConfig().getString("config.gym1poke1lvl")));
                               Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke2")) + " lvl" + (plugin.getConfig().getString("config.gym1poke2lvl")));
                              Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke3")) + " lvl" + (plugin.getConfig().getString("config.gym1poke3lvl")));
                              Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke4")) + " lvl" + (plugin.getConfig().getString("config.gym1poke4lvl")));
                              Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke5")) + " lvl" + (plugin.getConfig().getString("config.gym1poke5lvl")));
                              Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke6")) + " lvl" + (plugin.getConfig().getString("config.gym1poke6lvl")));
                             }
                         }
                       }
                        else if (plugin.getConfig().getString("config.groupman").equals("True")) {
                           if (plugin.getConfig().getString("config.enablegroup").equals("True")) {
                               Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "manuadd " + playerTarget.getName().toString() + " " + plugin.getConfig().getString("config.globalgroupname"));
                               Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "manuaddp " + playerTarget.getName().toString() + " pixelgym.gym1");
                             if (plugin.getConfig().getString("config.giveleaderpokemon").equals("True")) {
                               Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke1")) + " lvl" + (plugin.getConfig().getString("config.gym1poke1lvl")));
                               Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke2")) + " lvl" + (plugin.getConfig().getString("config.gym1poke2lvl")));
                              Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke3")) + " lvl" + (plugin.getConfig().getString("config.gym1poke3lvl")));
                              Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke4")) + " lvl" + (plugin.getConfig().getString("config.gym1poke4lvl")));
                              Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke5")) + " lvl" + (plugin.getConfig().getString("config.gym1poke5lvl")));
                              Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke6")) + " lvl" + (plugin.getConfig().getString("config.gym1poke6lvl")));
                             }
                           }
                           else {
                          Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "manuaddp " + playerTarget.getName().toString() + " pixelgym.leader");
                          Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "manuaddp " + playerTarget.getName().toString() + " pixelgym.gym1");
                           if (plugin.getConfig().getString("config.giveleaderpokemon").equals("True")) {
                              Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke1")) + " lvl" + (plugin.getConfig().getString("config.gym1poke1lvl")));
                               Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke2")) + " lvl" + (plugin.getConfig().getString("config.gym1poke2lvl")));
                              Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke3")) + " lvl" + (plugin.getConfig().getString("config.gym1poke3lvl")));
                              Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke4")) + " lvl" + (plugin.getConfig().getString("config.gym1poke4lvl")));
                              Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke5")) + " lvl" + (plugin.getConfig().getString("config.gym1poke5lvl")));
                              Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke6")) + " lvl" + (plugin.getConfig().getString("config.gym1poke6lvl")));
                             }
                           }
                        }
                        else if (plugin.getConfig().getString("config.bperm").equals("True")) {
                          if (plugin.getConfig().getString("config.enablegroup").equals("True")) {
                              Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "world " + playerTarget.getWorld().getName());
                              Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "user " + playerTarget.getName().toString());
                               Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "user setgroup " + plugin.getConfig().getString("config.globalgroupname"));
                               Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "user addperm pixelgym.gym1");
                             if (plugin.getConfig().getString("config.giveleaderpokemon").equals("True")) {
                                   Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke1")) + " lvl" + (plugin.getConfig().getString("config.gym1poke1lvl")));
                                   Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke2")) + " lvl" + (plugin.getConfig().getString("config.gym1poke2lvl")));
                                  Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke3")) + " lvl" + (plugin.getConfig().getString("config.gym1poke3lvl")));
                                  Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke4")) + " lvl" + (plugin.getConfig().getString("config.gym1poke4lvl")));
                                  Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke5")) + " lvl" + (plugin.getConfig().getString("config.gym1poke5lvl")));
                                  Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke6")) + " lvl" + (plugin.getConfig().getString("config.gym1poke6lvl")));
                                 }
                          }
                          else {
                          Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "world " + playerTarget.getWorld().getName());
                          Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "user " + playerTarget.getName().toString());
                          Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "user addperm pixelgym.leader");
                          Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "user addperm pixelgym.gym1");
                           if (plugin.getConfig().getString("config.giveleaderpokemon").equals("True")) {
                               Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke1")) + " lvl" + (plugin.getConfig().getString("config.gym1poke1lvl1")));
                               Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke2")) + " lvl" + (plugin.getConfig().getString("config.gym1poke1lvl2")));
                               Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke3")) + " lvl" + (plugin.getConfig().getString("config.gym1poke1lvl3")));
                               Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke4")) + " lvl" + (plugin.getConfig().getString("config.gym1poke1lvl4")));
                               Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke5")) + " lvl" + (plugin.getConfig().getString("config.gym1poke1lvl5")));
                              }
                      }
                        }
                          p.sendMessage(playerTarget.getName().toString() + " has succesfully been added as a " + plugin.getConfig().getString("config.gym1") + " leader!");
                   }
    However so i want it to check what args are at args[2] and input that into the console command dispatch and other things like .getConfig strings.

    My problems are i don't know how to check just that args[2] exist, and to read what is being put in args[2] and then in the console dispatch command just call + args[2] + when needed like this:

    Code:
    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pex user " + playerTarget.getName().toString() + " add pixelgym." + args[2]);
    However i have no idea how i could get args[2] to modify the getConfig().getString("config.gym1poke1") for example, it would kinda look something like this: ?

    getConfig().getString("config." + args[2])
    I just tried that as a test and it gave no errors, would that work the way i wanted it?

    So i may have solved the config part.
    But i still need to check what is in args[2] in the if statement right? How would i do that?

    Thanks a lot for any help!
    - Ckay
     
  2. Offline

    mine-care

    the variable args is a String array, so essentially an array. > https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html Here you can find everything you need to know about it.

    That is going to set a value to the provided path to a string, there is no reason why it would throw an 'error', the changes in the config willa pear only once it is saved tho.

    Also some notes about your code above:

    Why not using booleans? The way you have it now, it will check if the string on the provided path is equal to True, so if i write true instead of True, it wont work. On the other hand, booleans will work either way and will be much more efficient.

    I dont think all that is needed to be repeated over and over when a single condition changes.
    I would say, start over the code (or this class) and think before performing an operation "How can i make it efficient?" and think of alternative ways that will make your code readable, safer and more efficient.
    For example, you could have an enum for the config keys, just saying. If i had some more code, i may have been able to sugest other changes and possible improvments. So far its quite hard for me to read and understand what you are trying to achieve with that piece of code.

    Also i am prety sure you could use a loop instead of
     
    Last edited: Oct 4, 2015
  3. I don't want it to set anything, i just want it to get something.
    For example, in my config i have gym1: and gym2:
    I want it to be that it will get the correct config path based off of args[2], so as i mentioned getConfig().getString("config." +args[2]).

    If the user types args gym1 in args two, in the string it will find config.gym1 and if they type gym2 in args[2] it will get config.gym2 ?

    Also i know this code is messy and awful to read, all i need to know is how to get args[2].
    Because if i don't call it in the IF, it won't look for it right?

    Or do i just need to put + args[2] + when i need it?

    Also, for the part you were talking about checking if it is "True" is simply because it is nicer in caps and most people will follow that rule. If i get extra time to run through the code and correct it i will. But for now i am just trying to get this new feature in.

    Finally, i could not quite find what i was supposed to find with the array's.
    Would you be able to just tell me what little snippet of code i would need there to literally store what is in args[2] so i can call it in the //do stuff.

    Thanks again.
     
  4. Offline

    mcdorli

    One thing, that you should store the "Bukkit.getServer()..." in a variable (same with the Bukket.getConsoleSender() etc.), and voilá, every line is shorter by at least 30 letter.

    2.: "...it's nicer in caps and most people will follow it this way" Hell no, this sentence is wrong on so many levels. Who cares if it's nice or not, they only want it to work. I can write my essays in Times new roman with hands too, and it would be nicer, but i wouldn't get more points for it. And setting it a boolean is more efficient too.

    3.: Why not use switch statements?

    4.: Use loops and function insted of repeating yourself over-and-over.

    5.: Rewrite the whole class, and use these tips, its way faster than going trough the code and repairing everything

    I hope i helped, don't take any of these offensive, its not meant to be.
     
    Last edited: Oct 4, 2015
  5. Thank you for the pointers, i will re look at all of my code soon enough.
    I am fairly new, well i know a bit but no where as much as most of these guys on here.

    All i need to know for now is how to make sure args[2] is there and to store it so i can then call it in the //do stuff.
    This is a part of the whole conversion to making the code better. I need it to be able to change outcomes based on different inputs to args[2].

    Thank you :p
     
  6. Offline

    mcdorli

    @kayc01 So, just for example, I show you how to make this segment of the code more efficient:

    Code:
    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke1")) + " lvl" + (plugin.getConfig().getString("config.gym1poke1lvl")));
    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke2")) + " lvl" + (plugin.getConfig().getString("config.gym1poke2lvl")));
    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke3")) + " lvl" + (plugin.getConfig().getString("config.gym1poke3lvl")));
    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke4")) + " lvl" + (plugin.getConfig().getString("config.gym1poke4lvl")));
    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke5")) + " lvl" + (plugin.getConfig().getString("config.gym1poke5lvl")));
    Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "pokegive " + playerTarget.getWorld().getName() + " " + (plugin.getConfig().getString("config.gym1poke6")) + " lvl" + (plugin.getConfig().getString("config.gym1poke6lvl")));
    
    First, a lot of these are Strings, and they seem to repeat, and the "Bukkit.getConsoleSender()" too, so, you can store them in a variable like this way:

    Code:
    //This can be placed on the beginning of the section too
    
    ConsoleCommandsSender sender = Bukkit.getConsoleSender();
    String s = "pokegive "+ playerTarget.getWorld().getName()+" ");
    
    //Some code
    
    Bukkit.getServer().dispatchCommand(sender, s +(plugin.getConfig().getString("config.gym1poke1"))+" lvl"+(plugin.getConfig().getString("config.gym1poke1lvl")));
    
    Bukkit.getServer().dispatchCommand(sender, s +(plugin.getConfig().getString("config.gym1poke2"))+" lvl"+(plugin.getConfig().getString("config.gym1poke2lvl")));
    
    Bukkit.getServer().dispatchCommand(sender, s +(plugin.getConfig().getString("config.gym1poke3"))+" lvl"+(plugin.getConfig().getString("config.gym1poke3lvl")));
    
    Bukkit.getServer().dispatchCommand(sender, s +(plugin.getConfig().getString("config.gym1poke4"))+" lvl"+(plugin.getConfig().getString("config.gym1poke4lvl")));
    
    Bukkit.getServer().dispatchCommand(sender, s +(plugin.getConfig().getString("config.gym1poke5"))+" lvl"+(plugin.getConfig().getString("config.gym1poke5lvl")));
    
    Bukkit.getServer().dispatchCommand(sender, s +(plugin.getConfig().getString("config.gym1poke6"))+" lvl"+(plugin.getConfig().getString("config.gym1poke6lvl")));
    
    Much shorter, but if you see closely, you notice that the number in the String "config.gym1poke1lvl" increases by 1 every time, so you can use a loop for it:

    Code:
    for (int i = 1; i <= 6; i++) {
        Bukkit.getServer().dispatchCommand(sender, s +(plugin.getConfig().getString("config.gym1poke" + i))+" lvl"+(plugin.getConfig().getString("config.gym1poke" + i + "lvl")));
    }
    
    See, you can do the code in 3 lines, instead of 5, and you can place the variables "s" and "sender" on the beginning of the whole process, so it doesn't take up more space.

    If I'm right, this code repeats itself in every if statement, so, you can put it on the end of the if statements only once, and it does the same thing.

    I hope you understood me and nothing is wrong with my code. Have a nice day!
     
    Last edited: Oct 4, 2015

  7. Thanks for those notes man. I will use some of them for other things. I actually solved it myself by doing a similar thing that just gets args2.

    This has helped me compress my coding though. So thanks!
     
Thread Status:
Not open for further replies.

Share This Page