How to update a scoreboard? | SimpleScoreboard

Discussion in 'Plugin Development' started by 1kps, Jun 5, 2022.

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

    1kps

    Hello everyone, this is my first post, so excuse me for any mistake I make, I also clarify that I don't know how to speak English so I'm using google translate, once I have clarified all that I proceed with the reason for the publication.

    I started learning how to develop plugins about 2 weeks ago and I wanted to create a scoreboard, so I found a post from a user who left a code called SimpleScoreboard, I proceed to leave the code below:

    Code:
    public class SimpleScoreboard {
    
        private Scoreboard scoreboard;
    
        private String title;
        private Map<String, Integer> scores;
        private List<Team> teams;
    
        public SimpleScoreboard(String title) {
            this.scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
            this.title = title;
            this.scores = Maps.newLinkedHashMap();
            this.teams = Lists.newArrayList();
        }
    
        public void blankLine() {
            add(" ");
        }
    
        public void add(String text) {
            add(text, null);
        }
    
        public void add(String text, Integer score) {
            Preconditions.checkArgument(text.length() < 48, "text cannot be over 48 characters in length");
            text = fixDuplicates(text);
            scores.put(text, score);
        }
    
        private String fixDuplicates(String text) {
            while (scores.containsKey(text)) {
                text += "§r";
            }
            if (text.length() > 48) {
                text = text.substring(0, 47);
            }
            return text;
        }
    
        private Map.Entry<Team, String> createTeam(String text) {
            String result = "";
            if (text.length() <= 16) {
                return new AbstractMap.SimpleEntry<>(null, text);
            }
            Team team = scoreboard.registerNewTeam("text-" + scoreboard.getTeams().size());
            Iterator<String> iterator = Splitter.fixedLength(16).split(text).iterator();
            team.setPrefix(iterator.next());
            result = iterator.next();
            if (text.length() > 32) {
                team.setSuffix(iterator.next());
            }
            teams.add(team);
            return new AbstractMap.SimpleEntry<>(team, result);
        }
    
        public void build() {
            Objective obj = scoreboard.registerNewObjective((title.length() > 16 ? title.substring(0, 15) : title),
                    "dummy");
            obj.setDisplayName(title);
            obj.setDisplaySlot(DisplaySlot.SIDEBAR);
    
            int index = scores.size();
    
            for (Map.Entry<String, Integer> text : scores.entrySet()) {
                Map.Entry<Team, String> team = createTeam(text.getKey());
                Integer score = text.getValue() != null ? text.getValue() : index;
                String value = team.getValue();
                if (team.getKey() != null) {
                    team.getKey().addEntry(value);
                }
                obj.getScore(value).setScore(score);
                index -= 1;
            }
        }
    
        public void reset() {
            title = null;
            scores.clear();
            for (Team t : teams) {
                t.unregister();
            }
            teams.clear();
        }
    
        public Scoreboard getScoreboard() {
            return scoreboard;
        }
    
        public void send(Player... onlinePlayers) {
            for (Player p : onlinePlayers) {
                p.setScoreboard(scoreboard);
            }
        }
    
    }
    And the code I use to create the scoreboard is:

    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    import es.miplugin.SimpleScoreboard;
    import me.clip.placeholderapi.PlaceholderAPI;
    
    public class Scoreboard implements Listener{
      
        @EventHandler
        public void onScore(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            String prefix = "%luckperms_prefix%";
            prefix = PlaceholderAPI.setPlaceholders(event.getPlayer(), prefix);
          
            SimpleScoreboard scoreboard = new SimpleScoreboard("§f\u16CB§e§l Title §f\u16CB");
            scoreboard.add("§f§m---------§e§m---------");
            scoreboard.add("§f§m---------§e§m---------");
            scoreboard.add("§e\u00BB§fNombre");
            scoreboard.add("§e"+player.getDisplayName());
            scoreboard.blankLine();
            scoreboard.add("§e\u00BB§fRango");
            scoreboard.add(prefix);
            scoreboard.blankLine();
            scoreboard.add("§e\u00BB§fJugadores");
            scoreboard.add("§e"+Bukkit.getServer().getOnlinePlayers().size());
            scoreboard.blankLine();
            scoreboard.add("§7§otitle.xyz");
            scoreboard.add("§e§m---------§f§m---------");
            scoreboard.build();
            scoreboard.send(player);
        }
    }
    The code works perfectly and marks everything I want, but my problem is that I don't know how to update it, that is, I don't know how to create a method to update it, I tried several codes that I found in the forums, but none of them worked and if I come back to create the marker once I restore it, I have flickering problems, I hope someone can help me please, I clarify that the codes are not mine since I am new to development in general, first of all thank you very much.

    PD: I would appreciate if you could help me to create a flashless update method, first of all thank you very much and sorry for the inconvenience.
    I also clarify that the only thing I want to update is the prefix, which I get from luckperms and the number of players online.
     
  2. Online

    timtower Administrator Administrator Moderator

    @1kps Get the old scoreboard instead of making a new one.
     
Thread Status:
Not open for further replies.

Share This Page