Modify scoreboard to use teams to support duplicate lines and up to 32 in length

This commit is contained in:
libraryaddict 2014-12-07 20:58:31 +13:00
parent 715862b365
commit f5d9d8dcf0

View File

@ -1,13 +1,8 @@
package nautilus.game.arcade.scoreboard;
import java.util.ArrayList;
import java.util.HashSet;
import mineplex.core.common.Rank;
import mineplex.core.common.util.C;
import mineplex.core.common.util.UtilMath;
import mineplex.core.common.util.UtilTime;
import mineplex.core.common.util.UtilTime.TimeUnit;
import nautilus.game.arcade.game.Game;
import nautilus.game.arcade.game.GameTeam;
@ -16,312 +11,298 @@ import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Score;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
public class GameScoreboard
{
private Game Game;
private Game Game;
private Scoreboard _scoreboard;
private Objective _sideObjective;
private Scoreboard _scoreboard;
private Objective _sideObjective;
private ArrayList<ScoreboardElement> _elements = new ArrayList<ScoreboardElement>();
private String[] _current = new String[15];
private ArrayList<ScoreboardElement> _elements = new ArrayList<ScoreboardElement>();
private char[] _chars = "1234567890abcdefghijklmnopqrstuvwxyz".toCharArray();
private String _title;
private int _shineIndex;
private boolean _shineDirection = true;
private boolean _debug = false;
public GameScoreboard(Game game)
{
Game = game;
private String _title;
private int _shineIndex;
private boolean _shineDirection = true;
_title = " MINEPLEX ";
private boolean _debug = false;
//Scoreboard
_scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
public GameScoreboard(Game game)
{
Game = game;
_sideObjective = _scoreboard.registerNewObjective("Obj"+UtilMath.r(999999999), "dummy");
_sideObjective.setDisplaySlot(DisplaySlot.SIDEBAR);
_sideObjective.setDisplayName(C.Bold + _title);
}
_title = " MINEPLEX ";
public Scoreboard GetScoreboard()
{
return _scoreboard;
}
// Scoreboard
_scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
public Objective GetObjectiveSide()
{
return _sideObjective;
}
_sideObjective = _scoreboard.registerNewObjective("Obj" + UtilMath.r(999999999), "dummy");
_sideObjective.setDisplaySlot(DisplaySlot.SIDEBAR);
_sideObjective.setDisplayName(C.Bold + _title);
}
public void UpdateTitle()
{
String out;
if (_shineDirection)
{
out = C.cGold + C.Bold;
}
else
{
out = C.cWhite + C.Bold;
}
for (int i=0 ; i < _title.length() ; i++)
{
char c = _title.charAt(i);
if (_shineDirection)
{
if (i == _shineIndex)
out += C.cYellow + C.Bold;
if (i == _shineIndex + 1)
out += C.cWhite + C.Bold;
}
else
{
if (i == _shineIndex)
out += C.cYellow + C.Bold;
if (i == _shineIndex + 1)
out += C.cGold + C.Bold;
}
out += c;
}
_sideObjective.setDisplayName(out);
_shineIndex++;
if (_shineIndex == _title.length()*2)
{
_shineIndex = 0;
_shineDirection = !_shineDirection;
}
}
public Scoreboard GetScoreboard()
{
return _scoreboard;
}
public String ParseTeamName(String name)
{
return name.substring(0, Math.min(16, name.length()));
}
public Objective GetObjectiveSide()
{
return _sideObjective;
}
public void CreateTeams()
{
System.out.println("Creating Scoreboard Teams.");
public void UpdateTitle()
{
String out;
_scoreboard.registerNewTeam(ParseTeamName("SPEC")).setPrefix(ChatColor.GRAY + "");
//Team Groups
for (GameTeam team : Game.GetTeamList())
{
System.out.println("Scoreboard Team: " + team.GetName().toUpperCase());
_scoreboard.registerNewTeam(ParseTeamName(team.GetName().toUpperCase())).setPrefix(team.GetColor() + "");
}
/*
//Base Groups
for (Rank rank : Rank.values())
{
//_scoreboard.registerNewTeam(ParseTeamName(rank.Name + "SPEC")).setPrefix(ChatColor.GRAY + "");
}
if (_shineDirection)
{
out = C.cGold + C.Bold;
}
else
{
out = C.cWhite + C.Bold;
}
//Team Groups
for (GameTeam team : Game.GetTeamList())
{
System.out.println("Scoreboard Team: " + team.GetName().toUpperCase());
for (Rank rank : Rank.values())
{
_scoreboard.registerNewTeam(ParseTeamName(rank.Name + team.GetName().toUpperCase())).setPrefix(team.GetColor() + "");
}
}
*/
}
for (int i = 0; i < _title.length(); i++)
{
char c = _title.charAt(i);
public void SetPlayerTeam(Player player, String teamName)
{
for (Team team : _scoreboard.getTeams())
team.removePlayer(player);
if (_shineDirection)
{
if (i == _shineIndex)
out += C.cYellow + C.Bold;
if (teamName == null)
teamName = "";
if (i == _shineIndex + 1)
out += C.cWhite + C.Bold;
}
else
{
if (i == _shineIndex)
out += C.cYellow + C.Bold;
String team = ParseTeamName(teamName);
if (i == _shineIndex + 1)
out += C.cGold + C.Bold;
}
try
{
_scoreboard.getTeam(team).addPlayer(player);
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("ERROR ADDING PLAYER TO TEAM: " + team);
}
}
out += c;
}
public void ResetScore(String line)
{
_scoreboard.resetScores(line);
}
_sideObjective.setDisplayName(out);
/*
public void Reset()
{
for (ScoreboardElement elem : _elements)
{
for (String line : elem.GetLines())
{
ResetScore(line);
}
}
_shineIndex++;
_elements.clear();
if (_shineIndex == _title.length() * 2)
{
_shineIndex = 0;
_shineDirection = !_shineDirection;
}
}
_space = " ";
}
*/
public String ParseTeamName(String name)
{
return name.substring(0, Math.min(16, name.length()));
}
public String Clean(String line)
{
if (line.length() >= 16)
line = line.substring(0, 15);
public void CreateTeams()
{
System.out.println("Creating Scoreboard Teams.");
return line;
}
_scoreboard.registerNewTeam(ParseTeamName("SPEC")).setPrefix(ChatColor.GRAY + "");
public void Write(String line)
{
line = Clean(line);
// Team Groups
for (GameTeam team : Game.GetTeamList())
{
System.out.println("Scoreboard Team: " + team.GetName().toUpperCase());
_scoreboard.registerNewTeam(ParseTeamName(team.GetName().toUpperCase())).setPrefix(team.GetColor() + "");
}
_elements.add(new ScoreboardElementText(line));
}
/*
//Base Groups
for (Rank rank : Rank.values())
{
//_scoreboard.registerNewTeam(ParseTeamName(rank.Name + "SPEC")).setPrefix(ChatColor.GRAY + "");
}
public void WriteOrdered(String key, String line, int value, boolean prependScore)
{
if (prependScore)
line = value + " " + line;
//Team Groups
for (GameTeam team : Game.GetTeamList())
{
System.out.println("Scoreboard Team: " + team.GetName().toUpperCase());
for (Rank rank : Rank.values())
{
_scoreboard.registerNewTeam(ParseTeamName(rank.Name + team.GetName().toUpperCase())).setPrefix(team.GetColor() + "");
}
}
*/
}
line = Clean(line);
public void SetPlayerTeam(Player player, String teamName)
{
for (Team team : _scoreboard.getTeams())
team.removePlayer(player);
for (ScoreboardElement elem : _elements)
{
if (elem instanceof ScoreboardElementScores)
{
ScoreboardElementScores scores = (ScoreboardElementScores)elem;
if (teamName == null)
teamName = "";
if (scores.IsKey(key))
{
scores.AddScore(line, value);
return;
}
}
}
String team = ParseTeamName(teamName);
_elements.add(new ScoreboardElementScores(key, line, value, true));
}
try
{
_scoreboard.getTeam(team).addPlayer(player);
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("ERROR ADDING PLAYER TO TEAM: " + team);
}
}
public void WriteBlank()
{
_elements.add(new ScoreboardElementText(" "));
}
public void ResetScore(String line)
{
_scoreboard.resetScores(line);
}
public void Draw()
{
if (_debug) System.out.println();
if (_debug) System.out.println("/////////////////////////");
/*
public void Reset()
{
for (ScoreboardElement elem : _elements)
{
for (String line : elem.GetLines())
{
ResetScore(line);
}
}
//Generate Lines
ArrayList<String> newLines = new ArrayList<String>();
_elements.clear();
for (ScoreboardElement elem : _elements)
{
for (String line : elem.GetLines())
{
//Ensure no duplicate lines
while (true)
{
boolean matched = false;
_space = " ";
}
*/
for (String otherLine : newLines)
{
if (line.equals(otherLine))
{
line += ChatColor.RESET;
matched = true;
}
}
public String Clean(String line)
{
if (line.length() > 28)
{
// Due to the scoreboard using teams, You can use prefix and suffix for a total length of 32.
// this means that the total length of the string can't extend 32.
// Reason for the fancy logic is that the beginning of the suffix needs to use colors from line1 else the line is pure
// white. And line2 can't have its length extend 16..
String line1 = line.substring(0, 16);
String color = ChatColor.getLastColors(line1);
String line2 = line.substring(16);
int length = 16 - (color + line2).length();
if (length > 0)
{
return line1 + line2.substring(0, line2.length() - length);
}
}
if (!matched)
break;
}
return line;
}
newLines.add(line);
}
}
public void Write(String line)
{
line = Clean(line);
//Find Changes
HashSet<Integer> toAdd = new HashSet<Integer>();
HashSet<Integer> toDelete = new HashSet<Integer>();
_elements.add(new ScoreboardElementText(line));
}
for (int i=0 ; i<15 ; i++)
{
//Delete Old Excess Row
if (i >= newLines.size())
{
if (_current[i] != null)
{
if (_debug) System.out.println("Delete: " + i + " [" + _current[i] + "]");
toDelete.add(i);
}
public void WriteOrdered(String key, String line, int value, boolean prependScore)
{
if (prependScore)
line = value + " " + line;
continue;
}
line = Clean(line);
//Update or Add Row
if (_current[i] == null || !_current[i].equals(newLines.get(i)))
{
if (_debug) System.out.println("Update: " + i + " [" + newLines.get(i) + "]");
toDelete.add(i);
toAdd.add(i);
}
}
for (ScoreboardElement elem : _elements)
{
if (elem instanceof ScoreboardElementScores)
{
ScoreboardElementScores scores = (ScoreboardElementScores) elem;
//Delete Elements - Must happen before Add
for (int i : toDelete)
{
//Remove Old Line at Index
if (_current[i] != null)
{
if (_debug) System.out.println("Deleting: " + i + " [" + _current[i] + "]");
if (scores.IsKey(key))
{
scores.AddScore(line, value);
return;
}
}
}
ResetScore(_current[i]);
_current[i] = null;
}
}
_elements.add(new ScoreboardElementScores(key, line, value, true));
}
//Add Elements
for (int i : toAdd)
{
//Insert New Line
String newLine = newLines.get(i);
GetObjectiveSide().getScore(newLine).setScore(15-i);
_current[i] = newLine;
public void WriteBlank()
{
_elements.add(new ScoreboardElementText(" "));
}
if (_debug) System.out.println("Setting: " + (15-i) + " [" + newLine + "]");
}
}
public void Draw()
{
if (_debug)
System.out.println();
if (_debug)
System.out.println("/////////////////////////");
public void Reset()
{
_elements.clear();
}
// Generate Lines
ArrayList<String> newLines = new ArrayList<String>();
for (ScoreboardElement elem : _elements)
{
for (String line : elem.GetLines())
{
newLines.add(line);
}
}
int i = 0;
while (true)
{
if (i >= _chars.length)
break;
String str = ChatColor.COLOR_CHAR + "" + _chars[i] + ChatColor.RESET;
Score score = GetObjectiveSide().getScore(str);
if (newLines.size() <= i)
{
if (score.isScoreSet())
{
ResetScore(str);
}
else
{
break;
}
}
else
{
Team team = GetScoreboard().getTeam(str);
if (team == null)
{
team = GetScoreboard().registerNewTeam(str);
team.addEntry(str);
}
String line = newLines.get(i);
team.setPrefix(line.substring(0, Math.min(line.length(), 16)));
team.setSuffix(ChatColor.getLastColors(line) + line.substring(team.getPrefix().length()));
if (!score.isScoreSet())
{
if (i == 15)
{
score.setScore(1);
}
score.setScore(15 - i);
}
}
i++;
}
}
public void Reset()
{
_elements.clear();
}
}