Running out of sync

Discussion in 'Plugin Development' started by BaconStripzMan, Feb 4, 2015.

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

    BaconStripzMan

    Hello! So I am making a plugin for my server (Survival Games, Private) and I am working on the timer. It was working fine with my old code then I restarted my code because it was messy and did the exact same thing and now it runs out of sync (When it checks for 30 seconds it runs at 31 seconds and says 30 seconds)

    upload_2015-2-5_15-49-14.png

    Code:
    Code:java
    1.  
    2. package me.PizzaNetwork.SG;
    3.  
    4. import java.util.ArrayList;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.entity.Player;
    8.  
    9. public class GameManager implements Runnable {
    10. public static int timer;
    11. public static boolean waiting = false;
    12. public static boolean platformTeleported = false;
    13. public static boolean hasStarted = false;
    14. public static boolean canStart = false;
    15. public static ArrayList<Player> alive = new ArrayList<Player>();
    16. public static ArrayList<Player> spectators = new ArrayList<Player>();
    17.  
    18. public void run() {
    19.  
    20. for(Player p : Bukkit.getOnlinePlayers()){
    21. p.setLevel(timer);
    22. }
    23.  
    24. if(timer > 0){
    25. timer--;
    26. }
    27.  
    28. if(canStart == true && waiting == true && platformTeleported == false && hasStarted == false && timer == 300 || timer == 240 || timer == 180 || timer == 120
    29. || timer == 60 || timer == 30 || timer == 20 || timer == 15 || timer == 10 || timer == 5 || timer == 4 || timer == 3 || timer == 2 || timer == 1){
    30. Bukkit.broadcastMessage("§9Timer §b§l> §a" + timer + " §aseconds left!");
    31. } else if(canStart == true && waiting == true && timer == 0){
    32. waiting = false;
    33. } else if(canStart == true && waiting == false && platformTeleported == false && timer == 0){
    34. //Teleport Here
    35. Bukkit.broadcastMessage("§9Game §b§l> §aYou will now be teleported to the arena!");
    36. Bukkit.broadcastMessage("§9Game §b§l> §aYou will then have 20 seconds to get ready!");
    37. platformTeleported = true;
    38. timer = 20;
    39. }
    40. }
    41. }
    42.  
     
  2. Offline

    mine-care

    Hmmm first of wall two things I saw,
    1. Why is that much static needed? Why is everything static? I same for atleast half of those fields, you don't want them same between instances.
    2. You don't need to compare boolean values in if's instead of :
    If(boolean == true)
    You can do if(boolean)
    And to check for false, if(!boolean)

    Also instead of manually checking for seconds to broadcast like 200 190 180 ect. You can do a if(time%10 is zero) -meaning that the timer devided by 10 is perfect- broadcast the message.

    I didn't find a why it could be happening actually but I am in phone ATM so I can't properly see your code :-( has any of those timers start before the one that causes sync error?
     
  3. Offline

    sirrus86

    The issue appears to be between lines 31 through 33. In the first "else if" conditional you check if "waiting" is true, then set it to false. Because one part of your if/else chain was true, it finishes the chain and has to loop through it again the next time the task runs. Only use "else if" if you only want one part of the chain to run before moving on to the end of it.

    Also I'm not an expert at Java but I didn't think using AND and OR in the same conditional would work properly, maybe someone can confirm?
     
  4. Offline

    xTrollxDudex

    It works properly. The main problem is the complexity of that statement, not the actual function of it.
     
Thread Status:
Not open for further replies.

Share This Page