Skip to content

Commit

Permalink
server.scoreboards tag
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Aug 30, 2021
1 parent 870d485 commit 7bd0a51
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 31 deletions.
Expand Up @@ -114,9 +114,7 @@ private enum Action {ADD, REMOVE}

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

for (Argument arg : scriptEntry.getProcessedArgs()) {

if (!scriptEntry.hasObject("action")
&& arg.matchesEnum(Action.values())) {
scriptEntry.addObject("action", arg.asElement());
Expand Down Expand Up @@ -158,7 +156,6 @@ else if (!scriptEntry.hasObject("viewers")
arg.reportUnhandled();
}
}

scriptEntry.defaultObject("action", new ElementTag("add"));
scriptEntry.defaultObject("id", new ElementTag("main"));
}
Expand All @@ -175,12 +172,8 @@ public static OfflinePlayer getOfflinePlayer(String name) {
@SuppressWarnings("unchecked")
@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();

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");
Expand All @@ -198,21 +191,9 @@ public void execute(final ScriptEntry scriptEntry) {
displaySlot = new ElementTag("sidebar");
}
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), action.debug() +
id.debug() +
(viewers != null ? ArgumentHelper.debugObj("viewers", viewers.toString()) : "") +
(objective != null ? objective.debug() : "") +
(!lines.isEmpty() ? lines.debug() : "") +
(!act.equals(Action.ADD) ? "" :
(score == null ? "" : score.debug())
+ (objective == null ? "" : (
displaySlot.debug()
+ criteria.debug()
+ (displayName == null ? "" : displayName.debug())
))));
Debug.report(scriptEntry, getName(), action, id, viewers != null ? ArgumentHelper.debugObj("viewers", viewers.toString()) : "", objective, lines, score, objective, displaySlot, criteria, displayName);
}
Scoreboard board = null;

// Get the main scoreboard by default
if (id.asString().equalsIgnoreCase("main")) {
board = ScoreboardHelper.getMain();
Expand All @@ -230,22 +211,17 @@ else 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.getResidingQueue(), "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());
Expand All @@ -256,7 +232,6 @@ else 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()));
Expand All @@ -267,7 +242,6 @@ else if (hadCriteria && !obj.getCriteria().equals(criteria.asString())) {
else if (!existedAlready) {
obj.setDisplayName(objective.asString());
}

if (!lines.isEmpty()) {
// If we've gotten this far, but the score is null,
// use a score of 0
Expand Down Expand Up @@ -299,7 +273,6 @@ 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()) {
Expand Down Expand Up @@ -338,7 +311,6 @@ else if (viewers == null) {
ScoreboardHelper.deleteScoreboard(id.asString());
}
}

if (viewers != null) {
for (PlayerTag viewer : viewers) {
// Add viewers for this scoreboard
Expand All @@ -348,7 +320,6 @@ else if (viewers == null) {
if (!id.asString().equalsIgnoreCase("main")) {
ScoreboardHelper.viewerMap.put(viewer.getName(), id.asString());
}

// Make this player view the scoreboard if he/she
// is already online
if (viewer.isOnline()) {
Expand Down
Expand Up @@ -310,6 +310,21 @@ else if (recipe instanceof CookingRecipe<?>) {
return;
}

// <--[tag]
// @attribute <server.scoreboards>
// @returns ListTag
// @description
// Returns a list of scoreboard IDs currently registered on the server.
// -->
if (attribute.startsWith("scoreboards") && attribute.hasContext(1)) {
ListTag result = new ListTag();
for (String board : ScoreboardHelper.scoreboardMap.keySet()) {
result.addObject(new ElementTag(board));
}
event.setReplacedObject(result.getObjectAttribute(attribute.fulfill(1)));
return;
}

if (attribute.startsWith("scoreboard")) {
Scoreboard board;
String name = "main";
Expand Down

0 comments on commit 7bd0a51

Please sign in to comment.