If statements aren't working properly?

Discussion in 'Plugin Development' started by Vamure, Apr 25, 2015.

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

    Vamure

    This checks how many potions are in your inventory. In this code, with a full inventory of potions, it will stop messaging the potions after the first if statement. This was working before..
    Code:
                 if (pots > 0) {
    
                     p.sendMessage(ChatColor.GREEN + "Instant Health (II)§e: " + ChatColor.GREEN + pots + ChatColor.YELLOW + ((pots == 1) ? " potion" : " potions"));
                 }
               else if (swp2s > 0) {
                     p.sendMessage(ChatColor.GREEN + "SWP2§e: " + ChatColor.GREEN + swp2s + ChatColor.YELLOW + ((swp2s == 1) ? " potion" : " potions"));
                 }
                else if (frps > 0) {
                     p.sendMessage(ChatColor.GREEN + "FRPE§e: " + ChatColor.GREEN + frps + ChatColor.YELLOW + ((frps == 1) ? " potion" : " potions"));
                 }
                 else {
                     p.sendMessage(ChatColor.RED + "No main potions are in your inventory!");
                     return;
                 }
    This works properly. It sends all 3 messages if potions are present:
    Code:
                 if (pots > 0) {
                     p.sendMessage(ChatColor.GREEN + "Instant Health (II)§e: " + ChatColor.GREEN + pots + ChatColor.YELLOW + ((pots == 1) ? " potion" : " potions"));
                 }
                 if (swp2s > 0) {
                     p.sendMessage(ChatColor.GREEN + "SWP2§e: " + ChatColor.GREEN + swp2s + ChatColor.YELLOW + ((swp2s == 1) ? " potion" : " potions"));
                 }
                 if (frps > 0) {
                     p.sendMessage(ChatColor.GREEN + "FRPE§e: " + ChatColor.GREEN + frps + ChatColor.YELLOW + ((frps == 1) ? " potion" : " potions"));
                 }
                 else if (frps == 0 && pots == 0 && swp2s == 0){
                     p.sendMessage(ChatColor.RED + "No main potions are in your inventory!");
                     return;
                 }
    Any idea? The first method is more efficient (in my eyes) but stopped working from last time I tried it.
     
  2. Offline

    Skionz

    @Vamure You never really told us what the problem was.
     
  3. Offline

    Vamure

    Say I have 1 instant health potion, 1 swift 2 potion, and 1 fire resistance potion in my inventory. The if statements check if they are > 0, if so, it sends me a message of the amount. With the first method, it will only tell me that I have 1 instant health potion, the second method, it tells me I have all 3.
     
  4. Offline

    teej107

    Maybe you should learn how if-else works
     
    BlazingBroGamer and Msrules123 like this.
Thread Status:
Not open for further replies.

Share This Page