Fastest way to update scoreboards

Discussion in 'Plugin Development' started by jimbo8, Aug 11, 2014.

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

    jimbo8

    Hi there!

    Thanks for clicking in on my thread - i got some problems with updating my scoreboards.

    I'm running a scheduler

    Code:java
    1. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    2. scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
    3. @Override
    4. public void run() {
    5. for(Player player : Bukkit.getServer().getOnlinePlayers()){
    6. Scoreboards.initializeScore(player);
    7. }
    8. }
    9. }, 0L, 20L);


    which updates the score here

    Code:java
    1. public static void initializeScore(Player player){
    2. ScoreLib scoreboard = new ScoreLib(player.getName());
    3. scoreboard.add("Penger:", ValueGetters.getCoins(player));
    4. scoreboard.add("EXP:", ValueGetters.getEXP(player));
    5. scoreboard.add("NivÄer:", ValueGetters.getLevels(player));
    6.  
    7. scoreboard.build();
    8. scoreboard.send(player);
    9. }


    using @RainoBoy97's lib, but i'm 100% sure it's not because of his library. I'm getting the information from a MySQL-server when the player's log in and putting it in a hashmap(not seen here):

    Code:java
    1. public static void getCoinsFromDB(UUID uuid) {
    2. try {
    3. Connection conn = DriverManager.getConnection(url, user, pass);
    4. Statement statement = conn.createStatement();
    5. ResultSet res = statement
    6. .executeQuery("SELECT * FROM verdier WHERE uuid='" + uuid
    7. + "';");
    8. while (res.next()) {
    9. if (res.getString(6) == null) {
    10. ValueGetters.coins.put(uuid, 0);
    11. } else {
    12. ValueGetters.coins.put(uuid, res.getInt(6));
    13. }
    14. }
    15. statement.close();
    16. conn.close();
    17. } catch (SQLException e) {
    18. e.printStackTrace();
    19. }
    20. }


    I also have getEXPFromDB and getLevelsFromDB with almost the same code.

    Now, every time i get information from the hashmaps

    Code:java
    1. public static HashMap<UUID, Integer> coins = new HashMap<UUID, Integer>();
    2. public static HashMap<UUID, Integer> EXP = new HashMap<UUID, Integer>();
    3. public static HashMap<UUID, Integer> levels = new HashMap<UUID, Integer>();


    it starts to lag a looot. The server is unusable for a few seconds. This is every time the scheduler runs.

    I tried to run it ASynchroblabla, but then it just spews out alot of errors.

    Any ideas?

    Thanks :)
     
  2. Offline

    Dubehh

    Isn't it just easier to use the Bukkit's scoreboard API
     
  3. Offline

    fireblast709

    jimbo8 you need to run your SQL async. If you get errors, post those instead.

    As a sidenote, stop abusing static.
     
    thomasb454 and jimbo8 like this.
  4. Offline

    jimbo8

    fireblast709

    Hmm, okay. I'll try to run SQL ASync and get back to you guys with it.

    Also, i use static because i'm lazy :p I never really understood why i shouldn't use it.

    fireblast709
    Okay, so i didn't really think that much when i answered you.

    The SQL-queries are only ran on player login and logoff. When they log in, their information gets saved into a HashMap, when they log off it's the opposite.

    So the information i'm getting when initializing the score is from a hashmap.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  5. Offline

    thomasb454


    Java is OOP, essentially static defeats this purpose.
     
    jimbo8 likes this.
  6. Offline

    jimbo8

    A shameful little bump.
     
  7. Offline

    Dealyise

    Try using Bukkit Scoreboard API instead of using a lib.
    If it's still lagging, tell us.
     
  8. Offline

    jimbo8

    I tried using the Bukkit scoreboard API exactly the same way as this lib works, but it still lags. Thanks for leaving an answer, though :) Dealyise
     
  9. Offline

    Dealyise

    jimbo8
    I think I know why it lags. You are putting players in a HashMap with a Scheduler. synchronized() could fix your lags.
    I am not 100% sure, but this might help
     
  10. Offline

    jimbo8

    Where would i put the synchronized? ;)

    I haven't used that function before.

    Dealyise
    Nevermind. I'm stupid. :p

    It works a bit, it lags less, but still a bit too much :/
     
  11. Offline

    Dealyise

    jimbo8
    You got Skype? If so, write me your name.
     
  12. Offline

    jimbo8

  13. Offline

    Dealyise

Thread Status:
Not open for further replies.

Share This Page