feat: add GreedyArgumentType in command api

This commit is contained in:
cattyn
2026-04-20 00:51:45 +03:00
parent c44bb88faa
commit fcb7dc420f
5 changed files with 44 additions and 4 deletions
@@ -17,7 +17,7 @@ import java.util.concurrent.CompletableFuture;
import static me.alpha432.oyvey.features.commands.ArgumentSuggestions.suggest;
public class ColorArgumentType implements ArgumentType<Color> {
public final class ColorArgumentType implements ArgumentType<Color> {
private static final List<String> EXAMPLES = List.of("hsb:55,100,100", "255,0,255", "0,255,255,80");
private static final String[] RGBA_VALUES = {"Red", "Green", "Blue", "Alpha"};
@@ -15,7 +15,7 @@ import java.util.concurrent.CompletableFuture;
import static me.alpha432.oyvey.features.commands.ArgumentSuggestions.suggest;
public class CommandArgumentType implements ArgumentType<Command>, CommandExceptionType {
public final class CommandArgumentType implements ArgumentType<Command>, CommandExceptionType {
@Override
public Command parse(StringReader reader) throws CommandSyntaxException {
String value = reader.readString().toLowerCase();
@@ -14,7 +14,7 @@ import java.util.concurrent.CompletableFuture;
import static me.alpha432.oyvey.features.commands.ArgumentSuggestions.suggest;
public class EnumArgumentType<T extends Enum<?>> implements ArgumentType<T> {
public final class EnumArgumentType<T extends Enum<?>> implements ArgumentType<T> {
private final Map<String, T> enumNameMap = new HashMap<>();
private final T defaultValue;
@@ -0,0 +1,40 @@
package me.alpha432.oyvey.features.commands.argument;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import java.lang.reflect.Array;
import java.util.concurrent.CompletableFuture;
public record GreedyArgumentType<T>(ArgumentType<T> parent, Class<T> type) implements ArgumentType<T[]> {
@Override
@SuppressWarnings("unchecked")
public T[] parse(StringReader reader) throws CommandSyntaxException {
String input = reader.getRemaining();
reader.setCursor(reader.getTotalLength());
String[] split = input.split(" ");
T[] values = (T[]) Array.newInstance(type, split.length);
for (int i = 0; i < split.length; i++) {
values[i] = parent.parse(new StringReader(split[i]));
}
return values;
}
@Override public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
String[] s = builder.getRemaining().split(" ", -1);
if (s.length == 0) {
return parent.listSuggestions(context, builder);
}
int st = builder.getStart() + builder.getRemaining().length() - s[s.length - 1].length();
return parent.listSuggestions(context, new SuggestionsBuilder(builder.getInput(), st));
}
public static <T> GreedyArgumentType<T> greedy(ArgumentType<T> parent, Class<T> type) {
return new GreedyArgumentType<>(parent, type);
}
}
@@ -16,7 +16,7 @@ import java.util.concurrent.CompletableFuture;
import static me.alpha432.oyvey.features.commands.ArgumentSuggestions.suggest;
public class KeybindArgumentType implements ArgumentType<Bind> {
public final class KeybindArgumentType implements ArgumentType<Bind> {
private static final List<String> EXAMPLES = List.of("A", "RIGHT_SHIFT", "MOUSE0");
@Override