-
-
Notifications
You must be signed in to change notification settings - Fork 66
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
CommandAPI command executors should have an overload for arguments accessible via a map #360
Comments
For those who want extra reading on the context of this suggestion, it started as a comment in discord |
My idea was: (as discussed on Discord) new CommandAPICommand("suggestion")
.withArguments(new ItemStackArgument("item"))
.executesPlayer(info -> {
ItemStack possibilityOne = info.args().get(0); // @Nullable public <T> T get(int index)
ItemStack possibilityTwo = info.args().get("item"); // @Nullable public <T> T get(String identifier)
// ^ @Nullable because the developer should handle these cases, it shouldn't throw a "hidden" exception
info.player().getInventory().addItem(possibilityOne, possibilityTwo);
})
.register(); This merges The implementation code I had in mind for this was: public record ExecutionInfo<Sender extends CommandSender> (
Sender sender,
CommandArgs args
) {}
public class CommandArgs {
private final Map<String, Object> args; // IMPL: use a map that can be indexed
// ... constructor ...
@Nullable
public <T> T get(int index) {
/* IMPL: get value from the map by index */
}
@Nullable
public <T> T get(String identifier) {
return (T) args.get(identifier); // casting null to T is a no-op
}
} |
* adding #360 * provide ExecutionInfo with a record * fixing JavaDocs * fixing some things * adding a CommandArguments class to hold provided arguments * remove method parameters, fix documentation code to match new syntax * fix Kotlin DSL to match new syntax * fixing Converter * fix some more documentation issues * fixing some annotation examples to match new syntax * fix annotation generation to match new syntax * handle String[] for converted commands correctly * parse arguments only once * Make argsToObjectArr return a CommandArguments object. Change name of argsToObjectArr to argsToCommandArgs * fixing generateCommand method * fix Brigadier#parseArguments method * add Javadocs to ExecutionInfo * Remove test command thingy from CommandAPIMain. Add Javadocs to CommandArguments * clean up after merge * fixing a method call in Brigadier#parseArguments * fixing CommandTree DSL * optimizing some imports * fixing some formatting issues * fixing documentation example code * fix commands not being executable * fixing build error
Implemented for 9.0.0. |
Description
CommandAPI command executors currently only consist of a command sender and an array of arguments:
It is useful to be able to access arguments by node name, rather than by index - especially in the case of using optional arguments:
It would be ideal to implement an overload for the various
executes()
methods that instead of accepting a(sender, args)
, it can instead accept a generalized(executioninfo)
which would allow the CommandAPI to be more easily extensible in the future and provide users with a more accessible API.Expected code
This could be implemented using a
record
instead:Generic structure for a player execution, using the generic type to enforce the return type of
sender()
as asPlayer
object:The implementation of
argsMap
would be as simple as tweaking the implementation ofCommandAPIHandler.argsToObjectArr()
, and shoving them into aLinkedHashMap
. We'll useMap
as the main interface forargsMap()
for simplicity, but mention in JavaDocs that the implementation is declared in order (so can be iterated upon if users so desire...)Extra details
No response
The text was updated successfully, but these errors were encountered: