From 722cf9aa4bbcccbe8244e2d91a4e22559dd51345 Mon Sep 17 00:00:00 2001 From: 3add <3add.development@gmail.com> Date: Sun, 31 May 2026 12:38:37 +0200 Subject: [PATCH 1/2] add: SyntaxStringBuilder --- .../lang/SyntaxStringBuilder.java | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/main/java/io/github/syst3ms/skriptparser/lang/SyntaxStringBuilder.java diff --git a/src/main/java/io/github/syst3ms/skriptparser/lang/SyntaxStringBuilder.java b/src/main/java/io/github/syst3ms/skriptparser/lang/SyntaxStringBuilder.java new file mode 100644 index 00000000..dea3a1c3 --- /dev/null +++ b/src/main/java/io/github/syst3ms/skriptparser/lang/SyntaxStringBuilder.java @@ -0,0 +1,102 @@ +package io.github.syst3ms.skriptparser.lang; + +import org.jetbrains.annotations.NotNull; + +import java.util.StringJoiner; + +/** + * Utility class to build syntax strings, primarily intended for use + * in {@link SyntaxElement#toString(TriggerContext, boolean)} implementations. + * Spaces are automatically added between the provided objects. + */ +public class SyntaxStringBuilder { + + private final boolean debug; + private final TriggerContext context; + private final StringJoiner joiner = new StringJoiner(" "); + + /** + * Creates a new SyntaxStringBuilder. + * + * @param context The context to get information from. + * @param debug If true this should print more information, if false this should print what is shown to the end user + */ + public SyntaxStringBuilder(TriggerContext context, boolean debug) { + this.context = context; + this.debug = debug; + } + + /** + * Adds an object to the string and returns the builder. + * Spaces are automatically added between the provided objects. + * If the object is a {@link SyntaxElement} it will be formatted using + * {@link SyntaxElement#toString(TriggerContext, boolean)}. + * + * @param object The object to add. + * @return The builder. + * @see #appendIf(boolean, Object) + */ + public SyntaxStringBuilder append(@NotNull Object object) { + if (object instanceof SyntaxElement debuggable) { + joiner.add(debuggable.toString(context, debug)); + } else { + joiner.add(object.toString()); + } + return this; + } + + /** + * Adds multiple objects to the string and returns the builder. + * Spaces are automatically added between the provided objects. + * + * @param objects The objects to add. + * @return The builder. + * @see #appendIf(boolean, Object...) + */ + public SyntaxStringBuilder append(@NotNull Object... objects) { + for (Object object : objects) { + append(object); + } + return this; + } + + /** + * Adds an object to the string and returns the builder, if the given condition is true. + * Spaces are automatically added between the provided objects. + * If the object is a {@link SyntaxElement} it will be formatted using + * {@link SyntaxElement#toString(TriggerContext, boolean)}. + * + * @param condition The condition. + * @param object The object to add. Ensure this is not null. + * @return The builder. + * @see #append(Object) + */ + public SyntaxStringBuilder appendIf(boolean condition, Object object) { + if (condition) { + append(object); + } + return this; + } + + /** + * Adds multiple objects to the string and returns the builder, if the given condition is true. + * Spaces are automatically added between the provided objects. + * + * @param condition The condition. + * @param objects The objects to add. Ensure this is not null. + * @return The builder. + * @see #append(Object...) + */ + public SyntaxStringBuilder appendIf(boolean condition, Object... objects) { + if (condition) { + append(objects); + } + return this; + } + + @Override + public String toString() { + return joiner.toString(); + } + +} From 78d3ed79a1935786cd3797d26624797cc1820584 Mon Sep 17 00:00:00 2001 From: 3add <141489004+3add@users.noreply.github.com> Date: Sun, 31 May 2026 15:02:50 +0200 Subject: [PATCH 2/2] Update src/main/java/io/github/syst3ms/skriptparser/lang/SyntaxStringBuilder.java Co-authored-by: Shane Bee --- .../io/github/syst3ms/skriptparser/lang/SyntaxStringBuilder.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/io/github/syst3ms/skriptparser/lang/SyntaxStringBuilder.java b/src/main/java/io/github/syst3ms/skriptparser/lang/SyntaxStringBuilder.java index dea3a1c3..3af8821b 100644 --- a/src/main/java/io/github/syst3ms/skriptparser/lang/SyntaxStringBuilder.java +++ b/src/main/java/io/github/syst3ms/skriptparser/lang/SyntaxStringBuilder.java @@ -100,3 +100,4 @@ public String toString() { } } +