2025-11-24 22:52:51 +03:00

72 lines
2.9 KiB
Java

/*
* Decompiled with CFR 0.152.
*
* Could not load the following classes:
* com.mojang.brigadier.ParseResults
* com.mojang.brigadier.context.CommandContextBuilder
* com.mojang.brigadier.context.ParsedArgument
* com.mojang.brigadier.context.ParsedCommandNode
* com.mojang.brigadier.tree.ArgumentCommandNode
* com.mojang.brigadier.tree.CommandNode
* org.jspecify.annotations.Nullable
*/
package net.minecraft.network.chat;
import com.mojang.brigadier.ParseResults;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.context.ParsedArgument;
import com.mojang.brigadier.context.ParsedCommandNode;
import com.mojang.brigadier.tree.ArgumentCommandNode;
import com.mojang.brigadier.tree.CommandNode;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.commands.arguments.SignedArgument;
import org.jspecify.annotations.Nullable;
public record SignableCommand<S>(List<Argument<S>> arguments) {
public static <S> boolean hasSignableArguments(ParseResults<S> command) {
return !SignableCommand.of(command).arguments().isEmpty();
}
public static <S> SignableCommand<S> of(ParseResults<S> command) {
CommandContextBuilder child;
CommandContextBuilder rootContext;
String commandString = command.getReader().getString();
CommandContextBuilder context = rootContext = command.getContext();
List<Argument<S>> arguments = SignableCommand.collectArguments(commandString, context);
while ((child = context.getChild()) != null && child.getRootNode() != rootContext.getRootNode()) {
arguments.addAll(SignableCommand.collectArguments(commandString, child));
context = child;
}
return new SignableCommand<S>(arguments);
}
private static <S> List<Argument<S>> collectArguments(String commandString, CommandContextBuilder<S> context) {
ArrayList<Argument<S>> arguments = new ArrayList<Argument<S>>();
for (ParsedCommandNode node : context.getNodes()) {
ParsedArgument parsed;
ArgumentCommandNode argument;
CommandNode commandNode = node.getNode();
if (!(commandNode instanceof ArgumentCommandNode) || !((argument = (ArgumentCommandNode)commandNode).getType() instanceof SignedArgument) || (parsed = (ParsedArgument)context.getArguments().get(argument.getName())) == null) continue;
String value = parsed.getRange().get(commandString);
arguments.add(new Argument(argument, value));
}
return arguments;
}
public @Nullable Argument<S> getArgument(String name) {
for (Argument<S> argument : this.arguments) {
if (!name.equals(argument.name())) continue;
return argument;
}
return null;
}
public record Argument<S>(ArgumentCommandNode<S, ?> node, String value) {
public String name() {
return this.node.getName();
}
}
}