Creating a Delay between If Statements (After the player leaves).

Discussion in 'Plugin Development' started by kayc01, Dec 2, 2014.

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

    mythbusterma

    kayc01

    You can simply check if the player you found is also the one that left. It really isn't that hard. If you don't understand that you really need to learn more Java.
     
    Jaaakee224 likes this.

  2. Code:java
    1. final boolean hasPerm1 = p.hasPermission("pixelgym.gym1");
    2. final boolean hasPerm2 = p.hasPermission("pixelgym.gym2");
    3.  
    4. if(hasPerm1) {
    5. if ((getConfig().getString("config.gym1stat").equals("Open"))) {
    6.  
    7. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    8.  
    9. @Override
    10. public void run() {
    11.  
    12. for(Player p1: Bukkit.getOnlinePlayers()) {
    13.  
    14. if(!p1.hasPermission("pixelgym.gym1")) {
    15. p1.sendMessage(ChatColor.DARK_GRAY
    16. + "["
    17. + ChatColor.AQUA
    18. + getConfig().getString("config.title")
    19. + ChatColor.DARK_GRAY
    20. + "] "
    21. + ChatColor.translateAlternateColorCodes('&',
    22. getConfig().getString("config.gym1colour"))
    23. + "The " + getConfig().getString("config.gym1")
    24. + "Gym is now " + ChatColor.RED + "Closed");
    25. getConfig().set("config.gym1stat", "Closed");
    26. board.resetScores(Bukkit.getOfflinePlayer(ChatColor
    27. .translateAlternateColorCodes('&', getConfig()
    28. .getString("config.gym1colour"))
    29. + getConfig().getString("config.gym1")));
    30. }
    31. }
    32. }
    33. }, 20L);
    34. }
    35. }
    36.  
    37. if(hasPerm2) {
    38. if ((getConfig().getString("config.gym2stat").equals("Open"))) {
    39.  
    40. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    41.  
    42. @Override
    43. public void run() {
    44.  
    45. for(Player p1: Bukkit.getOnlinePlayers()) {
    46.  
    47. if(!p1.hasPermission("pixelgym.gym2")) {
    48. p1.sendMessage(ChatColor.DARK_GRAY
    49. + "["
    50. + ChatColor.AQUA
    51. + getConfig().getString("config.title")
    52. + ChatColor.DARK_GRAY
    53. + "] "
    54. + ChatColor.translateAlternateColorCodes('&',
    55. getConfig().getString("config.gym2colour"))
    56. + "The " + getConfig().getString("config.gym2")
    57. + "Gym is now " + ChatColor.RED + "Closed");
    58. getConfig().set("config.gym2stat", "Closed");
    59. board.resetScores(Bukkit.getOfflinePlayer(ChatColor
    60. .translateAlternateColorCodes('&', getConfig()
    61. .getString("config.gym2colour"))
    62. + getConfig().getString("config.gym2")));
    63. }
    64. }
    65. }
    66. }, 20L);
    67. }
    68.  
    69. }
    70. }


    This works to an extent. But instead of checking if any other players have the permission. And if they don't, then do something.

    It is just sending the message (do something) to anyone that does not have the permission. And closing the gym no matter what.

    If you can make any sense out of that.. lol :p
     
  3. Offline

    SyTeck

    What exactly are you trying to do?
     
  4. I explained at the bottom.
    But basically when someone logs out with "pixelgym.gym1" it needs to check if anyone else on the server has the permission ("pixelgym.gym1"). If no one else on the server has the permission, then close the gym (all the code including (board.resetScores..., p1.sendMessage... (However that was bukkit.broadcastMessage before, but it spams for how many people are on the server)).

    However, if someone else does have the permission ("pixelgym.gym1") then don't close the gym or send the message that the gym has closed.

    It currently just sends anyone that does not have the permission the message and closes the gym no matter what.
    When it needs to be doing all of the above instead.

    Make sense now??
     
  5. Offline

    MajorSkillage

    Where is run() being called? What i would do is PlayerQuitEvent for what you are trying to do without a scheduler because that way it would only check after a player leaves (what you want) then do exactly what you did in the for loop
     
  6. Could you give me a little example?
    Or i guess 'spoon feed me' it?
    The best way for me to learn (personally) is seeing an example that would work and then be able to write it myself in the future for other things.

    Thanks.

    Also, this is what i started off with before the Schedular:
    Code:java
    1. //if (ap.hasPermission("pixelgym.gym1")) {
    2. //foundLeader1 = true;
    3. //}
    4.  
    5. //boolean foundLeader1 = false
    6.  
    7.  
    8. //if (p.hasPermission("pixelgym.gym1")) {
    9. //if (!foundLeader1) {
    10. //Bukkit.broadcastMessage(ChatColor.DARK_GRAY
    11. //+ "["
    12. //+ ChatColor.AQUA
    13. //+ getConfig().getString("config.title")
    14. //+ ChatColor.DARK_GRAY
    15. //+ "] "
    16. //+ ChatColor.translateAlternateColorCodes('&',
    17. //getConfig().getString("config.gym1colour"))
    18. //+ "The " + getConfig().getString("config.gym1")
    19. //+ "Gym is now " + ChatColor.RED + "Closed");
    20. //getConfig().set("config.gym1stat", "Closed");
    21. //this.board.resetScores(Bukkit.getOfflinePlayer(ChatColor
    22. //.translateAlternateColorCodes('&', getConfig()
    23. //.getString("config.gym1colour"))
    24. //+ getConfig().getString("config.gym1")));
    25. //}
    26. //}

    Ignore the comments, it is just from eclipse because i kept it incase i needed it in the future. :)
     
  7. Offline

    SyTeck

    The things you are trying to do, is not very complicated.

    Maybe you should try doing easier project before moving on to stuff like this.
     
    mythbusterma likes this.
  8. Offline

    mythbusterma

    kayc01

    Alright, I sense a very stupid, poorly written answer incoming, so, before someone else says something stupid, you don't need to delay it at all.

    All you need to do is this:

    Code:java
    1. public void onQuit(PlayerLeaveEvent event) {
    2. if(!event.getPlayer().hasPermission("whatever")) {
    3. return;
    4. }
    5. for (Player player: Bukkit.getOnlinePlayers()) {
    6. if(player.equals(event.getPlayer())) {
    7. continue;
    8. }
    9. if(player.hasPermission("whatever")) {
    10. // make them the gym leader
    11. }
    12. }
    13. }
    14.  
     
    SyTeck likes this.
  9. I get that it is not, however... it does not want to work.
    Telling me to not do it is not going to help me. No offence. It needs to be done.

    But everything i try, does not work. Arghhhh

    As i say, i almost got it myself.. however it will look at the player leaving as having the permission? Surely.. with the method i had at the start. So i went to a Scheduler ... and now people are saying not to use that... :L
     
  10. Offline

    MajorSkillage

    I noticed that if any player doesn't have permission for example everyone except 1 guy has the permission it will close, here is how you can fix that (also just try use this code)
    Code:
    public class Main extends JavaPlugin implements Listener{
        public void onEnable(){
            getServer().getPluginManager().registerEvents(this, this);
        }
            @EventHandler
            public void onLeave(PlayerQuitEvent e){
                int numofPermissionless = 0;
                ArrayList<Player> peps = new ArrayList<Player>();
                peps.remove(e.getPlayer());
            if (getConfig().getString("config.gym2stat").equals("Open")) {
                if(e.getPlayer().hasPermission("pixelgym.gym1")){
                    for(Player p1: Bukkit.getOnlinePlayers()) {
                        peps.add(p1);
                    }
                    for(int i = 0; i <= peps.size(); i++){
                        if(!peps.get(i).hasPermission("pixelgym.gym2")) {
                            numofPermissionless++;
                            if(numofPermissionless != peps.size())
                                return;
                          peps.get(i).sendMessage(ChatColor.DARK_GRAY
                          + "["
                          + ChatColor.AQUA
                          + getConfig().getString("config.title")
                          + ChatColor.DARK_GRAY
                          + "] "
                          + ChatColor.translateAlternateColorCodes('&',
                                  getConfig().getString("config.gym2colour"))
                                  + "The " + getConfig().getString("config.gym2")
                                  + "Gym is now " + ChatColor.RED + "Closed");
                          getConfig().set("config.gym2stat", "Closed");
                          board.resetScores(Bukkit.getOfflinePlayer(ChatColor
                                  .translateAlternateColorCodes('&', getConfig()
                                          .getString("config.gym2colour"))
                                          + getConfig().getString("config.gym2")));
                        }
                        }
                } else if(e.getPlayer().hasPermission("pixelgym.gym1")){
                    if ((getConfig().getString("config.gym1stat").equals("Open"))) {
                        for(Player p1: Bukkit.getOnlinePlayers()) {
                            peps.add(p1);
                        }
                        for(int i = 0; i < peps.size(); i++){
                            numofPermissionless++;
                            if(numofPermissionless != peps.size())
                                return;
                            if(!peps.get(i).hasPermission("pixelgym.gym1")){
                              peps.get(i).sendMessage(ChatColor.DARK_GRAY
                              + "["
                              + ChatColor.AQUA
                              + getConfig().getString("config.title")
                              + ChatColor.DARK_GRAY
                              + "] "
                              + ChatColor.translateAlternateColorCodes('&',
                                      getConfig().getString("config.gym1colour"))
                                      + "The " + getConfig().getString("config.gym1")
                                      + "Gym is now " + ChatColor.RED + "Closed");
                              getConfig().set("config.gym1stat", "Closed");
                              board.resetScores(Bukkit.getOfflinePlayer(ChatColor
                                      .translateAlternateColorCodes('&', getConfig()
                                              .getString("config.gym1colour"))
                                              + getConfig().getString("config.gym1")));
                            }
                        }
                        }
        }
                    }
            }
     
    }
    I would disagree with this if everyone but 1 person has the permission then it wont work

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  11. Offline

    SyTeck

    I'll probably get sniped but you know that the "player" he is calling is a part of a loop, meaning it will loop through all players online and processing them one at the time. That code looks perfectly fine.
     
  12. Offline

    MajorSkillage

    it checks them 1 at a time that is the problem if the first player it checks has not got permission but everyone else does then it will still close it

    seriously though all these if statements are making me think of switch statements ._.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  13. So MajorSkillage SyTeck do you recommend i use:

    Code:java
    1. public class Main extends JavaPlugin implements Listener{
    2. public void onEnable(){
    3. getServer().getPluginManager().registerEvents(this, this);
    4. }
    5. @EventHandler
    6. public void onLeave(PlayerQuitEvent e){
    7. int numofPermissionless = 0;
    8. ArrayList<Player> peps = new ArrayList<Player>();
    9. peps.remove(e.getPlayer());
    10. if (getConfig().getString("config.gym2stat").equals("Open")) {
    11. if(e.getPlayer().hasPermission("pixelgym.gym1")){
    12. for(Player p1: Bukkit.getOnlinePlayers()) {
    13. peps.add(p1);
    14. }
    15. for(int i = 0; i <= peps.size(); i++){
    16. if(!peps.get(i).hasPermission("pixelgym.gym2")) {
    17. numofPermissionless++;
    18. if(numofPermissionless != peps.size())
    19. return;
    20. peps.get(i).sendMessage(ChatColor.DARK_GRAY
    21. + "["
    22. + ChatColor.AQUA
    23. + getConfig().getString("config.title")
    24. + ChatColor.DARK_GRAY
    25. + "] "
    26. + ChatColor.translateAlternateColorCodes('&',
    27. getConfig().getString("config.gym2colour"))
    28. + "The " + getConfig().getString("config.gym2")
    29. + "Gym is now " + ChatColor.RED + "Closed");
    30. getConfig().set("config.gym2stat", "Closed");
    31. board.resetScores(Bukkit.getOfflinePlayer(ChatColor
    32. .translateAlternateColorCodes('&', getConfig()
    33. .getString("config.gym2colour"))
    34. + getConfig().getString("config.gym2")));
    35. }
    36. }
    37. } else if(e.getPlayer().hasPermission("pixelgym.gym1")){
    38. if ((getConfig().getString("config.gym1stat").equals("Open"))) {
    39. for(Player p1: Bukkit.getOnlinePlayers()) {
    40. peps.add(p1);
    41. }
    42. for(int i = 0; i < peps.size(); i++){
    43. numofPermissionless++;
    44. if(numofPermissionless != peps.size())
    45. return;
    46. if(!peps.get(i).hasPermission("pixelgym.gym1")){
    47. peps.get(i).sendMessage(ChatColor.DARK_GRAY
    48. + "["
    49. + ChatColor.AQUA
    50. + getConfig().getString("config.title")
    51. + ChatColor.DARK_GRAY
    52. + "] "
    53. + ChatColor.translateAlternateColorCodes('&',
    54. getConfig().getString("config.gym1colour"))
    55. + "The " + getConfig().getString("config.gym1")
    56. + "Gym is now " + ChatColor.RED + "Closed");
    57. getConfig().set("config.gym1stat", "Closed");
    58. board.resetScores(Bukkit.getOfflinePlayer(ChatColor
    59. .translateAlternateColorCodes('&', getConfig()
    60. .getString("config.gym1colour"))
    61. + getConfig().getString("config.gym1")));
    62. }
    63. }
    64. }
    65. }
    66. }
    67. }
    68.  
    69. }


    Thanks:)
     
  14. Offline

    SyTeck

    No, not really. It's poorly written, use @mythbusterma's code since it's written correctly. Might need slight modifications to make it work the way you want.
     
  15. Offline

    mythbusterma


    Son, I don't think you understand how Java works. Please, try and actually understand Java before you try and tell someone else how to write it. You can't just ignore lines you don't understand in the code, in this case the for-each loop.
     
  16. So the only thing i do not understand with this is, does it looks for other players to have the permission on the server.

    I get that it is checking if the one is leaving has the permission..
    But, not checking if anyone else has the permission also. If no one else has the permission, then close the gym.
    Or am i reading it wrong??

    Code:java
    1. if(!p.getPlayer().hasPermission("pixelgym.gym1")) {
    2. return;
    3. }
    4. for (Player player: Bukkit.getOnlinePlayers()) {
    5. if(!player.equals(p.getPlayer())) {
    6. //If the player does not = the one leaving
    7. continue;
    8. }
    9.  
    10. if(!player.hasPermission("whatever")) {
    11. //Check if they do not have the permission
    12. //close the gym
    13. }
    14. }
    15. }

    mythbusterma Would that work? It is supposedly checking if the player leaving has the permision:
    Code:java
    1. if(!p.getPlayer().hasPermission("pixelgym.gym1")) {
    2. return;
    3. }

    Or would i need to add an else in..
    Code:java
    1. if(!p.getPlayer().hasPermission("pixelgym.gym1")) {
    2. return;
    3. }
    4. else {
    5. for (Player player: Bukkit.getOnlinePlayers()) {
    6. if(!player.equals(p.getPlayer())) {
    7. //If the player does not = the one leaving
    8. continue;
    9.  

    And then if player (from for all players) is not the one leaving, then continue checking the player:
    Code:java
    1. if(!p.getPlayer().hasPermission("pixelgym.gym1")) {
    2. return;
    3. }
    4. else {
    5. for (Player player: Bukkit.getOnlinePlayers()) {
    6. if(!player.equals(p.getPlayer())) {
    7. //If the player does not = the one leaving
    8. continue;
    9. }
    10.  
    11. if(!player.hasPermission("whatever")) {
    12. //Check if they do not have the permission
    13. //close the gym
    14. }
    15. }
    16. }
    17. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  17. Offline

    mythbusterma

    kayc01

    Do you know what the continue statement does? My code did not need another else-statement or the negations of boolean values. It wouldn't have posted something that sloppy.

    It worked just the way it was, except you would probably want a break statement inside of the second if.
     
  18. So break; instead of continue; ?
    The only reason i change it is because as i say, it did not look like it was checking if the other players had the permission. If they don't (no one at all) then do stuff.
    Thanks
     
  19. Offline

    mythbusterma

    kayc01

    No......just take a second to think about what the code is doing.....It checks if they're the same player, and if they are, skips them. Then it checks to see if the player has the permission...
     
  20. Right ok, but where as you have made it where ... If the other players have the permission, do something. But i want it as; If the other players do not have the permission, do something.

    So i want to put:
    Code:java
    1. if(!player.hasPermission("pixelgym.gym1")) {
    2. //close the gym because there are no other gym leaders online for the gym to be open.
    3. // you put "give them gym leader" which is not what i want..
    4. }
     
  21. Offline

    mythbusterma

    kayc01

    Then you would simply set a boolean inside the unmodified code, and break. Then check the status of said boolean after.
     
  22. Would you be able to alter the code you gave me so i don't get it wrong?
    Very much appreciated. Will finally end this nightmare xD

    ? Because you are telling me to do things, but not where to put them..

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  23. Offline

    Esophose

    kayc01
    Not to be rude, but at this point you should at least have some idea where everything should go. After reading this entire thread it has come apparent you don't really understand much of what you are doing. Instead of asking us if something will work, you could try doing it yourself first.

    Summary of what everyone here has said:
    - Use a method to check all 32 permissions you need
    - Don't use runnables, skip the player you're iterating through
    - Learn more Java first
    - Use a boolean to represent open and closed gyms

    You should be able to accomplish what you want (Checking all 32 permissions) in under 40 lines. (That's a stretch)
     
  24. But i have obviously tried myself multiple times. I know what i am doing with it as such, but i have never used break's or continues before.

    And again, just being told what to do is not helping me much..
    Yes i am fairly new to java, i have been updating this one plugin for about 6 months and has very messy code.

    So when you or others say;
    Just do this and that and this to do this...

    Instead of actually giving me code, is not helping me learn how to write what i need. And giving the code ensures i get it right, which i can then use in the future for reference. TO LEARN.

    So instead of listing my flaw's, and delaying me finishing this, giving me the code that should work which i have been asking for multiple times. Because i know thats what i need to do for me to understand what is going on and how it is different.

    Like, i tried to modify mythbusterma 's code that was provided, but when i tried and got it wrong... he could of just in-put it himself with the different requirements if its that easy.
    But if i don't know it, i can't do it.

    And he says.. put a break in... ok, so i know what a break does.. but that does not mean i will know where to put it.

    So, sorry for the rant. But when i ask for code, i am not just being lazy. I honestly don't want to get things wrong again, making me look more stupid and then delaying solving this even longer.....
     
  25. Offline

    ColonelHedgehog

    kayc01

    "Coders are stupid for learning how to code," said no (sensible) one ever.

    You will need to have a firm understanding of Java before pursuing an API such as Bukkit. APIs give you the tools to quickly do things with a programming language. Now I'm not going to assume you don't have a firm understanding of Java, however I will say that altering @mythbusterma's code will not help. You really need to take them more as examples and less as things to inject directly.
     
  26. Offline

    mythbusterma

    kayc01

    I give up, I'll just give it to you.

    Code:
    public void onQuit(PlayerLeaveEvent event) {
      if(!event.getPlayer().hasPermission("pixelmon.gym1") {
          return;
      }
     
      boolean toClose = true;
     
      for (Player player: Bukkit.getOnlinePlayers()) {
          if(player.equals(event.getPlayer()) {
              continue;
          }
          if (player.hasPermission("pixelmon.gym1")) {
              toClose = false;
              break;
          }
        }
        if(toClose) {
          // close gym
        }
    }
    There.
     
  27. Offline

    xTrollxDudex

    mythbusterma
    I spy with my little eye a flaw: if you break out of the loop, your if (toClose) will never be progressed to!
     
  28. Offline

    mythbusterma

    xTrollxDudex

    That's just an error in my indentation, not the code.
     
  29. Offline

    teej107

    You forgot this:
    Code:
    System.exit(0);
    It's the anti-blindly-copying code part.
     
    mythbusterma likes this.
  30. Offline

    xTrollxDudex

Thread Status:
Not open for further replies.

Share This Page