Add check for resource location

This commit is contained in:
Noel Németh 2022-07-08 23:40:41 +02:00
parent 89d2f1b54c
commit 7d247003ca
1 changed files with 10 additions and 2 deletions

View File

@ -4,16 +4,24 @@ import net.minestom.server.command.CommandReader;
import net.minestom.server.command.builder.arguments.Argument;
import org.jetbrains.annotations.NotNull;
import java.util.regex.Pattern;
public class ArgumentResourceLocation extends Argument<String> {
public static final Pattern RESOURCE_REGEX = Pattern.compile("([a-z\\d_\\-.]+:)?[a-z\\d_\\-./]+");
public ArgumentResourceLocation(@NotNull String id) {
super(id);
}
@Override
public @NotNull Result<String> parse(CommandReader reader) {
//todo shouldn't this have some syntax checks?
return Result.success(reader.readWord());
final String word = reader.readWord();
if (RESOURCE_REGEX.matcher(word).matches()) {
return Result.success(word);
} else {
return Result.syntaxError("Invalid resource location format", word, -1);
}
}
@Override