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
54 changes: 31 additions & 23 deletions src/main/java/ch/njol/skript/effects/EffBan.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.",
Expand All @@ -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%]",
Expand All @@ -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<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 +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) {
Expand Down Expand Up @@ -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();
}

}
16 changes: 11 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,16 @@ 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.append("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.append("the first");
builder.append(needles, "in", haystack, "with", replacement);
if (caseSensitive)
builder.append("with case sensitivity");

return builder.toString();
}

}
65 changes: 65 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,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);
Efnilite marked this conversation as resolved.
Show resolved Hide resolved
}
}

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

}