Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utility class for easier building of toString #7168

Merged
merged 17 commits into from
Nov 11, 2024
Merged
37 changes: 23 additions & 14 deletions src/main/java/ch/njol/skript/effects/EffBan.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,7 +55,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%]",
Expand All @@ -64,18 +65,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<String> reason;
@Nullable
private Expression<Timespan> 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) {
Expand All @@ -87,7 +88,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) {
Expand Down Expand Up @@ -137,15 +138,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.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();
}

}
20 changes: 15 additions & 5 deletions src/main/java/ch/njol/skript/effects/EffReplace.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -140,11 +141,20 @@ private void replace(Event event, Object[] needles, Expression<?> haystackExpr)

@Override
public String toString(@Nullable Event event, boolean debug) {
SyntaxStringBuilder builder = new SyntaxStringBuilder(event, debug);

builder.add("replace");
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 + ")";
builder.add("the first");
builder.add(needles);
builder.add("in");
builder.add(haystack);
builder.add("with");
builder.add(replacement);
Efnilite marked this conversation as resolved.
Show resolved Hide resolved
if (caseSensitive)
builder.add("with case sensitivity");

return builder.toString();
}

}
117 changes: 117 additions & 0 deletions src/main/java/ch/njol/skript/lang/SyntaxStringBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
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, 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) {
Efnilite marked this conversation as resolved.
Show resolved Hide resolved
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);
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) {
Preconditions.checkNotNull(debuggable);
joiner.add(debuggable.toString(event, debug));
return this;
}

@Override
public String toString() {
return joiner.toString();
}

}