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; 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,6 +11,7 @@ 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;
@ -27,7 +23,7 @@ public class GameScoreboard
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;
@ -41,10 +37,10 @@ public class GameScoreboard
_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);
} }
@ -72,7 +68,7 @@ public class GameScoreboard
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);
@ -93,7 +89,6 @@ public class GameScoreboard
out += C.cGold + C.Bold; out += C.cGold + C.Bold;
} }
out += c; out += c;
} }
@ -101,7 +96,7 @@ public class GameScoreboard
_shineIndex++; _shineIndex++;
if (_shineIndex == _title.length()*2) if (_shineIndex == _title.length() * 2)
{ {
_shineIndex = 0; _shineIndex = 0;
_shineDirection = !_shineDirection; _shineDirection = !_shineDirection;
@ -117,10 +112,9 @@ public class GameScoreboard
{ {
System.out.println("Creating Scoreboard Teams."); System.out.println("Creating Scoreboard Teams.");
_scoreboard.registerNewTeam(ParseTeamName("SPEC")).setPrefix(ChatColor.GRAY + ""); _scoreboard.registerNewTeam(ParseTeamName("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());
@ -192,8 +186,21 @@ public class GameScoreboard
public String Clean(String line) public String Clean(String line)
{ {
if (line.length() >= 16) if (line.length() > 28)
line = line.substring(0, 15); {
// 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);
}
}
return line; return line;
} }
@ -216,7 +223,7 @@ public class GameScoreboard
{ {
if (elem instanceof ScoreboardElementScores) if (elem instanceof ScoreboardElementScores)
{ {
ScoreboardElementScores scores = (ScoreboardElementScores)elem; ScoreboardElementScores scores = (ScoreboardElementScores) elem;
if (scores.IsKey(key)) if (scores.IsKey(key))
{ {
@ -236,87 +243,61 @@ public class GameScoreboard
public void Draw() public void Draw()
{ {
if (_debug) System.out.println(); if (_debug)
if (_debug) System.out.println("/////////////////////////"); System.out.println();
if (_debug)
System.out.println("/////////////////////////");
//Generate Lines // Generate Lines
ArrayList<String> newLines = new ArrayList<String>(); ArrayList<String> newLines = new ArrayList<String>();
for (ScoreboardElement elem : _elements) for (ScoreboardElement elem : _elements)
{ {
for (String line : elem.GetLines()) 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); newLines.add(line);
} }
} }
//Find Changes int i = 0;
HashSet<Integer> toAdd = new HashSet<Integer>(); while (true)
HashSet<Integer> toDelete = new HashSet<Integer>();
for (int i=0 ; i<15 ; i++)
{ {
//Delete Old Excess Row if (i >= _chars.length)
if (i >= newLines.size()) break;
String str = ChatColor.COLOR_CHAR + "" + _chars[i] + ChatColor.RESET;
Score score = GetObjectiveSide().getScore(str);
if (newLines.size() <= i)
{ {
if (_current[i] != null) if (score.isScoreSet())
{ {
if (_debug) System.out.println("Delete: " + i + " [" + _current[i] + "]"); ResetScore(str);
toDelete.add(i);
} }
else
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) + "]"); break;
toDelete.add(i);
toAdd.add(i);
} }
} }
else
//Delete Elements - Must happen before Add
for (int i : toDelete)
{ {
//Remove Old Line at Index Team team = GetScoreboard().getTeam(str);
if (_current[i] != null) if (team == null)
{ {
if (_debug) System.out.println("Deleting: " + i + " [" + _current[i] + "]"); team = GetScoreboard().registerNewTeam(str);
team.addEntry(str);
ResetScore(_current[i]); }
_current[i] = null; 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++;
//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 + "]");
} }
} }