Bukkit runnable making server lag

Discussion in 'Plugin Development' started by Innofly, Jul 27, 2014.

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

    Innofly

    Hello guys I recently started coding a last man standing plugin and whenever I run this runnable the server lag as f*.
    If someone know an alternative way to do this.

    Code:java
    1.  
    2. public void aboutToStart()
    3. {
    4. a = 2*60;
    5. Bukkit.broadcastMessage("§8[§bLMS§8] §eA §6LastManStanding§e game is starting in §62 §eminutes!");
    6. taskAboutToStart = new BukkitRunnable()
    7. {
    8. public void run()
    9. {
    10. a--;
    11. int timer = a;
    12. int remainder = timer % 3600;
    13. int minutes = remainder / 60;
    14. int seconds = remainder % 60;
    15. String disMinu = ((minutes < 10) ? "" : "") + minutes;
    16. String disSec = ((seconds < 10) ? "0" : "") + seconds;
    17. formattedb = disMinu + ":" + disSec;
    18. for(Player players : Bukkit.getOnlinePlayers())
    19. {
    20. if(playersingame.contains(players.getName()))
    21. {
    22. scoreboardHandler.scoreboard(players);
    23. }
    24. }
    25. if(a == 60)
    26. {
    27. Bukkit.broadcastMessage("§8[§bLMS§8] §e~ §660§e seconds!");
    28. }
    29. if(a == 30)
    30. {
    31. Bukkit.broadcastMessage("§8[§bLMS§8] §e~ §630§e seconds!");
    32. }
    33. if(a == 10)
    34. {
    35. Bukkit.broadcastMessage("§8[§bLMS§8] §e~ §610§e seconds!");
    36. }
    37. if(a == 5)
    38. {
    39. Bukkit.broadcastMessage("§8[§bLMS§8] §e~ §65§e seconds!");
    40. }
    41. if (a == 0)
    42. {
    43. Bukkit.broadcastMessage("§8[§bLMS§8] §eThe game has started with a total of §6" + playersingame.size() + "§e players!");
    44. StartingTheGame();
    45. taskAboutToStart.cancel();
    46. }
    47. }
    48. };
    49. taskAboutToStart.runTaskTimer(this, 0L, 5L);
    50. }
    51.  
    52.  


    Here is the scoreboard code:

    Code:java
    1.  
    2. @SuppressWarnings("deprecation")
    3. public void scoreboard(Player player) {
    4. ScoreboardManager manager = Bukkit.getScoreboardManager();
    5.  
    6. plugin.board = manager.getNewScoreboard();
    7.  
    8. Objective objective = plugin.board.registerNewObjective("x", "x1");
    9.  
    10. objective.setDisplayName("§6>> §b§lLMS §6<<");
    11. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    12.  
    13. Score score1 = objective.getScore(Bukkit.getOfflinePlayer(" §a"));
    14. score1.setScore(0);
    15.  
    16. Score score = objective.getScore(Bukkit.getOfflinePlayer("§ein: §a" + plugin.formattedb));
    17. score.setScore(1);
    18.  
    19. Score score2 = objective.getScore(Bukkit.getOfflinePlayer("§eGame starting"));
    20. score2.setScore(2);
    21.  
    22. Score score3 = objective.getScore(Bukkit.getOfflinePlayer(" §b"));
    23. score3.setScore(3);
    24. player.setScoreboard(plugin.board);
    25. }
    26.  


    Thanks :D
     
  2. Offline

    Giraffeknee

    Innofly
    I'm not sure why that'd lag the server but instead of checking 'a' so many times, make it a single line if statement with or checks in it and put 'a' in the string that you broadcast.
     
  3. Offline

    mythbusterma

    Innofly

    It's lagging because you're creating a new scoredboard for every player 4 times a second. Perhaps you should reconsider this.
     
  4. Offline

    Shevchik

    Because you are using Bukkit.getOfflinePlayer(string);
     
  5. Offline

    Giraffeknee

    Lol I can't believe I missed the 5L
     
Thread Status:
Not open for further replies.

Share This Page