Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the option to retrieve raw arguments #459

Merged
merged 32 commits into from Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
3c7aa7f
Add the option to retrieve raw arguments
DerEchtePilz Jun 8, 2023
4baa0a4
Fix SuggestionsBranch.java. Change String[]::new to new String[0]
DerEchtePilz Jun 8, 2023
b186494
Fix CommandArgument.java
DerEchtePilz Jun 8, 2023
b50afe8
Merge dev/dev into dev/raw-argument-input
DerEchtePilz Jun 15, 2023
8981a2a
Add tests for retrieving raw arguments
DerEchtePilz Jun 15, 2023
6cf237d
Move raw argument creation into CommandAPIHandler
DerEchtePilz Jun 15, 2023
c24d084
Fix raw argument tests failing when using the ChatComponentArgument o…
JorelAli Jun 15, 2023
46deb77
Add empty documentation page
DerEchtePilz Jun 17, 2023
bafaacf
Add documentation (1)
DerEchtePilz Jun 23, 2023
ff7fcc1
Add documentation (2)
DerEchtePilz Jun 27, 2023
a0cfc2d
Modify CommandArguments to remove deprecations and add getOrDefault m…
DerEchtePilz Jun 28, 2023
eb431b0
Address some code review
DerEchtePilz Jun 30, 2023
9fe5879
Fix markdownlint issues
DerEchtePilz Jun 30, 2023
d0b59d3
Make sure commandarguments.md lists every method
DerEchtePilz Jun 30, 2023
3af41ab
Link CommandArguments references to commandarguments.md
DerEchtePilz Jun 30, 2023
f67a012
Merge branch 'dev/dev' into dev/raw-argument-input
DerEchtePilz Jun 30, 2023
5643f0d
Update intro.md, optional_arguments.md and upgrading.md
DerEchtePilz Jul 1, 2023
6ebf8e1
Remove 'What terms are used' section, refactor 'Access the inner stru…
DerEchtePilz Jul 3, 2023
e2fd959
Improve explanation for unsafe arguments
DerEchtePilz Jul 4, 2023
a3db5ea
Add an explanation of raw arguments to the JavaDocs for methods deali…
DerEchtePilz Jul 4, 2023
c8c1de6
Remove unnecessary sentence from commandarguments.md
DerEchtePilz Jul 4, 2023
7ff5cf6
Convert CommandArguments.java into a record
DerEchtePilz Jul 4, 2023
c5916bb
Replace usage of CommandArguments#getFullInput in CommandAPIExecutor …
DerEchtePilz Jul 4, 2023
b1104a3
Make Java examples use the node name to access arguments
DerEchtePilz Jul 5, 2023
3f096b8
Make Kotlin examples use the node name to access arguments
DerEchtePilz Jul 5, 2023
cae4298
Make Kotlin DSL examples use the node name to access arguments
DerEchtePilz Jul 5, 2023
ec80dc5
Fix Bukkit Core dependencies
DerEchtePilz Jul 5, 2023
d05c9eb
Add documentation examples
DerEchtePilz Jul 5, 2023
0641607
Update global changelog
DerEchtePilz Jul 5, 2023
4753950
Merge branch 'dev/dev' into dev/raw-argument-input
DerEchtePilz Jul 5, 2023
897739d
Add JavaDocs to record parameters
DerEchtePilz Jul 12, 2023
ad1621d
Merge branch 'dev/raw-argument-input' of https://github.com/JorelAli/…
DerEchtePilz Jul 12, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -375,6 +375,7 @@ This is the current roadmap for the CommandAPI (as of 11th May 2023):
<td valign="top">
<ul>
<li>Fixed the CommandAPI disabling datapacks on Paper 1.20.1 #40+ because it thought it was running on a Folia server</li>
<li>https://github.com/JorelAli/CommandAPI/pull/459 Added the ability to access raw arguments in the command executor</li>
<li>https://github.com/JorelAli/CommandAPI/issues/469 Adds <code>AdventureChatColorArgument</code></li>
</ul>
</td>
Expand Down
Expand Up @@ -83,7 +83,7 @@ public int execute(ExecutionInfo<CommandSender, WrapperType> info) throws Comman
} catch (WrapperCommandSyntaxException e) {
throw e.getException();
} catch (Throwable ex) {
CommandAPI.getLogger().severe("Unhandled exception executing '" + info.args().getFullInput() + "'", ex);
CommandAPI.getLogger().severe("Unhandled exception executing '" + info.args().fullInput() + "'", ex);
if (ex instanceof Exception) {
throw ex;
} else {
Expand All @@ -97,7 +97,7 @@ public int execute(ExecutionInfo<CommandSender, WrapperType> info) throws Comman
} catch (WrapperCommandSyntaxException e) {
throw e.getException();
} catch (Throwable ex) {
CommandAPI.getLogger().severe("Unhandled exception executing '" + info.args().getFullInput() + "'", ex);
CommandAPI.getLogger().severe("Unhandled exception executing '" + info.args().fullInput() + "'", ex);
if (ex instanceof Exception) {
throw ex;
} else {
Expand Down
Expand Up @@ -23,8 +23,6 @@
import java.awt.Component;
import java.io.File;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -102,11 +100,15 @@ public static <CommandSource> String getRawArgumentInput(CommandContext<CommandS
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.
if(parsedArgument != null) {
// the arguments for this command context (most likely because it's a redirected command).
// We need to figure out how to handle this case.
if (parsedArgument != null) {
// Sanity check: See https://github.com/JorelAli/CommandAPI/wiki/Implementation-details#chatcomponentargument-raw-arguments
StringRange range = parsedArgument.getRange();
return cmdCtx.getInput().substring(range.getStart(), range.getEnd());
if (range.getEnd() > cmdCtx.getInput().length()) {
range = StringRange.between(range.getStart(), cmdCtx.getInput().length());
}
return range.get(cmdCtx.getInput());
} else {
return "";
}
Expand Down Expand Up @@ -223,7 +225,7 @@ public AbstractCommandSender<? extends CommandSender> senderWrapper() {

@Override
public CommandArguments args() {
return new CommandArguments(result, new LinkedHashMap<>(), "/" + cmdCtx.getInput());
return new CommandArguments(result, new LinkedHashMap<>(), result, new LinkedHashMap<>(), "/" + cmdCtx.getInput());
}
};

Expand Down Expand Up @@ -274,16 +276,30 @@ CommandArguments argsToCommandArgs(CommandContext<Source> cmdCtx, Argument[] arg
// LinkedHashMap for arguments for executor
Map<String, Object> argsMap = new LinkedHashMap<>();

// List for raw arguments
List<String> rawArguments = new ArrayList<>();

// LinkedHashMap for raw arguments
Map<String, String> rawArgumentsMap = new LinkedHashMap<>();

// Populate array
for (Argument argument : args) {
if (argument.isListed()) {
Object parsedArgument = parseArgument(cmdCtx, argument.getNodeName(), argument, new CommandArguments(argList.toArray(), argsMap, "/" + cmdCtx.getInput()));
Object parsedArgument = parseArgument(cmdCtx, argument.getNodeName(), argument, new CommandArguments(argList.toArray(), argsMap, rawArguments.toArray(new String[0]), rawArgumentsMap, "/" + cmdCtx.getInput()));

// Add the parsed argument
argList.add(parsedArgument);
argsMap.put(argument.getNodeName(), parsedArgument);

// Add the raw argument
String rawArgumentString = getRawArgumentInput(cmdCtx, argument.getNodeName());

rawArguments.add(rawArgumentString);
rawArgumentsMap.put(argument.getNodeName(), rawArgumentString);
}
}

return new CommandArguments(argList.toArray(), argsMap, "/" + cmdCtx.getInput());
return new CommandArguments(argList.toArray(), argsMap, rawArguments.toArray(new String[0]), rawArgumentsMap, "/" + cmdCtx.getInput());
}

/**
Expand Down Expand Up @@ -809,14 +825,20 @@ CommandArguments generatePreviousArguments(CommandContext<Source> context, Argum
// LinkedHashMap for arguments
Map<String, Object> argsMap = new LinkedHashMap<>();

// List for raw arguments
List<String> rawArguments = new ArrayList<>();

// LinkedHashMap for raw arguments
Map<String, String> rawArgumentsMap = new LinkedHashMap<>();

for (Argument arg : args) {
if (arg.getNodeName().equals(nodeName) && !(arg instanceof Literal)) {
break;
}

Object result;
try {
result = parseArgument(context, arg.getNodeName(), arg, new CommandArguments(previousArguments.toArray(), argsMap, "/" + context.getInput()));
result = parseArgument(context, arg.getNodeName(), arg, new CommandArguments(previousArguments.toArray(), argsMap, rawArguments.toArray(new String[0]), rawArgumentsMap, "/" + context.getInput()));
} catch (IllegalArgumentException e) {
/*
* Redirected commands don't parse previous arguments properly. Simplest way to
Expand All @@ -829,11 +851,18 @@ CommandArguments generatePreviousArguments(CommandContext<Source> context, Argum
result = null;
}
if (arg.isListed()) {
// Add the parsed argument
previousArguments.add(result);
argsMap.put(arg.getNodeName(), result);

// Add the raw argument
String rawArgumentString = getRawArgumentInput(context, arg.getNodeName());

rawArguments.add(rawArgumentString);
rawArgumentsMap.put(arg.getNodeName(), rawArgumentString);
}
}
return new CommandArguments(previousArguments.toArray(), argsMap, "/" + context.getInput());
return new CommandArguments(previousArguments.toArray(), argsMap, rawArguments.toArray(new String[0]), rawArgumentsMap, "/" + context.getInput());
}

SuggestionProvider<Source> toSuggestions(Argument theArgument, Argument[] args,
Expand Down
Expand Up @@ -79,7 +79,7 @@ private ArgumentSuggestions<CommandSender> getNextSuggestion(CommandSender sende

if (currentSuggestion != null) {
// Validate argument on the path
SuggestionInfo<CommandSender> info = new SuggestionInfo<>(sender, new CommandArguments(processedArguments.toArray(), new HashMap<>(), currentInput.toString()), currentInput.toString(), "");
SuggestionInfo<CommandSender> info = new SuggestionInfo<>(sender, new CommandArguments(processedArguments.toArray(), new LinkedHashMap<>(), processedArguments.toArray(new String[0]), new LinkedHashMap<>(), currentInput.toString()), currentInput.toString(), "");
SuggestionsBuilder builder = new SuggestionsBuilder(currentInput.toString(), currentInput.length());
currentSuggestion.suggest(info, builder);
if (builder.build().getList().stream().map(Suggestion::getText).noneMatch(currentArgument::equals)) {
Expand Down Expand Up @@ -184,7 +184,7 @@ private EnforceReplacementsResult enforceReplacements(CommandSender sender, Stri

if (currentSuggestion != null) {
// Validate argument on the path
SuggestionInfo<CommandSender> info = new SuggestionInfo<>(sender, new CommandArguments(processedArguments.toArray(), new HashMap<>(), currentInput.toString()), currentInput.toString(), "");
SuggestionInfo<CommandSender> info = new SuggestionInfo<>(sender, new CommandArguments(processedArguments.toArray(), new LinkedHashMap<>(), processedArguments.toArray(new String[0]), new LinkedHashMap<>(), currentInput.toString()), currentInput.toString(), "");
SuggestionsBuilder builder = new SuggestionsBuilder(currentInput.toString(), currentInput.length());
try {
currentSuggestion.suggest(info, builder);
Expand Down