Scoreboard plugin super laggy?

Discussion in 'Plugin Development' started by Starfire1337, Oct 23, 2014.

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

    Starfire1337

    What I am trying to do is display a player's balance on a scoreboard, and have it automatically update as needed. The best way I could find to do this was using Bukkit Scheduler, but after testing everything out, I found out it consumes a lot of CPU and drops my TPS down a whole lot. Here is my code:
    Code:java
    1. package com.starfire1337.ScoreboardBalance;
    2.  
    3. import net.milkbowl.vault.economy.Economy;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.RegisteredServiceProvider;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10. import org.bukkit.scoreboard.DisplaySlot;
    11. import org.bukkit.scoreboard.Objective;
    12. import org.bukkit.scoreboard.Score;
    13. import org.bukkit.scoreboard.Scoreboard;
    14.  
    15. public class ScoreboardBalance extends JavaPlugin {
    16.  
    17. public Economy econ;
    18.  
    19. public void onEnable() {
    20. startTimer();
    21. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    22. econ = (Economy) rsp.getProvider();
    23. }
    24.  
    25. public void createScoreboard(Player p) {
    26. String balance = econ.format(econ.getBalance(p.getName())).replace("$", "");
    27. double newBalance = Double.parseDouble(balance);
    28. int finalBalance = (int) newBalance;
    29. Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
    30. Objective objective = board.registerNewObjective("Currency", "dummy");
    31. objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    32. Score score = objective.getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.GREEN + "Money:"));
    33. score.setScore(finalBalance);
    34. p.setScoreboard(board);
    35. }
    36.  
    37. public void updateScoreboard(Player p) {
    38. String balance = econ.format(econ.getBalance(p.getName())).replace("$", "");
    39. double newBalance = Double.parseDouble(balance);
    40. int finalBalance = (int) newBalance;
    41. Scoreboard b = p.getScoreboard();
    42. if (b.getObjective("Currency") != null) {
    43. Objective ob = b.getObjective("Currency");
    44. Score score = ob.getScore(Bukkit.getServer().getOfflinePlayer(ChatColor.GREEN + "Money:"));
    45. score.setScore(finalBalance);
    46. } else {
    47. createScoreboard(p);
    48. }
    49. }
    50.  
    51. public void startTimer() {
    52. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    53. public void run() {
    54. for (Player p : getServer().getOnlinePlayers()) {
    55. updateScoreboard(p);
    56. }
    57. }
    58. }, 60L, 100L);
    59. }
    60. }

    Is there something here that I am doing wrong? Or is this whole scoreboard updating process just very CPU consuming? My TPS was constantly dropping, from a perfect 20, all the way down to about a 2.1. If this in general is CPU consuming, what do you think would be the best solution to have balances update when they physically change on a player's account, yet still keep the CPU low?
     
  2. Offline

    TheCodingCat

    your code seems fine Starfire1337 but istead of using a schedueler, why dont you jsyt listen to all the commands and events that can give the player a higher balance. Then update
     
  3. Offline

    fireblast709

    Starfire1337 don't use OfflinePlayers as scores, use Strings. OfflinePlayers will check the Mojang API for the right UUID, and such HTTP requests are slow.
     
  4. Offline

    Starfire1337

    fireblast709
    Thanks! Changing it to a string increased the TPS quite a bit :)
     
Thread Status:
Not open for further replies.

Share This Page