Skip to content

Instantly share code, notes, and snippets.

@Exerosis
Created February 16, 2018 13:35
Show Gist options
  • Save Exerosis/43fc9bcf12cf4efa4e2ee7698809abb7 to your computer and use it in GitHub Desktop.
Save Exerosis/43fc9bcf12cf4efa4e2ee7698809abb7 to your computer and use it in GitHub Desktop.
public interface Scoreboard {
/**
* @param line - The text to display on the new line, 64 characters max.
* @param score - The score value to give the new line.
*/
void set(Object line, int score);
/**
* Removes the line with the given score.
*
* @param score - The score value associated with the target line.
*/
void remove(int score);
/**
* Sets both {@link org.bukkit.scoreboard.Objective}s display names.
*
* @param title - The desired title for the {@link org.bukkit.scoreboard.Scoreboard}.
*/
void setTitle(Object title);
/**
* @param score - The score value to give the new blank line.
*/
default void blank(int score) {
set("", score);
}
/**
* Returns the underlying {@link org.bukkit.scoreboard.Scoreboard}.
*
* @return - The underlying {@link org.bukkit.scoreboard.Scoreboard}.
*/
org.bukkit.scoreboard.Scoreboard getScoreboard();
}
/**
* A 32 character no flicker scoreboard implementation, fast and lightweight.
*/
public class TeamsScoreboard implements Scoreboard {
private final Objective objective;
private final org.bukkit.scoreboard.Scoreboard board;
private final Map<Integer, String> entries = new HashMap<>();
private final List<Integer> shown = new ArrayList<>();
private int index = 0;
private int times = 0;
public TeamsScoreboard() {
this.board = Bukkit.getScoreboardManager().getNewScoreboard();
objective = board.registerNewObjective("test", "dummy");
objective.setDisplaySlot(DisplaySlot.SIDEBAR);
}
@Override
public void set(Object line, int score) {
String stringLine = ChatColor.translateAlternateColorCodes('&', line.toString());
Team team = board.getTeam(String.valueOf(score));
if (team == null) {
team = board.registerNewTeam(String.valueOf(score));
String entry = getNextEntry();
team.addEntry(entry);
entries.put(score, entry);
}
if (!shown.contains(score)) {
objective.getScore(entries.get(score)).setScore(score);
shown.add(score);
}
String prefix = stringLine.substring(0, min(stringLine.length(), 16));
team.setPrefix(prefix);
String lastColors = ChatColor.getLastColors(prefix);
if (stringLine.length() > 16)
team.setSuffix(lastColors + stringLine.substring(16, min(stringLine.length(), 32 - lastColors.length())));
if (stringLine.length() > 32)
System.err.println("[Scoreboard] A " + stringLine.length() + " character line was truncated to 32 characters. Line: " + score + " Text: '" + stringLine + "'");
}
@Override
public void remove(int score) {
if (entries.containsKey(score))
board.resetScores(entries.get(score));
if (shown.contains(score))
shown.remove(Integer.valueOf(score));
}
@Override
public void setTitle(Object title) {
objective.setDisplayName(ChatColor.translateAlternateColorCodes('&', title.toString()));
}
@Override
public org.bukkit.scoreboard.Scoreboard getScoreboard() {
return board;
}
private String getNextEntry() {
ChatColor color = ChatColor.values()[index++];
if (index >= ChatColor.values().length) {
index = 0;
times++;
}
return color.toString() + ChatColor.values()[times] + ChatColor.RESET;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment