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
1 changed files with 243 additions and 262 deletions

View File

@ -1,13 +1,8 @@
package nautilus.game.arcade.scoreboard; package nautilus.game.arcade.scoreboard;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import mineplex.core.common.Rank;
import mineplex.core.common.util.C; import mineplex.core.common.util.C;
import mineplex.core.common.util.UtilMath; 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.Game;
import nautilus.game.arcade.game.GameTeam; import nautilus.game.arcade.game.GameTeam;
@ -16,312 +11,298 @@ import org.bukkit.ChatColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scoreboard.DisplaySlot; import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective; import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Score;
import org.bukkit.scoreboard.Scoreboard; import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team; import org.bukkit.scoreboard.Team;
public class GameScoreboard public class GameScoreboard
{ {
private Game Game; private Game Game;
private Scoreboard _scoreboard; private Scoreboard _scoreboard;
private Objective _sideObjective; private Objective _sideObjective;
private ArrayList<ScoreboardElement> _elements = new ArrayList<ScoreboardElement>(); private ArrayList<ScoreboardElement> _elements = new ArrayList<ScoreboardElement>();
private String[] _current = new String[15]; private char[] _chars = "1234567890abcdefghijklmnopqrstuvwxyz".toCharArray();
private String _title; private String _title;
private int _shineIndex; private int _shineIndex;
private boolean _shineDirection = true; private boolean _shineDirection = true;
private boolean _debug = false; private boolean _debug = false;
public GameScoreboard(Game game) public GameScoreboard(Game game)
{ {
Game = game; Game = game;
_title = " MINEPLEX "; _title = " MINEPLEX ";
//Scoreboard // Scoreboard
_scoreboard = Bukkit.getScoreboardManager().getNewScoreboard(); _scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
_sideObjective = _scoreboard.registerNewObjective("Obj"+UtilMath.r(999999999), "dummy"); _sideObjective = _scoreboard.registerNewObjective("Obj" + UtilMath.r(999999999), "dummy");
_sideObjective.setDisplaySlot(DisplaySlot.SIDEBAR); _sideObjective.setDisplaySlot(DisplaySlot.SIDEBAR);
_sideObjective.setDisplayName(C.Bold + _title); _sideObjective.setDisplayName(C.Bold + _title);
} }
public Scoreboard GetScoreboard() public Scoreboard GetScoreboard()
{ {
return _scoreboard; return _scoreboard;
} }
public Objective GetObjectiveSide() public Objective GetObjectiveSide()
{ {
return _sideObjective; return _sideObjective;
} }
public void UpdateTitle() public void UpdateTitle()
{ {
String out; String out;
if (_shineDirection) if (_shineDirection)
{ {
out = C.cGold + C.Bold; out = C.cGold + C.Bold;
} }
else else
{ {
out = C.cWhite + C.Bold; out = C.cWhite + C.Bold;
} }
for (int i=0 ; i < _title.length() ; i++) for (int i = 0; i < _title.length(); i++)
{ {
char c = _title.charAt(i); char c = _title.charAt(i);
if (_shineDirection) if (_shineDirection)
{ {
if (i == _shineIndex) if (i == _shineIndex)
out += C.cYellow + C.Bold; out += C.cYellow + C.Bold;
if (i == _shineIndex + 1) if (i == _shineIndex + 1)
out += C.cWhite + C.Bold; out += C.cWhite + C.Bold;
} }
else else
{ {
if (i == _shineIndex) if (i == _shineIndex)
out += C.cYellow + C.Bold; out += C.cYellow + C.Bold;
if (i == _shineIndex + 1) if (i == _shineIndex + 1)
out += C.cGold + C.Bold; out += C.cGold + C.Bold;
} }
out += c;
}
out += c; _sideObjective.setDisplayName(out);
}
_sideObjective.setDisplayName(out); _shineIndex++;
_shineIndex++; if (_shineIndex == _title.length() * 2)
{
_shineIndex = 0;
_shineDirection = !_shineDirection;
}
}
if (_shineIndex == _title.length()*2) public String ParseTeamName(String name)
{ {
_shineIndex = 0; return name.substring(0, Math.min(16, name.length()));
_shineDirection = !_shineDirection; }
}
}
public String ParseTeamName(String name) public void CreateTeams()
{ {
return name.substring(0, Math.min(16, name.length())); System.out.println("Creating Scoreboard Teams.");
}
public void CreateTeams() _scoreboard.registerNewTeam(ParseTeamName("SPEC")).setPrefix(ChatColor.GRAY + "");
{
System.out.println("Creating Scoreboard Teams.");
// Team Groups
for (GameTeam team : Game.GetTeamList())
{
System.out.println("Scoreboard Team: " + team.GetName().toUpperCase());
_scoreboard.registerNewTeam(ParseTeamName(team.GetName().toUpperCase())).setPrefix(team.GetColor() + "");
}
_scoreboard.registerNewTeam(ParseTeamName("SPEC")).setPrefix(ChatColor.GRAY + ""); /*
//Base Groups
for (Rank rank : Rank.values())
{
//_scoreboard.registerNewTeam(ParseTeamName(rank.Name + "SPEC")).setPrefix(ChatColor.GRAY + "");
}
//Team Groups //Team Groups
for (GameTeam team : Game.GetTeamList()) for (GameTeam team : Game.GetTeamList())
{ {
System.out.println("Scoreboard Team: " + team.GetName().toUpperCase()); System.out.println("Scoreboard Team: " + team.GetName().toUpperCase());
_scoreboard.registerNewTeam(ParseTeamName(team.GetName().toUpperCase())).setPrefix(team.GetColor() + "");
}
/* for (Rank rank : Rank.values())
//Base Groups {
for (Rank rank : Rank.values()) _scoreboard.registerNewTeam(ParseTeamName(rank.Name + team.GetName().toUpperCase())).setPrefix(team.GetColor() + "");
{ }
//_scoreboard.registerNewTeam(ParseTeamName(rank.Name + "SPEC")).setPrefix(ChatColor.GRAY + ""); }
} */
}
//Team Groups public void SetPlayerTeam(Player player, String teamName)
for (GameTeam team : Game.GetTeamList()) {
{ for (Team team : _scoreboard.getTeams())
System.out.println("Scoreboard Team: " + team.GetName().toUpperCase()); team.removePlayer(player);
for (Rank rank : Rank.values()) if (teamName == null)
{ teamName = "";
_scoreboard.registerNewTeam(ParseTeamName(rank.Name + team.GetName().toUpperCase())).setPrefix(team.GetColor() + "");
}
}
*/
}
public void SetPlayerTeam(Player player, String teamName) String team = ParseTeamName(teamName);
{
for (Team team : _scoreboard.getTeams())
team.removePlayer(player);
if (teamName == null) try
teamName = ""; {
_scoreboard.getTeam(team).addPlayer(player);
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("ERROR ADDING PLAYER TO TEAM: " + team);
}
}
String team = ParseTeamName(teamName); public void ResetScore(String line)
{
_scoreboard.resetScores(line);
}
try /*
{ public void Reset()
_scoreboard.getTeam(team).addPlayer(player); {
} for (ScoreboardElement elem : _elements)
catch (Exception e) {
{ for (String line : elem.GetLines())
e.printStackTrace(); {
System.out.println("ERROR ADDING PLAYER TO TEAM: " + team); ResetScore(line);
} }
} }
public void ResetScore(String line) _elements.clear();
{
_scoreboard.resetScores(line);
}
/* _space = " ";
public void Reset() }
{ */
for (ScoreboardElement elem : _elements)
{
for (String line : elem.GetLines())
{
ResetScore(line);
}
}
_elements.clear(); 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);
}
}
_space = " "; return line;
} }
*/
public String Clean(String line) public void Write(String line)
{ {
if (line.length() >= 16) line = Clean(line);
line = line.substring(0, 15);
return line; _elements.add(new ScoreboardElementText(line));
} }
public void Write(String line) public void WriteOrdered(String key, String line, int value, boolean prependScore)
{ {
line = Clean(line); if (prependScore)
line = value + " " + line;
_elements.add(new ScoreboardElementText(line)); line = Clean(line);
}
public void WriteOrdered(String key, String line, int value, boolean prependScore) for (ScoreboardElement elem : _elements)
{ {
if (prependScore) if (elem instanceof ScoreboardElementScores)
line = value + " " + line; {
ScoreboardElementScores scores = (ScoreboardElementScores) elem;
line = Clean(line); if (scores.IsKey(key))
{
scores.AddScore(line, value);
return;
}
}
}
for (ScoreboardElement elem : _elements) _elements.add(new ScoreboardElementScores(key, line, value, true));
{ }
if (elem instanceof ScoreboardElementScores)
{
ScoreboardElementScores scores = (ScoreboardElementScores)elem;
if (scores.IsKey(key)) public void WriteBlank()
{ {
scores.AddScore(line, value); _elements.add(new ScoreboardElementText(" "));
return; }
}
}
}
_elements.add(new ScoreboardElementScores(key, line, value, true)); public void Draw()
} {
if (_debug)
System.out.println();
if (_debug)
System.out.println("/////////////////////////");
public void WriteBlank() // Generate Lines
{ ArrayList<String> newLines = new ArrayList<String>();
_elements.add(new ScoreboardElementText(" "));
}
public void Draw() for (ScoreboardElement elem : _elements)
{ {
if (_debug) System.out.println(); for (String line : elem.GetLines())
if (_debug) System.out.println("/////////////////////////"); {
newLines.add(line);
}
}
//Generate Lines int i = 0;
ArrayList<String> newLines = new ArrayList<String>(); 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++;
}
}
for (ScoreboardElement elem : _elements) public void Reset()
{ {
for (String line : elem.GetLines()) _elements.clear();
{ }
//Ensure no duplicate lines
while (true)
{
boolean matched = false;
for (String otherLine : newLines)
{
if (line.equals(otherLine))
{
line += ChatColor.RESET;
matched = true;
}
}
if (!matched)
break;
}
newLines.add(line);
}
}
//Find Changes
HashSet<Integer> toAdd = new HashSet<Integer>();
HashSet<Integer> toDelete = new HashSet<Integer>();
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);
}
continue;
}
//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);
}
}
//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] + "]");
ResetScore(_current[i]);
_current[i] = null;
}
}
//Add Elements
for (int i : toAdd)
{
//Insert New Line
String newLine = newLines.get(i);
GetObjectiveSide().getScore(newLine).setScore(15-i);
_current[i] = newLine;
if (_debug) System.out.println("Setting: " + (15-i) + " [" + newLine + "]");
}
}
public void Reset()
{
_elements.clear();
}
} }