Solved Timer speeding up after player joins. Help!

Discussion in 'Plugin Development' started by Jetsinsu, Apr 19, 2014.

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

    Jetsinsu

    I have been haunted with this thing for about 2 months and I have stopped working on it. I am now going to ask how to fix it cause I don't know how to. PROBLEM: Every time a player joins the array list, the timer speeds up faster than the original one, and the message are sent twice or three time! This will bring me so much joy if you helped me fix this! Thanks! (I might be afk cause I have some stuff to do besides this)

    Code:
    Code:java
    1. if (join.size() >= 2){
    2.  
    3. new BukkitRunnable(){
    4. int i = 30;
    5.  
    6. @Override
    7. public void run(){
    8. if (i > 0){
    9. for (String playername : join){
    10. Player players = getServer().getPlayerExact(playername);
    11. switch (i){
    12. case 29:
    13. players.sendMessage(theendersgame + "There are 30 seconds remaining untill game starts!");
    14. break;
    15. case 15:
    16. players.sendMessage(theendersgame + "There are 15 seconds remaining untill game starts!");
    17. break;
    18. case 10:
    19. players.sendMessage(theendersgame + "There are 10 seconds remaining untill game starts!");
    20. break;
    21. case 5:
    22. players.sendMessage(theendersgame + "There are 5 seconds remaining untill game starts!");
    23. break;
    24. }
    25. }
    26.  
    27. i--;
    28.  
    29. }else{
    30. for (String playername : join){
    31. Player players = getServer().getPlayerExact(playername);
    32. players.getInventory().addItem(new ItemStack(Material.getMaterial(amount)));
    33. freeze.remove(players);
    34. players.sendMessage(theendersgame + " You have 2 minutes untill game ends!");
    35. cancel();
    36. ID = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable(){
    37. int ii = 120;
    38.  
    39. @Override
    40. public void run(){
    41. for (String playername : join){
    42. Player players = getServer().getPlayerExact(playername);
    43. if (ii > 0){
    44. switch (ii){
    45. case 60*1:
    46. players.sendMessage(theendersgame + "There is 1 minute remaining!");
    47. break;
    48. case 30:
    49. players.sendMessage(theendersgame + "There is 30 seconds remaining!");
    50. break;
    51. case 15:
    52. players.sendMessage(theendersgame + "There are 15 seconds remaining!");
    53. break;
    54. case 10:
    55. players.sendMessage(theendersgame + "There are 10 seconds remaining!");
    56. break;
    57. case 5:
    58. players.sendMessage(theendersgame + "There are 5 seconds remaining!");
    59. break;
    60. }
    61.  
    62. ii--;
    63.  
    64. }else{
    65. for (String playername1 : join){
    66. Player players1 = getServer().getPlayerExact(playername1);
    67. World w = Bukkit.getServer().getWorld(getConfig().getString("lobby.world"));
    68. double x = getConfig().getDouble("lobby.x");
    69. double y = getConfig().getDouble("lobby.y");
    70. double z = getConfig().getDouble("lobby.z");
    71. players1.teleport(new Location(w, x, y, z));
    72. join.remove(players1.getName());
    73. damage.remove(players1.getName());
    74. freeze.remove(players1.getName());
    75. players1.getInventory().clear();
    76. players1.setHealth(20.0);
    77. players1.setFoodLevel(20);
    78. players1.sendMessage(theendersgame + " No one won the game!");
    79. int x1 = getConfig().getInt("sign.x");
    80. int y1 = getConfig().getInt("sign.y");
    81. int z1 = getConfig().getInt("sign.z");
    82. Block sign = players.getWorld().getBlockAt(x1, y1, z1);
    83. Sign s = (Sign) sign.getState();
    84. if (s.getLine(1).equalsIgnoreCase("§2ClickToJoin")){
    85. totalplayers--;
    86. s.setLine(2, totalplayers+"/"+maxplayers);
    87. s.update(true);
    88. }
    89.  
    90. Bukkit.getScheduler().cancelTask(ID);
    91. }
    92. }
    93. }
    94. }
    95. }, 0, 20L);
    96. }
    97. }
    98. }
    99. }.runTaskTimer(this, 0, 20L);
    100. }
     
  2. Offline

    xXDJONESXx

    Were is that code pasted? Event? Method?
     
  3. Offline

    Jetsinsu

    xXDJONESXx
    Event
     
  4. Offline

    xXDJONESXx

    Which Event? There are a lot of them.
     
  5. Offline

    Jetsinsu

    xXDJONESXx
    public void onPlayerInteract(final PlayerInteractEvent e){
     
  6. Offline

    xXDJONESXx

    I believe that your creating more than 1 BukkitRunnable's, so what you need to do is re-structure your method for doing so.

    I've made one of these timers in the past and the code is below:
    Code:java
    1. public boolean isTimerRunning = false;
    2.  
    3. public void runStartTimer(){
    4. if (!isTimerRunning){
    5. isTimerRunning = true;
    6. new BukkitRunnable(){
    7. public int StartAmount = (0 * 60) + 10;
    8. public int CountDown = StartAmount;
    9. public void run(){
    10. if (Bukkit.getOnlinePlayers().length >= minStartPlayers) {
    11. int Minutes = CountDown / 60;
    12.  
    13. int Seconds = CountDown - Minutes * 60;
    14.  
    15. if (CountDown == 0) {
    16. for (Player player : Bukkit.getOnlinePlayers())
    17. BarAPI.removeBar(player);
    18. Kenzie.match.Start();
    19. this.cancel();
    20. } else {
    21. for (Player player : Bukkit.getOnlinePlayers()) {
    22. BarAPI.setMessage("§9Match is starting in §l" + Minutes + "§r§9 minutes and §l" + Seconds + "§r§9 Seconds", (CountDown * 100.0f) / StartAmount);
    23. }
    24. if (Seconds%5 == 0 || CountDown <= 10)
    25. Bukkit.broadcastMessage("§9Match is starting in §l" + Minutes + "§r§9 minutes and §l" + Seconds + "§r§9 Seconds.");
    26. CountDown--;
    27. }
    28. }
    29. else {
    30. for (Player player : Bukkit.getOnlinePlayers())
    31. BarAPI.removeBar(player);
    32. Kenzie.match.isTimerRunning = false;
    33. Bukkit.broadcastMessage("§cMatch start timer has been cancelled!");
    34. this.cancel();
    35. }
    36. }
    37. }.runTaskTimer(plugin, 20, 20);
    38.  
    39. }
    40.  
    41. }


    ^ This timer runs for 10 seconds and prints out its value every interval of 5 seconds.
    Hope this helps.
     
  7. Offline

    Jetsinsu

    xXDJONESXx
    Do you know by any chance of one written for an arraylist of players. Like if (join.size() >= 2){ without the speed up bug? That code relating to mine won't help as much cause this is a small minigame.
     
  8. Offline

    xXDJONESXx

    in the for loops change "Bukkit.getOnlinePlayers()" to your players array.
    That method I gave you is what you should work around, just change a couple of things to fit your needs.
     
  9. Offline

    Jetsinsu

    Bump, I just want to know if there is a an easier instruction than xXDJONESXx cause i'm confused.
     
  10. Offline

    Jetsinsu

    YES I DID IT! I took time to try your code and to works just fine! <3 I LOVE UUUUUU........... no homo.
     
    xXDJONESXx likes this.
Thread Status:
Not open for further replies.

Share This Page