diff --git a/src/main/java/ch/njol/skript/effects/EffBan.java b/src/main/java/ch/njol/skript/effects/EffBan.java index 88f815c96df..ee5d39cab9e 100644 --- a/src/main/java/ch/njol/skript/effects/EffBan.java +++ b/src/main/java/ch/njol/skript/effects/EffBan.java @@ -18,15 +18,6 @@ */ package ch.njol.skript.effects; -import java.net.InetSocketAddress; -import java.util.Date; - -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; @@ -35,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.", @@ -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,23 @@ 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) { + SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug); + + if (ipBan) + builder.append("IP"); + builder.append(ban ? "ban" : "unban"); + if (kick) + builder.append("and kick"); + builder.append(players); + if (reason != null) + builder.append("on account of", reason); + if (expires != null) + 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 2c29668effb..86193954e25 100644 --- a/src/main/java/ch/njol/skript/effects/EffReplace.java +++ b/src/main/java/ch/njol/skript/effects/EffReplace.java @@ -13,6 +13,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; @@ -168,10 +169,16 @@ private void replace(Event event, Object[] needles, Expression haystackExpr) @Override public String toString(@Nullable Event event, boolean debug) { - return "replace " + (replaceFirst ? "the first " : "") + (replaceRegex ? "regex " : "") + needles.toString(event, debug) + - " in " + haystack.toString(event, debug) + - " with " + replacement.toString(event, debug) + - (caseSensitive ? " (case sensitive)" : ""); + SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug); + + builder.append("replace"); + if (replaceFirst) + builder.append("the first"); + builder.append(needles, "in", haystack, "with", replacement); + if (caseSensitive) + 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 new file mode 100644 index 00000000000..b9ce40d7b19 --- /dev/null +++ b/src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java @@ -0,0 +1,65 @@ +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; + +import java.util.StringJoiner; + +/** + * 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 { + + 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 an object to the string. + * 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. + */ + public void append(@NotNull Object object) { + Preconditions.checkNotNull(object); + if (object instanceof Debuggable debuggable) { + joiner.add(debuggable.toString(event, debug)); + } else { + joiner.add(object.toString()); + } + } + + /** + * 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) { + for (Object object : objects) { + append(object); + } + } + + @Override + public String toString() { + return joiner.toString(); + } + +}