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