if statement not stopping when I want it to

Discussion in 'Plugin Development' started by krazyswaggaO, Aug 28, 2012.

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

    krazyswaggaO

    Code:
    PermissionsEx.getUser((Player) sender).getGroups();
                if (PermissionsEx.getUser((Player) sender).inGroup(groupsq)) {
                    if(en.has(sender.getName(), 1500)){
                        PermissionsEx.getUser((Player) sender).addGroup(knight);
                        sender.sendMessage(ChatColor.GREEN + "[Ranks]"
                                + ChatColor.WHITE
                                + " You have been promoted to Knight!");
                        Bukkit.getServer().broadcastMessage(
                                ChatColor.GREEN + "[Ranks]" + ChatColor.RED
                                + " Player " + sender.getName()
                                + " has been promoted to Knight!");
                    } else {
                        sender.sendMessage(ChatColor.GREEN + "[Ranks]" + ChatColor.RED + ("You need $1500"));
                    }
                }
                if (PermissionsEx.getUser((Player) sender).inGroup(knight)) {
                    PermissionsEx.getUser((Player) sender).addGroup(lord);
                    if(en.has(sender.getName(), 6500)){
                        sender.sendMessage(ChatColor.GREEN + "[Ranks]"
                                + ChatColor.WHITE + " You have been promoted to Lord!");
                        Bukkit.getServer().broadcastMessage(
                                ChatColor.GREEN + "[Ranks]" + ChatColor.RED
                                + " Player " + sender.getName()
     
                                + " has been promoted to Lord!");
                    } else {
                        sender.sendMessage(ChatColor.GREEN + "[Ranks]" + ChatColor.RED + ("You need $6500"));
     
                    }
                }  if (PermissionsEx.getUser((Player) sender).inGroup(lord)) {
                    PermissionsEx.getUser((Player) sender).addGroup(baron);
                    sender.sendMessage(ChatColor.GREEN + "[Ranks]"
                            + ChatColor.WHITE + " You have been promoted to Baron!");
                    Bukkit.getServer().broadcastMessage(
                            ChatColor.GREEN + "[Ranks]" + ChatColor.RED
                            + " Player " + sender.getName()
                            + " has been promoted to Baron!");
                } if (PermissionsEx.getUser((Player) sender).inGroup(baron)) {
                    PermissionsEx.getUser((Player) sender).addGroup(count);
                    sender.sendMessage(ChatColor.GREEN + "[Ranks]"
                            + ChatColor.WHITE + " You have been promoted to Count!");
                    Bukkit.getServer().broadcastMessage(
                            ChatColor.GREEN + "[Ranks]" + ChatColor.RED
                            + " Player " + sender.getName()
                            + " has been promoted to count!");
                }
                if (PermissionsEx.getUser((Player) sender).inGroup(count)) {
                    PermissionsEx.getUser((Player) sender).addGroup(duke);
                    sender.sendMessage(ChatColor.GREEN + "[Ranks]"
                            + ChatColor.WHITE + " You have been promoted to Duke!");
                    Bukkit.getServer().broadcastMessage(
                            ChatColor.GREEN + "[Ranks]" + ChatColor.RED
                            + " Player " + sender.getName()
                            + " has been promoted to duke!");   
     
                }
                if (PermissionsEx.getUser((Player) sender).inGroup(duke)) {
                    PermissionsEx.getUser((Player) sender).addGroup(archduke);
                    sender.sendMessage(ChatColor.GREEN + "[Ranks]"
                            + ChatColor.WHITE + " You have been promoted to ArchDuke!");
                    Bukkit.getServer().broadcastMessage(
                            ChatColor.GREEN + "[Ranks]" + ChatColor.RED
                            + " Player " + sender.getName()
                            + " has been promoted to ArchDuke!!");   
     
                }
                if(PermissionsEx.getUser((Player) sender).inGroup(archduke)) {
                    PermissionsEx.getUser((Player) sender).addGroup(prince);
                    sender.sendMessage(ChatColor.GREEN + "[Ranks]"
                            + ChatColor.WHITE + " You have been promoted to Prince!");
                    Bukkit.getServer().broadcastMessage(
                            ChatColor.GREEN + "[Ranks]" + ChatColor.RED
                            + " Player " + sender.getName()
                            + " has been promoted to Prince!!");    
    Alright,so basically,when I do /rankup and have no money,it tells me the message.When I do,instead of just giving me one rank,it gives me them all until the last one.My question is,how do I make it so it only promotes one rank,then you have to do /rankup again.I think it's safe to call this a loop?
    a simple code to do this is fine,I know it,but I just don't remember it....
     
  2. Offline

    ZeusAllMighty11

    I think it's because you are checking if they have a minimum amount. Well if they have like this:


    Dog: $15
    Cat: $16
    Fish: $ 300
    Creeper: $9000

    Let's say billy has $2500. We checked if he has at least 15 for the dog. Which he does. But by doing /purchase (or rankup in your case), it just checks if he has that amount. But Billy also has enough for the cat! And the Fish! So it buys all those for him too!
     
    Woobie likes this.
  3. Offline

    krazyswaggaO

    Yea,I get what you mean,at the moment things work out,but what I want it to do,is:
    if I do /rankup,and I got $5000000
    saying that:
    Dog: $15
    Cat: $100
    Fish: $1000
    it won't buy all of these guys,but only the dog,until I do /rankup again.
     
  4. Offline

    sternmin8or

    Simply add:
    return true;
    after you rank someone up
     
  5. Offline

    krazyswaggaO

    Which I did,
    Code:
    if (PermissionsEx.getUser((Player) sender).inGroup(groupsq)) {
                    if(en.has(sender.getName(), 4000)){
                        en.withdrawPlayer(sender.getName(), 4000);
                        PermissionsEx.getUser((Player) sender).addGroup(knight);
                        sender.sendMessage(ChatColor.GREEN + "[Ranks]"
                                + ChatColor.WHITE
                                + " You have been promoted to Knight!");
                        Bukkit.getServer().broadcastMessage(
                                ChatColor.GREEN + "[Ranks]" + ChatColor.RED
                                + " Player " + sender.getName()
                                + " has been promoted to Knight!");
                    } else {
                        sender.sendMessage(ChatColor.GREEN + "[Ranks]" + ChatColor.RED + ("You need $4000"));
                        return true;
    And:
    http://gyazo.com/11c9d8bcc937869954b4d7d04580bb56
     
  6. Offline

    sternmin8or

    Code:java
    1. Bukkit.getServer().broadcastMessage(
    2. ChatColor.GREEN + "[Ranks]" + ChatColor.RED
    3. + " Player " + sender.getName()
    4. + " has been promoted to Knight!");
    5. // return statement here too
    6.  


    Edit: actually that comes with its own problems. You have to reorganize your code so that the higher ranks come first.
     
  7. Offline

    krazyswaggaO

    Hmm,I'll play around with the code,I really think it has something to do with the returns.If that doesn't work,there should be a few plugins that have the same system.

    In the meantime,
    Digi, Giant ,you guys seem to be active around here,and give some good tips.Any idea?
     
  8. Offline

    sternmin8or

    The easiest solution is to reverse the order of the rank checks, that way the function cant climb up the line of ranks and give each one. It simply gives the highest one possible. trust me...
     
  9. Offline

    krazyswaggaO

    That didn't work.Everytime I try to rank up,it says you have been promoted to knight,and it doesn't rank me up to any other rank
     
  10. Offline

    sternmin8or

    Are you sure that the rankchecks are in this order: archduke,duke,count,baron,lord,knight,groupsq? Also, you still do need return statements after a successful promotion/failed promotion.
     
  11. I suggest you move the promotion importance in backwards order and use if-else-if-else-if... so the user doesn't get spammed if he gets instantly promoted to prince xD
    That way you'll also avoid the money message for each rank.

    Still, I also suggest you remove all the repeatitive code and try to do it in one go... make some arrays with the rank name, price and permission requirements (ordered from the highest to the lowest as stated before) and then just loop through them and check...
    Here's an example:

    Code:
    String[] ranks = new String[] {"Prince", "Lord", "Farmer"};
    String[] permissions = new String[] {"c", "b", "a"};
    int[] prices = new int[]{750,50,5};
    
    for(int i = 0; i < ranks.length; i++)
    {
        if(Permission.has(player, permissions[i]) && Money.has(player, prices[i]))
        {
             Bukkit.broadcastMessage(player.getName() + " has been promoted to " + rank[i] + " !");
             break;
        }
    }
    However, to make it better, I suggest you use a List and use a custom class which holds the String for rank name, the string permission and the price... and you can even make a comparison method in the custom class so you can easily sort the list with Collections.sort();

    I also suggest you don't use a single permission plugin, like you're using PEX, you should use Vault so it supports most permission plugins and most economy plugins.

    EDIT: Giant yes, I forgot to add it :}
     
  12. Offline

    Giant

    Digi, your code will sort of still continue to promote the user... Seeing how you keep on looping, even if the user has had his permission set.

    krazyswaggaO, I would suggest adding a "break;" after the broadcast on the code from Digi, what this does is immediately stop execution of the entire loop. The other friend of the "break;" statement, would be "continue;", which means: skip rest of this loop segment, but keep looping.

    Edit:
    Did you really have to make your name so annoying to write? T__T

    Edit 2:
    Digi Oh well, we all make mistakes sometimes! :D
     
  13. Actually... according to:
    That code I provided isn't doing that tough, it promotes to the highest possible... but if you want that, all you need to change is to reverse order of the ranks.

    Here's an example with those exact values:
    old code - I recommend the other one below (open)
    Code:
    String[] ranks = new String[] {"Dog", "Cat", "Fish"};
    String[] groups = new String[] {"groupDog", "groupCat", "groupFish"};
    int[] prices = new int[] {5, 100, 1000};
    
    for(int i = 0; i < ranks.length; i++)
    {
        if(Money.has(player, prices[i]) && (i == 0 || groups[i - 1] == null || Group.has(player, groups[i - 1])))
        {
             Bukkit.broadcastMessage(player.getName() + " has been promoted to " + rank[i] + " !");
             Groups.set(player, groups[i]);
             Rank.set(player, rank[i]);
             break;
        }
    }

    Ofc some methods and classes are as examples.

    EDIT:
    Hmm, actually (again) I misunderstood those permissions.... re-edited.

    EDIT #2:
    Even better, without a loop:
    Code:
    // fields
    String[] ranks = new String[] {"Dog", "Cat", "Fish"};
    double[] rankPrices = new double[] {0.0, 2.5, 99.99};
    HashMap<String, Integer> playerRank = new HashMap<String, Integer>();
    
    
    // display rank - all new players will be "Dog" by default, even if not added in the hashmap
    int rankId = playerRank.get(player.getName()); // in a primitive type variable so its 0 if it doesn't exist (null)
    player.sendMessage("You have the " + ranks[rankId] + " rank");
    
    
    // ranking up - new players will rank up to "Cat".
    int nextRank = rankId + 1; // need same rankId var from above
    
    if(nextRank < ranks.length)
    {
        if(!Money.has(player, rankPrices[nextRank]))
        {
            Money.subtract(player, rankPrices[nextRank]); // subtract money
    
            playerRank.put(player.getName(), nextRank); // set rank
    
            // eventually set the group, add another array with group names... and you could also remove the old group if you want, rankId would be the old index since it wasn't updated.
    
            Bukkit.broadcastMessage(player.getName() + " has been promoted to " + ranks[nextRank]); // broadcast
        }
        else
            player.sendMessage("You need " + Money.format(rankPrices[nextRank]) + " to rank up to " + ranks[nextRank]);
    }
    else
        player.sendMessage("You have the highest possible rank!");
     
  14. Offline

    Giant

    Oh lord, here goes that hard to write name again...
    krazyswaggaO don't forget to remove the break; in the last example from Digi, as it will otherwise ONLY output 1 option!

    Edit:
    nevermind :p
     
  15. Offline

    krazyswaggaO

    Okay,just got back from a trip.
    Digi
    Code:
    } else if (cmd.getName().equalsIgnoreCase("rankup")) {
                String[] ranks = new String[] {"serf", "squire", "knight", "lord", "baron", "count", "duke", "archduke", "prince"};
                double[] rankPrices = new double[] {4000.0, 8000.0, 16000.0, 32000.0, 64000.0, 128000.0, 256000.0, 512000.0,};
                HashMap<String, Integer> playerRank = new HashMap<String, Integer>();
               
                int rankId = playerRank.get(sender.getName());
                sender.sendMessage("You have the " + ranks[rankId] + " rank");
               
                int nextRank = rankId + 1;
                if(nextRank < ranks.length)
                {
                    if(!en.has(sender.getName(), rankPrices[nextRank])){
                        en.withdrawPlayer(sender.getName(), rankPrices[nextRank]);
                        playerRank.put(sender.getName(), nextRank);
                        Bukkit.broadcastMessage(ChatColor.GREEN + "[Ranks]" + ChatColor.WHITE + sender.getName() + "  has been promoted to " + ranks[nextRank]);
                    }
                    else
                        sender.sendMessage(ChatColor.GREEN + "[Ranks]" + ChatColor.WHITE + "You need " + en.format(rankPrices[nextRank]) + " to rank up to " + ranks[nextRank]);
                }
                else
                    sender.sendMessage(ChatColor.GREEN + "[Ranks]" + ChatColor.WHITE + " You have the highest possible rank!");
        }
            return false;
        }
    }
    Is my code
    http://gyazo.com/eec28481d75df9f2bd9ba7817217cd4b
    Is the error
    http://gyazo.com/eec28481d75df9f2bd9ba7817217cd4b
    Is the console

    Not sure why it gives the error either.
     
  16. NPE at Main.java line 179 :)
     
  17. Offline

    krazyswaggaO

    Yea,I know,but why is it being caused?
     
  18. It's caused by a method being called on a pointer (variable/method) that is asigned to null.
    Example:
    Code:
    Player player = null;
    
    player.sendMessage("This will throw a NPE");
    Another example:
    Code:
    private String getMyString()
    {
        return null;
    }
    
    // ...
    
    String str1 = getMyString(); // str1 will be null, no NPE because you're not doing anything wrong
    String str2 = getMyString().toLowerCase(); // throws NPE because you can't call methods on null
    If you can't solve it, post the code at line 179 and the surrounding code... eventually post the code where you define the pointers used in the line as well.
     
Thread Status:
Not open for further replies.

Share This Page