use of org.bukkit.scoreboard.Scoreboard in project Denizen-For-Bukkit by DenizenScript.
the class ScoreboardCommand method execute.
@Override
public void execute(final ScriptEntry scriptEntry) {
List<PlayerTag> viewers = (List<PlayerTag>) scriptEntry.getObject("viewers");
ListTag lines = scriptEntry.hasObject("lines") ? ListTag.valueOf(scriptEntry.getElement("lines").asString(), scriptEntry.getContext()) : new ListTag();
ElementTag action = scriptEntry.getElement("action");
ElementTag id = scriptEntry.getElement("id");
ElementTag objective = scriptEntry.getElement("objective");
ElementTag criteria = scriptEntry.getElement("criteria");
ElementTag score = scriptEntry.getElement("score");
ElementTag displaySlot = scriptEntry.getElement("displayslot");
ElementTag displayName = scriptEntry.getElement("displayname");
ElementTag renderType = scriptEntry.getElement("rendertype");
Action act = Action.valueOf(action.asString().toUpperCase());
boolean hadCriteria = criteria != null;
boolean hadDisplaySlot = displaySlot != null;
if (!hadCriteria) {
criteria = new ElementTag("dummy");
}
if (!hadDisplaySlot) {
displaySlot = new ElementTag("sidebar");
}
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), action, id, db("viewers", viewers), objective, lines, score, objective, displaySlot, criteria, displayName, renderType);
}
Scoreboard board = null;
// Get the main scoreboard by default
if (id.asString().equalsIgnoreCase("main")) {
board = ScoreboardHelper.getMain();
} else if (id.asString().equalsIgnoreCase("player")) {
board = Utilities.getEntryPlayer(scriptEntry).getPlayerEntity().getScoreboard();
} else {
// If this scoreboard already exists, get it
if (ScoreboardHelper.hasScoreboard(id.asString())) {
board = ScoreboardHelper.getScoreboard(id.asString());
} else // Else, create a new one if Action is ADD
if (act.equals(Action.ADD)) {
board = ScoreboardHelper.createScoreboard(id.asString());
}
}
// Don't progress if we ended up with a null board
if (board == null) {
Debug.echoError(scriptEntry, "Scoreboard " + id.asString() + " does not exist!");
return;
}
Objective obj;
if (act.equals(Action.ADD)) {
if (objective != null) {
// Try getting the objective from the board
obj = board.getObjective(objective.asString());
boolean existedAlready = obj != null;
// Create the objective if it does not already exist
if (obj == null) {
obj = board.registerNewObjective(objective.asString(), criteria.asString());
} else // recreate the objective
if (hadCriteria && !obj.getCriteria().equals(criteria.asString())) {
obj.unregister();
obj = board.registerNewObjective(objective.asString(), criteria.asString());
}
// Change the objective's display slot
if ((!existedAlready || hadDisplaySlot) && !displaySlot.asString().equalsIgnoreCase("none")) {
obj.setDisplaySlot(DisplaySlot.valueOf(displaySlot.asString().toUpperCase()));
}
if (renderType != null) {
obj.setRenderType(RenderType.valueOf(renderType.asString().toUpperCase()));
}
if (displayName != null) {
obj.setDisplayName(displayName.asString());
} else if (!existedAlready) {
obj.setDisplayName(objective.asString());
}
if (!lines.isEmpty()) {
// use a score of 0
if (score == null) {
score = new ElementTag(0);
}
for (ObjectTag line : lines.objectForms) {
ScoreboardHelper.addScore(obj, checkLine(line), score.asInt());
}
}
} else // the command cannot do anything at all, so print a message about that
if (viewers == null && !lines.isEmpty()) {
Debug.echoDebug(scriptEntry, "Cannot add lines without specifying an objective!");
}
} else if (act.equals(Action.REMOVE)) {
if (objective != null) {
// Try getting the objective from the board
obj = board.getObjective(objective.asString());
if (obj != null) {
// Remove the entire objective if no lines have been specified
if (lines.isEmpty()) {
Debug.echoDebug(scriptEntry, "Removing objective " + obj.getName() + " from scoreboard " + id.asString());
obj.unregister();
} else {
for (ObjectTag line : lines.objectForms) {
ScoreboardHelper.removeScore(obj, checkLine(line));
}
}
} else {
Debug.echoError(scriptEntry, "Objective " + objective.asString() + " does not exist in scoreboard " + id.asString());
}
} else // lines from every objective
if (!lines.isEmpty()) {
Debug.echoDebug(scriptEntry, "Removing lines " + lines.identify() + " from all objectives in scoreboard " + id.asString());
for (ObjectTag line : lines.objectForms) {
ScoreboardHelper.removePlayer(id.asString(), checkLine(line));
}
} else // of viewers should be removed instead)
if (viewers == null) {
Debug.echoDebug(scriptEntry, "Removing scoreboard " + id.asString());
ScoreboardHelper.deleteScoreboard(id.asString());
}
}
if (viewers != null) {
for (PlayerTag viewer : viewers) {
// Add viewers for this scoreboard
if (act.equals(Action.ADD)) {
// to the map of viewers saved by Denizen
if (!id.asString().equalsIgnoreCase("main")) {
ScoreboardHelper.viewerMap.put(viewer.getUUID(), id.asString());
}
// is already online
if (viewer.isOnline()) {
viewer.getPlayerEntity().setScoreboard(board);
}
} else // Remove viewers for this scoreboard
if (act.equals(Action.REMOVE)) {
// Take this player out of the map of viewers
ScoreboardHelper.viewerMap.remove(viewer.getUUID());
// provided by Bukkit)
if (viewer.isOnline()) {
viewer.getPlayerEntity().setScoreboard(ScoreboardHelper.createScoreboard());
}
}
}
}
}
use of org.bukkit.scoreboard.Scoreboard in project Denizen-For-Bukkit by DenizenScript.
the class CustomEntityHelperImpl method spawnFakePlayer.
public static FakePlayer spawnFakePlayer(Location location, String name, String skin, boolean doAdd) throws IllegalArgumentException {
String fullName = name;
String prefix = null;
String suffix = null;
if (name == null) {
return null;
} else if (fullName.length() > 16) {
prefix = fullName.substring(0, 16);
if (fullName.length() > 30) {
int len = 30;
name = fullName.substring(16, 30);
if (name.matches(".*[^A-Za-z0-9_].*")) {
if (fullName.length() >= 32) {
len = 32;
name = fullName.substring(16, 32);
} else if (fullName.length() == 31) {
len = 31;
name = fullName.substring(16, 31);
}
} else if (name.length() > 46) {
throw new IllegalArgumentException("You must specify a name with no more than 46 characters for FAKE_PLAYER entities!");
} else {
name = ChatColor.RESET + name;
}
suffix = fullName.substring(len);
} else {
name = fullName.substring(16);
if (!name.matches(".*[^A-Za-z0-9_].*")) {
name = ChatColor.RESET + name;
}
if (name.length() > 16) {
suffix = name.substring(16);
name = name.substring(0, 16);
}
}
}
if (skin != null && skin.length() > 16) {
throw new IllegalArgumentException("You must specify a name with no more than 16 characters for FAKE_PLAYER entity skins!");
}
CraftWorld world = (CraftWorld) location.getWorld();
WorldServer worldServer = world.getHandle();
PlayerProfile playerProfile = new PlayerProfile(name, null);
if (skin == null && !name.matches(".*[^A-Za-z0-9_].*")) {
playerProfile = NMSHandler.getInstance().fillPlayerProfile(playerProfile);
}
if (skin != null) {
PlayerProfile skinProfile = new PlayerProfile(skin, null);
skinProfile = NMSHandler.getInstance().fillPlayerProfile(skinProfile);
playerProfile.setTexture(skinProfile.getTexture());
playerProfile.setTextureSignature(skinProfile.getTextureSignature());
}
UUID uuid = UUID.randomUUID();
if (uuid.version() == 4) {
long msb = uuid.getMostSignificantBits();
msb &= ~0x0000000000004000L;
msb |= 0x0000000000002000L;
uuid = new UUID(msb, uuid.getLeastSignificantBits());
}
playerProfile.setUniqueId(uuid);
GameProfile gameProfile = new GameProfile(playerProfile.getUniqueId(), playerProfile.getName());
gameProfile.getProperties().put("textures", new Property("textures", playerProfile.getTexture(), playerProfile.getTextureSignature()));
final EntityFakePlayerImpl fakePlayer = new EntityFakePlayerImpl(worldServer.getMinecraftServer(), worldServer, gameProfile, new PlayerInteractManager(worldServer), doAdd);
fakePlayer.setPositionRotation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
CraftFakePlayerImpl craftFakePlayer = fakePlayer.getBukkitEntity();
craftFakePlayer.fullName = fullName;
if (prefix != null) {
Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
String teamName = "FAKE_PLAYER_TEAM_" + fullName;
String hash = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = teamName.getBytes(StandardCharsets.UTF_8);
md.update(bytes, 0, bytes.length);
hash = new BigInteger(1, md.digest()).toString(16).substring(0, 16);
} catch (Exception e) {
Debug.echoError(e);
}
if (hash != null) {
Team team = scoreboard.getTeam(hash);
if (team == null) {
team = scoreboard.registerNewTeam(hash);
team.setPrefix(prefix);
if (suffix != null) {
team.setSuffix(suffix);
}
}
team.addPlayer(craftFakePlayer);
}
}
return craftFakePlayer;
}
use of org.bukkit.scoreboard.Scoreboard in project Denizen-For-Bukkit by DenizenScript.
the class CustomEntityHelperImpl method spawnFakePlayer.
public static FakePlayer spawnFakePlayer(Location location, String name, String skin, boolean doAdd) throws IllegalArgumentException {
String fullName = name;
String prefix = null;
String suffix = null;
if (name == null) {
Debug.echoError("FAKE_PLAYER: null name, cannot spawn");
return null;
} else if (fullName.length() > 16) {
prefix = fullName.substring(0, 16);
if (fullName.length() > 30) {
int len = 30;
name = fullName.substring(16, 30);
if (name.matches(".*[^A-Za-z0-9_].*")) {
if (fullName.length() >= 32) {
len = 32;
name = fullName.substring(16, 32);
} else if (fullName.length() == 31) {
len = 31;
name = fullName.substring(16, 31);
}
} else if (name.length() > 46) {
throw new IllegalArgumentException("You must specify a name with no more than 46 characters for FAKE_PLAYER entities!");
} else {
name = ChatColor.RESET + name;
}
suffix = fullName.substring(len);
} else {
name = fullName.substring(16);
if (!name.matches(".*[^A-Za-z0-9_].*")) {
name = ChatColor.RESET + name;
}
if (name.length() > 16) {
suffix = name.substring(16);
name = name.substring(0, 16);
}
}
}
if (skin != null && skin.length() > 16) {
throw new IllegalArgumentException("You must specify a name with no more than 16 characters for FAKE_PLAYER entity skins!");
}
CraftWorld world = (CraftWorld) location.getWorld();
ServerLevel worldServer = world.getHandle();
PlayerProfile playerProfile = new PlayerProfile(name, null);
if (skin == null && !name.matches(".*[^A-Za-z0-9_].*")) {
playerProfile = NMSHandler.getInstance().fillPlayerProfile(playerProfile);
}
if (skin != null) {
PlayerProfile skinProfile = new PlayerProfile(skin, null);
skinProfile = NMSHandler.getInstance().fillPlayerProfile(skinProfile);
playerProfile.setTexture(skinProfile.getTexture());
playerProfile.setTextureSignature(skinProfile.getTextureSignature());
}
UUID uuid = UUID.randomUUID();
if (uuid.version() == 4) {
long msb = uuid.getMostSignificantBits();
msb &= ~0x0000000000004000L;
msb |= 0x0000000000002000L;
uuid = new UUID(msb, uuid.getLeastSignificantBits());
}
playerProfile.setUniqueId(uuid);
GameProfile gameProfile = new GameProfile(playerProfile.getUniqueId(), playerProfile.getName());
gameProfile.getProperties().put("textures", new Property("textures", playerProfile.getTexture(), playerProfile.getTextureSignature()));
final EntityFakePlayerImpl fakePlayer = new EntityFakePlayerImpl(worldServer.getServer(), worldServer, gameProfile, doAdd);
fakePlayer.forceSetPositionRotation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
CraftFakePlayerImpl craftFakePlayer = fakePlayer.getBukkitEntity();
craftFakePlayer.fullName = fullName;
if (prefix != null) {
Scoreboard scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
String teamName = "FAKE_PLAYER_TEAM_" + fullName;
String hash = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = teamName.getBytes(StandardCharsets.UTF_8);
md.update(bytes, 0, bytes.length);
hash = new BigInteger(1, md.digest()).toString(16).substring(0, 16);
} catch (Exception e) {
Debug.echoError(e);
}
if (hash != null) {
Team team = scoreboard.getTeam(hash);
if (team == null) {
team = scoreboard.registerNewTeam(hash);
team.setPrefix(prefix);
if (suffix != null) {
team.setSuffix(suffix);
}
}
team.addPlayer(craftFakePlayer);
}
}
return craftFakePlayer;
}
use of org.bukkit.scoreboard.Scoreboard in project FunnyGuilds by FunnyGuilds.
the class IndividualPrefix method initialize.
private void initialize() {
if (getUser() == null) {
return;
}
List<Guild> guilds = GuildUtils.getGuilds();
Scoreboard scoreboard = getUser().getScoreboard();
Guild guild = getUser().getGuild();
if (guild != null) {
guilds.remove(guild);
PluginConfig config = Settings.getConfig();
String our = config.prefixOur;
String ally = config.prefixAllies;
String enemy = config.prefixEnemies;
String other = config.prefixOther;
Team team = scoreboard.getTeam(guild.getTag());
if (team == null) {
team = scoreboard.registerNewTeam(guild.getTag());
}
for (User u : guild.getMembers()) {
if (u.getName() == null) {
continue;
}
if (!team.hasEntry(u.getName())) {
team.addEntry(u.getName());
}
}
team.setPrefix(replace(our, "{TAG}", guild.getTag()));
for (Guild one : guilds) {
if (one == null || one.getTag() == null) {
continue;
}
team = scoreboard.getTeam(one.getTag());
if (team == null) {
team = scoreboard.registerNewTeam(one.getTag());
}
for (User u : one.getMembers()) {
if (u.getName() == null) {
continue;
}
if (!team.hasEntry(u.getName())) {
team.addEntry(u.getName());
}
}
if (guild.getAllies().contains(one)) {
team.setPrefix(replace(ally, "{TAG}", one.getTag()));
} else if (guild.getEnemies().contains(one)) {
team.setPrefix(replace(enemy, "{TAG}", one.getTag()));
} else {
team.setPrefix(replace(other, "{TAG}", one.getTag()));
}
}
} else {
String other = Settings.getConfig().prefixOther;
for (Guild one : guilds) {
if (one == null || one.getTag() == null) {
continue;
}
Team team = scoreboard.getTeam(one.getTag());
if (team == null) {
team = scoreboard.registerNewTeam(one.getTag());
}
for (User u : one.getMembers()) {
if (u.getName() == null) {
continue;
}
if (!team.hasEntry(u.getName())) {
team.addEntry(u.getName());
}
}
team.setPrefix(replace(other, "{TAG}", one.getTag()));
}
}
}
use of org.bukkit.scoreboard.Scoreboard in project Bukkit by Bukkit.
the class ScoreboardCommand method execute.
@Override
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
if (!testPermission(sender))
return true;
if (args.length < 1 || args[0].length() == 0) {
sender.sendMessage(ChatColor.RED + "Usage: /scoreboard <objectives|players|teams>");
return false;
}
final Scoreboard mainScoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
if (args[0].equalsIgnoreCase("objectives")) {
if (args.length == 1) {
sender.sendMessage(ChatColor.RED + "Usage: /scoreboard objectives <list|add|remove|setdisplay>");
return false;
}
if (args[1].equalsIgnoreCase("list")) {
Set<Objective> objectives = mainScoreboard.getObjectives();
if (objectives.isEmpty()) {
sender.sendMessage(ChatColor.RED + "There are no objectives on the scoreboard");
return false;
}
sender.sendMessage(ChatColor.DARK_GREEN + "Showing " + objectives.size() + " objective(s) on scoreboard");
for (Objective objective : objectives) {
sender.sendMessage("- " + objective.getName() + ": displays as '" + objective.getDisplayName() + "' and is type '" + objective.getCriteria() + "'");
}
} else if (args[1].equalsIgnoreCase("add")) {
if (args.length < 4) {
sender.sendMessage(ChatColor.RED + "/scoreboard objectives add <name> <criteriaType> [display name ...]");
return false;
}
String name = args[2];
String criteria = args[3];
if (criteria == null) {
sender.sendMessage(ChatColor.RED + "Invalid objective criteria type. Valid types are: " + stringCollectionToString(OBJECTIVES_CRITERIA));
} else if (name.length() > 16) {
sender.sendMessage(ChatColor.RED + "The name '" + name + "' is too long for an objective, it can be at most 16 characters long");
} else if (mainScoreboard.getObjective(name) != null) {
sender.sendMessage(ChatColor.RED + "An objective with the name '" + name + "' already exists");
} else {
String displayName = null;
if (args.length > 4) {
displayName = StringUtils.join(ArrayUtils.subarray(args, 4, args.length), ' ');
if (displayName.length() > 32) {
sender.sendMessage(ChatColor.RED + "The name '" + displayName + "' is too long for an objective, it can be at most 32 characters long");
return false;
}
}
Objective objective = mainScoreboard.registerNewObjective(name, criteria);
if (displayName != null && displayName.length() > 0) {
objective.setDisplayName(displayName);
}
sender.sendMessage("Added new objective '" + name + "' successfully");
}
} else if (args[1].equalsIgnoreCase("remove")) {
if (args.length != 3) {
sender.sendMessage(ChatColor.RED + "/scoreboard objectives remove <name>");
return false;
}
String name = args[2];
Objective objective = mainScoreboard.getObjective(name);
if (objective == null) {
sender.sendMessage(ChatColor.RED + "No objective was found by the name '" + name + "'");
} else {
objective.unregister();
sender.sendMessage("Removed objective '" + name + "' successfully");
}
} else if (args[1].equalsIgnoreCase("setdisplay")) {
if (args.length != 3 && args.length != 4) {
sender.sendMessage(ChatColor.RED + "/scoreboard objectives setdisplay <slot> [objective]");
return false;
}
String slotName = args[2];
DisplaySlot slot = OBJECTIVES_DISPLAYSLOT.get(slotName);
if (slot == null) {
sender.sendMessage(ChatColor.RED + "No such display slot '" + slotName + "'");
} else {
if (args.length == 4) {
String objectiveName = args[3];
Objective objective = mainScoreboard.getObjective(objectiveName);
if (objective == null) {
sender.sendMessage(ChatColor.RED + "No objective was found by the name '" + objectiveName + "'");
return false;
}
objective.setDisplaySlot(slot);
sender.sendMessage("Set the display objective in slot '" + slotName + "' to '" + objective.getName() + "'");
} else {
Objective objective = mainScoreboard.getObjective(slot);
if (objective != null) {
objective.setDisplaySlot(null);
}
sender.sendMessage("Cleared objective display slot '" + slotName + "'");
}
}
}
} else if (args[0].equalsIgnoreCase("players")) {
if (args.length == 1) {
sender.sendMessage(ChatColor.RED + "/scoreboard players <set|add|remove|reset|list>");
return false;
}
if (args[1].equalsIgnoreCase("set") || args[1].equalsIgnoreCase("add") || args[1].equalsIgnoreCase("remove")) {
if (args.length != 5) {
if (args[1].equalsIgnoreCase("set")) {
sender.sendMessage(ChatColor.RED + "/scoreboard players set <player> <objective> <score>");
} else if (args[1].equalsIgnoreCase("add")) {
sender.sendMessage(ChatColor.RED + "/scoreboard players add <player> <objective> <count>");
} else {
sender.sendMessage(ChatColor.RED + "/scoreboard players remove <player> <objective> <count>");
}
return false;
}
String objectiveName = args[3];
Objective objective = mainScoreboard.getObjective(objectiveName);
if (objective == null) {
sender.sendMessage(ChatColor.RED + "No objective was found by the name '" + objectiveName + "'");
return false;
} else if (!objective.isModifiable()) {
sender.sendMessage(ChatColor.RED + "The objective '" + objectiveName + "' is read-only and cannot be set");
return false;
}
String valueString = args[4];
int value;
try {
value = Integer.parseInt(valueString);
} catch (NumberFormatException e) {
sender.sendMessage(ChatColor.RED + "'" + valueString + "' is not a valid number");
return false;
}
if (value < 1 && !args[1].equalsIgnoreCase("set")) {
sender.sendMessage(ChatColor.RED + "The number you have entered (" + value + ") is too small, it must be at least 1");
return false;
}
String playerName = args[2];
if (playerName.length() > 16) {
sender.sendMessage(ChatColor.RED + "'" + playerName + "' is too long for a player name");
return false;
}
Score score = objective.getScore(playerName);
int newScore;
if (args[1].equalsIgnoreCase("set")) {
newScore = value;
} else if (args[1].equalsIgnoreCase("add")) {
newScore = score.getScore() + value;
} else {
newScore = score.getScore() - value;
}
score.setScore(newScore);
sender.sendMessage("Set score of " + objectiveName + " for player " + playerName + " to " + newScore);
} else if (args[1].equalsIgnoreCase("reset")) {
if (args.length != 3) {
sender.sendMessage(ChatColor.RED + "/scoreboard players reset <player>");
return false;
}
String playerName = args[2];
if (playerName.length() > 16) {
sender.sendMessage(ChatColor.RED + "'" + playerName + "' is too long for a player name");
return false;
}
mainScoreboard.resetScores(playerName);
sender.sendMessage("Reset all scores of player " + playerName);
} else if (args[1].equalsIgnoreCase("list")) {
if (args.length > 3) {
sender.sendMessage(ChatColor.RED + "/scoreboard players list <player>");
return false;
}
if (args.length == 2) {
Set<String> entries = mainScoreboard.getEntries();
if (entries.isEmpty()) {
sender.sendMessage(ChatColor.RED + "There are no tracked players on the scoreboard");
} else {
sender.sendMessage(ChatColor.DARK_GREEN + "Showing " + entries.size() + " tracked players on the scoreboard");
sender.sendMessage(stringCollectionToString(entries));
}
} else {
String playerName = args[2];
if (playerName.length() > 16) {
sender.sendMessage(ChatColor.RED + "'" + playerName + "' is too long for a player name");
return false;
}
Set<Score> scores = mainScoreboard.getScores(playerName);
if (scores.isEmpty()) {
sender.sendMessage(ChatColor.RED + "Player " + playerName + " has no scores recorded");
} else {
sender.sendMessage(ChatColor.DARK_GREEN + "Showing " + scores.size() + " tracked objective(s) for " + playerName);
for (Score score : scores) {
sender.sendMessage("- " + score.getObjective().getDisplayName() + ": " + score.getScore() + " (" + score.getObjective().getName() + ")");
}
}
}
}
} else if (args[0].equalsIgnoreCase("teams")) {
if (args.length == 1) {
sender.sendMessage(ChatColor.RED + "/scoreboard teams <list|add|remove|empty|join|leave|option>");
return false;
}
if (args[1].equalsIgnoreCase("list")) {
if (args.length == 2) {
Set<Team> teams = mainScoreboard.getTeams();
if (teams.isEmpty()) {
sender.sendMessage(ChatColor.RED + "There are no teams registered on the scoreboard");
} else {
sender.sendMessage(ChatColor.DARK_GREEN + "Showing " + teams.size() + " teams on the scoreboard");
for (Team team : teams) {
sender.sendMessage("- " + team.getName() + ": '" + team.getDisplayName() + "' has " + team.getSize() + " players");
}
}
} else if (args.length == 3) {
String teamName = args[2];
Team team = mainScoreboard.getTeam(teamName);
if (team == null) {
sender.sendMessage(ChatColor.RED + "No team was found by the name '" + teamName + "'");
} else {
Set<OfflinePlayer> players = team.getPlayers();
if (players.isEmpty()) {
sender.sendMessage(ChatColor.RED + "Team " + team.getName() + " has no players");
} else {
sender.sendMessage(ChatColor.DARK_GREEN + "Showing " + players.size() + " player(s) in team " + team.getName());
sender.sendMessage(offlinePlayerSetToString(players));
}
}
} else {
sender.sendMessage(ChatColor.RED + "/scoreboard teams list [name]");
return false;
}
} else if (args[1].equalsIgnoreCase("add")) {
if (args.length < 3) {
sender.sendMessage(ChatColor.RED + "/scoreboard teams add <name> [display name ...]");
return false;
}
String name = args[2];
if (name.length() > 16) {
sender.sendMessage(ChatColor.RED + "The name '" + name + "' is too long for a team, it can be at most 16 characters long");
} else if (mainScoreboard.getTeam(name) != null) {
sender.sendMessage(ChatColor.RED + "A team with the name '" + name + "' already exists");
} else {
String displayName = null;
if (args.length > 3) {
displayName = StringUtils.join(ArrayUtils.subarray(args, 3, args.length), ' ');
if (displayName.length() > 32) {
sender.sendMessage(ChatColor.RED + "The display name '" + displayName + "' is too long for a team, it can be at most 32 characters long");
return false;
}
}
Team team = mainScoreboard.registerNewTeam(name);
if (displayName != null && displayName.length() > 0) {
team.setDisplayName(displayName);
}
sender.sendMessage("Added new team '" + team.getName() + "' successfully");
}
} else if (args[1].equalsIgnoreCase("remove")) {
if (args.length != 3) {
sender.sendMessage(ChatColor.RED + "/scoreboard teams remove <name>");
return false;
}
String name = args[2];
Team team = mainScoreboard.getTeam(name);
if (team == null) {
sender.sendMessage(ChatColor.RED + "No team was found by the name '" + name + "'");
} else {
team.unregister();
sender.sendMessage("Removed team " + name);
}
} else if (args[1].equalsIgnoreCase("empty")) {
if (args.length != 3) {
sender.sendMessage(ChatColor.RED + "/scoreboard teams clear <name>");
return false;
}
String name = args[2];
Team team = mainScoreboard.getTeam(name);
if (team == null) {
sender.sendMessage(ChatColor.RED + "No team was found by the name '" + name + "'");
} else {
Set<OfflinePlayer> players = team.getPlayers();
if (players.isEmpty()) {
sender.sendMessage(ChatColor.RED + "Team " + team.getName() + " is already empty, cannot remove nonexistant players");
} else {
for (OfflinePlayer player : players) {
team.removePlayer(player);
}
sender.sendMessage("Removed all " + players.size() + " player(s) from team " + team.getName());
}
}
} else if (args[1].equalsIgnoreCase("join")) {
if ((sender instanceof Player) ? args.length < 3 : args.length < 4) {
sender.sendMessage(ChatColor.RED + "/scoreboard teams join <team> [player...]");
return false;
}
String teamName = args[2];
Team team = mainScoreboard.getTeam(teamName);
if (team == null) {
sender.sendMessage(ChatColor.RED + "No team was found by the name '" + teamName + "'");
} else {
Set<String> addedPlayers = new HashSet<String>();
if ((sender instanceof Player) && args.length == 3) {
team.addPlayer((Player) sender);
addedPlayers.add(sender.getName());
} else {
for (int i = 3; i < args.length; i++) {
String playerName = args[i];
OfflinePlayer offlinePlayer;
Player player = Bukkit.getPlayerExact(playerName);
if (player != null) {
offlinePlayer = player;
} else {
offlinePlayer = Bukkit.getOfflinePlayer(playerName);
}
team.addPlayer(offlinePlayer);
addedPlayers.add(offlinePlayer.getName());
}
}
sender.sendMessage("Added " + addedPlayers.size() + " player(s) to team " + team.getName() + ": " + stringCollectionToString(addedPlayers));
}
} else if (args[1].equalsIgnoreCase("leave")) {
if (!(sender instanceof Player) && args.length < 3) {
sender.sendMessage(ChatColor.RED + "/scoreboard teams leave [player...]");
return false;
}
Set<String> left = new HashSet<String>();
Set<String> noTeam = new HashSet<String>();
if ((sender instanceof Player) && args.length == 2) {
Team team = mainScoreboard.getPlayerTeam((Player) sender);
if (team != null) {
team.removePlayer((Player) sender);
left.add(sender.getName());
} else {
noTeam.add(sender.getName());
}
} else {
for (int i = 2; i < args.length; i++) {
String playerName = args[i];
OfflinePlayer offlinePlayer;
Player player = Bukkit.getPlayerExact(playerName);
if (player != null) {
offlinePlayer = player;
} else {
offlinePlayer = Bukkit.getOfflinePlayer(playerName);
}
Team team = mainScoreboard.getPlayerTeam(offlinePlayer);
if (team != null) {
team.removePlayer(offlinePlayer);
left.add(offlinePlayer.getName());
} else {
noTeam.add(offlinePlayer.getName());
}
}
}
if (!left.isEmpty()) {
sender.sendMessage("Removed " + left.size() + " player(s) from their teams: " + stringCollectionToString(left));
}
if (!noTeam.isEmpty()) {
sender.sendMessage("Could not remove " + noTeam.size() + " player(s) from their teams: " + stringCollectionToString(noTeam));
}
} else if (args[1].equalsIgnoreCase("option")) {
if (args.length != 4 && args.length != 5) {
sender.sendMessage(ChatColor.RED + "/scoreboard teams option <team> <friendlyfire|color|seefriendlyinvisibles> <value>");
return false;
}
String teamName = args[2];
Team team = mainScoreboard.getTeam(teamName);
if (team == null) {
sender.sendMessage(ChatColor.RED + "No team was found by the name '" + teamName + "'");
return false;
}
String option = args[3].toLowerCase();
if (!option.equals("friendlyfire") && !option.equals("color") && !option.equals("seefriendlyinvisibles")) {
sender.sendMessage(ChatColor.RED + "/scoreboard teams option <team> <friendlyfire|color|seefriendlyinvisibles> <value>");
return false;
}
if (args.length == 4) {
if (option.equals("color")) {
sender.sendMessage(ChatColor.RED + "Valid values for option color are: " + stringCollectionToString(TEAMS_OPTION_COLOR.keySet()));
} else {
sender.sendMessage(ChatColor.RED + "Valid values for option " + option + " are: true and false");
}
} else {
String value = args[4].toLowerCase();
if (option.equals("color")) {
ChatColor color = TEAMS_OPTION_COLOR.get(value);
if (color == null) {
sender.sendMessage(ChatColor.RED + "Valid values for option color are: " + stringCollectionToString(TEAMS_OPTION_COLOR.keySet()));
return false;
}
team.setPrefix(color.toString());
team.setSuffix(ChatColor.RESET.toString());
} else {
if (!value.equals("true") && !value.equals("false")) {
sender.sendMessage(ChatColor.RED + "Valid values for option " + option + " are: true and false");
return false;
}
if (option.equals("friendlyfire")) {
team.setAllowFriendlyFire(value.equals("true"));
} else {
team.setCanSeeFriendlyInvisibles(value.equals("true"));
}
}
sender.sendMessage("Set option " + option + " for team " + team.getName() + " to " + value);
}
}
} else {
sender.sendMessage(ChatColor.RED + "Usage: /scoreboard <objectives|players|teams>");
return false;
}
return true;
}
Aggregations