Solved Only first "if" statement works

Discussion in 'Plugin Development' started by SuperEvan200, Mar 24, 2013.

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

    SuperEvan200

    Hi. I'm trying to get my if statements to execute different commands on different commands. The problem is, only
    /kingdom help
    /k join Delolia
    works. (The first and second if statements) The rest do not work nor write to config.yml when I execute the proper command.

    Here are my if statements:
    Code:
        public boolean onCommand(CommandSender sender, Command cmd,  String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("kingdom")){
                if(args[0].equalsIgnoreCase("help"))
                    player.sendMessage(ChatColor.DARK_RED + "To join a kingdom, say /k join Delolia, Siviel, Natura, or Valkyerie");
            }else if(commandLabel.equalsIgnoreCase("k")){
                if(args[0].equalsIgnoreCase("join"))
                    if(args[1].equalsIgnoreCase("delolia"))
                        player.setDisplayName(kingdom1 + player.getDisplayName());
                        getConfig().getString("prefixes.", "Default Value");
                            this.getConfig().set("prefixes."+player.getName(),kingdom3);       
                            saveConfig();
                            reloadConfig();
            }else if(commandLabel.equalsIgnoreCase("k")){
                if(args[0].equalsIgnoreCase("join"))
                    if(args[1].equalsIgnoreCase("syviel"))
                        player.setDisplayName(kingdom2 + player.getDisplayName());
                        getConfig().getString("prefixes.", "Default Value");
                            this.getConfig().set("prefixes."+player.getName(),kingdom2);       
                            saveConfig();
                            reloadConfig();
            }else if(commandLabel.equalsIgnoreCase("k")){
                if(args[0].equalsIgnoreCase("join"))
                    if(args[1].equalsIgnoreCase("natura"))
                        player.setDisplayName(kingdom3 + player.getDisplayName());   
                        getConfig().getString("prefixes.", "Default Value");
                            this.getConfig().set("prefixes."+player.getName(),kingdom3);       
                            saveConfig();
                            reloadConfig();
            }else if(commandLabel.equalsIgnoreCase("k")){
                if(args[0].equalsIgnoreCase("join"))
                    if(args[1].equalsIgnoreCase("valkyrie"))
                        player.setDisplayName(kingdom4 + player.getDisplayName());
                        getConfig().getString("prefixes.", "Default Value");
                            this.getConfig().set("prefixes."+player.getName(),kingdom4);       
                            saveConfig();
                            reloadConfig();
            }
     
  2. Offline

    WolfGangSen

    quite simple

    see where you keep saying

    else if(commandLabel.equalsIgnoreCase("k")){

    well after the first one it is never going to get to any of the later ones

    because its gone into the first one and therefore is skipping all the other else ifs
     
  3. Offline

    SuperEvan200


    Thanks. How can I change it to what I want though?
     
  4. Offline

    raGan.

    Generally by understanding how ifs work.
    More specifically by having only one if for "k" and only one if for "join", but multiple ifs for {"delolia", "syviel", "natura', "valkyrie"}.
     
    SuperEvan200 likes this.
  5. Offline

    WolfGangSen

    you want to be doing something more like this

    Code:
    if(commandLabel.equalsIgnoreCase("kingdom"))
    {
    //kingdom stuff...
    }
    else if(commandLabel.equalsIgnoreCase("kingdom")
    {
        if(args[0].equalsIgnoreCase("join"))
        {
          if(args[1].equalsIgnoreCase("delolia"))
          {
              //join delolia code....
          }
          else if(args[1].equalsIgnoreCase("syviel"))
          {
              //join syviel code here
          }
          //etc.....
        }
    }
    
     
    SuperEvan200 likes this.
  6. Offline

    Lolmewn

    Some more brackets would be nice :)
     
    SuperEvan200 likes this.
  7. Offline

    SuperEvan200

    raGan. WolfGangSen

    Thanks, it worked! One problem however. Even though it sets the correct prefix, it writes the wrong prefix in the config.yml. If I logout and back in, my current prefixes are lost and I get the prefix "[Valkyrie]", the last one in the if statements.

    Here is my updated code.
    Code:
        public boolean onCommand(CommandSender sender, Command cmd,  String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("kingdom")){
                if(args[0].equalsIgnoreCase("help"))
                    player.sendMessage(ChatColor.DARK_RED + "To join a kingdom, say /k join Delolia, Siviel, Natura, or Valkyerie");
            }else if(commandLabel.equalsIgnoreCase("k")){
                if(args[0].equalsIgnoreCase("join"))
                    if(args[1].equalsIgnoreCase("delolia"))
                        player.setDisplayName(kingdom1 + player.getDisplayName());
                        getConfig().getString("prefixes.", "Default Value");
                            this.getConfig().set("prefixes."+player.getName(),kingdom3);       
                            saveConfig();
                            reloadConfig();
                    if(args[1].equalsIgnoreCase("syviel"))
                        player.setDisplayName(kingdom2 + player.getDisplayName());
                        getConfig().getString("prefixes.", "Default Value");
                            this.getConfig().set("prefixes."+player.getName(),kingdom2);       
                            saveConfig();
                            reloadConfig();
                    if(args[1].equalsIgnoreCase("natura"))
                        player.setDisplayName(kingdom3 + player.getDisplayName());   
                        getConfig().getString("prefixes.", "Default Value");
                            this.getConfig().set("prefixes."+player.getName(),kingdom3);       
                            saveConfig();
                            reloadConfig();
                    if(args[1].equalsIgnoreCase("valkyrie"))
                        player.setDisplayName(kingdom4 + player.getDisplayName());
                        getConfig().getString("prefixes.", "Default Value");
                            this.getConfig().set("prefixes."+player.getName(),kingdom4);       
                            saveConfig();
                            reloadConfig();
            }
    Thanks so much! I fixed the brackets, and everything works perfectly. If anyone stumbles upon this and needs any help here is the code that works.

    Code:
        public boolean onCommand(CommandSender sender, Command cmd,  String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("kingdom"))
            {
                if(args[0].equalsIgnoreCase("help"))
                    player.sendMessage(ChatColor.DARK_RED + "To join a kingdom, say /k join Delolia, Siviel, Natura, or Valkyerie");
            }
            else if(commandLabel.equalsIgnoreCase("k"))
            {
                if(args[0].equalsIgnoreCase("join"))
                {
                    if(args[1].equalsIgnoreCase("delolia"))
                    {
                        player.setDisplayName(kingdom1 + player.getDisplayName());
                        getConfig().getString("prefixes.", "Default Value");
                            this.getConfig().set("prefixes."+player.getName(),kingdom1);       
                            saveConfig();
                            reloadConfig();
                    }
                    if(args[1].equalsIgnoreCase("syviel"))
                    {
                        player.setDisplayName(kingdom2 + player.getDisplayName());
                        getConfig().getString("prefixes.", "Default Value");
                            this.getConfig().set("prefixes."+player.getName(),kingdom2);       
                            saveConfig();
                            reloadConfig();
                    }
                    if(args[1].equalsIgnoreCase("natura"))
                    {
                        player.setDisplayName(kingdom3 + player.getDisplayName());   
                        getConfig().getString("prefixes.", "Default Value");
                            this.getConfig().set("prefixes."+player.getName(),kingdom3);       
                            saveConfig();
                            reloadConfig();
                    }
                    if(args[1].equalsIgnoreCase("valkyrie"))
                    {
                        player.setDisplayName(kingdom4 + player.getDisplayName());
                        getConfig().getString("prefixes.", "Default Value");
                            this.getConfig().set("prefixes."+player.getName(),kingdom4);       
                            saveConfig();
                            reloadConfig();
                    }
                }
            }
            return true;
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  8. Offline

    WolfGangSen

    *edit*grumble grumble, frigin sniped again *edit*

    Not to derail the topic but
    GAH
    It would help people allot more if you bracketed your code nicely.

    Everyone has their own style of coding and thats fine.
    But for the sake of getting help. Throw them a bone.

    Personally I am a bracket whore, I write my code with brackets always having their own liens and brackets after almost every if loop whatever.
    When looking at it it is very quick and clear where what is and what goes into which statement, I show my code to people ALLOT (I casually teach friends how to script in games and do things like bukkit) and even if they dont write their code in that style, it is very nice and easy to read. It would make getting help allot easier.

    Anyway

    seems to me that you have a problem in you loading or in your onplayerjoin event for when you assign the prefix to the player when they come back
     
  9. Offline

    SuperEvan200

    WolfGangSen

    Everything works fine. Just one little problem a bit off topic that I don't think should have its own private thread.
    When a player first joins (A new player) his prefix is "null". (Before he joins a kingdom)

    Is there somekind of onnewplayerjoin event that I can use to set the default prefix for new players?
     
  10. You can detect if it's a new player with OfflinePlayer but you don't need that.
    You can just get his prefix then check if it's null and asign it to the default prefix.
     
    SuperEvan200 likes this.
  11. Offline

    WolfGangSen

    This.

    when they connect get their current prefix.
    then do something like if prefix equals null then prefix equals "[DEFAULT]"
     
    SuperEvan200 likes this.
  12. Offline

    SuperEvan200

    Digi WolfGangSen

    I did what you said, but nothing has changed. Here is my code for when a player joins:
    Code:
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            reloadConfig();
            Player player = e.getPlayer();
            if("prefixes.".equals("null"))
            {
                getConfig().getString("prefixes.", "Default Value");
                    this.getConfig().set("prefixes."+player.getName(),"[Newbie]");
            }
            String prefix = getConfig().getString("prefixes."+player.getDisplayName());
            player.setDisplayName(prefix + player.getDisplayName());
            reloadConfig();
      }
    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  13. "prefixes." can never be equal to "null".. since it's equal to "prefixes." :)

    Also null should not be a string, it's a key word which means empty, unasigned, etc.

    You need to create a String variable and store the prefix from config and check that...

    Example
    Code:
    String prefix = getConfig().getString("config.node.here");
    
    if(prefix == null)
    {
        prefix = "Default prefix";
        // eventually set the default prefix back to config.
    }
    
    Or you can just use getString()'s 2nd argument:
    Code:
    String prefix = getConfig().getString("config.node.here", "Default prefix");
    But I wanted to show you how to check against null properly.

    And also, reloadConfig() does not save the config, so when you set() something and do reloadConfig() afterwards, what you set previously will be lost.

    I suggest you don't save/reload config every time a player joins/leaves, instead just save the config in onDisable().
    You also don't need to load the config manually since it's done automatically by getConfig() the first time you call that, the 2nd+ times it's going to return the memory-stored config.
     
    SuperEvan200 likes this.
  14. Offline

    SuperEvan200

    Digi

    I don't fully understand what you said. When I add another "String prefix" it makes me rename prefix no prefix1, prefix2, etc.

    Also, what is "config.node.here"? Do I have to change that?
     
  15. SuperEvan200
    You need to be more specific on what you didn't understand.

    And of course it does that... if it were to allow multiple variables with the same name, how could you tell which one you are using ? :)

    The "config.node.here" is the config node, the path to the value... I don't really know how to explain that to you since you are using one in your code... you should not just copy paste code and then forget about it, try to understand what it does.

    I suggest you go over to the requests section if you only want the plugin done and you don't care about learning.
     
  16. Offline

    SuperEvan200

    Digi

    No, I want to learn how to make plugins. I just don't understand everything. After playing around with the code a bunch of times, nothing works. Here's my code now (Which doesn't work)
    Code:
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player player = e.getPlayer();
            String prefix = getConfig().getString("prefixes."+player.getDisplayName());
            player.setDisplayName(prefix + player.getDisplayName());
            String prefix1 = getConfig().getString("prefixes."+player.getDisplayName());
            if(prefix1 == null)
            {
                prefix = "[Nomad]";
            }
        }
    The shorter code did not work as well. Am I getting the config.node.here wrong?

    [EDIT] Maybe we misunderstood each other. Ingame, my name is not null before I join a kingdom, its nullSuperEvan200
     
  17. Offline

    stuntguy3000

  18. Offline

    SuperEvan200

    stuntguy3000

    Post what? I posted my onplayerjoin event code in post #18. Do you want my full code?
     
  19. Does that code look like it makes sense to you ?
    Seriously, you need to read the lines and understand what they actually do, then you should be able to decide what to place where and in what order.
     
  20. Offline

    raGan.

    You are setting display name before nullchecking prefix. You are also unnecessarily creating 2 prefixes with the same value.
    Code:
    @EventHandler
    public void onJoin(PlayerJoinEvent e) {
        Player player = e.getPlayer();
        String prefix = getConfig().getString("prefixes."+player.getDisplayName());
        if(prefix == null) {
            prefix = "[Nomad]";
        }
        player.setDisplayName(prefix + player.getDisplayName());
    }
    Edit: I might have been too hasty giving you the code, but it's already here, so I won't remove it.
     
    SuperEvan200 likes this.
  21. Offline

    SuperEvan200

    raGan.

    Thanks, it works :D.

    Digi
    I try my best to understand the code. I'm learning as I develop this plugin. Without any help, I always can't just understand what something means :p
     
Thread Status:
Not open for further replies.

Share This Page