Solved Looping through online players, do stuff if no one on the server has a permission?!

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

Thread Status:
Not open for further replies.
  1. Hey guys, so i know i have been posting a lot lately. ( i am a noob ).

    But something here does not seem right.

    I have a command (/gym leaders) which searches through all online players with:
    Code:
    for (Player staff : Bukkit.getOnlinePlayers()) {
    And then it check if the players have a certain permission and if they do, put them in chat (here is an example of checking for two different permissions):


    Code:
    if (staff.hasPermission("pixelgym.gym32")) {
                  if (enable32.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym32colour")) + staff.getName() + " - " + getConfig().getString("config.gym32") + " Gym");
                    }
                  }
                 if (staff.hasPermission("pixelgym.gym31"))
                  {
                  if (enable31.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym31colour")) + staff.getName() + " - " + getConfig().getString("config.gym31") + " Gym");
                    }
                  }
    However, say one player has both permissions ("pixelgym.gym31" and "pixelgym.gym32"). When someone does "/gym leaders" it will show that player, but only show them as having pixelgym.gym32 (which is the top if). So you would of thought if it then finds they also have another permission lower down the if, it would still add it. Because it is not an else if.

    But that is not the case.

    My only other idea is to create a new
    Code:
    for (Player staff : Bukkit.getOnlinePlayers()) {
    every
    Code:
    if (staff.hasPermission("pixelgym.gym#")) {
    Let me know what you guys think i should do to fix this.
    Thanks!

    Here is the whole command:


    Code:
     else if (args[0].equals("leaders")) {
              p.sendMessage(ChatColor.GOLD + "----- Online Gym Leaders -----");
              p.sendMessage("");
              for (Player staff : Bukkit.getOnlinePlayers()) {
                  if (staff.hasPermission("pixelgym.gym32")) {
                  if (enable32.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym32colour")) + staff.getName() + " - " + getConfig().getString("config.gym32") + " Gym");
                    }
                  }
                 if (staff.hasPermission("pixelgym.gym31"))
                  {
                  if (enable31.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym31colour")) + staff.getName() + " - " + getConfig().getString("config.gym31") + " Gym");
                    }
                  }
                 if (staff.hasPermission("pixelgym.gym30"))
                  {
                  if (enable30.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym30colour")) + staff.getName() + " - " + getConfig().getString("config.gym30") + " Gym");
                    }
                  }
                 if (staff.hasPermission("pixelgym.gym29"))
                  {
                  if (enable29.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym29colour")) + staff.getName() + " - " + getConfig().getString("config.gym29") + " Gym");
                    }
                  }
                 if (staff.hasPermission("pixelgym.gym28"))
                  {
                  if (enable28.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym28colour")) + staff.getName() + " - " + getConfig().getString("config.gym28") + " Gym");
                    }
                  }
                 if (staff.hasPermission("pixelgym.gym27"))
                  {
                  if (enable27.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym27colour")) + staff.getName() + " - " + getConfig().getString("config.gym27") + " Gym");
                    }
                  }
                 if (staff.hasPermission("pixelgym.gym26"))
                  {
                  if (enable26.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym26colour")) + staff.getName() + " - " + getConfig().getString("config.gym26") + " Gym");
                    }
                  }
                 if (staff.hasPermission("pixelgym.gym25"))
                 {
                  if (enable25.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym25colour")) + staff.getName() + " - " + getConfig().getString("config.gym25") + " Gym");
                  }
        
                }           
                 if (staff.hasPermission("pixelgym.gym24"))
                {
                  if (enable24.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym24colour")) + staff.getName() + " - " + getConfig().getString("config.gym24") + " Gym");
                  }
                }
                 if (staff.hasPermission("pixelgym.gym23"))
                {
                  if (enable23.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym23colour")) + staff.getName() + " - " + getConfig().getString("config.gym23") + " Gym");
                  }
                }
                 if (staff.hasPermission("pixelgym.gym22"))
                {
                  if (enable22.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym22colour")) + staff.getName() + " - " + getConfig().getString("config.gym22") + " Gym");
                  }
                }
                 if (staff.hasPermission("pixelgym.gym21"))
                {
                  if (enable21.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym21colour")) + staff.getName() + " - " + getConfig().getString("config.gym21") + " Gym");
                  }
                }
                 if (staff.hasPermission("pixelgym.gym20"))
                {
                  if (enable20.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym20colour")) + staff.getName() + " - " + getConfig().getString("config.gym20") + " Gym");
                  }
                }
                 if (staff.hasPermission("pixelgym.gym19"))
                {
                  if (enablegym19.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym19colour")) + staff.getName() + " - " + getConfig().getString("config.gym19") + " Gym");
                  }
                }
                 if (staff.hasPermission("pixelgym.gym18"))
                {
                  if (enablegym18.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym18colour")) + staff.getName() + " - " + getConfig().getString("config.gym18") + " Gym");
                  }
                }
                 if (staff.hasPermission("pixelgym.gym17"))
                 {
                  if (enablegym17.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym17colour")) + staff.getName() + " - " + getConfig().getString("config.gym17") + " Gym");
                }
              }
                 if (staff.hasPermission("pixelgym.gym16"))
                  {
                  if (enablegym16.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym16colour")) + staff.getName() + " - " + getConfig().getString("config.gym16") + " Gym");
                    }
                  }
                 if (staff.hasPermission("pixelgym.gym15"))
                  {
                  if (enablegym15.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym15colour")) + staff.getName() + " - " + getConfig().getString("config.gym15") + " Gym");
                    }
                  }
                 if (staff.hasPermission("pixelgym.gym14"))
                  {
                  if (enablegym14.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym14colour")) + staff.getName() + " - " + getConfig().getString("config.gym14") + " Gym");
                    }
                  }
                 if (staff.hasPermission("pixelgym.gym13"))
                  {
                  if (enablegym13.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym13colour")) + staff.getName() + " - " + getConfig().getString("config.gym13") + " Gym");
                    }
                  }
                if (staff.hasPermission("pixelgym.gym12"))
                  {
                  if (enablegym12.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym12colour")) + staff.getName() + " - " + getConfig().getString("config.gym12") + " Gym");
                    }
                  }
               if (staff.hasPermission("pixelgym.gym11"))
                  {
                  if (enablegym11.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym11colour")) + staff.getName() + " - " + getConfig().getString("config.gym11") + " Gym");
                    }
                  }
                if (staff.hasPermission("pixelgym.gym10"))
                  {
                  if (enablegym10.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym10colour")) + staff.getName() + " - " + getConfig().getString("config.gym10") + " Gym");
                    }
                  }
                if (staff.hasPermission("pixelgym.gym9"))
                            {
                  if (enable9.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym9colour")) + staff.getName() + " - " + getConfig().getString("config.gym9") + " Gym");
                  }
                
                }           
                if (staff.hasPermission("pixelgym.gym8"))
                {
                  if (enable8.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym8colour")) + staff.getName() + " - " + getConfig().getString("config.gym8") + " Gym");
                  }
                }
                if (staff.hasPermission("pixelgym.gym7"))
                {
                  if (enable7.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym7colour")) + staff.getName() + " - " + getConfig().getString("config.gym7") + " Gym");
                  }
                }
                if (staff.hasPermission("pixelgym.gym6"))
                {
                  if (enable6.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym6colour")) + staff.getName() + " - " + getConfig().getString("config.gym6") + " Gym");
                  }
                }
                if (staff.hasPermission("pixelgym.gym5"))
                {
                  if (enable5.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym5colour")) + staff.getName() + " - " + getConfig().getString("config.gym5") + " Gym");
                  }
                }
                if (staff.hasPermission("pixelgym.gym4"))
                {
                  if (enable4.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym4colour")) + staff.getName() + " - " + getConfig().getString("config.gym4") + " Gym");
                  }
                }
                if (staff.hasPermission("pixelgym.gym3"))
                {
                  if (enable3.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym3colour")) + staff.getName() + " - " + getConfig().getString("config.gym3") + " Gym");
                  }
                }
                if (staff.hasPermission("pixelgym.gym2"))
                {
                  if (enable2.equalsIgnoreCase("true")) {
                    p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym2colour")) + staff.getName() + " - " + getConfig().getString("config.gym2") + " Gym");
                  }
                }
                if ((staff.hasPermission("pixelgym.gym1")) &&
                  (enablegym1.equalsIgnoreCase("true"))) {
                  p.sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym1colour")) + staff.getName() + " - " + getConfig().getString("config.gym1") + " Gym");
                }
    
              }
    
            }
     
  2. Offline

    I Al Istannen

    @kayc01 If you could readjust the formatting (CTRL + SHIFT + F in eclipse), it would be a lot easier to read. And what do these hundreds of "pixelgym.gymXX" mean? Why not just use a for loop and count downwards from 32?

    But to the problem you mentioned, if all these ifs are on the same level and not nested, it should work just fine I think.
     
  3. Hey yeah i solved that, turned out i was testing the older .jar. Silly me. The if's made the difference.
    Also, I am trying to check if the person leaving the server is the last player with a certain permission. Then do stuff.

    I was trying to get into looping for this.

    I have:


    Code:
     int i = 1;
    for (Player online : Bukkit.getOnlinePlayers()) {
            while (online.hasPermission("pixelgym.gym"+i)) {
                //keep the gym open as a player has the permission (by default is "pixelgym.gym1")
                            i++
                //add one to i so it then checks players for the permission "pixelgym.gym2" etc...
            }
             else {
                //close the gym as there is no one else on the server with the permission.
             }         
    
    }          
    Does this look like it would work? It is un-tested but seems to make sense.

    EDIT: Else would not work with while, however should i use continue or break?
    After the i++; ?

    Also how would i want to then do stuff if the while is then false?

    Bump!!!

    + New update, i have come up with this. About to test it but give it a read over and see if it should work or not.
    I will get update when i find the results.

    Read through the comments.

    Code:
    @SuppressWarnings("deprecation")
    public void onLeave(PlayerQuitEvent l) {
          Player p = l.getPlayer();
        
          int i = 1;
        
          for (Player on : Bukkit.getOnlinePlayers()) {
              if (on.hasPermission("pixelgym.gym"+i) && (getConfig().getString("config.gym"+i+"stat").equals("Open"))) {
                  //if any player online has the permission "pixelgym.gym1" to "pixelgym.gym32" (based on int = i) and gym stat = open via config
                  //then
                  //do nothing, keep gym open
                 if (i < 32){
                     //if i is less than 32 (it needs to check a players permission 32 times(If they have any of the permissions; "pixelgym.gym1" to pixelgym.gym32")
                  i++;
                  //if i is less than 32 (31 is max, then it can add one and continue(Go to next permission (pixelgym.gym1 to pixelgym.gym2 for the same player?))
                  continue;
                 }
                 else {
                     i = 1;
                     //If i is 32, reset i to 1 and continue looking through other players
                     continue;
                 }
              }
              else if (!on.hasPermission("pixelgym.gym"+i) && (getConfig().getString("config.gym"+i+"stat").equals("Open"))) {
                
                  //else if no one on the server has permission "pixelgym.gym1" (for example) but gym1 is open
                  //then
                  //set gym to closed.
                  Bukkit.broadcastMessage(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + getConfig().getString("config.title") + ChatColor.DARK_GRAY + "] " + ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym"+i+"colour")) + "The " + getConfig().getString("config.gym"+i)+ " Gym is now " + ChatColor.RED + "Closed");
                  //broadcast that the gym has been closed based on integer
                  getConfig().set("config.gym"+i+"stat", "Closed");
                  //set the gym to closed via config
                  this.board.resetScores(Bukkit.getOfflinePlayer(ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym"+i+"colour")) + getConfig().getString("config.gym"+1)));
                  //set gym to closed via scoreobard
                  continue;
                  //continue looking for other players having other "pixelgym.gym#" permissions if there gym is open.
                  //if they are loop back round and close them.
              }
            
          }
    EDIT by Timtower: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 7, 2015
  4. Offline

    mcdorli

    Steps:
    1. Open the projecf in eclipse
    2. Hit Ctrl+Shift+F
    3. Realize what this combination does
    4. Be happy about this
    5. Post the whole code again
     
    PDKnight likes this.
  5. :p

    Code:
    @SuppressWarnings("deprecation")
        public void onLeave(PlayerQuitEvent l) {
            // Player p = l.getPlayer();
    
            int i = 1;
    
            // if (p.hasPermission("pixelgym.gym"+i)) {
            for (Player on : Bukkit.getOnlinePlayers()) {
                if (on.hasPermission("pixelgym.gym" + i)
                        && (getConfig().getString("config.gym" + i + "stat").equals("Open"))) {
                    // if any player online has the permission "pixelgym.gym1" to
                    // "pixelgym.gym32" (based on int = i) and gym stat = open via
                    // config
                    // then
                    // do nothing, keep gym open
                    if (i < 32) {
                        // if i is less than 32 (it needs to check a players
                        // permission 32 times(If they have any of the permissions;
                        // "pixelgym.gym1" to pixelgym.gym32")
                        i++;
                        // if i is less than 32 (31 is max, then it can add one and
                        // continue(Go to next permission (pixelgym.gym1 to
                        // pixelgym.gym2 for the same player?))
                        continue;
                    } else {
                        i = 1;
                        // If i is 32, reset i to 1 and continue looking through
                        // other players
                        continue;
                    }
                } else if (!on.hasPermission("pixelgym.gym" + i)
                        && (getConfig().getString("config.gym" + i + "stat").equals("Open"))) {
    
                    // else if no one on the server has permission "pixelgym.gym1"
                    // (for example) but gym1 is open
                    // then
                    // set gym to closed.
                    Bukkit.broadcastMessage(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA
                            + getConfig().getString("config.title") + ChatColor.DARK_GRAY + "] "
                            + ChatColor.translateAlternateColorCodes('&',
                                    getConfig().getString("config.gym" + i + "colour"))
                            + "The " + getConfig().getString("config.gym" + i) + " Gym is now " + ChatColor.RED + "Closed");
                    // broadcast that the gym has been closed based on integer
                    getConfig().set("config.gym" + i + "stat", "Closed");
                    // set the gym to closed via config
                    this.board.resetScores(Bukkit.getOfflinePlayer(
                            ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym" + i + "colour"))
                                    + getConfig().getString("config.gym" + 1)));
                    // set gym to closed via scoreobard
                    continue;
                    // continue looking for other players having other
                    // "pixelgym.gym#" permissions if there gym is open.
                    // if they are loop back round and close them.
                }
    
            }
    
        }
     
  6. Offline

    timtower Administrator Administrator Moderator

    @kayc01 Random tags aren't appreciated.
     
  7. I am sorry. D:
    Kinda in a rush to see what is wrong with this :S
     
  8. Offline

    caderape

    @kayc01
    - Make a loops from 1 to you max permission number.
    - check if the player who left has this permission.
    - if yes, make a loops of onlineplayers, and increase an int everytime the permission is found.
    - if the int is 1, then your guy is the last one with this permission.

    Is it what you're asking for ?
     
  9. Almost, the current example i gave a few posts up explains it best i can.

    1) A player leaves the server
    2) It then checks if other people on the server have a certain permission. (for online players)
    3) If it finds someone with the permission, then add +1 to the int so that it checks for the next permission (it needs to get from "pixelgym.gym1" to "pixelgym.gym32" for each player checking if they have the permission or not)
    4) If no one else has the permission then close the gym.

    I think you were on the right lines in some parts but doing it back to front almost.
    Also, i think i need to also make sure the person leaving can not be found by for online players. How would i do that?

    It may work, that may be the only reason it does not.

    If you get confused, try re-read the comments on this:


    Code:
    @SuppressWarnings("deprecation")
        public void onLeave(PlayerQuitEvent l) {
            // Player p = l.getPlayer();
    
            int i = 1;
    
            // if (p.hasPermission("pixelgym.gym"+i)) {
            for (Player on : Bukkit.getOnlinePlayers()) {
                if (on.hasPermission("pixelgym.gym" + i)
                        && (getConfig().getString("config.gym" + i + "stat").equals("Open"))) {
                    // if any player online has the permission "pixelgym.gym1" to
                    // "pixelgym.gym32" (based on int = i) and gym stat = open via
                    // config
                    // then
                    // do nothing, keep gym open
                    if (i < 32) {
                        // if i is less than 32 (it needs to check a players
                        // permission 32 times(If they have any of the permissions;
                        // "pixelgym.gym1" to pixelgym.gym32")
                        i++;
                        // if i is less than 32 (31 is max, then it can add one and
                        // continue(Go to next permission (pixelgym.gym1 to
                        // pixelgym.gym2 for the same player?))
                        continue;
                    } else {
                        i = 1;
                        // If i is 32, reset i to 1 and continue looking through
                        // other players
                        continue;
                    }
                } else if (!on.hasPermission("pixelgym.gym" + i)
                        && (getConfig().getString("config.gym" + i + "stat").equals("Open"))) {
    
                    // else if no one on the server has permission "pixelgym.gym1"
                    // (for example) but gym1 is open
                    // then
                    // set gym to closed.
                    Bukkit.broadcastMessage(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA
                            + getConfig().getString("config.title") + ChatColor.DARK_GRAY + "] "
                            + ChatColor.translateAlternateColorCodes('&',
                                    getConfig().getString("config.gym" + i + "colour"))
                            + "The " + getConfig().getString("config.gym" + i) + " Gym is now " + ChatColor.RED + "Closed");
                    // broadcast that the gym has been closed based on integer
                    getConfig().set("config.gym" + i + "stat", "Closed");
                    // set the gym to closed via config
                    this.board.resetScores(Bukkit.getOfflinePlayer(
                            ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym" + i + "colour"))
                                    + getConfig().getString("config.gym" + 1)));
                    // set gym to closed via scoreobard
                    continue;
                    // continue looking for other players having other
                    // "pixelgym.gym#" permissions if there gym is open.
                    // if they are loop back round and close them.
                }
    
            }
    
        }
    
    The if p (person that leaves) has permission is commented out for 2 reasons:
    1, it needs to check if they have anywhere from Pixelgym.gym1 to pixelgym.gym32 permission, however if i check it by int, if the int is on a different number to the permission that the guy has that left the server, it would be out of line and mess up.

    (int is on 18 for example, person that leaves has "pixelgym.gym29", so if (p.hasPermission("pixelgym.gym"+i)) would not work.

    So i am getting it to check all players whenever anyone leaves.
     
  10. Offline

    caderape

    @kayc01 PlayerQuitEvernt is called before the player leaves. So he will be include in the loops.
    just checxk if the name of the player in the loops is not the same as the one who is leaving
     
  11. So would this work?


    Code:
    @SuppressWarnings("deprecation")
        public void onLeave(PlayerQuitEvent l) {
             Player p = l.getPlayer();
    
            int i = 1;
    
            // if (p.hasPermission("pixelgym.gym"+i)) {
            for (Player on : Bukkit.getOnlinePlayers()) {
                if (on.equals(p)) {
                    continue;
                }
                else {
                if (on.hasPermission("pixelgym.gym" + i)
                        && (getConfig().getString("config.gym" + i + "stat").equals("Open"))) {
                    // if any player online has the permission "pixelgym.gym1" to
                    // "pixelgym.gym32" (based on int = i) and gym stat = open via
                    // config
                    // then
                    // do nothing, keep gym open
                    if (i < 32) {
                        // if i is less than 32 (it needs to check a players
                        // permission 32 times(If they have any of the permissions;
                        // "pixelgym.gym1" to pixelgym.gym32")
                        i++;
                        // if i is less than 32 (31 is max, then it can add one and
                        // continue(Go to next permission (pixelgym.gym1 to
                        // pixelgym.gym2 for the same player?))
                        continue;
                    } else {
                        i = 1;
                        // If i is 32, reset i to 1 and continue looking through
                        // other players
                        continue;
                    }
                } else if (!on.hasPermission("pixelgym.gym" + i)
                        && (getConfig().getString("config.gym" + i + "stat").equals("Open"))) {
    
                    // else if no one on the server has permission "pixelgym.gym1"
                    // (for example) but gym1 is open
                    // then
                    // set gym to closed.
                    Bukkit.broadcastMessage(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA
                            + getConfig().getString("config.title") + ChatColor.DARK_GRAY + "] "
                            + ChatColor.translateAlternateColorCodes('&',
                                    getConfig().getString("config.gym" + i + "colour"))
                            + "The " + getConfig().getString("config.gym" + i) + " Gym is now " + ChatColor.RED + "Closed");
                    // broadcast that the gym has been closed based on integer
                    getConfig().set("config.gym" + i + "stat", "Closed");
                    // set the gym to closed via config
                    this.board.resetScores(Bukkit.getOfflinePlayer(
                            ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym" + i + "colour"))
                                    + getConfig().getString("config.gym" + 1)));
                    // set gym to closed via scoreobard
                    continue;
                    // continue looking for other players having other
                    // "pixelgym.gym#" permissions if there gym is open.
                    // if they are loop back round and close them.
                }
    
            }
    
          }
        }
    The part that changed:

    Code:
    if (on.equals(p)) {
                    continue;
                }
                else {
                if (on.hasPermission("pixelgym.gym" + i)
                        && (getConfig().getString("config.gym" + i + "stat").equals("Open"))) {
    It does not work... :/

    Any other idea's guys?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 29, 2015
  12. Offline

    caderape

    @kayc01 i dun know dude. it's hard to understand something with all the text.
    But where is your loops for permissions ? actually you just check for the permission 'pixelgym.gym1'
     
  13. bump

    How am i only checking for pixelgym.gym1? It uses the int, so when the int is changing each loop, it is looking for that permission node as well. Hence the

    Code:
    if (on.hasPermission("pixelgym.gym" + i)
    At first it looks for gym1, second loop it looks for gym2. Surely?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 29, 2015
  14. Offline

    caderape

    @kayc01but you loop online players. If there's only two players, you will not reach pixelgym.gym3

    Try to do like this ;
    loops from 1 to 32 for check eeach permission
    In the loops, check if the player have the permission and if it's 'open'
    set an int to 0;
    loops online player still the end, except the one leaving, and increase the int if the player has the permission
    if the int equals 0, that means is the only one left with that permsission.
     
  15. Could you try modify my code?
    To that? Could not get my head quite round it.
    Mainly order of the way you are putting things confuses me a little.

    I would really appreciate it.
     
  16. Offline

    caderape

    @kayc01
    Code:
    public void onCheckOperator(imTheOnlyOp op) {
            Player p = op.getPlayer();
    
            for (int i = 1; i <= 6; i++) {
                if (p.hasPermission("operator" + i)
                        && getConfig().getString("operator" + i).equalsIgnoreCase("allow")) {
                    int count = 0;
    
                    for (Player online : Bukkit.getOnlinePlayers()) {
                        if (!p.getName().equalsIgnoreCase(online.getName())
                                && online.hasPermission("operator" + i)) {
                            count++;
                        }
                    }
    
                    if (count == 0) {
                        p.sendMessage("You are the last operator" + i);
                    }
    
                }
            }
        }
     
  17. I want to change the public void from onCheckOperator(imTheOnlyOp op) to onLeave (PlayerQuitEvent l) right?

    Also where do i type the stuff to do (if no one has the permission)

    This is what i have changed it to.
    I also change 6 to 32. That is correct right?


    Code:
    public void onLeave1(PlayerQuitEvent l) {
          Player p = l.getPlayer();
    
          for (int i = 1; i <= 32; i++) {
              if (p.hasPermission("pixelgym.gym" + i)
                      && getConfig().getString("pixelgym.gym" + i).equalsIgnoreCase("Open")) {
                  int count = 0;
    
                  for (Player online : Bukkit.getOnlinePlayers()) {
                      if (!p.getName().equalsIgnoreCase(online.getName())
                              && online.hasPermission("pixelgym.gym" + i)) {
                          count++;
                      }
                  }
    
                  if (count == 0) {
                      p.sendMessage("You are the last" + getConfig().getString("pixelgym.gym"+i) + "gym leader" + i);
                  }
    
              }
          }
      }
    Just need to know where to add the lines to close the gym now.

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 29, 2015
  18. Offline

    Zombie_Striker

    @kayc01
    Please wait 24 hours before bumping.

    Did you debug (If you do not know what this is, then google it)? Which line of code is causing the problem? Are you sure that the event is being called and nothing is null(you don't seem to have any null checks)?
     
  19. Oh right, understood about the bumps.
    There is no error's.

    I don't get how anything could be null if you are inputting value's.

    I am looking into debugging now.

    This is what the code looks like:


    Code:
     @SuppressWarnings("deprecation")
    public void onLeave1(PlayerQuitEvent l) {
          Player p = l.getPlayer();
    
          for (int i = 1; i <= 32; i++) {
              if (p.hasPermission("pixelgym.gym" + i)
                      && getConfig().getString("pixelgym.gym" + i+ "stat").equalsIgnoreCase("Open")) {
                  int count = 0;
    
                  for (Player online : Bukkit.getOnlinePlayers()) {
                      if (!p.getName().equalsIgnoreCase(online.getName())
                              && online.hasPermission("pixelgym.gym" + i)) {
                          count++;
                      }
                  }
    
                  if (count == 0) {
                      p.sendMessage("You are the last" + getConfig().getString("pixelgym.gym"+i) + "gym leader" + i);
                      Bukkit.broadcastMessage(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA
                              + getConfig().getString("config.title") + ChatColor.DARK_GRAY + "] "
                              + ChatColor.translateAlternateColorCodes('&',
                                      getConfig().getString("config.gym" + i + "colour"))
                              + "The " + getConfig().getString("config.gym" + i) + " Gym is now " + ChatColor.RED + "Closed");
                      // broadcast that the gym has been closed based on integer
                      getConfig().set("config.gym" + i + "stat", "Closed");
                      // set the gym to closed via config
                      this.board.resetScores(Bukkit.getOfflinePlayer(
                              ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym" + i + "colour"))
                                      + getConfig().getString("config.gym" + i)));
                  }
    
              }
          }
      }
    Let me know if anything stands out to you.

    I could not quite figure debugging. (I found a tutorial) but i was not sure where to put the initial connection line.

    Anyway...

    So still need assistance on this. It looks like its really close, but its not.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 29, 2015
  20. Offline

    Zombie_Striker

    @kayc01
    simply put "System.out.println("FLAG (number)")" anywhere in your code. If it prints out that message, that bit of code is being read. You use debugging to see what if statements are true, and which are false. So before and after each if statement, add a debug message (like so):
    Code:
    System.out.println("Test Number 1");
    if(true == false){
    System.out.println("Test Number 1 was a success")
    }else{
    System.out.println("Test Number 1 was Not a success")
    }
    
    Do this, post updated code, and say which lines are coming up true or false.
     
  21. Updated the code to this:


    Code:
    @SuppressWarnings({ "unused", "deprecation" })
    public void onLeave1(PlayerQuitEvent l) {
          Player p = l.getPlayer();
    
          for (int i = 1; i <= 32; i++) {
              if (p.hasPermission("pixelgym.gym" + i)
                      && getConfig().getString("pixelgym.gym" + i+ "stat").equalsIgnoreCase("Open")) {
                  System.out.println("Test Number 1");
                  if(true == false){
                  System.out.println("Test Number 1 was a success");
                  }else{
                  System.out.println("Test Number 1 was Not a success");
                  }
                  int count = 0;
    
                  for (Player online : Bukkit.getOnlinePlayers()) {
                      System.out.println("Test Number 2");
                      if(true == false){
                      System.out.println("Test Number 2 was a success");
                      }else{
                      System.out.println("Test Number 2 was Not a success");
                      }
                      if (!p.getName().equalsIgnoreCase(online.getName())
                              && online.hasPermission("pixelgym.gym" + i)) {
                          System.out.println("Test Number 3");
                          if(true == false){
                          System.out.println("Test Number 3 was a success");
                          }else{
                          System.out.println("Test Number 3 was Not a success");
                          }
                          count++;
                      }
                  }
    
                  if (count == 0) {
                      System.out.println("Test Number 4");
                      if(true == false){
                      System.out.println("Test Number 4 was a success");
                      }else{
                      System.out.println("Test Number 4 was Not a success");
                      }
                      p.sendMessage("You are the last" + getConfig().getString("pixelgym.gym"+i) + "gym leader" + i);
                      Bukkit.broadcastMessage(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA
                              + getConfig().getString("config.title") + ChatColor.DARK_GRAY + "] "
                              + ChatColor.translateAlternateColorCodes('&',
                                      getConfig().getString("config.gym" + i + "colour"))
                              + "The " + getConfig().getString("config.gym" + i) + " Gym is now " + ChatColor.RED + "Closed");
                      // broadcast that the gym has been closed based on integer
                      getConfig().set("config.gym" + i + "stat", "Closed");
                      // set the gym to closed via config
                      this.board.resetScores(Bukkit.getOfflinePlayer(
                              ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym" + i + "colour"))
                                      + getConfig().getString("config.gym" + i)));
                  }
    
              }
          }
      }
    When i leave the server, nothing shows:


    Code:
    [19:55:36 INFO]: [PixelmonGym] The Rock Gym is now Open
    [19:55:40 INFO]: ABkayCkay issued server command: /gym scoreboard
    [19:55:45 INFO]: ABkayCkay lost connection: Disconnected
    [19:55:45 INFO]: ABkayCkay left the game.
    :/ looks like none of it is being read?
     
  22. Offline

    Zombie_Striker

    @kayc01
    Do you have the @EventHandler tag? It looks like you don't. @EventHandler Tag must be above all events.

    Please try to remove/ work around anything that is deprecated. Its okay if you can't, but its deprecated for a reason. Also please remove anything that is Unused. If it's unused, why is it there?
     
  23. Welp, i did not notice that.
    Also the unused is from the printin code that you gave me.

    And the deprecated is from this.board.

    I will give it another test now i have added Event Handler :p

    Ok, so i got an error:
    Code:
    [20:11:12 INFO]: [PixelmonGym] The Rock Gym is now Open
    [20:11:15 INFO]: ABkayCkay lost connection: Disconnected
    [20:11:15 ERROR]: Could not pass event PlayerQuitEvent to PixelmonGym v4.2
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:294) ~[Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PlayerList.disconnect(PlayerList.java:307) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:705) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:130) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
    Caused by: java.lang.NullPointerException
            at me.Ckay.gym.PixelGym.onLeave1(PixelGym.java:250) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_60]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_60]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_60]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_60]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:292) ~[Craftbukkit.jar:git-Bukkit-1.7.9-R0.2-10-ge6cd8c0-b3096jnks]
            ... 11 more
    [20:11:15 INFO]: ABkayCkay left the game.
    By my understanding, the null is on line 250 of the code?
    Which is this if that is split between line 249 and 250:

    Code:
    if (p.hasPermission("pixelgym.gym" + i)
                      && getConfig().getString("pixelgym.gym" + i+ "stat").equalsIgnoreCase("Open")) {
    EDIT: It needs to be ("config.gym" +i+ "stat").
    Ill re-test.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 29, 2015
  24. Offline

    Zombie_Striker

    Is the config null? Is "i" null? Is the String you're getting null?

    Add null checks for these and debug those if statement. On of those three things should be null.
     
  25. Success :D


    Code:
    [20:17:40 INFO]: ABkayCkay issued server command: /gym open gym2
    [20:17:40 INFO]: [PixelmonGym] The Grass Gym is now Open
    [20:17:44 INFO]: ABkayCkay lost connection: Disconnected
    [20:17:44 INFO]: Test Number 1
    [20:17:44 INFO]: Test Number 1 was Not a success
    [20:17:44 INFO]: Test Number 2
    [20:17:44 INFO]: Test Number 2 was Not a success
    [20:17:44 INFO]: Test Number 4
    [20:17:44 INFO]: Test Number 4 was Not a success
    [20:17:44 INFO]: [PixelmonGym] The Grass Gym is now Closed
    [20:17:45 INFO]: ABkayCkay left the game.
    Thank you very much.

    I am about to post a thread about another problem i am having.
    About saving Custom Player Inventory's to a hashmap and then loading them by command.
    If you don't mind, ill tag you in it.
     
Thread Status:
Not open for further replies.

Share This Page