From a7b32154e23a0f1c860b5d388a3c786bea1fbdde Mon Sep 17 00:00:00 2001
From: david <89749147+davight@users.noreply.github.com>
Date: Wed, 24 Sep 2025 13:59:39 +0200
Subject: [PATCH 1/6] initialwork
---
.../commands/player/SidebarCommand.java | 148 +++++++++++++++++-
1 file changed, 146 insertions(+), 2 deletions(-)
diff --git a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/SidebarCommand.java b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/SidebarCommand.java
index 456750948c..181e05845c 100644
--- a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/SidebarCommand.java
+++ b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/SidebarCommand.java
@@ -2,6 +2,7 @@
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.utilities.Utilities;
+import com.denizenscript.denizencore.scripts.commands.generator.*;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.abstracts.Sidebar;
@@ -29,9 +30,10 @@ public SidebarCommand() {
setName("sidebar");
setSyntax("sidebar (add/remove/{set}/set_line) (title:
) (scores:<#>|...) (values:|...) (start:<#>/{num_of_lines}) (increment:<#>/{-1}) (players:|...) (per_player)");
setRequiredArguments(1, 8);
- setParseArgs(false);
+ setParseArgs(true);
Denizen.getInstance().getServer().getPluginManager().registerEvents(new SidebarEvents(), Denizen.getInstance());
isProcedural = false;
+ autoCompile();
}
// <--[command]
@@ -101,7 +103,149 @@ public SidebarCommand() {
// TODO: Clean me!
- private enum Action {ADD, REMOVE, SET, SET_LINE}
+ public enum Action { ADD, REMOVE, SET, SET_LINE }
+
+ public static void autoExecute(ScriptEntry scriptEntry,
+ @ArgName("action") @ArgDefaultText("set") Action action,
+ @ArgName("title") @ArgPrefixed @ArgDefaultNull String title, // String due to unparsed value?
+ @ArgName("scores") @ArgPrefixed @ArgDefaultNull String scores, // done
+ @ArgName("values") @ArgPrefixed @ArgDefaultNull String values, // done
+ @ArgName("start") @ArgPrefixed @ArgDefaultNull String start, //
+ @ArgName("increment") @ArgPrefixed @ArgDefaultText("-1") String increment,
+ @ArgName("players") @ArgPrefixed @ArgDefaultNull @ArgSubType(PlayerTag.class) List players,
+ @ArgName("per_player") boolean perPlayer) {
+
+ ElementTag parsedTitle = (perPlayer || title == null) ? null : new ElementTag(TagManager.tag(title, scriptEntry.getContext()));
+ ListTag parsedValues = (perPlayer || values == null) ? null : ListTag.valueOf(TagManager.tag(values, scriptEntry.getContext()), scriptEntry.getContext());
+ ListTag parsedScores = (perPlayer || scores == null) ? null : ListTag.valueOf(TagManager.tag(scores, scriptEntry.getContext()), scriptEntry.getContext());
+ ElementTag parsedStart = (perPlayer || start == null) ? null : new ElementTag(TagManager.tag(start, scriptEntry.getContext()));
+ ElementTag parsedIncrement = (perPlayer) ? null : new ElementTag(TagManager.tag(increment, scriptEntry.getContext()));
+
+ Map sidebarData = new HashMap<>();
+ PlayerSidebarData parsedData = new PlayerSidebarData(parsedTitle, parsedScores, parsedValues, parsedStart, parsedIncrement);
+ for (PlayerTag player : players) {
+ if (player == null || !player.isValid()) {
+ Debug.echoError("Invalid player!");
+ continue;
+ }
+ if (perPlayer) {
+ sidebarData.put(player, parsedData);
+ }
+ else {
+ sidebarData.put(player, new PlayerSidebarData(new BukkitTagContext(player, Utilities.getEntryNPC(scriptEntry), scriptEntry, scriptEntry.shouldDebug(), scriptEntry.getScript()), title, scores, values, start, increment));
+ }
+ }
+ switch (action) {
+ case ADD -> {
+ for (Map.Entry entry : sidebarData.entrySet()) {
+ Sidebar sidebar = createSidebar(entry.getKey());
+ if (sidebar == null) {
+ continue;
+ }
+ List current = sidebar.getLines();
+ // todo use PSD
+ try {
+ int index = entry.getValue().getStart().asInt();
+ //int index = start != null ? start.asInt() : (!current.isEmpty() ? current.get(current.size() - 1).score : entry.getValue().getValues().size());
+ int incr = entry.getValue().getIncrement().asInt();
+ for (int i = 0; i < entry.getValue().getValues().size(); i++, index += incr) {
+ int score = (entry.getValue().getScores() != null && i < entry.getValue().getScores().size()) ? Integer.parseInt(entry.getValue().getScores().get(i)) : index;
+ while (hasScoreAlready(current, score)) {
+ score += (incr == 0 ? 1 : incr);
+ }
+ current.add(new Sidebar.SidebarLine(entry.getValue().getValues().get(i), score));
+ }
+ } catch (NumberFormatException e) {
+ Debug.echoError(e);
+ continue;
+ }
+ sidebar.setLines(current);
+ sidebar.sendUpdate();
+ }
+ }
+ case REMOVE -> {
+ for (Map.Entry entry : sidebarData.entrySet()) {
+ Sidebar sidebar = createSidebar(entry.getKey());
+ if (sidebar == null) {
+ continue;
+ }
+ List current = sidebar.getLines();
+ if (entry.getValue().getScores() != null) {
+ for (String scoreString : entry.getValue().getScores()) {
+ int score = Integer.parseInt(scoreString);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ public static class PlayerSidebarData {
+ BukkitTagContext context;
+
+ String rawTitle = null;
+ String rawScores = null;
+ String rawValues = null;
+ String rawStart = null;
+ String rawIncrement = null;
+
+ ElementTag parsedTitle = null;
+ ListTag parsedScores = null;
+ ListTag parsedValues = null;
+ ElementTag parsedStart = null;
+ ElementTag parsedIncrement = null;
+
+ PlayerSidebarData(BukkitTagContext context, String rawTitle, String rawScores, String rawValues, String rawStart, String rawIncrement) {
+ this.context = context;
+ this.rawTitle = rawTitle;
+ this.rawScores = rawScores;
+ this.rawValues = rawValues;
+ this.rawStart = rawStart;
+ this.rawIncrement = rawIncrement;
+ }
+
+ PlayerSidebarData(ElementTag title, ListTag scores, ListTag values, ElementTag start, ElementTag increment) {
+ this.parsedTitle = title;
+ this.parsedScores = scores;
+ this.parsedValues = values;
+ this.parsedStart = start;
+ this.parsedIncrement = increment;
+ }
+
+ public ElementTag getTitle() {
+ if (parsedTitle == null) {
+ parsedTitle = new ElementTag(TagManager.tag(rawTitle, context));
+ }
+ return parsedTitle;
+ }
+
+ public ListTag getScores() {
+ if (parsedScores == null) {
+ parsedScores = ListTag.getListFor(TagManager.tagObject(rawScores, context), context);
+ }
+ return parsedScores;
+ }
+
+ public ListTag getValues() {
+ if (parsedValues == null) {
+ parsedValues = ListTag.getListFor(TagManager.tagObject(rawValues, context), context);
+ }
+ return parsedValues;
+ }
+
+ public ElementTag getIncrement() {
+ if (parsedIncrement == null) {
+ parsedIncrement = new ElementTag(TagManager.tag(rawIncrement, context));
+ }
+ return parsedIncrement;
+ }
+ public ElementTag getStart() {
+ if (parsedStart == null) {
+ parsedStart = new ElementTag(TagManager.tag(rawStart, context));
+ }
+ return parsedStart;
+ }
+ }
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
From 9257df7a83c58ea64f378a6ecf32f3d10f580c51 Mon Sep 17 00:00:00 2001
From: david <89749147+davight@users.noreply.github.com>
Date: Wed, 19 Nov 2025 10:58:02 +0100
Subject: [PATCH 2/6] fixes, work
---
.../commands/player/SidebarCommand.java | 525 +++++-------------
1 file changed, 148 insertions(+), 377 deletions(-)
diff --git a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/SidebarCommand.java b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/SidebarCommand.java
index 181e05845c..97d7c1b1dc 100644
--- a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/SidebarCommand.java
+++ b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/SidebarCommand.java
@@ -8,14 +8,11 @@
import com.denizenscript.denizen.nms.abstracts.Sidebar;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.tags.BukkitTagContext;
-import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
-import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
-import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.tags.TagManager;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@@ -30,7 +27,6 @@ public SidebarCommand() {
setName("sidebar");
setSyntax("sidebar (add/remove/{set}/set_line) (title:) (scores:<#>|...) (values:|...) (start:<#>/{num_of_lines}) (increment:<#>/{-1}) (players:|...) (per_player)");
setRequiredArguments(1, 8);
- setParseArgs(true);
Denizen.getInstance().getServer().getPluginManager().registerEvents(new SidebarEvents(), Denizen.getInstance());
isProcedural = false;
autoCompile();
@@ -101,59 +97,66 @@ public SidebarCommand() {
// - sidebar remove
// -->
- // TODO: Clean me!
-
public enum Action { ADD, REMOVE, SET, SET_LINE }
public static void autoExecute(ScriptEntry scriptEntry,
@ArgName("action") @ArgDefaultText("set") Action action,
- @ArgName("title") @ArgPrefixed @ArgDefaultNull String title, // String due to unparsed value?
- @ArgName("scores") @ArgPrefixed @ArgDefaultNull String scores, // done
- @ArgName("values") @ArgPrefixed @ArgDefaultNull String values, // done
- @ArgName("start") @ArgPrefixed @ArgDefaultNull String start, //
- @ArgName("increment") @ArgPrefixed @ArgDefaultText("-1") String increment,
+ @ArgName("title") @ArgPrefixed @ArgUnparsed @ArgDefaultNull String title,
+ @ArgName("scores") @ArgPrefixed @ArgUnparsed @ArgDefaultNull String scores,
+ @ArgName("values") @ArgPrefixed @ArgUnparsed @ArgDefaultNull String values,
+ @ArgName("start") @ArgPrefixed @ArgUnparsed @ArgDefaultNull String start,
+ @ArgName("increment") @ArgPrefixed @ArgUnparsed @ArgDefaultText("-1") String increment,
@ArgName("players") @ArgPrefixed @ArgDefaultNull @ArgSubType(PlayerTag.class) List players,
@ArgName("per_player") boolean perPlayer) {
-
+ if (action == Action.ADD && values == null) {
+ Debug.echoError("Missing 'values' parameter!");
+ return;
+ }
+ if (action == Action.SET && values == null && title == null) {
+ Debug.echoError("Must specify at least one of: value(s), title, increment, or start for that action!");
+ return;
+ }
+ if (action == Action.SET && scores == null && values == null) {
+ Debug.echoError("Must specify value(s) when setting scores!");
+ return;
+ }
+ if (players == null) {
+ players = Utilities.entryHasPlayer(scriptEntry) ? Collections.singletonList(Utilities.getEntryPlayer(scriptEntry)) : Collections.emptyList();
+ }
ElementTag parsedTitle = (perPlayer || title == null) ? null : new ElementTag(TagManager.tag(title, scriptEntry.getContext()));
ListTag parsedValues = (perPlayer || values == null) ? null : ListTag.valueOf(TagManager.tag(values, scriptEntry.getContext()), scriptEntry.getContext());
ListTag parsedScores = (perPlayer || scores == null) ? null : ListTag.valueOf(TagManager.tag(scores, scriptEntry.getContext()), scriptEntry.getContext());
ElementTag parsedStart = (perPlayer || start == null) ? null : new ElementTag(TagManager.tag(start, scriptEntry.getContext()));
- ElementTag parsedIncrement = (perPlayer) ? null : new ElementTag(TagManager.tag(increment, scriptEntry.getContext()));
-
- Map sidebarData = new HashMap<>();
- PlayerSidebarData parsedData = new PlayerSidebarData(parsedTitle, parsedScores, parsedValues, parsedStart, parsedIncrement);
+ ElementTag parsedIncrement = perPlayer ? null : new ElementTag(TagManager.tag(increment, scriptEntry.getContext()));
+ Map sidebarData = new HashMap<>(players.size());
for (PlayerTag player : players) {
if (player == null || !player.isValid()) {
Debug.echoError("Invalid player!");
continue;
}
- if (perPlayer) {
- sidebarData.put(player, parsedData);
- }
- else {
- sidebarData.put(player, new PlayerSidebarData(new BukkitTagContext(player, Utilities.getEntryNPC(scriptEntry), scriptEntry, scriptEntry.shouldDebug(), scriptEntry.getScript()), title, scores, values, start, increment));
+ Sidebar sidebar = createSidebar(player);
+ if (sidebar == null) {
+ continue;
}
+ sidebarData.put(player, perPlayer ?
+ new PlayerSidebarData(sidebar, new BukkitTagContext(player, Utilities.getEntryNPC(scriptEntry), scriptEntry, scriptEntry.shouldDebug(), scriptEntry.getScript()), title, scores, values, start, increment) :
+ new PlayerSidebarData(sidebar, parsedTitle, parsedScores, parsedValues, parsedStart, parsedIncrement));
}
switch (action) {
case ADD -> {
for (Map.Entry entry : sidebarData.entrySet()) {
- Sidebar sidebar = createSidebar(entry.getKey());
- if (sidebar == null) {
- continue;
- }
+ Sidebar sidebar = entry.getValue().sidebar;
List current = sidebar.getLines();
- // todo use PSD
+ PlayerSidebarData data = entry.getValue();
try {
- int index = entry.getValue().getStart().asInt();
- //int index = start != null ? start.asInt() : (!current.isEmpty() ? current.get(current.size() - 1).score : entry.getValue().getValues().size());
- int incr = entry.getValue().getIncrement().asInt();
- for (int i = 0; i < entry.getValue().getValues().size(); i++, index += incr) {
- int score = (entry.getValue().getScores() != null && i < entry.getValue().getScores().size()) ? Integer.parseInt(entry.getValue().getScores().get(i)) : index;
+ int index = data.getStart() != null ? data.getStart().asInt() : (!current.isEmpty() ? current.get(current.size() - 1).score : data.getValues().size());
+ int incr = data.getIncrement().asInt();
+ for (int i = 0; i < data.getValues().size(); i++, index += incr) {
+ int score = (data.getScores() != null && i < data.getScores().size()) ? Integer.parseInt(data.getScores().get(i)) : index;
while (hasScoreAlready(current, score)) {
score += (incr == 0 ? 1 : incr);
}
- current.add(new Sidebar.SidebarLine(entry.getValue().getValues().get(i), score));
+ current.add(new Sidebar.SidebarLine(data.getValues().get(i), score));
}
} catch (NumberFormatException e) {
Debug.echoError(e);
@@ -165,16 +168,111 @@ public static void autoExecute(ScriptEntry scriptEntry,
}
case REMOVE -> {
for (Map.Entry entry : sidebarData.entrySet()) {
- Sidebar sidebar = createSidebar(entry.getKey());
- if (sidebar == null) {
- continue;
+ Sidebar sidebar = entry.getValue().sidebar;
+ boolean removedAny = false;
+ List current = sidebar.getLines();
+ PlayerSidebarData data = entry.getValue();
+ if (data.getScores() != null) {
+ try {
+ for (String scoreString : data.getScores()) {
+ int score = Integer.parseInt(scoreString);
+ for (int i = 0; i < current.size(); i++) {
+ if (current.get(i).score == score) {
+ current.remove(i--);
+ }
+ }
+ }
+ }
+ catch (NumberFormatException e) {
+ Debug.echoError(e);
+ continue;
+ }
+ sidebar.setLines(current);
+ sidebar.sendUpdate();
+ removedAny = true;
}
+ if (data.getValues() != null) {
+ for (String line : data.getValues()) {
+ for (int i = 0; i < current.size(); i++) {
+ if (current.get(i).text.equalsIgnoreCase(line)) {
+ current.remove(i--);
+ }
+ }
+ }
+ sidebar.setLines(current);
+ sidebar.sendUpdate();
+ removedAny = true;
+ }
+ if (!removedAny) {
+ sidebar.remove();
+ sidebars.remove(entry.getKey().getPlayerEntity().getUniqueId());
+ }
+ }
+ }
+ case SET_LINE -> {
+ for (Map.Entry entry : sidebarData.entrySet()) {
+ PlayerSidebarData data = entry.getValue();
+ if (data.getScores() == null || data.getScores().isEmpty()) {
+ Debug.echoError("Missing or invalid 'scores' parameter!");
+ return;
+ }
+ if (data.getValues() == null || data.getValues().size() != data.getScores().size()) {
+ Debug.echoError("Missing or invalid 'values' parameter!");
+ return;
+ }
+ Sidebar sidebar = entry.getValue().sidebar;
List current = sidebar.getLines();
- if (entry.getValue().getScores() != null) {
- for (String scoreString : entry.getValue().getScores()) {
- int score = Integer.parseInt(scoreString);
+ try {
+ for (int i = 0; i < data.getValues().size(); i++) {
+ if (!ArgumentHelper.matchesInteger(data.getScores().get(i))) {
+ Debug.echoError("Sidebar command scores input contains not-a-valid-number: " + data.getScores().get(i));
+ return;
+ }
+ int score = Integer.parseInt(data.getScores().get(i));
+ if (hasScoreAlready(current, score)) {
+ for (Sidebar.SidebarLine line : current) {
+ if (line.score == score) {
+ line.text = data.getValues().get(i);
+ break;
+ }
+ }
+ }
+ else {
+ current.add(new Sidebar.SidebarLine(data.getValues().get(i), score));
+ }
+ }
+ } catch (NumberFormatException e) {
+ Debug.echoError(e);
+ continue;
+ }
+ sidebar.setLines(current);
+ sidebar.sendUpdate();
+ }
+ }
+ case SET -> {
+ for (Map.Entry entry : sidebarData.entrySet()) {
+ Sidebar sidebar = entry.getValue().sidebar;
+ List current = new ArrayList<>();
+ PlayerSidebarData data = entry.getValue();
+ if (data.getValues() != null) {
+ try {
+ int index = data.getStart() != null ? data.getStart().asInt() : data.getValues().size();
+ int incr = data.getIncrement() != null ? data.getIncrement().asInt() : -1;
+ for (int i = 0; i < data.getValues().size(); i++, index += incr) {
+ int score = (data.getScores() != null && i < data.getScores().size()) ? Integer.parseInt(data.getScores().get(i)) : index;
+ current.add(new Sidebar.SidebarLine(data.getValues().get(i), score));
+ }
}
+ catch (NumberFormatException e) {
+ Debug.echoError(e);
+ continue;
+ }
+ sidebar.setLines(current);
}
+ if (data.getTitle() != null) {
+ sidebar.setTitle(data.getTitle().asString());
+ }
+ sidebar.sendUpdate();
}
}
}
@@ -182,12 +280,9 @@ public static void autoExecute(ScriptEntry scriptEntry,
public static class PlayerSidebarData {
BukkitTagContext context;
+ Sidebar sidebar;
- String rawTitle = null;
- String rawScores = null;
- String rawValues = null;
- String rawStart = null;
- String rawIncrement = null;
+ String rawTitle = null, rawScores = null, rawValues = null, rawStart = null, rawIncrement = null;
ElementTag parsedTitle = null;
ListTag parsedScores = null;
@@ -195,7 +290,8 @@ public static class PlayerSidebarData {
ElementTag parsedStart = null;
ElementTag parsedIncrement = null;
- PlayerSidebarData(BukkitTagContext context, String rawTitle, String rawScores, String rawValues, String rawStart, String rawIncrement) {
+ PlayerSidebarData(Sidebar sidebar, BukkitTagContext context, String rawTitle, String rawScores, String rawValues, String rawStart, String rawIncrement) {
+ this.sidebar = sidebar;
this.context = context;
this.rawTitle = rawTitle;
this.rawScores = rawScores;
@@ -204,7 +300,8 @@ public static class PlayerSidebarData {
this.rawIncrement = rawIncrement;
}
- PlayerSidebarData(ElementTag title, ListTag scores, ListTag values, ElementTag start, ElementTag increment) {
+ PlayerSidebarData(Sidebar sidebar, ElementTag title, ListTag scores, ListTag values, ElementTag start, ElementTag increment) {
+ this.sidebar = sidebar;
this.parsedTitle = title;
this.parsedScores = scores;
this.parsedValues = values;
@@ -214,94 +311,40 @@ public static class PlayerSidebarData {
public ElementTag getTitle() {
if (parsedTitle == null) {
- parsedTitle = new ElementTag(TagManager.tag(rawTitle, context));
+ parsedTitle = rawTitle == null ? null : new ElementTag(TagManager.tag(rawTitle, context));
}
return parsedTitle;
}
public ListTag getScores() {
if (parsedScores == null) {
- parsedScores = ListTag.getListFor(TagManager.tagObject(rawScores, context), context);
+ parsedScores = rawScores == null ? null : ListTag.getListFor(TagManager.tagObject(rawScores, context), context);
}
return parsedScores;
}
public ListTag getValues() {
if (parsedValues == null) {
- parsedValues = ListTag.getListFor(TagManager.tagObject(rawValues, context), context);
+ parsedValues = rawValues == null ? null : ListTag.getListFor(TagManager.tagObject(rawValues, context), context);
}
return parsedValues;
}
public ElementTag getIncrement() {
if (parsedIncrement == null) {
- parsedIncrement = new ElementTag(TagManager.tag(rawIncrement, context));
+ parsedIncrement = rawIncrement == null ? null : new ElementTag(TagManager.tag(rawIncrement, context));
}
return parsedIncrement;
}
+
public ElementTag getStart() {
if (parsedStart == null) {
- parsedStart = new ElementTag(TagManager.tag(rawStart, context));
+ parsedStart = rawStart == null ? null : new ElementTag(TagManager.tag(rawStart, context));
}
return parsedStart;
}
}
- @Override
- public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
- Action action = Action.SET;
- for (Argument arg : ArgumentHelper.interpret(scriptEntry, scriptEntry.getOriginalArguments())) {
- if (!scriptEntry.hasObject("action")
- && arg.matchesEnum(Action.class)) {
- action = Action.valueOf(arg.getValue().toUpperCase());
- }
- else if (!scriptEntry.hasObject("title")
- && arg.matchesPrefix("title", "t", "objective", "obj", "o")) {
- scriptEntry.addObject("title", arg.asElement());
- }
- else if (!scriptEntry.hasObject("scores")
- && arg.matchesPrefix("scores", "score", "lines", "line", "l")) {
- scriptEntry.addObject("scores", arg.asElement());
- }
- else if (!scriptEntry.hasObject("value")
- && arg.matchesPrefix("value", "values", "val", "v")) {
- scriptEntry.addObject("value", arg.asElement());
- }
- else if (!scriptEntry.hasObject("increment")
- && arg.matchesPrefix("increment", "inc", "i")) {
- scriptEntry.addObject("increment", arg.asElement());
- }
- else if (!scriptEntry.hasObject("start")
- && arg.matchesPrefix("start", "s")) {
- scriptEntry.addObject("start", arg.asElement());
- }
- else if (!scriptEntry.hasObject("players")
- && arg.matchesPrefix("players", "player", "p")) {
- scriptEntry.addObject("players", arg.asElement());
- }
- else if (!scriptEntry.hasObject("per_player")
- && arg.matches("per_player")) {
- scriptEntry.addObject("per_player", new ElementTag(true));
- }
- else {
- arg.reportUnhandled();
- }
- }
- if (action == Action.ADD && !scriptEntry.hasObject("value")) {
- throw new InvalidArgumentsException("Must specify value(s) for that action!");
- }
- if (action == Action.SET && !scriptEntry.hasObject("value") && !scriptEntry.hasObject("title")
- && !scriptEntry.hasObject("increment") && !scriptEntry.hasObject("start")) {
- throw new InvalidArgumentsException("Must specify at least one of: value(s), title, increment, or start for that action!");
- }
- if (action == Action.SET && scriptEntry.hasObject("scores") && !scriptEntry.hasObject("value")) {
- throw new InvalidArgumentsException("Must specify value(s) when setting scores!");
- }
- scriptEntry.addObject("action", new ElementTag(action));
- scriptEntry.defaultObject("per_player", new ElementTag(false));
- scriptEntry.defaultObject("players", new ElementTag(Utilities.entryHasPlayer(scriptEntry) ? Utilities.getEntryPlayer(scriptEntry).identify() : "li@"));
- }
-
public static boolean hasScoreAlready(List lines, int score) {
for (Sidebar.SidebarLine line : lines) {
if (line.score == score) {
@@ -311,278 +354,6 @@ public static boolean hasScoreAlready(List lines, int score
return false;
}
- @Override
- public void execute(ScriptEntry scriptEntry) {
- ElementTag action = scriptEntry.getElement("action");
- ElementTag elTitle = scriptEntry.getElement("title");
- ElementTag elScores = scriptEntry.getElement("scores");
- ElementTag elValue = scriptEntry.getElement("value");
- ElementTag elIncrement = scriptEntry.getElement("increment");
- ElementTag elStart = scriptEntry.getElement("start");
- ElementTag elPlayers = scriptEntry.getElement("players");
- ElementTag elPerPlayer = scriptEntry.getElement("per_player");
- ListTag players = ListTag.valueOf(TagManager.tag(elPlayers.asString(), scriptEntry.getContext()), scriptEntry.getContext());
- boolean per_player = elPerPlayer.asBoolean();
- String perTitle = null;
- String perScores = null;
- String perValue = null;
- String perIncrement = null;
- String perStart = null;
- ElementTag title = null;
- ListTag scores = null;
- ListTag value = null;
- ElementTag increment = null;
- ElementTag start = null;
- if (per_player) {
- if (elTitle != null) {
- perTitle = elTitle.asString();
- }
- if (elScores != null) {
- perScores = elScores.asString();
- }
- if (elValue != null) {
- perValue = elValue.asString();
- }
- if (elIncrement != null) {
- perIncrement = elIncrement.asString();
- }
- if (elStart != null) {
- perStart = elStart.asString();
- }
- if (scriptEntry.dbCallShouldDebug()) {
- Debug.report(scriptEntry, getName(), action, elTitle, elScores, elValue, elIncrement, elStart, db("players", players));
- }
- }
- else {
- BukkitTagContext context = (BukkitTagContext) scriptEntry.getContext();
- if (elTitle != null) {
- title = new ElementTag(TagManager.tag(elTitle.asString(), context));
- }
- if (elScores != null) {
- scores = ListTag.getListFor(TagManager.tagObject(elScores.asString(), context), context);
- }
- if (elValue != null) {
- value = ListTag.getListFor(TagManager.tagObject(elValue.asString(), context), context);
- }
- if (elIncrement != null) {
- increment = new ElementTag(TagManager.tag(elIncrement.asString(), context));
- }
- if (elStart != null) {
- start = new ElementTag(TagManager.tag(elStart.asString(), context));
- }
- if (scriptEntry.dbCallShouldDebug()) {
- Debug.report(scriptEntry, getName(), action, title, scores, value, increment, start, db("players", players));
- }
- }
- switch (Action.valueOf(action.asString())) {
- case ADD:
- for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
- if (player == null || !player.isValid()) {
- Debug.echoError("Invalid player!");
- continue;
- }
- Sidebar sidebar = createSidebar(player);
- if (sidebar == null) {
- continue;
- }
- List current = sidebar.getLines();
- if (per_player) {
- TagContext context = new BukkitTagContext(player, Utilities.getEntryNPC(scriptEntry),
- scriptEntry, scriptEntry.shouldDebug(), scriptEntry.getScript());
- value = ListTag.getListFor(TagManager.tagObject(perValue, context), context);
- if (perScores != null) {
- scores = ListTag.getListFor(TagManager.tagObject(perScores, context), context);
- }
- }
- try {
- int index = start != null ? start.asInt() : (current.size() > 0 ? current.get(current.size() - 1).score : value.size());
- int incr = increment != null ? increment.asInt() : -1;
- for (int i = 0; i < value.size(); i++, index += incr) {
- int score = (scores != null && i < scores.size()) ? Integer.parseInt(scores.get(i)) : index;
- while (hasScoreAlready(current, score)) {
- score += (incr == 0 ? 1 : incr);
- }
- current.add(new Sidebar.SidebarLine(value.get(i), score));
- }
- }
- catch (Exception e) {
- Debug.echoError(e);
- continue;
- }
- sidebar.setLines(current);
- sidebar.sendUpdate();
- }
- break;
- case REMOVE:
- for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
- if (player == null || !player.isValid()) {
- Debug.echoError("Invalid player!");
- continue;
- }
- Sidebar sidebar = createSidebar(player);
- if (sidebar == null) {
- continue;
- }
- List current = sidebar.getLines();
- if (per_player) {
- TagContext context = new BukkitTagContext(player, Utilities.getEntryNPC(scriptEntry),
- scriptEntry, scriptEntry.shouldDebug(), scriptEntry.getScript());
- if (perValue != null) {
- value = ListTag.getListFor(TagManager.tagObject(perValue, context), context);
- }
- if (perScores != null) {
- scores = ListTag.getListFor(TagManager.tagObject(perScores, context), context);
- }
- }
- boolean removedAny = false;
- if (scores != null) {
- try {
- for (String scoreString : scores) {
- int score = Integer.parseInt(scoreString);
- for (int i = 0; i < current.size(); i++) {
- if (current.get(i).score == score) {
- current.remove(i--);
- }
- }
- }
- }
- catch (Exception e) {
- Debug.echoError(e);
- continue;
- }
- sidebar.setLines(current);
- sidebar.sendUpdate();
- removedAny = true;
- }
- if (value != null) {
- for (String line : value) {
- for (int i = 0; i < current.size(); i++) {
- if (current.get(i).text.equalsIgnoreCase(line)) {
- current.remove(i--);
- }
- }
- }
- sidebar.setLines(current);
- sidebar.sendUpdate();
- removedAny = true;
- }
- if (!removedAny) {
- sidebar.remove();
- sidebars.remove(player.getPlayerEntity().getUniqueId());
- }
- }
- break;
- case SET_LINE:
- for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
- if (player == null || !player.isValid()) {
- Debug.echoError("Invalid player!");
- continue;
- }
- if ((scores == null || scores.isEmpty()) && perScores == null) {
- Debug.echoError("Missing or invalid 'scores' parameter.");
- return;
- }
- if ((value == null || value.size() != scores.size()) && perValue == null) {
- Debug.echoError("Missing or invalid 'values' parameter.");
- return;
- }
- Sidebar sidebar = createSidebar(player);
- if (sidebar == null) {
- continue;
- }
- List current = sidebar.getLines();
- if (per_player) {
- TagContext context = new BukkitTagContext(player, Utilities.getEntryNPC(scriptEntry),
- scriptEntry, scriptEntry.shouldDebug(), scriptEntry.getScript());
- if (perValue != null) {
- value = ListTag.getListFor(TagManager.tagObject(perValue, context), context);
- }
- if (perScores != null) {
- scores = ListTag.getListFor(TagManager.tagObject(perScores, context), context);
- }
- }
- try {
- for (int i = 0; i < value.size(); i++) {
- if (!ArgumentHelper.matchesInteger(scores.get(i))) {
- Debug.echoError("Sidebar command scores input contains not-a-valid-number: " + scores.get(i));
- return;
- }
- int score = Integer.parseInt(scores.get(i));
- if (hasScoreAlready(current, score)) {
- for (Sidebar.SidebarLine line : current) {
- if (line.score == score) {
- line.text = value.get(i);
- break;
- }
- }
- }
- else {
- current.add(new Sidebar.SidebarLine(value.get(i), score));
- }
- }
- }
- catch (Exception e) {
- Debug.echoError(e);
- continue;
- }
- sidebar.setLines(current);
- sidebar.sendUpdate();
- }
- break;
- case SET:
- for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
- if (player == null || !player.isValid()) {
- Debug.echoError("Invalid player!");
- continue;
- }
- Sidebar sidebar = createSidebar(player);
- if (sidebar == null) {
- continue;
- }
- List current = new ArrayList<>();
- if (per_player) {
- TagContext context = new BukkitTagContext(player, Utilities.getEntryNPC(scriptEntry),
- scriptEntry, scriptEntry.shouldDebug(), scriptEntry.getScript());
- if (perValue != null) {
- value = ListTag.getListFor(TagManager.tagObject(perValue, context), context);
- }
- if (perScores != null) {
- scores = ListTag.getListFor(TagManager.tagObject(perScores, context), context);
- }
- if (perStart != null) {
- start = new ElementTag(TagManager.tag(perStart, context));
- }
- if (perIncrement != null) {
- increment = new ElementTag(TagManager.tag(perIncrement, context));
- }
- if (perTitle != null) {
- title = new ElementTag(TagManager.tag(perTitle, context));
- }
- }
- if (value != null) {
- try {
- int index = start != null ? start.asInt() : value.size();
- int incr = increment != null ? increment.asInt() : -1;
- for (int i = 0; i < value.size(); i++, index += incr) {
- int score = (scores != null && i < scores.size()) ? Integer.parseInt(scores.get(i)) : index;
- current.add(new Sidebar.SidebarLine(value.get(i), score));
- }
- }
- catch (Exception e) {
- Debug.echoError(e);
- continue;
- }
- sidebar.setLines(current);
- }
- if (title != null) {
- sidebar.setTitle(title.asString());
- }
- sidebar.sendUpdate();
- }
- break;
- }
- }
-
private static final Map sidebars = new HashMap<>();
private static Sidebar createSidebar(PlayerTag denizenPlayer) {
From 934234892ebb37c52ce36d4242a2fd0918597772 Mon Sep 17 00:00:00 2001
From: david <89749147+davight@users.noreply.github.com>
Date: Fri, 21 Nov 2025 23:20:33 +0100
Subject: [PATCH 3/6] some work
---
.../commands/player/SidebarCommand.java | 69 ++++++++-----------
1 file changed, 30 insertions(+), 39 deletions(-)
diff --git a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/SidebarCommand.java b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/SidebarCommand.java
index 97d7c1b1dc..c1bec64678 100644
--- a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/SidebarCommand.java
+++ b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/SidebarCommand.java
@@ -3,6 +3,7 @@
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizencore.scripts.commands.generator.*;
+import com.denizenscript.denizencore.tags.ParseableTag;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.abstracts.Sidebar;
@@ -20,6 +21,7 @@
import org.bukkit.event.player.PlayerQuitEvent;
import java.util.*;
+import java.util.function.Supplier;
public class SidebarCommand extends AbstractCommand {
@@ -123,11 +125,11 @@ public static void autoExecute(ScriptEntry scriptEntry,
if (players == null) {
players = Utilities.entryHasPlayer(scriptEntry) ? Collections.singletonList(Utilities.getEntryPlayer(scriptEntry)) : Collections.emptyList();
}
- ElementTag parsedTitle = (perPlayer || title == null) ? null : new ElementTag(TagManager.tag(title, scriptEntry.getContext()));
+ ElementTag parsedTitle = (perPlayer || title == null) ? null : TagManager.tagObject(title, scriptEntry.getContext()).asElement();
ListTag parsedValues = (perPlayer || values == null) ? null : ListTag.valueOf(TagManager.tag(values, scriptEntry.getContext()), scriptEntry.getContext());
ListTag parsedScores = (perPlayer || scores == null) ? null : ListTag.valueOf(TagManager.tag(scores, scriptEntry.getContext()), scriptEntry.getContext());
- ElementTag parsedStart = (perPlayer || start == null) ? null : new ElementTag(TagManager.tag(start, scriptEntry.getContext()));
- ElementTag parsedIncrement = perPlayer ? null : new ElementTag(TagManager.tag(increment, scriptEntry.getContext()));
+ ElementTag parsedStart = (perPlayer || start == null) ? null : TagManager.tagObject(start, scriptEntry.getContext()).asElement();
+ ElementTag parsedIncrement = perPlayer ? null : TagManager.tagObject(increment, scriptEntry.getContext()).asElement();
Map sidebarData = new HashMap<>(players.size());
for (PlayerTag player : players) {
if (player == null || !player.isValid()) {
@@ -172,7 +174,7 @@ public static void autoExecute(ScriptEntry scriptEntry,
boolean removedAny = false;
List current = sidebar.getLines();
PlayerSidebarData data = entry.getValue();
- if (data.getScores() != null) {
+ if (data.getScores() != null && !data.getScores().isEmpty()) {
try {
for (String scoreString : data.getScores()) {
int score = Integer.parseInt(scoreString);
@@ -187,11 +189,9 @@ public static void autoExecute(ScriptEntry scriptEntry,
Debug.echoError(e);
continue;
}
- sidebar.setLines(current);
- sidebar.sendUpdate();
removedAny = true;
}
- if (data.getValues() != null) {
+ if (data.getValues() != null && !data.getValues().isEmpty()) {
for (String line : data.getValues()) {
for (int i = 0; i < current.size(); i++) {
if (current.get(i).text.equalsIgnoreCase(line)) {
@@ -199,14 +199,16 @@ public static void autoExecute(ScriptEntry scriptEntry,
}
}
}
- sidebar.setLines(current);
- sidebar.sendUpdate();
removedAny = true;
}
if (!removedAny) {
sidebar.remove();
sidebars.remove(entry.getKey().getPlayerEntity().getUniqueId());
}
+ else {
+ sidebar.setLines(current);
+ sidebar.sendUpdate();
+ }
}
}
case SET_LINE -> {
@@ -252,9 +254,9 @@ public static void autoExecute(ScriptEntry scriptEntry,
case SET -> {
for (Map.Entry entry : sidebarData.entrySet()) {
Sidebar sidebar = entry.getValue().sidebar;
- List current = new ArrayList<>();
PlayerSidebarData data = entry.getValue();
- if (data.getValues() != null) {
+ if (data.getValues() != null && !data.getValues().isEmpty()) {
+ List current = new ArrayList<>(data.getValues().size());
try {
int index = data.getStart() != null ? data.getStart().asInt() : data.getValues().size();
int incr = data.getIncrement() != null ? data.getIncrement().asInt() : -1;
@@ -310,38 +312,34 @@ public static class PlayerSidebarData {
}
public ElementTag getTitle() {
- if (parsedTitle == null) {
- parsedTitle = rawTitle == null ? null : new ElementTag(TagManager.tag(rawTitle, context));
- }
- return parsedTitle;
+ return parsedTitle = lazyParse(parsedTitle, rawTitle, () -> TagManager.tagObject(rawTitle, context).asElement());
}
public ListTag getScores() {
- if (parsedScores == null) {
- parsedScores = rawScores == null ? null : ListTag.getListFor(TagManager.tagObject(rawScores, context), context);
- }
- return parsedScores;
+ return parsedScores = lazyParse(parsedScores, rawScores, () -> ListTag.getListFor(TagManager.tagObject(rawScores, context), context));
}
public ListTag getValues() {
- if (parsedValues == null) {
- parsedValues = rawValues == null ? null : ListTag.getListFor(TagManager.tagObject(rawValues, context), context);
- }
- return parsedValues;
+ return parsedValues = lazyParse(parsedValues, rawValues, () -> ListTag.getListFor(TagManager.tagObject(rawValues, context), context));
}
public ElementTag getIncrement() {
- if (parsedIncrement == null) {
- parsedIncrement = rawIncrement == null ? null : new ElementTag(TagManager.tag(rawIncrement, context));
- }
- return parsedIncrement;
+ return parsedIncrement = lazyParse(parsedIncrement, rawIncrement, () -> TagManager.tagObject(rawIncrement, context).asElement());
}
public ElementTag getStart() {
- if (parsedStart == null) {
- parsedStart = rawStart == null ? null : new ElementTag(TagManager.tag(rawStart, context));
+ return parsedStart = lazyParse(parsedStart, rawStart, () -> TagManager.tagObject(rawStart, context).asElement());
+ }
+
+ public static T lazyParse(T parsedValue, String rawValue, Supplier parser) {
+ if (parsedValue == null) {
+ if (rawValue == null) {
+ return null;
+ }
+ Debug.log("Sidebar command lazy parsing: " + rawValue);
+ parsedValue = parser.get();
}
- return parsedStart;
+ return parsedValue;
}
}
@@ -361,18 +359,11 @@ private static Sidebar createSidebar(PlayerTag denizenPlayer) {
return null;
}
Player player = denizenPlayer.getPlayerEntity();
- UUID uuid = player.getUniqueId();
- if (!sidebars.containsKey(uuid)) {
- sidebars.put(uuid, NMSHandler.instance.createSidebar(player));
- }
- return sidebars.get(player.getUniqueId());
+ return sidebars.computeIfAbsent(player.getUniqueId(), uuid -> NMSHandler.instance.createSidebar(player));
}
public static Sidebar getSidebar(PlayerTag denizenPlayer) {
- if (!denizenPlayer.isOnline()) {
- return null;
- }
- return sidebars.get(denizenPlayer.getPlayerEntity().getUniqueId());
+ return denizenPlayer.isOnline() ? sidebars.get(denizenPlayer.getPlayerEntity().getUniqueId()) : null;
}
public static class SidebarEvents implements Listener {
From edcc178281d4ab6b12dd0050d53839ab3e99816e Mon Sep 17 00:00:00 2001
From: david <89749147+davight@users.noreply.github.com>
Date: Sat, 22 Nov 2025 14:48:01 +0100
Subject: [PATCH 4/6] other work
---
.../commands/player/SidebarCommand.java | 86 ++++++++-----------
1 file changed, 37 insertions(+), 49 deletions(-)
diff --git a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/SidebarCommand.java b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/SidebarCommand.java
index c1bec64678..154c5668d5 100644
--- a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/SidebarCommand.java
+++ b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/SidebarCommand.java
@@ -2,8 +2,10 @@
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.utilities.Utilities;
+import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.commands.generator.*;
import com.denizenscript.denizencore.tags.ParseableTag;
+import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.abstracts.Sidebar;
@@ -21,7 +23,7 @@
import org.bukkit.event.player.PlayerQuitEvent;
import java.util.*;
-import java.util.function.Supplier;
+import java.util.function.Function;
public class SidebarCommand extends AbstractCommand {
@@ -125,12 +127,9 @@ public static void autoExecute(ScriptEntry scriptEntry,
if (players == null) {
players = Utilities.entryHasPlayer(scriptEntry) ? Collections.singletonList(Utilities.getEntryPlayer(scriptEntry)) : Collections.emptyList();
}
- ElementTag parsedTitle = (perPlayer || title == null) ? null : TagManager.tagObject(title, scriptEntry.getContext()).asElement();
- ListTag parsedValues = (perPlayer || values == null) ? null : ListTag.valueOf(TagManager.tag(values, scriptEntry.getContext()), scriptEntry.getContext());
- ListTag parsedScores = (perPlayer || scores == null) ? null : ListTag.valueOf(TagManager.tag(scores, scriptEntry.getContext()), scriptEntry.getContext());
- ElementTag parsedStart = (perPlayer || start == null) ? null : TagManager.tagObject(start, scriptEntry.getContext()).asElement();
- ElementTag parsedIncrement = perPlayer ? null : TagManager.tagObject(increment, scriptEntry.getContext()).asElement();
- Map sidebarData = new HashMap<>(players.size());
+ ParseableTags parseableTags = new ParseableTags(title, scores, values, start, increment, scriptEntry.getContext());
+ LazyParser globalParser = perPlayer ? null : new LazyParser(parseableTags, (BukkitTagContext) scriptEntry.getContext());
+ Map sidebarData = new HashMap<>(players.size());
for (PlayerTag player : players) {
if (player == null || !player.isValid()) {
Debug.echoError("Invalid player!");
@@ -141,15 +140,15 @@ public static void autoExecute(ScriptEntry scriptEntry,
continue;
}
sidebarData.put(player, perPlayer ?
- new PlayerSidebarData(sidebar, new BukkitTagContext(player, Utilities.getEntryNPC(scriptEntry), scriptEntry, scriptEntry.shouldDebug(), scriptEntry.getScript()), title, scores, values, start, increment) :
- new PlayerSidebarData(sidebar, parsedTitle, parsedScores, parsedValues, parsedStart, parsedIncrement));
+ new LazyParser(parseableTags, new BukkitTagContext(player, Utilities.getEntryNPC(scriptEntry), scriptEntry, scriptEntry.shouldDebug(), scriptEntry.getScript())) :
+ globalParser);
}
switch (action) {
case ADD -> {
- for (Map.Entry entry : sidebarData.entrySet()) {
- Sidebar sidebar = entry.getValue().sidebar;
+ for (Map.Entry entry : sidebarData.entrySet()) {
+ Sidebar sidebar = createSidebar(entry.getKey());
List current = sidebar.getLines();
- PlayerSidebarData data = entry.getValue();
+ LazyParser data = entry.getValue();
try {
int index = data.getStart() != null ? data.getStart().asInt() : (!current.isEmpty() ? current.get(current.size() - 1).score : data.getValues().size());
int incr = data.getIncrement().asInt();
@@ -169,11 +168,11 @@ public static void autoExecute(ScriptEntry scriptEntry,
}
}
case REMOVE -> {
- for (Map.Entry entry : sidebarData.entrySet()) {
- Sidebar sidebar = entry.getValue().sidebar;
+ for (Map.Entry entry : sidebarData.entrySet()) {
+ Sidebar sidebar = createSidebar(entry.getKey());
boolean removedAny = false;
List current = sidebar.getLines();
- PlayerSidebarData data = entry.getValue();
+ LazyParser data = entry.getValue();
if (data.getScores() != null && !data.getScores().isEmpty()) {
try {
for (String scoreString : data.getScores()) {
@@ -212,8 +211,8 @@ public static void autoExecute(ScriptEntry scriptEntry,
}
}
case SET_LINE -> {
- for (Map.Entry entry : sidebarData.entrySet()) {
- PlayerSidebarData data = entry.getValue();
+ for (Map.Entry entry : sidebarData.entrySet()) {
+ LazyParser data = entry.getValue();
if (data.getScores() == null || data.getScores().isEmpty()) {
Debug.echoError("Missing or invalid 'scores' parameter!");
return;
@@ -222,7 +221,7 @@ public static void autoExecute(ScriptEntry scriptEntry,
Debug.echoError("Missing or invalid 'values' parameter!");
return;
}
- Sidebar sidebar = entry.getValue().sidebar;
+ Sidebar sidebar = createSidebar(entry.getKey());
List current = sidebar.getLines();
try {
for (int i = 0; i < data.getValues().size(); i++) {
@@ -252,9 +251,9 @@ public static void autoExecute(ScriptEntry scriptEntry,
}
}
case SET -> {
- for (Map.Entry entry : sidebarData.entrySet()) {
- Sidebar sidebar = entry.getValue().sidebar;
- PlayerSidebarData data = entry.getValue();
+ for (Map.Entry entry : sidebarData.entrySet()) {
+ Sidebar sidebar = createSidebar(entry.getKey());
+ LazyParser data = entry.getValue();
if (data.getValues() != null && !data.getValues().isEmpty()) {
List current = new ArrayList<>(data.getValues().size());
try {
@@ -280,11 +279,15 @@ public static void autoExecute(ScriptEntry scriptEntry,
}
}
- public static class PlayerSidebarData {
- BukkitTagContext context;
- Sidebar sidebar;
+ public record ParseableTags(ParseableTag title, ParseableTag scores, ParseableTag values, ParseableTag start, ParseableTag increment) {
+ public ParseableTags(String title, String scores, String values, String start, String increment, TagContext context) {
+ this(TagManager.parseTextToTag(title, context), TagManager.parseTextToTag(scores, context), TagManager.parseTextToTag(values, context), TagManager.parseTextToTag(start, context), TagManager.parseTextToTag(increment, context));
+ }
+ }
- String rawTitle = null, rawScores = null, rawValues = null, rawStart = null, rawIncrement = null;
+ public static class LazyParser {
+ final ParseableTags parseableTags;
+ final BukkitTagContext context;
ElementTag parsedTitle = null;
ListTag parsedScores = null;
@@ -292,52 +295,37 @@ public static class PlayerSidebarData {
ElementTag parsedStart = null;
ElementTag parsedIncrement = null;
- PlayerSidebarData(Sidebar sidebar, BukkitTagContext context, String rawTitle, String rawScores, String rawValues, String rawStart, String rawIncrement) {
- this.sidebar = sidebar;
+ public LazyParser(ParseableTags parseableTags, BukkitTagContext context) {
+ this.parseableTags = parseableTags;
this.context = context;
- this.rawTitle = rawTitle;
- this.rawScores = rawScores;
- this.rawValues = rawValues;
- this.rawStart = rawStart;
- this.rawIncrement = rawIncrement;
- }
-
- PlayerSidebarData(Sidebar sidebar, ElementTag title, ListTag scores, ListTag values, ElementTag start, ElementTag increment) {
- this.sidebar = sidebar;
- this.parsedTitle = title;
- this.parsedScores = scores;
- this.parsedValues = values;
- this.parsedStart = start;
- this.parsedIncrement = increment;
}
public ElementTag getTitle() {
- return parsedTitle = lazyParse(parsedTitle, rawTitle, () -> TagManager.tagObject(rawTitle, context).asElement());
+ return parsedTitle = lazyParse(parsedTitle, parseableTags.title(), ObjectTag::asElement);
}
public ListTag getScores() {
- return parsedScores = lazyParse(parsedScores, rawScores, () -> ListTag.getListFor(TagManager.tagObject(rawScores, context), context));
+ return parsedScores = lazyParse(parsedScores, parseableTags.scores(), (obj) -> ListTag.getListFor(obj, context));
}
public ListTag getValues() {
- return parsedValues = lazyParse(parsedValues, rawValues, () -> ListTag.getListFor(TagManager.tagObject(rawValues, context), context));
+ return parsedValues = lazyParse(parsedValues, parseableTags.values(), (obj) -> ListTag.getListFor(obj, context));
}
public ElementTag getIncrement() {
- return parsedIncrement = lazyParse(parsedIncrement, rawIncrement, () -> TagManager.tagObject(rawIncrement, context).asElement());
+ return parsedIncrement = lazyParse(parsedIncrement, parseableTags.increment(), ObjectTag::asElement);
}
public ElementTag getStart() {
- return parsedStart = lazyParse(parsedStart, rawStart, () -> TagManager.tagObject(rawStart, context).asElement());
+ return parsedStart = lazyParse(parsedStart, parseableTags.start(), ObjectTag::asElement);
}
- public static T lazyParse(T parsedValue, String rawValue, Supplier parser) {
+ public T lazyParse(T parsedValue, ParseableTag rawValue, Function parser) {
if (parsedValue == null) {
if (rawValue == null) {
return null;
}
- Debug.log("Sidebar command lazy parsing: " + rawValue);
- parsedValue = parser.get();
+ parsedValue = parser.apply(rawValue.parse(context));
}
return parsedValue;
}
From c81a0626b8d1d943eb1dbf14216fc66db1f3522a Mon Sep 17 00:00:00 2001
From: david <89749147+davight@users.noreply.github.com>
Date: Sat, 22 Nov 2025 18:46:53 +0100
Subject: [PATCH 5/6] finalize pls
---
.../commands/player/SidebarCommand.java | 278 ++++++++++--------
1 file changed, 154 insertions(+), 124 deletions(-)
diff --git a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/SidebarCommand.java b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/SidebarCommand.java
index 154c5668d5..c2def5dea1 100644
--- a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/SidebarCommand.java
+++ b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/SidebarCommand.java
@@ -5,14 +5,12 @@
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.commands.generator.*;
import com.denizenscript.denizencore.tags.ParseableTag;
-import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.abstracts.Sidebar;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.tags.BukkitTagContext;
import com.denizenscript.denizencore.objects.core.ElementTag;
-import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
@@ -23,7 +21,6 @@
import org.bukkit.event.player.PlayerQuitEvent;
import java.util.*;
-import java.util.function.Function;
public class SidebarCommand extends AbstractCommand {
@@ -105,33 +102,32 @@ public enum Action { ADD, REMOVE, SET, SET_LINE }
public static void autoExecute(ScriptEntry scriptEntry,
@ArgName("action") @ArgDefaultText("set") Action action,
- @ArgName("title") @ArgPrefixed @ArgUnparsed @ArgDefaultNull String title,
- @ArgName("scores") @ArgPrefixed @ArgUnparsed @ArgDefaultNull String scores,
- @ArgName("values") @ArgPrefixed @ArgUnparsed @ArgDefaultNull String values,
- @ArgName("start") @ArgPrefixed @ArgUnparsed @ArgDefaultNull String start,
- @ArgName("increment") @ArgPrefixed @ArgUnparsed @ArgDefaultText("-1") String increment,
+ @ArgName("title") @ArgPrefixed @ArgUnparsed @ArgDefaultNull String stringTitle,
+ @ArgName("scores") @ArgPrefixed @ArgUnparsed @ArgDefaultNull String stringScores,
+ @ArgName("values") @ArgPrefixed @ArgUnparsed @ArgDefaultNull String stringValues,
+ @ArgName("start") @ArgPrefixed @ArgUnparsed @ArgDefaultNull String stringStart,
+ @ArgName("increment") @ArgPrefixed @ArgUnparsed @ArgDefaultText("-1") String stringIncrement,
@ArgName("players") @ArgPrefixed @ArgDefaultNull @ArgSubType(PlayerTag.class) List players,
@ArgName("per_player") boolean perPlayer) {
- if (action == Action.ADD && values == null) {
+ if (action == Action.ADD && stringValues == null) {
Debug.echoError("Missing 'values' parameter!");
return;
}
- if (action == Action.SET && values == null && title == null) {
+ if (action == Action.SET && stringValues == null && stringTitle == null) {
Debug.echoError("Must specify at least one of: value(s), title, increment, or start for that action!");
return;
}
- if (action == Action.SET && scores == null && values == null) {
+ if (action == Action.SET && stringScores == null && stringValues == null) {
Debug.echoError("Must specify value(s) when setting scores!");
return;
}
if (players == null) {
players = Utilities.entryHasPlayer(scriptEntry) ? Collections.singletonList(Utilities.getEntryPlayer(scriptEntry)) : Collections.emptyList();
}
- ParseableTags parseableTags = new ParseableTags(title, scores, values, start, increment, scriptEntry.getContext());
- LazyParser globalParser = perPlayer ? null : new LazyParser(parseableTags, (BukkitTagContext) scriptEntry.getContext());
- Map sidebarData = new HashMap<>(players.size());
+ LazyParser parser = new LazyParser(stringTitle, stringScores, stringValues, stringStart, stringIncrement, perPlayer ? null : (BukkitTagContext) scriptEntry.getContext());
+ List cleanedList = new ArrayList<>(players.size());
for (PlayerTag player : players) {
- if (player == null || !player.isValid()) {
+ if (player == null || !player.isValid() || !player.isOnline()) {
Debug.echoError("Invalid player!");
continue;
}
@@ -139,59 +135,52 @@ public static void autoExecute(ScriptEntry scriptEntry,
if (sidebar == null) {
continue;
}
- sidebarData.put(player, perPlayer ?
- new LazyParser(parseableTags, new BukkitTagContext(player, Utilities.getEntryNPC(scriptEntry), scriptEntry, scriptEntry.shouldDebug(), scriptEntry.getScript())) :
- globalParser);
+ cleanedList.add(player);
}
switch (action) {
case ADD -> {
- for (Map.Entry entry : sidebarData.entrySet()) {
- Sidebar sidebar = createSidebar(entry.getKey());
+ for (PlayerTag player : cleanedList) {
+ Sidebar sidebar = createSidebar(player);
+ BukkitTagContext context = getContext(player, (BukkitTagContext) scriptEntry.getContext(), perPlayer);
+ ListTag values = parser.getValues(context);
+ if (values.isEmpty()) {
+ continue;
+ }
List current = sidebar.getLines();
- LazyParser data = entry.getValue();
- try {
- int index = data.getStart() != null ? data.getStart().asInt() : (!current.isEmpty() ? current.get(current.size() - 1).score : data.getValues().size());
- int incr = data.getIncrement().asInt();
- for (int i = 0; i < data.getValues().size(); i++, index += incr) {
- int score = (data.getScores() != null && i < data.getScores().size()) ? Integer.parseInt(data.getScores().get(i)) : index;
- while (hasScoreAlready(current, score)) {
- score += (incr == 0 ? 1 : incr);
- }
- current.add(new Sidebar.SidebarLine(data.getValues().get(i), score));
+ int index = parser.getStart(context, !current.isEmpty() ? current.get(current.size() - 1).score : values.size());
+ int incr = parser.getIncrement(context);
+ List scores = parser.getScores(context);
+ for (int i = 0; i < values.size(); i++, index += incr) {
+ int score = (scores != null && i < scores.size()) ? scores.get(i) : index;
+ while (hasScoreAlready(current, score)) {
+ score += (incr == 0 ? 1 : incr);
}
- } catch (NumberFormatException e) {
- Debug.echoError(e);
- continue;
+ current.add(new Sidebar.SidebarLine(values.get(i), score));
}
sidebar.setLines(current);
sidebar.sendUpdate();
}
}
case REMOVE -> {
- for (Map.Entry entry : sidebarData.entrySet()) {
- Sidebar sidebar = createSidebar(entry.getKey());
- boolean removedAny = false;
+ for (PlayerTag player : cleanedList) {
+ BukkitTagContext context = getContext(player, (BukkitTagContext) scriptEntry.getContext(), perPlayer);
+ Sidebar sidebar = createSidebar(player);
List current = sidebar.getLines();
- LazyParser data = entry.getValue();
- if (data.getScores() != null && !data.getScores().isEmpty()) {
- try {
- for (String scoreString : data.getScores()) {
- int score = Integer.parseInt(scoreString);
- for (int i = 0; i < current.size(); i++) {
- if (current.get(i).score == score) {
- current.remove(i--);
- }
+ boolean removedAny = false;
+ List scores = parser.getScores(context);
+ if (scores != null && !scores.isEmpty()) {
+ for (int score : scores) {
+ for (int i = 0; i < current.size(); i++) {
+ if (current.get(i).score == score) {
+ current.remove(i--);
}
}
}
- catch (NumberFormatException e) {
- Debug.echoError(e);
- continue;
- }
removedAny = true;
}
- if (data.getValues() != null && !data.getValues().isEmpty()) {
- for (String line : data.getValues()) {
+ ListTag values = parser.getValues(context);
+ if (values != null && !values.isEmpty()) {
+ for (String line : values) {
for (int i = 0; i < current.size(); i++) {
if (current.get(i).text.equalsIgnoreCase(line)) {
current.remove(i--);
@@ -202,7 +191,7 @@ public static void autoExecute(ScriptEntry scriptEntry,
}
if (!removedAny) {
sidebar.remove();
- sidebars.remove(entry.getKey().getPlayerEntity().getUniqueId());
+ sidebars.remove(player.getPlayerEntity().getUniqueId());
}
else {
sidebar.setLines(current);
@@ -211,67 +200,56 @@ public static void autoExecute(ScriptEntry scriptEntry,
}
}
case SET_LINE -> {
- for (Map.Entry entry : sidebarData.entrySet()) {
- LazyParser data = entry.getValue();
- if (data.getScores() == null || data.getScores().isEmpty()) {
+ for (PlayerTag player : cleanedList) {
+ BukkitTagContext context = getContext(player, (BukkitTagContext) scriptEntry.getContext(), perPlayer);
+ List scores = parser.getScores(context);
+ if (scores == null || scores.isEmpty()) {
Debug.echoError("Missing or invalid 'scores' parameter!");
return;
}
- if (data.getValues() == null || data.getValues().size() != data.getScores().size()) {
+ ListTag values = parser.getValues(context);
+ if (values == null || values.size() != scores.size()) {
Debug.echoError("Missing or invalid 'values' parameter!");
return;
}
- Sidebar sidebar = createSidebar(entry.getKey());
+ Sidebar sidebar = getSidebar(player);
List current = sidebar.getLines();
- try {
- for (int i = 0; i < data.getValues().size(); i++) {
- if (!ArgumentHelper.matchesInteger(data.getScores().get(i))) {
- Debug.echoError("Sidebar command scores input contains not-a-valid-number: " + data.getScores().get(i));
- return;
- }
- int score = Integer.parseInt(data.getScores().get(i));
- if (hasScoreAlready(current, score)) {
- for (Sidebar.SidebarLine line : current) {
- if (line.score == score) {
- line.text = data.getValues().get(i);
- break;
- }
+ for (int i = 0; i < values.size(); i++) {
+ int score = scores.get(i);
+ if (hasScoreAlready(current, score)) {
+ for (Sidebar.SidebarLine line : current) {
+ if (line.score == score) {
+ line.text = values.get(i);
+ break;
}
}
- else {
- current.add(new Sidebar.SidebarLine(data.getValues().get(i), score));
- }
+ } else {
+ current.add(new Sidebar.SidebarLine(values.get(i), score));
}
- } catch (NumberFormatException e) {
- Debug.echoError(e);
- continue;
}
sidebar.setLines(current);
sidebar.sendUpdate();
}
}
case SET -> {
- for (Map.Entry entry : sidebarData.entrySet()) {
- Sidebar sidebar = createSidebar(entry.getKey());
- LazyParser data = entry.getValue();
- if (data.getValues() != null && !data.getValues().isEmpty()) {
- List current = new ArrayList<>(data.getValues().size());
- try {
- int index = data.getStart() != null ? data.getStart().asInt() : data.getValues().size();
- int incr = data.getIncrement() != null ? data.getIncrement().asInt() : -1;
- for (int i = 0; i < data.getValues().size(); i++, index += incr) {
- int score = (data.getScores() != null && i < data.getScores().size()) ? Integer.parseInt(data.getScores().get(i)) : index;
- current.add(new Sidebar.SidebarLine(data.getValues().get(i), score));
- }
- }
- catch (NumberFormatException e) {
- Debug.echoError(e);
- continue;
+ for (PlayerTag player : cleanedList) {
+ Sidebar sidebar = getSidebar(player);
+ BukkitTagContext context = getContext(player, (BukkitTagContext) scriptEntry.getContext(), perPlayer);
+ ListTag values = parser.getValues(context);
+ if (values != null && !values.isEmpty()) {
+ List scores = parser.getScores(context);
+ List current = new ArrayList<>(values.size());
+ int index = parser.getStart(context, values.size());
+ int incr = parser.getIncrement(context);
+ for (int i = 0; i < values.size(); i++, index += incr) {
+ int score = (scores != null && i < scores.size()) ? scores.get(i) : index;
+ current.add(new Sidebar.SidebarLine(values.get(i), score));
}
sidebar.setLines(current);
}
- if (data.getTitle() != null) {
- sidebar.setTitle(data.getTitle().asString());
+ ElementTag title = parser.getTitle(context);
+ if (title != null) {
+ sidebar.setTitle(title.asString());
}
sidebar.sendUpdate();
}
@@ -279,56 +257,108 @@ public static void autoExecute(ScriptEntry scriptEntry,
}
}
- public record ParseableTags(ParseableTag title, ParseableTag scores, ParseableTag values, ParseableTag start, ParseableTag increment) {
- public ParseableTags(String title, String scores, String values, String start, String increment, TagContext context) {
- this(TagManager.parseTextToTag(title, context), TagManager.parseTextToTag(scores, context), TagManager.parseTextToTag(values, context), TagManager.parseTextToTag(start, context), TagManager.parseTextToTag(increment, context));
+ public static BukkitTagContext getContext(PlayerTag player, BukkitTagContext global, boolean perPlayer) {
+ if (perPlayer) {
+ BukkitTagContext context = new BukkitTagContext(global);
+ context.player = player;
+ return context;
}
+ return global;
}
public static class LazyParser {
- final ParseableTags parseableTags;
- final BukkitTagContext context;
+ final BukkitTagContext globalContext;
+ final ParseableTag title, scores, values, start, increment;
ElementTag parsedTitle = null;
- ListTag parsedScores = null;
+ LinkedList parsedScores = null;
ListTag parsedValues = null;
- ElementTag parsedStart = null;
- ElementTag parsedIncrement = null;
-
- public LazyParser(ParseableTags parseableTags, BukkitTagContext context) {
- this.parseableTags = parseableTags;
- this.context = context;
- }
+ Integer parsedStart = null;
+ Integer parsedIncrement = null;
- public ElementTag getTitle() {
- return parsedTitle = lazyParse(parsedTitle, parseableTags.title(), ObjectTag::asElement);
+ public LazyParser(String title, String scores, String values, String start, String increment, BukkitTagContext globalContext) {
+ this.title = title == null ? null : TagManager.parseTextToTag(title, globalContext);
+ this.scores = scores == null ? null : TagManager.parseTextToTag(scores, globalContext);
+ this.values = values == null ? null : TagManager.parseTextToTag(values, globalContext);
+ this.start = start == null ? null : TagManager.parseTextToTag(start, globalContext);
+ this.increment = increment == null ? null : TagManager.parseTextToTag(increment, globalContext);
+ this.globalContext = globalContext;
}
- public ListTag getScores() {
- return parsedScores = lazyParse(parsedScores, parseableTags.scores(), (obj) -> ListTag.getListFor(obj, context));
+ public ElementTag getTitle(BukkitTagContext context) {
+ if (context == globalContext && parsedTitle != null) {
+ return parsedTitle;
+ }
+ ElementTag parsed = title == null ? null : title.parse(context).asElement();
+ if (context == globalContext) {
+ parsedTitle = parsed;
+ }
+ return parsed;
}
- public ListTag getValues() {
- return parsedValues = lazyParse(parsedValues, parseableTags.values(), (obj) -> ListTag.getListFor(obj, context));
+ public List getScores(BukkitTagContext context) {
+ if (context == globalContext && parsedScores != null) {
+ return parsedScores;
+ }
+ if (scores == null) {
+ return null;
+ }
+ ListTag parsed = ListTag.getListFor(scores.parse(context), context);
+ if (parsed == null) {
+ return null;
+ }
+ LinkedList scores = new LinkedList<>();
+ for (ObjectTag s : parsed.objectForms) {
+ scores.add(s.asElement().asInt());
+ }
+ if (context == globalContext) {
+ parsedScores = scores;
+ }
+ return scores;
}
- public ElementTag getIncrement() {
- return parsedIncrement = lazyParse(parsedIncrement, parseableTags.increment(), ObjectTag::asElement);
+ public ListTag getValues(BukkitTagContext context) {
+ if (context == globalContext && parsedValues != null) {
+ return parsedValues;
+ }
+ if (values == null) {
+ return null;
+ }
+ ListTag parsed = ListTag.getListFor(values.parse(context), context);
+ if (parsed == null) {
+ return null;
+ }
+ if (context == globalContext) {
+ parsedValues = parsed;
+ }
+ return parsed;
}
- public ElementTag getStart() {
- return parsedStart = lazyParse(parsedStart, parseableTags.start(), ObjectTag::asElement);
+ public int getIncrement(BukkitTagContext context) {
+ if (context == globalContext && parsedIncrement != null) {
+ return parsedIncrement;
+ }
+ int inc = increment.parse(context).asElement().asInt();
+ if (context == globalContext) {
+ parsedIncrement = inc;
+ }
+ return inc;
}
- public T lazyParse(T parsedValue, ParseableTag rawValue, Function parser) {
- if (parsedValue == null) {
- if (rawValue == null) {
- return null;
- }
- parsedValue = parser.apply(rawValue.parse(context));
+ public int getStart(BukkitTagContext context, int defaultValue) {
+ if (context == globalContext && parsedStart != null) {
+ return parsedStart;
+ }
+ if (start == null) {
+ return defaultValue;
}
- return parsedValue;
+ int parsed = start.parse(context).asElement().asInt();
+ if (context == globalContext) {
+ parsedIncrement = parsed;
+ }
+ return parsed;
}
+
}
public static boolean hasScoreAlready(List lines, int score) {
From 394e16f0b20cae097011e337263af9c1b9acc2be Mon Sep 17 00:00:00 2001
From: david <89749147+davight@users.noreply.github.com>
Date: Wed, 17 Dec 2025 14:50:07 +0100
Subject: [PATCH 6/6] add remapped prefixes
---
.../denizen/scripts/commands/player/SidebarCommand.java | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/SidebarCommand.java b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/SidebarCommand.java
index c2def5dea1..909c4e6e1e 100644
--- a/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/SidebarCommand.java
+++ b/plugin/src/main/java/com/denizenscript/denizen/scripts/commands/player/SidebarCommand.java
@@ -29,6 +29,12 @@ public SidebarCommand() {
setSyntax("sidebar (add/remove/{set}/set_line) (title:) (scores:<#>|...) (values:|...) (start:<#>/{num_of_lines}) (increment:<#>/{-1}) (players:|...) (per_player)");
setRequiredArguments(1, 8);
Denizen.getInstance().getServer().getPluginManager().registerEvents(new SidebarEvents(), Denizen.getInstance());
+ addRemappedPrefixes("title", "t", "objective", "obj", "o");
+ addRemappedPrefixes("scores", "score", "lines", "line", "l");
+ addRemappedPrefixes("values", "value", "val", "v");
+ addRemappedPrefixes("increment", "inc", "i");
+ addRemappedPrefixes("start", "s");
+ addRemappedPrefixes("players", "player", "p");
isProcedural = false;
autoCompile();
}