Double countdown message

Discussion in 'Spigot Plugin Development' started by LavaCode, Apr 25, 2017.

  1. Hello I have a small problem in my Doodle Jump Plugin.


    At the countdown actually works everything only the broadcast messages are always sent twice. Where is the error?





    Code (Text):
       public static int number = 61;
       
       @EventHandler
       public void onJoin(PlayerJoinEvent e)
       {
         if (Bukkit.getOnlinePlayers().size() == 2) {
           scheduler = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable()
           {
             public void run() {
               for (Player all : Bukkit.getOnlinePlayers()) {
               if (Lobby.number <= 61) {
                 Lobby.number -= 1;
                          all.setLevel(Lobby.number);
                     }
                 if (Lobby.number == 60) {
                 Bukkit.broadcastMessage("§7[§eDoodleJump§7] §3Das Spiel startet in §e" + Lobby.number + " §3Sekunden!");
                 } else if (Lobby.number == 30) {
                   Bukkit.broadcastMessage("§7[§eDoodleJump§7] §3Das Spiel startet in §e" + Lobby.number + " §3Sekunden!");
                 } else if (Lobby.number == 15) {
                   Bukkit.broadcastMessage("§7[§eDoodleJump§7] §3Das Spiel startet in §e" + Lobby.number + " §3Sekunden!");
                 } else if ((Lobby.number <= 10) && (Lobby.number != 0) && (Lobby.number != 1)) {
                   Bukkit.broadcastMessage("§7[§eDoodleJump§7] §3Das Spiel startet in §e" + Lobby.number + " §3Sekunden!");
                 } else if (Lobby.number == 1) {
                   Bukkit.broadcastMessage("§7[§eDoodleJump§7] §3Das Spiel startet in §eeiner §3Sekunde!");
                 } else if (Lobby.number == 0) {
                   Bukkit.broadcastMessage("§7[§eDoodleJump§7] §3Lasset die Spiele beginnen!");
                   Lobby.this.teleportarena();
                   Bukkit.getScheduler().cancelTask(Lobby.scheduler);
                 }
                 
               }
               
             
             
            }
           }, 0L, 20L);
         }
       }
     
  2. try putting returns in your else statements, this way when the method isn't able to do one then it will just back out of it. (not sure but this is my best guess since i'm not at a computer with eclipse rn)
     
  3. I did not understand it completely, can you at least a little trying to represent in a code?
     
  4. It will do as many broadcasts as there are people online, there's your error.
     
    • Agree Agree x 1
  5. OK. How can I fix it?
     
  6. Don't put the code inside the foreach players online...
     
  7. Where else?
    I do not understand you at the moment. Can you send my code edited?
     
  8. Nobody is going to spoonfeed you, It is really simple. You put your code inside a "for" loop that loops though the Array of OnlinePlayers. So for every online player (in your case 2 players) the code is being executed sending the message twice.

    Instead of wrapping all of your code with the for loop online put it around the setLevel() line.
     
  9. You can just send message to each player instead of broadcasting the message which would easily fix this issue.
    Player#sendMessage ("message here");
     
    • Agree Agree x 1
  10. You could also do that :D
     
  11.  
  12. Lol I just noticed that hes using the sign instead of chatcolor xD. Tbh it shouldnt matter much but yeah. I too recommand that the OP use ChatColor instead of the symbol.
     
  13. He was asking us to send him the edited code so I'm just pointing out the fact that nobody is going to spoonfeed him, I'm not attacking anyone in any way. After saying that I exactly explained what he was doing wrong and how he can fix his code. There is no need to start harassing people, this post was absolutely useless and its only going to confuse people reading this thread looking for help. So I suggest you shut up and you come back when you're mature enough to use the internet.
     
  14. Code (Text):
    public static int number = 61;
       
       @EventHandler
       public void onJoin(PlayerJoinEvent e)
       {
         if (Bukkit.getOnlinePlayers().size() == 2) {
           scheduler = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable()
           {
             public void run() {
               for (Player all : Bukkit.getOnlinePlayers()) {
               if (Lobby.number <= 61) {
                 Lobby.number -= 1;
                          all.setLevel(Lobby.number - 1);
               
                 if (Lobby.number == 60) {
                 all.sendMessage("§7[§eDoodleJump§7] §3Das Spiel startet in §e" + Lobby.number + " §3Sekunden!");
                 } else if (Lobby.number == 30) {
                   all.sendMessage("§7[§eDoodleJump§7] §3Das Spiel startet in §e" + Lobby.number + " §3Sekunden!");
                 } else if (Lobby.number == 15) {
                   all.sendMessage("§7[§eDoodleJump§7] §3Das Spiel startet in §e" + Lobby.number + " §3Sekunden!");
                 } else if ((Lobby.number <= 10) && (Lobby.number != 0) && (Lobby.number != 1)) {
                   all.sendMessage("§7[§eDoodleJump§7] §3Das Spiel startet in §e" + Lobby.number + " §3Sekunden!");
                 } else if (Lobby.number == 1) {
                   all.sendMessage("§7[§eDoodleJump§7] §3Das Spiel startet in §eeiner §3Sekunde!");
                 } else if (Lobby.number == 0) {
                   all.sendMessage("§7[§eDoodleJump§7] §3Lasset die Spiele beginnen!");
                   all.setLevel(0);
                   Lobby.this.teleportarena();
                   Bukkit.getScheduler().cancelTask(Lobby.scheduler);
                 }
                 
                }
               
             
              }


            }
           }, 0L, 20L);
         }
       }


    This is my code, but how do I get it, if you have joint, the player has 0 level?
     
  15. On top of your code add:
    Code (Text):
    playerWhoJoined.setLevel(0);
    That will set the level of the player joining to 0 :)

    EDIT: I just noticed that you're starting your game when online.size() == 2 but you probably want to change that to online.size() >= 2.
    This way your game will start if there is 2 or more people and not just when there is exactly 2 players on the server.
     
  16. there ya go. Theres the visuals that would have been helpfull in your furst message.