// SPDX-FileCopyrightText: 2019 The CC: Tweaked Developers // // SPDX-License-Identifier: MPL-2.0 package dan200.computercraft.shared.command.arguments; import com.google.gson.JsonObject; 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.exceptions.SimpleCommandExceptionType; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; import net.minecraft.commands.CommandBuildContext; import net.minecraft.commands.synchronization.ArgumentTypeInfo; import net.minecraft.commands.synchronization.ArgumentTypeInfos; import net.minecraft.core.RegistryAccess; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.ComponentSerialization; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.function.BiConsumer; /** * Reads one argument multiple times. *
* Note that this must be the last element in an argument chain: in order to improve the quality of error messages, * we will always try to consume another argument while there is input remaining. *
* One problem with how parsers function, is that they must consume some input: and thus we
*
* @param > {
private final ArgumentType child;
private final BiConsumer
, U> appender;
private final boolean flatten;
private final SimpleCommandExceptionType some;
private RepeatArgumentType(ArgumentType child, BiConsumer
, U> appender, boolean flatten, SimpleCommandExceptionType some) {
this.child = child;
this.appender = appender;
this.flatten = flatten;
this.some = some;
}
public static
> appender, SimpleCommandExceptionType missing) {
return new RepeatArgumentType<>(appender, List::addAll, true, missing);
}
@Override
public List
CompletableFuture context, SuggestionsBuilder builder) {
var reader = new StringReader(builder.getInput());
reader.setCursor(builder.getStart());
var previous = reader.getCursor();
while (reader.canRead()) {
try {
child.parse(reader);
} catch (CommandSyntaxException e) {
break;
}
var cursor = reader.getCursor();
reader.skipWhitespace();
if (cursor == reader.getCursor()) break;
previous = reader.getCursor();
}
reader.setCursor(previous);
return child.listSuggestions(context, builder.createOffset(previous));
}
@Override
public Collection