Update a scoreboard every second without flashing ?!?

Discussion in 'Plugin Development' started by micrlink, Jul 7, 2014.

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

    micrlink

    I am trying to get a hub scoreboard that updates every second, so it doesn't miss any changes, with changes. I doesn't use the right red numbers for values. so I can't
    Code:
    player.getScoreboard().getObjective(DisplaySlot.SIDEBAR).getScore("Score").setScore(4);
    
    I've tried:
    Code:
    Random rand = new Random();
    int i = rand.nextInt(100);
    player.getScoreboard().getObjective(DisplaySlot.SIDEBAR).setDisplayName(rand.nextInt(20) + "");
    
    It doesn't flash, but when I add:
    Code:
    Random rand = new Random();
    player.getScoreboard().resetScores("§f§l" + old.get(player.getName()));
    int i = rand.nextInt(100);
    if(old.containsKey(player.getName())){
          old.remove(player.getName());
    }
    old.put(player.getName(), i);
    player.getScoreboard().getObjective(DisplaySlot.SIDEBAR).getScore("§f§l" + i).setScore(10);
    It flashes. I also tried only updating on player chat, and It flashes after a while too.

    Here is what I mean by flashing:
    [​IMG]
     
    GrandmaJam likes this.
  2. Offline

    TGRHavoc

    Couldn't you just wait for a change in whatever you're displaying on the Scoreboard and then update the board accordingly?
    Doing this means that you don't use unnecessarily create a new scoreboard for all the players when the board doesn't need to be updated..
     
    GrandmaJam likes this.
  3. Offline

    micrlink

    TGRHavoc I've tried that and it still flashes
     
  4. Offline

    teej107

    micrlink How often does this happen? Your scoreboard layout looks similar to Mineplex's scoreboard layout and I even see their scoreboard flash every once in a while.
     
    GrandmaJam likes this.
  5. Offline

    fireblast709

    teej107 Solution from another thread - a bit in Wolvereness starts explaining the issue and a fix. Basically you need two Objectives, of which one plays the 'buffer'. You update the buffer, and then set the displayslot of the buffer to the displayslot to the current. After that, you update the -now- buffer with the current values.
     
  6. Offline

    micrlink

    I understand what your a saying but can you show an example
     
  7. Offline

    fireblast709

    Code:
    // General initialization
    function init()
    {
        // Create a scoreboard
        scoreboard = create();
        // With two objectives
        objective = scoreboard.createObjective();
        // Of which one will act as a buffer
        buffer = scoreboard.createObjective();
        // Basically like double buffering in graphics
    }
     
    function setScore(name, score)
    {
        // First you update the buffer
        buffer.setScore(name, score);
        // Then you tell the scoreboard to use the buffer
        // and swap the variables for our convenience
        swapBuffer();
        // And update the -what used to be objective- buffer
        buffer.setScore(name, score);
    }
     
    function swapBuffer()
    {
        // Simply change the slot, the scoreboard will now
        // push all updating packets to the player
        // Thus wasting not a single ms on executing this at
        // a later time
        buffer.setDisplaySlot(objective.getDisplaySlot());
        // Simply changing references for naming convenience
        t = objective;
        objective = buffer;
        buffer = t;
    }
    micrlink This is the basic principle. In the first call to setScore, you do all the objective updates you would need, then call swapBuffer(), then update the buffer. As shown in the code, I swap the references to active objective and buffer objective, just to keep it understandable what task each instance has. Might be a bit confusing, but tahg me if you have more questions.
     
    GrandmaJam, Garris0n and AdamQpzm like this.
  8. Offline

    micrlink

    fireblast709
    Nothing is happening with this code
    Code:
    package com.micrlink.bsb;
     
    import java.util.Random;
     
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class bsb extends JavaPlugin implements Listener {
     
        public void onEnable () {
            getServer().getPluginManager().registerEvents(this, this);
            getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
                public void run () {
                    for (Player player : getServer().getOnlinePlayers()) {
                        Random rand = new Random();
                        ServerScoreboardManager.getManager().set(player, "hi" + rand.nextInt(10), rand.nextInt(20));
                    }
                }
            }, 1, 1);
        }
     
        @EventHandler
        public void onPlayerJoin (PlayerJoinEvent e) {
            Player player = e.getPlayer();
            player.setScoreboard(ServerScoreboardManager.getManager().create());
        }
    }
    
    Code:
    package com.micrlink.bsb;
     
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
     
    public class ServerScoreboardManager {
     
        Scoreboard scoreboard;
        Objective objective;
        Objective buffer;
        Objective t;
     
        public Scoreboard create () {
            ScoreboardManager manager = Bukkit.getScoreboardManager();
            scoreboard = manager.getNewScoreboard();
            objective = scoreboard.registerNewObjective("Objective", "dummy");
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
            objective.setDisplayName("Sidebar");
            buffer = scoreboard.registerNewObjective("Buffer", "dummy");
            Score score = objective.getScore("hi");
            score.setScore(1);
            return scoreboard;
        }
     
        public Scoreboard get (Player player) {
            scoreboard = player.getScoreboard();
            objective = scoreboard.getObjective("Objective");
            buffer = scoreboard.getObjective("Buffer");
            return scoreboard;
        }
     
        public void set (Player player, String name, int score) {
            scoreboard = get(player);
            buffer.getScore(name).setScore(score);
            swapBuffer(player);
            buffer.getScore(name).setScore(score);
        }
     
        public void swapBuffer (Player player) {
            get(player);
            buffer.setDisplaySlot(objective.getDisplaySlot());
            buffer.setDisplayName(objective.getDisplayName());
            t = objective;
            objective = buffer;
            buffer = t;
        }
     
        public static ServerScoreboardManager getManager () {
            return new ServerScoreboardManager();
        }
    }
    
     
Thread Status:
Not open for further replies.

Share This Page