Skip to content

Instantly share code, notes, and snippets.

@heyimblake
Created November 12, 2016 23:06
Show Gist options
  • Save heyimblake/87ca5354f1938a2dfa97e1961cdb833f to your computer and use it in GitHub Desktop.
Save heyimblake/87ca5354f1938a2dfa97e1961cdb833f to your computer and use it in GitHub Desktop.
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Scoreboard;
import java.util.HashMap;
/**
* @author heyimblake
*/
public class Sidebar {
private Scoreboard scoreboard;
private Objective obj;
private String title;
private HashMap<Integer, String> scores;
private int SpaceCount;
/**
* @param SidebarTitle the text to display as the title
*/
public Sidebar(String SidebarTitle) {
this.scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
this.title = SidebarTitle;
this.scores = new HashMap<>();
this.SpaceCount = 1;
}
/**
* @param scorenum the score integer
* @param text the text to display on the score
*/
public void add(Integer scorenum, String text) {
this.scores.put(scorenum, text);
}
/**
* @param scorenum the score integer to add the blank line
*/
public void blankLine(Integer scorenum) {
String space = " ";
for (int i = 0; i < this.SpaceCount; i++) {
space = space + " ";
}
this.scores.put(scorenum, space);
this.SpaceCount += 1;
}
/**
* @param scorenum the score integer
* @param text the text to remove (must be exactly as it was added)
*/
public void remove(Integer scorenum, String text) {
this.scores.remove(scorenum, text);
}
public void build() {
this.obj = scoreboard.registerNewObjective((title.length() > 16 ? title.substring(0, 15) : title), "dummy");
this.obj.setDisplayName(title);
this.obj.setDisplaySlot(DisplaySlot.SIDEBAR);
for (int i = -16; i < this.scores.size(); i++) {
if (this.scores.get(i) != null) {
this.obj.getScore(this.scores.get(i)).setScore(i);
}
}
}
/**
* @param player player to send the sidebar to
*/
public void send(Player player) {
player.setScoreboard(this.scoreboard);
}
public void reset() {
this.title = null;
this.scores.clear();
}
/**
* @param player player to remove the sidebar from
*/
public void resetPlayer(Player player) {
Sidebar sidebarDEV = new Sidebar("");
sidebarDEV.build();
sidebarDEV.send(player);
sidebarDEV.reset();
}
/**
* @param score the score integer to replace
* @param text the new text to display
*/
public void update(Integer score, String text) {
Scoreboard scoreboard = this.obj.getScoreboard();
if (this.scores.containsKey(score)) {
scoreboard.resetScores(this.scores.get(score));
this.scores.remove(score);
this.scores.put(score, text);
scoreboard.getObjective(DisplaySlot.SIDEBAR).getScore(text).setScore(score);
}
}
/**
* @param newTitle the string to set as the new title
*/
public void updateTitle(String newTitle) {
this.obj.setDisplayName(newTitle);
this.title = newTitle;
}
/**
* @return hashmap of the scores/text of this sidebar
*/
public HashMap<Integer, String> getScores() {
return this.scores;
}
/**
* Gets the scoreboard that the sidebar is using.
*
* @return scoreboard
*/
public Scoreboard getScoreboard() {
return scoreboard;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment