Scoreboard API

Status
This thread has been locked.

felldownstairs

Gypsy Boy | Java Developer
Supreme
Feedback score
4
Posts
783
Reactions
144
Resources
0
Today I am going to be looking for an efficient scoreboard api. I don't have any money :( so i would like one which is free. please can you suggest one, only if i dont have to pm someone who never replies, and search through tons of pages to find it. please could you just give me a link. however i would like it to be easily updatable and provide a really easy way to set timers, a little like glaedr.
 
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

Bill

a rare billy boy has appeared
Premium
Feedback score
11
Posts
842
Reactions
336
Resources
0
Here is one you can use, I have used it quite alot. Updated for 1.8, although I think it works for 1.7 aswell. If it doesn´t let me know and I can get you the old version.

Util was made by RainoBoy97 from Bukkit Forums. It is modified a bit though.

//EDIT: Also this is the wrong forum/section to ask for development help aswell FYI.

Code:
public class ScoreboardAPI {
  
    private Scoreboard scoreboard;

    private String title;
    private Map<String, Integer> scores;
    private List<Team> teams;

    public ScoreboardAPI(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;
    }
  
    public Team newTeam(String name) {
        Team t = scoreboard.registerNewTeam(name);
        return t;
    }
  
    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() {
        final 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 (final Map.Entry<String, Integer> text : scores.entrySet()) {
            final 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 onlinePlayer : onlinePlayers) {
            onlinePlayer.setScoreboard(scoreboard);
        }
    }

}
 
Last edited:

ibeacon

Feedback score
0
Posts
63
Reactions
12
Resources
0
I need something
Here is one you can use, I have used it quite alot. Updated for 1.8, although I think it works for 1.7 aswell. If it doesn´t let me know and I can get you the old version.

Util was made by RainoBoy97 from Bukkit Forums. It is modified a bit though.

//EDIT: Also this is the wrong forum/section to ask for development help aswell FYI.

Code:
public class ScoreboardAPI {
 
    private Scoreboard scoreboard;

    private String title;
    private Map<String, Integer> scores;
    private List<Team> teams;

    public ScoreboardAPI(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;
    }
 
    public Team newTeam(String name) {
        Team t = scoreboard.registerNewTeam(name);
        return t;
    }
 
    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() {
        final 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 (final Map.Entry<String, Integer> text : scores.entrySet()) {
            final 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 onlinePlayer : onlinePlayers) {
            onlinePlayer.setScoreboard(scoreboard);
        }
    }

}
Could I use this to?
 

felldownstairs

Gypsy Boy | Java Developer
Supreme
Feedback score
4
Posts
783
Reactions
144
Resources
0
Here is one you can use, I have used it quite alot. Updated for 1.8, although I think it works for 1.7 aswell. If it doesn´t let me know and I can get you the old version.

Util was made by RainoBoy97 from Bukkit Forums. It is modified a bit though.

//EDIT: Also this is the wrong forum/section to ask for development help aswell FYI.

Code:
public class ScoreboardAPI {
 
    private Scoreboard scoreboard;

    private String title;
    private Map<String, Integer> scores;
    private List<Team> teams;

    public ScoreboardAPI(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;
    }
 
    public Team newTeam(String name) {
        Team t = scoreboard.registerNewTeam(name);
        return t;
    }
 
    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() {
        final 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 (final Map.Entry<String, Integer> text : scores.entrySet()) {
            final 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 onlinePlayer : onlinePlayers) {
            onlinePlayer.setScoreboard(scoreboard);
        }
    }

}
thanks for this dude, if u dont mind, could u really quickly run me through how i would make timers etc.[DOUBLEPOST=1501447578][/DOUBLEPOST]
Here is one you can use, I have used it quite alot. Updated for 1.8, although I think it works for 1.7 aswell. If it doesn´t let me know and I can get you the old version.

Util was made by RainoBoy97 from Bukkit Forums. It is modified a bit though.

//EDIT: Also this is the wrong forum/section to ask for development help aswell FYI.

Code:
public class ScoreboardAPI {
 
    private Scoreboard scoreboard;

    private String title;
    private Map<String, Integer> scores;
    private List<Team> teams;

    public ScoreboardAPI(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;
    }
 
    public Team newTeam(String name) {
        Team t = scoreboard.registerNewTeam(name);
        return t;
    }
 
    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() {
        final 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 (final Map.Entry<String, Integer> text : scores.entrySet()) {
            final 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 onlinePlayer : onlinePlayers) {
            onlinePlayer.setScoreboard(scoreboard);
        }
    }

}
thanks for this dude, if u dont mind, could u really quickly run me through how i would make timers etc.
 

Bill

a rare billy boy has appeared
Premium
Feedback score
11
Posts
842
Reactions
336
Resources
0
thanks for this dude, if u dont mind, could u really quickly run me through how i would make timers etc.[DOUBLEPOST=1501447578][/DOUBLEPOST]
thanks for this dude, if u dont mind, could u really quickly run me through how i would make timers etc.

