Edit a specific line on the scoreboard.

Discussion in 'Plugin Development' started by Petersoj, Sep 6, 2015.

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


    I have created an API for myself that makes it easy to edit a certain line on the scoreboard, but every time I call it, it will add an entire new line to the scoreboard. All I want to do it create a method that will edit the certain line of the scoreboard. Here is my method now.
    import org.bukkit.Bukkit;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    public class ScoreBoard{
        public void editScoreboard( Objective obj, int lineNumber, String text){
            Score jo = obj.getScore(Bukkit.getServer().getOfflinePlayer(text));
    EDIT: fixed the code @bwfcwalshy suggested
    Last edited: Sep 6, 2015
  2. Offline

    bwfcwalshy Retired Staff

    @Petersoj Why not just set the score to lineNumber.
    All them cases are just unneeded.
    mine-care likes this.
  3. Offline


    I tried that, but every time I call the method again, it will create a new line with that text.
  4. Offline


    For updating the text/entry name for a certain line (for a certain score) you would have to find out the current entry name for the given score/line-number, then remove that entry and add a new entry with the updated entry-name, but the same score.

    For finding the entry name for a certain score/line number: either you keep track yourself, which entry is in which line, or you will have to search through all entries and all scores of the scoreboard:

    Objective doesn't seem to provide all scores or entry names tracked by this specific objective, so you would have to get all entry names from scoreboard.getEntries(), then for each of those entry names get all scores (the scores for this entry name on all objectives of the scoreboard) via scoreboard.getScores(entry).
    Then for every of those Score objects check if score.getObjective() equals the objective you are trying to update the entry on.
    If it's the correct objective, check if score.getScore() equals your line number (assuming that you are setting the scores to the line numbers, otherwise you have to first get all scores for the objectve and then sort them). If the score corresponds to your intended line number, you have found your entry name and you can stop searching.

    Then you have to remove the score for this entry name from the affected objective. However, bukkit is not offering any method to remove the score for an entry name from only one objective, so you can only use scoreboard.resetScores(entry) to remove all scores for this entry name from all objects of the scoreboard. If you are using a custom scoreboard with only 1 objective at all times, then this might not be a problem for you. Otherwise you will have to restore the score entries for the other objectives again..

    After you have removed the old entry (in case there even was an 'old entry') you can add your new one: objective.getScore(textAkaEntryName).setScore(lineNumber)

    Another note: Don't use Bukkit.getServer().getOfflinePlayer(text) if you are running on newer versions of bukkit, because the offlineplayer-lookup causes lag. But instead use the text directly as entry, like described here.
  5. Offline


    @blablubbabc I'm not entirely sure here, but I got kinda stuck at this point. I tried a couple methods but they have all been failures. Here is my code now.
    public void editScoreboard(Objective obj, int lineNumber, String text){
    Set<String> set = obj.getScoreboard().getEntries();
    Set<Score> i = null;
    for(String s: set){
    i = obj.getScoreboard().getScores(s);
    // not sure what I should put here.
  6. Offline


    Scoreboard. This scoreboard supports 32 characters per row also you can see how it works, its something really easy to learn.
    //At join
    Board board = new Board();
    board.set(0, "Hi there");
    //To update
    Board board = new Board(player);
    board.set(0, "Hi there update");
    tmvkrpxl0 likes this.
  7. Offline


    @MaTaMoR_ Sweet thank you so much! :D

    Nice profile picture!!

    EDIT by Timtower: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Sep 9, 2015
    MaTaMoR_ likes this.
  8. Offline


  9. Offline


    @MrGriefer_HGTech XD did you make it or find it?
  10. Yup I found it!
Thread Status:
Not open for further replies.

Share This Page