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;
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 String _title;
private int _shineIndex;
private boolean _shineDirection = true;
private boolean _debug = false;
private boolean _debug = false;
public GameScoreboard(Game game)
{
Game = game;
public GameScoreboard(Game game)
{
Game = game;
_title = " MINEPLEX ";
_title = " MINEPLEX ";
//Scoreboard
_scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
// Scoreboard
_scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
_sideObjective = _scoreboard.registerNewObjective("Obj"+UtilMath.r(999999999), "dummy");
_sideObjective.setDisplaySlot(DisplaySlot.SIDEBAR);
_sideObjective.setDisplayName(C.Bold + _title);
}
_sideObjective = _scoreboard.registerNewObjective("Obj" + UtilMath.r(999999999), "dummy");
_sideObjective.setDisplaySlot(DisplaySlot.SIDEBAR);
_sideObjective.setDisplayName(C.Bold + _title);
}
public Scoreboard GetScoreboard()
{
return _scoreboard;
}
public Scoreboard GetScoreboard()
{
return _scoreboard;
}
public Objective GetObjectiveSide()
{
return _sideObjective;
}
public Objective GetObjectiveSide()
{
return _sideObjective;
}
public void UpdateTitle()
{
String out;
public void UpdateTitle()
{
String out;
if (_shineDirection)
{
out = C.cGold + C.Bold;
}
else
{
out = C.cWhite + C.Bold;
}
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);
for (int i = 0; i < _title.length(); i++)
{
char c = _title.charAt(i);
if (_shineDirection)
{
if (i == _shineIndex)
out += C.cYellow + C.Bold;
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.cWhite + C.Bold;
}
else
{
if (i == _shineIndex)
out += C.cYellow + C.Bold;
if (i == _shineIndex + 1)
out += C.cGold + C.Bold;
}
if (i == _shineIndex + 1)
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)
{
_shineIndex = 0;
_shineDirection = !_shineDirection;
}
}
public String ParseTeamName(String name)
{
return name.substring(0, Math.min(16, name.length()));
}
public String ParseTeamName(String name)
{
return name.substring(0, Math.min(16, name.length()));
}
public void CreateTeams()
{
System.out.println("Creating Scoreboard Teams.");
public void CreateTeams()
{
System.out.println("Creating Scoreboard Teams.");
_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() + "");
}
_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
for (GameTeam team : Game.GetTeamList())
{
System.out.println("Scoreboard Team: " + team.GetName().toUpperCase());
_scoreboard.registerNewTeam(ParseTeamName(team.GetName().toUpperCase())).setPrefix(team.GetColor() + "");
}
//Team Groups
for (GameTeam team : Game.GetTeamList())
{
System.out.println("Scoreboard Team: " + team.GetName().toUpperCase());
/*
//Base Groups
for (Rank rank : Rank.values())
{
//_scoreboard.registerNewTeam(ParseTeamName(rank.Name + "SPEC")).setPrefix(ChatColor.GRAY + "");
}
for (Rank rank : Rank.values())
{
_scoreboard.registerNewTeam(ParseTeamName(rank.Name + team.GetName().toUpperCase())).setPrefix(team.GetColor() + "");
}
}
*/
}
//Team Groups
for (GameTeam team : Game.GetTeamList())
{
System.out.println("Scoreboard Team: " + team.GetName().toUpperCase());
public void SetPlayerTeam(Player player, String teamName)
{
for (Team team : _scoreboard.getTeams())
team.removePlayer(player);
for (Rank rank : Rank.values())
{
_scoreboard.registerNewTeam(ParseTeamName(rank.Name + team.GetName().toUpperCase())).setPrefix(team.GetColor() + "");
}
}
*/
}
if (teamName == null)
teamName = "";
public void SetPlayerTeam(Player player, String teamName)
{
for (Team team : _scoreboard.getTeams())
team.removePlayer(player);
String team = ParseTeamName(teamName);
if (teamName == null)
teamName = "";
try
{
_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
{
_scoreboard.getTeam(team).addPlayer(player);
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("ERROR ADDING PLAYER TO TEAM: " + team);
}
}
/*
public void Reset()
{
for (ScoreboardElement elem : _elements)
{
for (String line : elem.GetLines())
{
ResetScore(line);
}
}
public void ResetScore(String line)
{
_scoreboard.resetScores(line);
}
_elements.clear();
/*
public void Reset()
{
for (ScoreboardElement elem : _elements)
{
for (String line : elem.GetLines())
{
ResetScore(line);
}
}
_space = " ";
}
*/
_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)
{
if (line.length() >= 16)
line = line.substring(0, 15);
public void Write(String line)
{
line = Clean(line);
return line;
}
_elements.add(new ScoreboardElementText(line));
}
public void Write(String line)
{
line = Clean(line);
public void WriteOrdered(String key, String line, int value, boolean prependScore)
{
if (prependScore)
line = value + " " + line;
_elements.add(new ScoreboardElementText(line));
}
line = Clean(line);
public void WriteOrdered(String key, String line, int value, boolean prependScore)
{
if (prependScore)
line = value + " " + line;
for (ScoreboardElement elem : _elements)
{
if (elem instanceof ScoreboardElementScores)
{
ScoreboardElementScores scores = (ScoreboardElementScores) elem;
line = Clean(line);
if (scores.IsKey(key))
{
scores.AddScore(line, value);
return;
}
}
}
for (ScoreboardElement elem : _elements)
{
if (elem instanceof ScoreboardElementScores)
{
ScoreboardElementScores scores = (ScoreboardElementScores)elem;
_elements.add(new ScoreboardElementScores(key, line, value, true));
}
if (scores.IsKey(key))
{
scores.AddScore(line, value);
return;
}
}
}
public void WriteBlank()
{
_elements.add(new ScoreboardElementText(" "));
}
_elements.add(new ScoreboardElementScores(key, line, value, true));
}
public void Draw()
{
if (_debug)
System.out.println();
if (_debug)
System.out.println("/////////////////////////");
public void WriteBlank()
{
_elements.add(new ScoreboardElementText(" "));
}
// Generate Lines
ArrayList<String> newLines = new ArrayList<String>();
public void Draw()
{
if (_debug) System.out.println();
if (_debug) System.out.println("/////////////////////////");
for (ScoreboardElement elem : _elements)
{
for (String line : elem.GetLines())
{
newLines.add(line);
}
}
//Generate Lines
ArrayList<String> newLines = new ArrayList<String>();
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++;
}
}
for (ScoreboardElement elem : _elements)
{
for (String line : elem.GetLines())
{
//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();
}
public void Reset()
{
_elements.clear();
}
}