net.minestom.server.command.builder.arguments.ArgumentString Maven / Gradle / Ivy
Show all versions of minestom-snapshots Show documentation
package net.minestom.server.command.builder.arguments;
import net.minestom.server.command.CommandSender;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import net.minestom.server.network.NetworkBuffer;
import net.minestom.server.utils.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Argument which will take a quoted string.
*
* Example: "Hey I am a string"
*/
public class ArgumentString extends Argument {
private static final char BACKSLASH = '\\';
private static final char DOUBLE_QUOTE = '"';
private static final char QUOTE = '\'';
public static final int QUOTE_ERROR = 1;
public ArgumentString(String id) {
super(id, true);
}
@NotNull
@Override
public String parse(@NotNull CommandSender sender, @NotNull String input) throws ArgumentSyntaxException {
return staticParse(input);
}
@Override
public String parser() {
return "brigadier:string";
}
@Override
public byte @Nullable [] nodeProperties() {
return NetworkBuffer.makeArray(NetworkBuffer.VAR_INT, 1); // Quotable phrase
}
/**
* @deprecated use {@link Argument#parse(CommandSender, Argument)}
*/
@Deprecated
public static String staticParse(@NotNull String input) throws ArgumentSyntaxException {
// Return if not quoted
if (!input.contains(String.valueOf(DOUBLE_QUOTE)) &&
!input.contains(String.valueOf(QUOTE)) &&
!input.contains(StringUtils.SPACE)) {
return input;
}
// Check if value start and end with quote
final char first = input.charAt(0);
final char last = input.charAt(input.length() - 1);
final boolean quote = input.length() >= 2 &&
first == last && (first == DOUBLE_QUOTE || first == QUOTE);
if (!quote)
throw new ArgumentSyntaxException("String argument needs to start and end with quotes", input, QUOTE_ERROR);
// Remove first and last characters (quotes)
input = input.substring(1, input.length() - 1);
// Verify backslashes
for (int i = 1; i < input.length(); i++) {
final char c = input.charAt(i);
if (c == first) {
final char lastChar = input.charAt(i - 1);
if (lastChar != BACKSLASH) {
throw new ArgumentSyntaxException("Non-escaped quote", input, QUOTE_ERROR);
}
}
}
return StringUtils.unescapeJavaString(input);
}
@Override
public String toString() {
return String.format("String<%s>", getId());
}
}