From 83ab073501fcdde733ed25ccff6316079f4534ca Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:39:32 +0100 Subject: [PATCH 01/11] init commit --- .../java/ch/njol/skript/effects/EffBan.java | 29 ++++----- .../ch/njol/skript/effects/EffReplace.java | 13 ++-- .../java/ch/njol/skript/lang/Debuggable.java | 62 +++++++++++++------ 3 files changed, 65 insertions(+), 39 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffBan.java b/src/main/java/ch/njol/skript/effects/EffBan.java index 88f815c96df..0edd149a2da 100644 --- a/src/main/java/ch/njol/skript/effects/EffBan.java +++ b/src/main/java/ch/njol/skript/effects/EffBan.java @@ -54,7 +54,7 @@ "ban and kick player due to \"inappropriate language\" for 2 days"}) @Since("1.4, 2.1.1 (ban reason), 2.5 (timespan), 2.9.0 (kick)") public class EffBan extends Effect { - + static { Skript.registerEffect(EffBan.class, "ban [kick:and kick] %strings/offlineplayers% [(by reason of|because [of]|on account of|due to) %-string%] [for %-timespan%]", @@ -64,18 +64,18 @@ public class EffBan extends Effect { "IP(-| )ban [kick:and kick] %players% [(by reason of|because [of]|on account of|due to) %-string%] [for %-timespan%]", "(IP(-| )unban|un[-]IP[-]ban) %players%"); } - + @SuppressWarnings("null") private Expression players; @Nullable private Expression reason; @Nullable private Expression expires; - + private boolean ban; private boolean ipBan; private boolean kick; - + @SuppressWarnings({"null", "unchecked"}) @Override public boolean init(final Expression[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) { @@ -87,7 +87,7 @@ public boolean init(final Expression[] exprs, final int matchedPattern, final kick = parseResult.hasTag("kick"); return true; } - + @SuppressWarnings("null") @Override protected void execute(final Event e) { @@ -137,15 +137,16 @@ protected void execute(final Event e) { } } } - + @Override - public String toString(final @Nullable Event event, final boolean debug) { - return (ipBan ? "IP-" : "") + - (this.ban ? "ban " : "unban ") + - (kick ? "and kick " : "") + - this.players.toString(event, debug) + - (this.reason != null ? " on account of " + this.reason.toString(event, debug) : "") + - (expires != null ? " for " + expires.toString(event, debug) : ""); + public String toString(@Nullable Event event, boolean debug) { + return formattedToString("%s %s %s %s %s %s", + option(ipBan, "IP"), + ban ? "ban" : "unban", + option(kick, "and kick"), + players.toString(event, debug), + optional(reason, reason -> "on account of " + reason.toString(event, debug)), + optional(expires, expires -> "for " + expires.toString(event, debug))); } - + } diff --git a/src/main/java/ch/njol/skript/effects/EffReplace.java b/src/main/java/ch/njol/skript/effects/EffReplace.java index 0bb52d4eb26..787926907b8 100644 --- a/src/main/java/ch/njol/skript/effects/EffReplace.java +++ b/src/main/java/ch/njol/skript/effects/EffReplace.java @@ -140,11 +140,12 @@ private void replace(Event event, Object[] needles, Expression haystackExpr) @Override public String toString(@Nullable Event event, boolean debug) { - if (replaceFirst) - return "replace first " + needles.toString(event, debug) + " in " + haystack.toString(event, debug) + " with " + replacement.toString(event, debug) - + "(case sensitive: " + caseSensitive + ")"; - return "replace " + needles.toString(event, debug) + " in " + haystack.toString(event, debug) + " with " + replacement.toString(event, debug) - + "(case sensitive: " + caseSensitive + ")"; + return formattedToString("replace %s %s in %s with %s %s", + option(replaceFirst, "first"), + needles.toString(event, debug), + haystack.toString(event, debug), + replacement.toString(event, debug), + option(caseSensitive, "with case sensitivity")); } - + } diff --git a/src/main/java/ch/njol/skript/lang/Debuggable.java b/src/main/java/ch/njol/skript/lang/Debuggable.java index 98714194d32..eb973cdbcb8 100644 --- a/src/main/java/ch/njol/skript/lang/Debuggable.java +++ b/src/main/java/ch/njol/skript/lang/Debuggable.java @@ -1,35 +1,22 @@ -/** - * This file is part of Skript. - * - * Skript is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Skript is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Skript. If not, see . - * - * Copyright Peter Güttinger, SkriptLang team and contributors - */ package ch.njol.skript.lang; import org.bukkit.event.Event; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.function.Function; + /** * Represents an element that can print details involving an event. */ public interface Debuggable { /** + * Returns a string representation of this object. + * * @param event The event to get information from. This is always null if debug == false. * @param debug If true this should print more information, if false this should print what is shown to the end user - * @return String representation of this object + * @return A string representation of this object. */ String toString(@Nullable Event event, boolean debug); @@ -39,4 +26,41 @@ public interface Debuggable { @Override String toString(); + default String formattedToString(@NotNull String string, Object... args) { + return string.formatted(args).replaceAll(" +", " "); + } + + default T option(boolean condition, T ifTrue, T ifFalse) { + return condition ? ifTrue : ifFalse; + } + + default String option(boolean condition, T ifTrue) { + return condition ? ifTrue.toString() : ""; + } + + default OptionalDebugParameter optional(T value, Function<@NotNull T, String> ifNotNull) { + return optional(value, ifNotNull, ""); + } + + default OptionalDebugParameter optional(T value, Function<@NotNull T, String> ifNotNull, String ifNull) { + return new OptionalDebugParameter<>(value, ifNotNull, ifNull); + } + + final class OptionalDebugParameter { + + private final T value; + private final Function<@NotNull T, String> ifNotNull; + private final String ifNull; + + public OptionalDebugParameter(T value, Function<@NotNull T, String> ifNotNull, String ifNull) { + this.value = value; + this.ifNotNull = ifNotNull; + this.ifNull = ifNull; + } + + @Override + public String toString() { + return value != null ? ifNotNull.apply(value) : ifNull; + } + } } From 1d61e447c7e7b257bc876a7cf19005fb015520f7 Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Tue, 29 Oct 2024 17:55:55 +0100 Subject: [PATCH 02/11] remove option --- src/main/java/ch/njol/skript/effects/EffBan.java | 4 ++-- src/main/java/ch/njol/skript/effects/EffReplace.java | 4 ++-- src/main/java/ch/njol/skript/lang/Debuggable.java | 10 ++-------- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffBan.java b/src/main/java/ch/njol/skript/effects/EffBan.java index 0edd149a2da..50f4d297183 100644 --- a/src/main/java/ch/njol/skript/effects/EffBan.java +++ b/src/main/java/ch/njol/skript/effects/EffBan.java @@ -141,9 +141,9 @@ protected void execute(final Event e) { @Override public String toString(@Nullable Event event, boolean debug) { return formattedToString("%s %s %s %s %s %s", - option(ipBan, "IP"), + ipBan ? "IP" : "", ban ? "ban" : "unban", - option(kick, "and kick"), + kick ? "and kick" : "", players.toString(event, debug), optional(reason, reason -> "on account of " + reason.toString(event, debug)), optional(expires, expires -> "for " + expires.toString(event, debug))); diff --git a/src/main/java/ch/njol/skript/effects/EffReplace.java b/src/main/java/ch/njol/skript/effects/EffReplace.java index 787926907b8..ba600849eef 100644 --- a/src/main/java/ch/njol/skript/effects/EffReplace.java +++ b/src/main/java/ch/njol/skript/effects/EffReplace.java @@ -141,11 +141,11 @@ private void replace(Event event, Object[] needles, Expression haystackExpr) @Override public String toString(@Nullable Event event, boolean debug) { return formattedToString("replace %s %s in %s with %s %s", - option(replaceFirst, "first"), + replaceFirst ? "first" : "", needles.toString(event, debug), haystack.toString(event, debug), replacement.toString(event, debug), - option(caseSensitive, "with case sensitivity")); + caseSensitive ? "with case sensitivity" : ""); } } diff --git a/src/main/java/ch/njol/skript/lang/Debuggable.java b/src/main/java/ch/njol/skript/lang/Debuggable.java index eb973cdbcb8..3a6369b53d0 100644 --- a/src/main/java/ch/njol/skript/lang/Debuggable.java +++ b/src/main/java/ch/njol/skript/lang/Debuggable.java @@ -30,14 +30,6 @@ default String formattedToString(@NotNull String string, Object... args) { return string.formatted(args).replaceAll(" +", " "); } - default T option(boolean condition, T ifTrue, T ifFalse) { - return condition ? ifTrue : ifFalse; - } - - default String option(boolean condition, T ifTrue) { - return condition ? ifTrue.toString() : ""; - } - default OptionalDebugParameter optional(T value, Function<@NotNull T, String> ifNotNull) { return optional(value, ifNotNull, ""); } @@ -62,5 +54,7 @@ public OptionalDebugParameter(T value, Function<@NotNull T, String> ifNotNull, S public String toString() { return value != null ? ifNotNull.apply(value) : ifNull; } + } + } From 32416bd9cd7cef008fb19e1866ec8f548bdca2af Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Tue, 29 Oct 2024 18:13:41 +0100 Subject: [PATCH 03/11] update javadoc --- .../java/ch/njol/skript/lang/Debuggable.java | 59 +++++++++++++++++-- 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/src/main/java/ch/njol/skript/lang/Debuggable.java b/src/main/java/ch/njol/skript/lang/Debuggable.java index 3a6369b53d0..7ba8c8484c5 100644 --- a/src/main/java/ch/njol/skript/lang/Debuggable.java +++ b/src/main/java/ch/njol/skript/lang/Debuggable.java @@ -26,25 +26,72 @@ public interface Debuggable { @Override String toString(); + /** + * Formats a string with the given (optional) arguments, for constructing a {@link #toString(Event, boolean)} + * representation. + * + * @param string The string to format. + * @param args The args to replace. + * @return The formatted string. + * @see String#format(String, Object...) + */ default String formattedToString(@NotNull String string, Object... args) { return string.formatted(args).replaceAll(" +", " "); } - default OptionalDebugParameter optional(T value, Function<@NotNull T, String> ifNotNull) { + /** + * Represents an optional value in a {@link #toString(Event, boolean)} method. + *
    + *
  • If {@code value} is not null, returns a class which returns the result of the function {@code ifNotNull}. + * This function applies a not-null {@code value} to get a string, which removes null warnings.
  • + *
  • If {@code value} is null, returns a class which returns an empty string.
  • + *
+ * + * @param value The potentially null value. + * @param ifNotNull The function to apply if {@code value} is not null. + * @return A class which gives the string representation of the value, or an empty string if it is null. + * @param The type of the value. + */ + default OptionalDebugParameter optional( + @Nullable T value, + @NotNull Function<@NotNull T, String> ifNotNull + ) { return optional(value, ifNotNull, ""); } - default OptionalDebugParameter optional(T value, Function<@NotNull T, String> ifNotNull, String ifNull) { + /** + * Represents an optional value in a {@link #toString(Event, boolean)} method. + *
    + *
  • If {@code value} is not null, returns a class which returns the result of the function {@code ifNotNull}. + * This function applies a not-null {@code value} to get a string, which removes null warnings.
  • + *
  • If {@code value} is null, returns a class which returns {@code ifNull}.
  • + *
+ * + * @param value The potentially null value. + * @param ifNotNull The function to apply if {@code value} is not null. + * @param ifNull The string to return if {@code value} is null. + * @return A class which gives the string representation of the value, or {@code ifNull} if it is null. + * @param The type of the value. + */ + default OptionalDebugParameter optional( + @Nullable T value, + @NotNull Function<@NotNull T, String> ifNotNull, + @NotNull String ifNull + ) { return new OptionalDebugParameter<>(value, ifNotNull, ifNull); } final class OptionalDebugParameter { - private final T value; - private final Function<@NotNull T, String> ifNotNull; - private final String ifNull; + private final @Nullable T value; + private final @NotNull Function<@NotNull T, String> ifNotNull; + private final @NotNull String ifNull; - public OptionalDebugParameter(T value, Function<@NotNull T, String> ifNotNull, String ifNull) { + public OptionalDebugParameter( + @Nullable T value, + @NotNull Function<@NotNull T, String> ifNotNull, + @NotNull String ifNull + ) { this.value = value; this.ifNotNull = ifNotNull; this.ifNull = ifNull; From b539f92ba5cc730f44a7cc0b6a0eac682d7d2a56 Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Tue, 29 Oct 2024 23:13:44 +0100 Subject: [PATCH 04/11] le fix --- .../java/ch/njol/skript/effects/EffBan.java | 22 +++-- .../ch/njol/skript/effects/EffReplace.java | 21 +++-- .../java/ch/njol/skript/lang/Debuggable.java | 81 ------------------- .../njol/skript/lang/SyntaxStringBuilder.java | 34 ++++++++ 4 files changed, 64 insertions(+), 94 deletions(-) create mode 100644 src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java diff --git a/src/main/java/ch/njol/skript/effects/EffBan.java b/src/main/java/ch/njol/skript/effects/EffBan.java index 50f4d297183..0b752e2711e 100644 --- a/src/main/java/ch/njol/skript/effects/EffBan.java +++ b/src/main/java/ch/njol/skript/effects/EffBan.java @@ -21,6 +21,7 @@ import java.net.InetSocketAddress; import java.util.Date; +import ch.njol.skript.lang.SyntaxStringBuilder; import org.bukkit.BanList; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; @@ -140,13 +141,20 @@ protected void execute(final Event e) { @Override public String toString(@Nullable Event event, boolean debug) { - return formattedToString("%s %s %s %s %s %s", - ipBan ? "IP" : "", - ban ? "ban" : "unban", - kick ? "and kick" : "", - players.toString(event, debug), - optional(reason, reason -> "on account of " + reason.toString(event, debug)), - optional(expires, expires -> "for " + expires.toString(event, debug))); + SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug); + + if (ipBan) + builder.add("IP"); + builder.add(ban ? "ban" : "unban"); + if (kick) + builder.add("and kick"); + builder.add(players); + if (reason != null) + builder.add("on account of").add(reason); + if (expires != null) + builder.add("for").add(expires); + + return builder.toString(); } } diff --git a/src/main/java/ch/njol/skript/effects/EffReplace.java b/src/main/java/ch/njol/skript/effects/EffReplace.java index ba600849eef..6980d8cc5ee 100644 --- a/src/main/java/ch/njol/skript/effects/EffReplace.java +++ b/src/main/java/ch/njol/skript/effects/EffReplace.java @@ -31,6 +31,7 @@ import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.ExpressionList; import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.SyntaxStringBuilder; import ch.njol.util.Kleenean; import ch.njol.util.StringUtils; import org.bukkit.event.Event; @@ -140,12 +141,20 @@ private void replace(Event event, Object[] needles, Expression haystackExpr) @Override public String toString(@Nullable Event event, boolean debug) { - return formattedToString("replace %s %s in %s with %s %s", - replaceFirst ? "first" : "", - needles.toString(event, debug), - haystack.toString(event, debug), - replacement.toString(event, debug), - caseSensitive ? "with case sensitivity" : ""); + SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug); + + builder.add("replace"); + if (replaceFirst) + builder.add("the first"); + builder.add(needles); + builder.add("in"); + builder.add(haystack); + builder.add("with"); + builder.add(replacement); + if (caseSensitive) + builder.add("with case sensitivity"); + + return builder.toString(); } } diff --git a/src/main/java/ch/njol/skript/lang/Debuggable.java b/src/main/java/ch/njol/skript/lang/Debuggable.java index 7ba8c8484c5..642998d2cf9 100644 --- a/src/main/java/ch/njol/skript/lang/Debuggable.java +++ b/src/main/java/ch/njol/skript/lang/Debuggable.java @@ -1,11 +1,8 @@ package ch.njol.skript.lang; import org.bukkit.event.Event; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.function.Function; - /** * Represents an element that can print details involving an event. */ @@ -26,82 +23,4 @@ public interface Debuggable { @Override String toString(); - /** - * Formats a string with the given (optional) arguments, for constructing a {@link #toString(Event, boolean)} - * representation. - * - * @param string The string to format. - * @param args The args to replace. - * @return The formatted string. - * @see String#format(String, Object...) - */ - default String formattedToString(@NotNull String string, Object... args) { - return string.formatted(args).replaceAll(" +", " "); - } - - /** - * Represents an optional value in a {@link #toString(Event, boolean)} method. - *
    - *
  • If {@code value} is not null, returns a class which returns the result of the function {@code ifNotNull}. - * This function applies a not-null {@code value} to get a string, which removes null warnings.
  • - *
  • If {@code value} is null, returns a class which returns an empty string.
  • - *
- * - * @param value The potentially null value. - * @param ifNotNull The function to apply if {@code value} is not null. - * @return A class which gives the string representation of the value, or an empty string if it is null. - * @param The type of the value. - */ - default OptionalDebugParameter optional( - @Nullable T value, - @NotNull Function<@NotNull T, String> ifNotNull - ) { - return optional(value, ifNotNull, ""); - } - - /** - * Represents an optional value in a {@link #toString(Event, boolean)} method. - *
    - *
  • If {@code value} is not null, returns a class which returns the result of the function {@code ifNotNull}. - * This function applies a not-null {@code value} to get a string, which removes null warnings.
  • - *
  • If {@code value} is null, returns a class which returns {@code ifNull}.
  • - *
- * - * @param value The potentially null value. - * @param ifNotNull The function to apply if {@code value} is not null. - * @param ifNull The string to return if {@code value} is null. - * @return A class which gives the string representation of the value, or {@code ifNull} if it is null. - * @param The type of the value. - */ - default OptionalDebugParameter optional( - @Nullable T value, - @NotNull Function<@NotNull T, String> ifNotNull, - @NotNull String ifNull - ) { - return new OptionalDebugParameter<>(value, ifNotNull, ifNull); - } - - final class OptionalDebugParameter { - - private final @Nullable T value; - private final @NotNull Function<@NotNull T, String> ifNotNull; - private final @NotNull String ifNull; - - public OptionalDebugParameter( - @Nullable T value, - @NotNull Function<@NotNull T, String> ifNotNull, - @NotNull String ifNull - ) { - this.value = value; - this.ifNotNull = ifNotNull; - this.ifNull = ifNull; - } - - @Override - public String toString() { - return value != null ? ifNotNull.apply(value) : ifNull; - } - - } - } diff --git a/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java b/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java new file mode 100644 index 00000000000..291aac63ff2 --- /dev/null +++ b/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java @@ -0,0 +1,34 @@ +package ch.njol.skript.lang; + +import org.bukkit.event.Event; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.StringJoiner; + +public class SyntaxStringBuilder { + + private final boolean debug; + private final @Nullable Event event; + private final StringJoiner joiner = new StringJoiner(" "); + + public SyntaxStringBuilder(@Nullable Event event, boolean debug) { + this.event = event; + this.debug = debug; + } + + public SyntaxStringBuilder add(@NotNull String string) { + joiner.add(string); + return this; + } + + public SyntaxStringBuilder add(@NotNull Debuggable debuggable) { + joiner.add(debuggable.toString(event, debug)); + return this; + } + + @Override + public String toString() { + return joiner.toString(); + } +} From b591d4a1af26e9b494f9b458602d31039326fcf8 Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Tue, 29 Oct 2024 23:16:51 +0100 Subject: [PATCH 05/11] update debuggable --- .../java/ch/njol/skript/lang/Debuggable.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/njol/skript/lang/Debuggable.java b/src/main/java/ch/njol/skript/lang/Debuggable.java index 642998d2cf9..cff6c039833 100644 --- a/src/main/java/ch/njol/skript/lang/Debuggable.java +++ b/src/main/java/ch/njol/skript/lang/Debuggable.java @@ -1,3 +1,21 @@ +/** + * This file is part of Skript. + * + * Skript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Skript is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Skript. If not, see . + * + * Copyright Peter Güttinger, SkriptLang team and contributors + */ package ch.njol.skript.lang; import org.bukkit.event.Event; @@ -9,11 +27,9 @@ public interface Debuggable { /** - * Returns a string representation of this object. - * * @param event The event to get information from. This is always null if debug == false. * @param debug If true this should print more information, if false this should print what is shown to the end user - * @return A string representation of this object. + * @return String representation of this object. */ String toString(@Nullable Event event, boolean debug); From 8c24f24b5af5fd968d6583fa31d558a297c09dc3 Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Tue, 29 Oct 2024 23:28:41 +0100 Subject: [PATCH 06/11] add docs --- .../java/ch/njol/skript/lang/Debuggable.java | 2 +- .../njol/skript/lang/SyntaxStringBuilder.java | 79 +++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/lang/Debuggable.java b/src/main/java/ch/njol/skript/lang/Debuggable.java index cff6c039833..98714194d32 100644 --- a/src/main/java/ch/njol/skript/lang/Debuggable.java +++ b/src/main/java/ch/njol/skript/lang/Debuggable.java @@ -29,7 +29,7 @@ public interface Debuggable { /** * @param event The event to get information from. This is always null if debug == false. * @param debug If true this should print more information, if false this should print what is shown to the end user - * @return String representation of this object. + * @return String representation of this object */ String toString(@Nullable Event event, boolean debug); diff --git a/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java b/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java index 291aac63ff2..dc8b4566848 100644 --- a/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java +++ b/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java @@ -6,22 +6,101 @@ import java.util.StringJoiner; +/** + * Utility class to build syntax strings, used in {@link Debuggable#toString(Event, boolean)}. + */ public class SyntaxStringBuilder { private final boolean debug; private final @Nullable Event event; private final StringJoiner joiner = new StringJoiner(" "); + /** + * Creates a new SyntaxStringBuilder. + * + * @param event The event to get information from. This is always null if debug == false. + * @param debug If true this should print more information, if false this should print what is shown to the end user + */ public SyntaxStringBuilder(@Nullable Event event, boolean debug) { this.event = event; this.debug = debug; } + /** + * Adds a boolean to the string. + * @param b The boolean to add. + * @return This SyntaxStringBuilder. + */ + public SyntaxStringBuilder add(boolean b) { + joiner.add(Boolean.toString(b)); + return this; + } + + /** + * Adds an integer to the string. + * @param i The integer to add. + * @return This SyntaxStringBuilder. + */ + public SyntaxStringBuilder add(int i) { + joiner.add(Integer.toString(i)); + return this; + } + + /** + * Adds a long to the string. + * @param l The long to add. + * @return This SyntaxStringBuilder. + */ + public SyntaxStringBuilder add(long l) { + joiner.add(Long.toString(l)); + return this; + } + + /** + * Adds a double to the string. + * @param d The double to add. + * @return This SyntaxStringBuilder. + */ + public SyntaxStringBuilder add(double d) { + joiner.add(Double.toString(d)); + return this; + } + + /** + * Adds a float to the string. + * @param f The float to add. + * @return This SyntaxStringBuilder. + */ + public SyntaxStringBuilder add(float f) { + joiner.add(Float.toString(f)); + return this; + } + + /** + * Adds a char to the string. + * @param c The char to add. + * @return This SyntaxStringBuilder. + */ + public SyntaxStringBuilder add(char c) { + joiner.add(Character.toString(c)); + return this; + } + + /** + * Adds a string to the string. + * @param string The string to add. + * @return This SyntaxStringBuilder. + */ public SyntaxStringBuilder add(@NotNull String string) { joiner.add(string); return this; } + /** + * Adds a {@link Debuggable} object to the string, which is usually an expression or literal. + * @param debuggable The {@link Debuggable} to add. + * @return This SyntaxStringBuilder. + */ public SyntaxStringBuilder add(@NotNull Debuggable debuggable) { joiner.add(debuggable.toString(event, debug)); return this; From 4c89bb2e085d83df89086a62b2b8ef9804c6794b Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Tue, 29 Oct 2024 23:35:30 +0100 Subject: [PATCH 07/11] add null check --- src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java b/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java index dc8b4566848..5e29d1dbf7b 100644 --- a/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java +++ b/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java @@ -1,5 +1,6 @@ package ch.njol.skript.lang; +import com.google.common.base.Preconditions; import org.bukkit.event.Event; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -92,6 +93,7 @@ public SyntaxStringBuilder add(char c) { * @return This SyntaxStringBuilder. */ public SyntaxStringBuilder add(@NotNull String string) { + Preconditions.checkNotNull(string); joiner.add(string); return this; } @@ -102,6 +104,7 @@ public SyntaxStringBuilder add(@NotNull String string) { * @return This SyntaxStringBuilder. */ public SyntaxStringBuilder add(@NotNull Debuggable debuggable) { + Preconditions.checkNotNull(debuggable); joiner.add(debuggable.toString(event, debug)); return this; } @@ -110,4 +113,5 @@ public SyntaxStringBuilder add(@NotNull Debuggable debuggable) { public String toString() { return joiner.toString(); } + } From d4bb59d22bb6b8ad3ddca9e43623e81ae56567a6 Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Wed, 30 Oct 2024 00:57:59 +0100 Subject: [PATCH 08/11] change to append, add general for objects --- .../java/ch/njol/skript/effects/EffBan.java | 12 ++-- .../ch/njol/skript/effects/EffReplace.java | 16 ++--- .../njol/skript/lang/SyntaxStringBuilder.java | 72 ++----------------- 3 files changed, 20 insertions(+), 80 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffBan.java b/src/main/java/ch/njol/skript/effects/EffBan.java index 0b752e2711e..5d503d22b40 100644 --- a/src/main/java/ch/njol/skript/effects/EffBan.java +++ b/src/main/java/ch/njol/skript/effects/EffBan.java @@ -144,15 +144,15 @@ public String toString(@Nullable Event event, boolean debug) { SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug); if (ipBan) - builder.add("IP"); - builder.add(ban ? "ban" : "unban"); + builder.append("IP"); + builder.append(ban ? "ban" : "unban"); if (kick) - builder.add("and kick"); - builder.add(players); + builder.append("and kick"); + builder.append(players); if (reason != null) - builder.add("on account of").add(reason); + builder.append("on account of").append(reason); if (expires != null) - builder.add("for").add(expires); + builder.append("for").append(expires); return builder.toString(); } diff --git a/src/main/java/ch/njol/skript/effects/EffReplace.java b/src/main/java/ch/njol/skript/effects/EffReplace.java index 6980d8cc5ee..8761190de84 100644 --- a/src/main/java/ch/njol/skript/effects/EffReplace.java +++ b/src/main/java/ch/njol/skript/effects/EffReplace.java @@ -143,16 +143,16 @@ private void replace(Event event, Object[] needles, Expression haystackExpr) public String toString(@Nullable Event event, boolean debug) { SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug); - builder.add("replace"); + builder.append("replace"); if (replaceFirst) - builder.add("the first"); - builder.add(needles); - builder.add("in"); - builder.add(haystack); - builder.add("with"); - builder.add(replacement); + builder.append("the first"); + builder.append(needles); + builder.append("in"); + builder.append(haystack); + builder.append("with"); + builder.append(replacement); if (caseSensitive) - builder.add("with case sensitivity"); + builder.append("with case sensitivity"); return builder.toString(); } diff --git a/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java b/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java index 5e29d1dbf7b..9502c5433ed 100644 --- a/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java +++ b/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java @@ -28,73 +28,13 @@ public SyntaxStringBuilder(@Nullable Event event, boolean debug) { } /** - * Adds a boolean to the string. - * @param b The boolean to add. + * Adds an object to the string. + * @param object The object to add. * @return This SyntaxStringBuilder. */ - public SyntaxStringBuilder add(boolean b) { - joiner.add(Boolean.toString(b)); - return this; - } - - /** - * Adds an integer to the string. - * @param i The integer to add. - * @return This SyntaxStringBuilder. - */ - public SyntaxStringBuilder add(int i) { - joiner.add(Integer.toString(i)); - return this; - } - - /** - * Adds a long to the string. - * @param l The long to add. - * @return This SyntaxStringBuilder. - */ - public SyntaxStringBuilder add(long l) { - joiner.add(Long.toString(l)); - return this; - } - - /** - * Adds a double to the string. - * @param d The double to add. - * @return This SyntaxStringBuilder. - */ - public SyntaxStringBuilder add(double d) { - joiner.add(Double.toString(d)); - return this; - } - - /** - * Adds a float to the string. - * @param f The float to add. - * @return This SyntaxStringBuilder. - */ - public SyntaxStringBuilder add(float f) { - joiner.add(Float.toString(f)); - return this; - } - - /** - * Adds a char to the string. - * @param c The char to add. - * @return This SyntaxStringBuilder. - */ - public SyntaxStringBuilder add(char c) { - joiner.add(Character.toString(c)); - return this; - } - - /** - * Adds a string to the string. - * @param string The string to add. - * @return This SyntaxStringBuilder. - */ - public SyntaxStringBuilder add(@NotNull String string) { - Preconditions.checkNotNull(string); - joiner.add(string); + public SyntaxStringBuilder append(Object object) { + Preconditions.checkNotNull(object); + joiner.add(object.toString()); return this; } @@ -103,7 +43,7 @@ public SyntaxStringBuilder add(@NotNull String string) { * @param debuggable The {@link Debuggable} to add. * @return This SyntaxStringBuilder. */ - public SyntaxStringBuilder add(@NotNull Debuggable debuggable) { + public SyntaxStringBuilder append(@NotNull Debuggable debuggable) { Preconditions.checkNotNull(debuggable); joiner.add(debuggable.toString(event, debug)); return this; From 969f8093c6b120b95fe17f37199faef530b71eb5 Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Wed, 30 Oct 2024 01:17:26 +0100 Subject: [PATCH 09/11] add multiple objects to chain --- .../java/ch/njol/skript/effects/EffBan.java | 23 +++++++++---------- .../ch/njol/skript/effects/EffReplace.java | 6 +---- .../njol/skript/lang/SyntaxStringBuilder.java | 21 +++++++++++------ 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/main/java/ch/njol/skript/effects/EffBan.java b/src/main/java/ch/njol/skript/effects/EffBan.java index 5d503d22b40..ee5d39cab9e 100644 --- a/src/main/java/ch/njol/skript/effects/EffBan.java +++ b/src/main/java/ch/njol/skript/effects/EffBan.java @@ -18,16 +18,6 @@ */ package ch.njol.skript.effects; -import java.net.InetSocketAddress; -import java.util.Date; - -import ch.njol.skript.lang.SyntaxStringBuilder; -import org.bukkit.BanList; -import org.bukkit.Bukkit; -import org.bukkit.OfflinePlayer; -import org.bukkit.entity.Player; -import org.bukkit.event.Event; - import ch.njol.skript.Skript; import ch.njol.skript.doc.Description; import ch.njol.skript.doc.Examples; @@ -36,10 +26,19 @@ import ch.njol.skript.lang.Effect; import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.skript.lang.SyntaxStringBuilder; import ch.njol.skript.util.Timespan; import ch.njol.util.Kleenean; +import org.bukkit.BanList; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; +import org.bukkit.event.Event; import org.jetbrains.annotations.Nullable; +import java.net.InetSocketAddress; +import java.util.Date; + @Name("Ban") @Description({"Bans or unbans a player or an IP address.", "If a reason is given, it will be shown to the player when they try to join the server while banned.", @@ -150,9 +149,9 @@ public String toString(@Nullable Event event, boolean debug) { builder.append("and kick"); builder.append(players); if (reason != null) - builder.append("on account of").append(reason); + builder.append("on account of", reason); if (expires != null) - builder.append("for").append(expires); + builder.append("for", expires); return builder.toString(); } diff --git a/src/main/java/ch/njol/skript/effects/EffReplace.java b/src/main/java/ch/njol/skript/effects/EffReplace.java index 8761190de84..ef2faf44282 100644 --- a/src/main/java/ch/njol/skript/effects/EffReplace.java +++ b/src/main/java/ch/njol/skript/effects/EffReplace.java @@ -146,11 +146,7 @@ public String toString(@Nullable Event event, boolean debug) { builder.append("replace"); if (replaceFirst) builder.append("the first"); - builder.append(needles); - builder.append("in"); - builder.append(haystack); - builder.append("with"); - builder.append(replacement); + builder.append(needles, "in", haystack, "with", replacement); if (caseSensitive) builder.append("with case sensitivity"); diff --git a/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java b/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java index 9502c5433ed..9aac8ddf11a 100644 --- a/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java +++ b/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java @@ -8,7 +8,8 @@ import java.util.StringJoiner; /** - * Utility class to build syntax strings, used in {@link Debuggable#toString(Event, boolean)}. + * Utility class to build syntax strings, primarily intended for use + * in {@link Debuggable#toString(Event, boolean)} implementations. */ public class SyntaxStringBuilder { @@ -30,23 +31,29 @@ public SyntaxStringBuilder(@Nullable Event event, boolean debug) { /** * Adds an object to the string. * @param object The object to add. - * @return This SyntaxStringBuilder. */ - public SyntaxStringBuilder append(Object object) { + public void append(Object object) { Preconditions.checkNotNull(object); joiner.add(object.toString()); - return this; } /** * Adds a {@link Debuggable} object to the string, which is usually an expression or literal. * @param debuggable The {@link Debuggable} to add. - * @return This SyntaxStringBuilder. */ - public SyntaxStringBuilder append(@NotNull Debuggable debuggable) { + public void append(@NotNull Debuggable debuggable) { Preconditions.checkNotNull(debuggable); joiner.add(debuggable.toString(event, debug)); - return this; + } + + /** + * Adds multiple objects to the string. + * @param objects The objects to add. + */ + public void append(@NotNull Object... objects) { + for (Object object : objects) { + append(object); + } } @Override From eabfc38345ee244c1400abf0c66439efa2cd03e9 Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Wed, 30 Oct 2024 15:42:55 +0100 Subject: [PATCH 10/11] oopsie --- .../njol/skript/lang/SyntaxStringBuilder.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java b/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java index 9aac8ddf11a..8b9df72ef97 100644 --- a/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java +++ b/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java @@ -30,20 +30,17 @@ public SyntaxStringBuilder(@Nullable Event event, boolean debug) { /** * Adds an object to the string. + * If the object is a {@link Debuggable} it will be formatted using {@link Debuggable#toString(Event, boolean)}. + * * @param object The object to add. */ - public void append(Object object) { + public void append(@NotNull Object object) { Preconditions.checkNotNull(object); - joiner.add(object.toString()); - } - - /** - * Adds a {@link Debuggable} object to the string, which is usually an expression or literal. - * @param debuggable The {@link Debuggable} to add. - */ - public void append(@NotNull Debuggable debuggable) { - Preconditions.checkNotNull(debuggable); - joiner.add(debuggable.toString(event, debug)); + if (object instanceof Debuggable debuggable) { + joiner.add(debuggable.toString(event, debug)); + } else { + joiner.add(object.toString()); + } } /** From e6c5ac2e8a27c1cab2428f4097c10371218d8373 Mon Sep 17 00:00:00 2001 From: Efnilite <35348263+Efnilite@users.noreply.github.com> Date: Sat, 9 Nov 2024 00:41:03 +0100 Subject: [PATCH 11/11] add space notice --- src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java b/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java index 8b9df72ef97..b9ce40d7b19 100644 --- a/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java +++ b/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java @@ -10,6 +10,7 @@ /** * Utility class to build syntax strings, primarily intended for use * in {@link Debuggable#toString(Event, boolean)} implementations. + * Spaces are automatically added between the provided objects. */ public class SyntaxStringBuilder { @@ -30,7 +31,9 @@ public SyntaxStringBuilder(@Nullable Event event, boolean debug) { /** * Adds an object to the string. - * If the object is a {@link Debuggable} it will be formatted using {@link Debuggable#toString(Event, boolean)}. + * 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 object The object to add. */ @@ -45,6 +48,7 @@ public void append(@NotNull Object object) { /** * Adds multiple objects to the string. + * Spaces are automatically added between the provided objects. * @param objects The objects to add. */ public void append(@NotNull Object... objects) {