Skip to content

Commit

Permalink
Resolve #310
Browse files Browse the repository at this point in the history
  • Loading branch information
willkroboth committed Sep 2, 2024
1 parent c02421f commit 4844a0e
Showing 1 changed file with 12 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -641,31 +641,12 @@ public static <Source> Map<String, LiteralCommandNode<Source>> getCommandNodeLit
public static <CommandSource> String getRawArgumentInput(CommandContext<CommandSource> cmdCtx, String key) {
final ParsedArgument<?, ?> parsedArgument = commandContextArguments.get(cmdCtx).get(key);

// TODO: Issue #310: Parsing this argument via /execute run <blah> doesn't have the value in
// the arguments for this command context (most likely because it's a redirected command).
// We need to figure out how to handle this case.

// TODO: What is this talking about? https://github.com/JorelAli/CommandAPI/issues/310

// TODO: Oh, I might have figured out what's wrong
// https://github.com/Mojang/brigadier/blob/master/src/main/java/com/mojang/brigadier/CommandDispatcher.java#L239
// Redirects work by adding children onto a context builder
// Seen in that line, the source of the command is copied onto the context, but the arguments are not
// The child context is the one used to run the commands, so the argument doesn't exist when the command is being run
// This is currently also affecting MultiLiteralArguments since they use redirects now
// I feel like this is a bug in Brigadier, but maybe there is a reason for this?
// I hope there is at least a work around
// https://github.com/Mojang/brigadier/issues/137
if (parsedArgument != null) {
// Sanity check: See https://github.com/JorelAli/CommandAPI/wiki/Implementation-details#chatcomponentargument-raw-arguments
StringRange range = parsedArgument.getRange();
if (range.getEnd() > cmdCtx.getInput().length()) {
range = StringRange.between(range.getStart(), cmdCtx.getInput().length());
}
return range.get(cmdCtx.getInput());
} else {
return "";
// Sanity check: See https://github.com/JorelAli/CommandAPI/wiki/Implementation-details#chatcomponentargument-raw-arguments
StringRange range = parsedArgument.getRange();
if (range.getEnd() > cmdCtx.getInput().length()) {
range = StringRange.between(range.getStart(), cmdCtx.getInput().length());
}
return range.get(cmdCtx.getInput());
}

/**
Expand All @@ -677,6 +658,13 @@ public static <CommandSource> String getRawArgumentInput(CommandContext<CommandS
* @throws CommandSyntaxException If an argument is improperly formatted and cannot be parsed
*/
public CommandArguments argsToCommandArgs(CommandContext<Source> cmdCtx, List<Argument> args) throws CommandSyntaxException {
// https://github.com/JorelAli/CommandAPI/issues/310 & https://github.com/Mojang/brigadier/issues/137
// If a command goes through a redirect, Brigadier puts all context after the redirect into the child
// CommandContext. When executing a command, Brigadier will pass us the child context. However, when
// suggesting a command, Brigadier passes us the root context for some reason. Assuming the behavior
// when executing a command is correct, when suggesting commands we should use the last child.
cmdCtx = cmdCtx.getLastChild();

// Array for arguments for executor
List<Object> argList = new ArrayList<>();

Expand Down

0 comments on commit 4844a0e

Please sign in to comment.