From 861a97ac2bb6bbd44a54afa26f49c12732b6691f Mon Sep 17 00:00:00 2001 From: Eren Kara Date: Mon, 3 Nov 2025 23:14:32 +0300 Subject: [PATCH] add appendIf --- .../njol/skript/lang/SyntaxStringBuilder.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java b/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java index e2ea883c5b7..f28b6acf8e8 100644 --- a/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java +++ b/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java @@ -37,6 +37,7 @@ public SyntaxStringBuilder(@Nullable Event event, boolean debug) { * * @param object The object to add. * @return The builder. + * @see #appendIf(boolean, Object) */ public SyntaxStringBuilder append(@NotNull Object object) { Preconditions.checkNotNull(object); @@ -54,6 +55,7 @@ public SyntaxStringBuilder append(@NotNull Object object) { * * @param objects The objects to add. * @return The builder. + * @see #appendIf(boolean, Object...) */ public SyntaxStringBuilder append(@NotNull Object... objects) { for (Object object : objects) { @@ -62,6 +64,40 @@ public SyntaxStringBuilder append(@NotNull Object... objects) { 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 Debuggable} it will be formatted using + * {@link Debuggable#toString(Event, boolean)}. + * + * @param condition The condition. + * @param object The object to add. + * @return The builder. + * @see #append(Object) + */ + public SyntaxStringBuilder appendIf(boolean condition, @NotNull 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. + * @return The builder. + * @see #append(Object...) + */ + public SyntaxStringBuilder appendIf(boolean condition, @NotNull Object... objects) { + if (condition) { + append(objects); + } + return this; + } + @Override public String toString() { return joiner.toString();