Easiest way of making a timer would be to:

1. Make a scheduler / runnable, and a public int.
2. Every 20 ticks (one second) subtract 1 from the Integer, like: <your int>--;

That is how you create a timer. Adding it/implementing it to the scoreboard isn´t much harder. Just make a scoreboard and update it every 20 ticks with the variable.
 

ibeacon

Feedback score
0
Posts
63
Reactions
12
Resources
0
Here is one you can use, I have used it quite alot. Updated for 1.8, although I think it works for 1.7 aswell. If it doesn´t let me know and I can get you the old version.

Util was made by RainoBoy97 from Bukkit Forums. It is modified a bit though.

//EDIT: Also this is the wrong forum/section to ask for development help aswell FYI.

Code:
public class ScoreboardAPI {
 
    private Scoreboard scoreboard;

    private String title;
    private Map<String, Integer> scores;
    private List<Team> teams;

    public ScoreboardAPI(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;
    }
 
    public Team newTeam(String name) {
        Team t = scoreboard.registerNewTeam(name);
        return t;
    }
 
    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() {
        final 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 (final Map.Entry<String, Integer> text : scores.entrySet()) {
            final 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 onlinePlayer : onlinePlayers) {
            onlinePlayer.setScoreboard(scoreboard);
        }
    }

}
What is the imports for this code?
 

ibeacon

Feedback score
0
Posts
63
Reactions
12
Resources
0
If you use eclipse just hit ctrl + shft + o / cmd + shft + o to import all missing imports.
So, When I join the server should the scoreboard show up or do I have to add something to it?
 

felldownstairs

Gypsy Boy | Java Developer
Supreme
Feedback score
4
Posts
783
Reactions
144
Resources
0
It is a utility, you need to set up the scoreboard yourself. This just makes it easier.
getting an error with private List<Team> teams;
The type List is not generic; it cannot be parameterized with arguments <Team>[DOUBLEPOST=1501506613][/DOUBLEPOST]
It is a utility, you need to set up the scoreboard yourself. This just makes it easier.
ah, got it working, but could you perhaps give some example code, like how to register a scoreboard saying something like example etc. so i know where to start?
 
Last edited:

felldownstairs

Gypsy Boy | Java Developer
Supreme
Feedback score
4
Posts
783
Reactions
144
Resources
0
Easiest way of making a timer would be to:

1. Make a scheduler / runnable, and a public int.
2. Every 20 ticks (one second) subtract 1 from the Integer, like: <your int>--;

That is how you create a timer. Adding it/implementing it to the scoreboard isn´t much harder. Just make a scoreboard and update it every 20 ticks with the variable.
anyway i can remove the scoreboard when an int hits 0
like
if(int == 0) {
scoreboard.remove()
}
or something similar
 

Bill

a rare billy boy has appeared
Premium
Feedback score
11
Posts
842
Reactions
336
Resources
0
getting an error with private List<Team> teams;
The type List is not generic; it cannot be parameterized with arguments <Team>[DOUBLEPOST=1501506613][/DOUBLEPOST]
ah, got it working, but could you perhaps give some example code, like how to register a scoreboard saying something like example etc. so i know where to start?

First off:
ScoreboardAPI myscoreboard = new ScoreboardAPI("§6Title");

myscoreboard.blankLine(); //Send a empty line to the scoreboard
myscoreboard.add("§1line 1"); //Add a line
myscoreboard.add("§2line 2", 123); //Add a line with the score "123"

myscoreboard.build() //Build the board

myscoreboard.send(Player); //Send the scoreboard

Hope this helps.

anyway i can remove the scoreboard when an int hits 0
like
if(int == 0) {
scoreboard.remove()
}
or something similar

Use myscoreboard.reset();
 

felldownstairs

Gypsy Boy | Java Developer
Supreme
Feedback score
4
Posts
783
Reactions
144
Resources
0
First off:
ScoreboardAPI myscoreboard = new ScoreboardAPI("§6Title");

myscoreboard.blankLine(); //Send a empty line to the scoreboard
myscoreboard.add("§1line 1"); //Add a line
myscoreboard.add("§2line 2", 123); //Add a line with the score "123"

myscoreboard.build() //Build the board

myscoreboard.send(Player); //Send the scoreboard

Hope this helps.



Use myscoreboard.reset();
wouldnt myscoreboard.rest(); get rid of all the scoreboard? also would it be ok if you could give me some code on how i would create something like when the login they get a 5 second timer which goes down and then when its at 0 it goes off the scoreboard?
 

Bill

a rare billy boy has appeared
Premium
Feedback score
11
Posts
842
Reactions
336
Resources
0
wouldnt myscoreboard.rest(); get rid of all the scoreboard? also would it be ok if you could give me some code on how i would create something like when the login they get a 5 second timer which goes down and then when its at 0 it goes off the scoreboard?

I won´t spoonfeed you more, go learn java if this is outside your knowledge.
 
Status
This thread has been locked.
Top