Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.statiocraft.api.scoreboard;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.bukkit.Bukkit;
- import org.bukkit.entity.Player;
- import org.bukkit.scoreboard.DisplaySlot;
- import org.bukkit.scoreboard.NameTagVisibility;
- import org.bukkit.scoreboard.Objective;
- import org.bukkit.scoreboard.Scoreboard;
- import org.bukkit.scoreboard.Team;
- public class scBoard_DISABLED_ {
- private static final Map<Player, scBoard_DISABLED_> m = new HashMap<Player, scBoard_DISABLED_>();
- /**
- * Creates a new scoreboard for the given player
- * @param player
- * @return
- */
- public static scBoard_DISABLED_ create(Player player, String displayName){
- if(m.containsKey(player))return null;
- return new scBoard_DISABLED_(player, displayName);
- }
- /**
- * Returns the scoreboard assigned to the given player
- * @param player
- * @return
- */
- public static scBoard_DISABLED_ get(Player player){
- return m.get(player);
- }
- private final Player p;
- private final Scoreboard sb;
- private final Objective o;
- private final Team t;
- private String dn, ntp, nts;
- private NameTagVisibility ntv;
- private final Content c;
- private String[] oc;
- private scBoard_DISABLED_(Player player, String displayName){
- if(m.containsKey(player))m.get(player).destroy();
- p = player;
- sb = Bukkit.getScoreboardManager().getNewScoreboard();
- o = sb.registerNewObjective("Objective", "dummy");
- t = sb.registerNewTeam("T_" + p.getName());
- dn = displayName;
- p.setScoreboard(sb);
- o.setDisplaySlot(DisplaySlot.SIDEBAR);
- t.addPlayer(p);
- ntp = "";
- nts = "";
- ntv = NameTagVisibility.NEVER;
- c = new Content();
- oc = new String[0];
- m.put(player, this);
- update();
- }
- /**
- * Destroys the scoreboard
- */
- public void destroy(){
- m.remove(p);
- p.setScoreboard(Bukkit.getScoreboardManager().getMainScoreboard());
- o.unregister();
- t.unregister();
- }
- /**
- * Returns the content
- * @return
- */
- public Content getContent(){
- return c;
- }
- /**
- * Returns the displayName
- * @return
- */
- public String getDisplayName(){
- return dn;
- }
- /**
- * Returns the nameTag prefix
- * @return
- */
- public String getNametagPrefix(){
- return ntp;
- }
- /**
- * Returns the nameTag suffix
- * @return
- */
- public String getNametagSuffix(){
- return nts;
- }
- /**
- * Returns the nameTag visibility
- * @return
- */
- public NameTagVisibility getNametagVisibility(){
- return ntv;
- }
- /**
- * Returns the bukkit-scoreboard
- * @return
- */
- public Scoreboard getScoreboard(){
- return sb;
- }
- /**
- * Returns the scoreboard size
- * @return
- */
- public int getSize(){
- return c.getSize();
- }
- /**
- * Sets the displayName of the scoreboard
- * @param displayName
- */
- public void setDisplayName(String displayName){
- dn = displayName;
- }
- /**
- * Sets the nameTag prefix
- * @param prefix
- */
- public void setNametagPrefix(String prefix){
- ntp = prefix;
- }
- /**
- * Sets the nameTag suffix
- * @param suffix
- */
- public void setNametagSuffix(String suffix){
- nts = suffix;
- }
- /**
- * Sets the nameTag visibility
- * @param visibility
- */
- public void setNametagVisibility(NameTagVisibility visibility){
- ntv = visibility;
- }
- /**
- * Updates the scoreboard
- */
- public void update(){
- //Update Sidebar
- o.setDisplayName(dn);
- List<String> r = new ArrayList<String>();
- String[] nc = c.getRaw();
- for(String os : oc){
- boolean f = false;
- for(String ns : nc){
- if(ns.equals(os)){
- f=true;
- break;
- }
- }
- if(!f)r.add(os);
- }
- for(String s : r){
- sb.resetScores(s);
- }
- for(int n = 0; n < nc.length; n ++){
- o.getScore(nc[n]).setScore(nc.length-n);
- }
- oc = nc;
- //Update Team
- if(ntp.isEmpty())ntp = "§f";
- if(nts.isEmpty())nts = "§f";
- t.setNameTagVisibility(ntv);
- t.setPrefix(ntp);
- t.setSuffix(nts);
- }
- public static class Content {
- private String[] c;
- public Content(int size){
- c = new String[size];
- }
- public Content(){
- this(0);
- }
- /**
- * Adds a line
- * @param text
- * @return
- */
- public int addLine(String text){
- setLine(c.length, text);
- return c.length-1;
- }
- /**
- * Returns the raw text
- * @return
- */
- protected String[] getRaw(){
- return c;
- }
- /**
- * Returns the size
- * @return
- */
- public int getSize(){
- return c.length;
- }
- /**
- * Inserts on the given lineNumber
- * @param line
- * @param text
- * @return
- */
- public int insertLine(int line, String text){
- if(line >= c.length){
- return setLine(line, text);
- }else{
- String[] nc = new String[c.length+1];
- for(int n = 0; n < line; n ++){
- nc[n] = c[n];
- }
- nc[line] = text;
- for(int n = line; n < c.length; n ++){
- nc[n+1]=c[n];
- }
- return line;
- }
- }
- /**
- * Removes the given lineNumber
- * @param line
- * @return
- */
- public boolean removeLine(int line){
- if(line >= c.length)return false;
- String[] nc = new String[c.length-1];
- if(line == 0){
- for(int n = 0; n < nc.length; n ++){
- nc[n]=c[n+1];
- }
- c=nc;
- return true;
- }
- if(line == c.length-1){
- setSize(c.length-1);
- return true;
- }
- for(int n = 0; n < line; n ++){
- nc[n]=c[n];
- }
- for(int n = line; n < nc.length; n ++){
- nc[n]=c[n+1];
- }
- return true;
- }
- /**
- * Sets the given lineNumber
- * @param line
- * @param text
- * @return
- */
- public int setLine(int line, String text){
- if(line >= c.length){
- setSize(c.length+1);
- c[c.length-1]=text;
- return c.length-1;
- }else{
- c[line]=text;
- return line;
- }
- }
- /**
- * Sets the size
- * @param size
- */
- public void setSize(int size){
- if(size == 0){
- c = new String[0];
- return;
- }
- else if(c.length == 0){
- c = new String[size];
- return;
- }
- String[] nc = new String[size];
- for(int n = 0; n < Math.min(nc.length, c.length); n ++){
- nc[n] = c[n];
- }
- c = nc;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement