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

Use StringArgumentType#string() for ServerCommand server argument #1363

Open
wants to merge 1 commit into
base: dev/3.0.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.velocitypowered.api.proxy.server.ServerInfo;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.TranslatableComponent;
Expand All @@ -44,6 +45,8 @@
public final class ServerCommand {
private static final String SERVER_ARG = "server";
public static final int MAX_SERVERS_TO_LIST = 50;
// Characters allowed in an unquoted String, as defined by `StringReader#isAllowedInUnquotedString`
private static final Pattern VALID_UNQUOTED = Pattern.compile("[0-9A-Za-z_\\-.+]+");

@SuppressWarnings("checkstyle:MissingJavadocMethod")
public static BrigadierCommand create(final ProxyServer server) {
Expand All @@ -56,14 +59,30 @@ public static BrigadierCommand create(final ProxyServer server) {
outputServerInformation(player, server);
return Command.SINGLE_SUCCESS;
})
.then(BrigadierCommand.requiredArgumentBuilder(SERVER_ARG, StringArgumentType.word())
.then(BrigadierCommand.requiredArgumentBuilder(SERVER_ARG, StringArgumentType.string())
.suggests((ctx, builder) -> {
final String argument = ctx.getArguments().containsKey(SERVER_ARG)
? StringArgumentType.getString(ctx, SERVER_ARG)
: "";
String remaining = builder.getRemaining();

boolean forceQuoted = false;
boolean forceUnquoted = false;
if (!remaining.isEmpty()) {
if (remaining.charAt(0) == '"') {
forceQuoted = true;
remaining = remaining.substring(1);
}
forceUnquoted = !forceQuoted;
}

for (final RegisteredServer sv : server.getAllServers()) {
final String serverName = sv.getServerInfo().getName();
if (serverName.regionMatches(true, 0, argument, 0, argument.length())) {
if (!serverName.regionMatches(true, 0, remaining, 0, remaining.length())) {
continue;
}

boolean needsQuotes = !VALID_UNQUOTED.matcher(serverName).matches();
if (forceQuoted || (!forceUnquoted && needsQuotes)) {
builder.suggest('"' + serverName + '"');
} else if (!needsQuotes) {
builder.suggest(serverName);
}
}
Expand Down