Optional arguments
Please describe your suggestion Optional arguments
Please supply examples of the desired inputs and outputs Normal usage: User enters this:
new CommandAPICommand("blah")
.withArgument(new IntegerArgument("int"))
.withOptionalArgument(new IntegerArgument("optional"), 10)
.executes((sender, args) -> {
int arg1 = (int) args[0];
int arg2 = (int) args[1]; // This is 10 if not existant
})
.register();
Basically gets converted into this:
new CommandAPICommand("blah")
.withArgument(new IntegerArgument("int"))
.withArgument(new IntegerArgument("optional"))
.executes((sender, args) -> {
int arg1 = (int) args[0];
int arg2 = (int) args[1];
})
.register();
and
new CommandAPICommand("blah")
.withArgument(new IntegerArgument("int"))
.executes((sender, args) -> {
int arg1 = (int) args[0];
int arg2 = (int) args[1]; // This is 10, args.length = 2
})
.register();
Using annotations: Either something like this:
@Subcommand("mycommand")
@Arg(name = "int", type = IntegerArgument.class)
@OptionalArg(name = "int", type = IntegerArgument.class, optional = "10")
public void onCommand(int arg1, int arg2) {
//...
}
or something like this:
@Subcommand("mycommand")
@Arg(name = "int", type = IntegerArgument.class)
@Arg(name = "optional", type = IntegerArgument.class)
public void onCommand(int arg1, @Default("10") int arg2) {
//...
}
Cons of this via annotations is that the value must be a string, primitive, enum, annotation or class, which is really restrictive.
Emmm, so is it really implemented? I like this feature, but I can't find it in v7.0 :(
@SNWCreations No, this feature is not implemented yet
Emmm, that's too bad :( If so, will it be available in v7.x? (I'm still working on 1.16.5, I need it!)
@SNWCreations This feature, despite how useful it is, is not relatively high on priority at the moment. This is because this feature can be implemented using other methods, for example, something like this:
new CommandAPICommand("blah")
.withArgument(new IntegerArgument("int"))
.withOptionalArgument(new IntegerArgument("optional"), 10)
.executes((sender, args) -> {
int arg1 = (int) args[0];
int arg2 = (int) args[1]; // This is 10 if not existant
})
.register();
Can already be implemented as this:
public void execute(CommandSender sender, int arg1, int arg2) {
// Command logic here
}
And then you implement your CommandAPICommands as follows:
new CommandAPICommand("blah")
.withArgument(new IntegerArgument("int"))
.executes((sender, args) -> {
int arg1 = (int) args[0];
execute(sender, arg1, 10);
})
.register();
new CommandAPICommand("blah")
.withArgument(new IntegerArgument("int"))
.withArgument(new IntegerArgument("optional"))
.executes((sender, args) -> {
int arg1 = (int) args[0];
int arg2 = (int) args[1];
execute(sender, arg1, arg2);
})
.register();
Optional arguments is also surprisingly difficult to implement and ends up causing more ambiguity and issues than explicitly stating everything manually.
@SNWCreations This feature, despite how useful it is, is not relatively high on priority at the moment. This is because this feature can be implemented using other methods, for example, something like this:
new CommandAPICommand("blah") .withArgument(new IntegerArgument("int")) .withOptionalArgument(new IntegerArgument("optional"), 10) .executes((sender, args) -> { int arg1 = (int) args[0]; int arg2 = (int) args[1]; // This is 10 if not existant }) .register();Can already be implemented as this:
public void execute(CommandSender sender, int arg1, int arg2) { // Command logic here }And then you implement your CommandAPICommands as follows:
new CommandAPICommand("blah") .withArgument(new IntegerArgument("int")) .executes((sender, args) -> { int arg1 = (int) args[0]; execute(sender, arg1, 10); }) .register(); new CommandAPICommand("blah") .withArgument(new IntegerArgument("int")) .withArgument(new IntegerArgument("optional")) .executes((sender, args) -> { int arg1 = (int) args[0]; int arg2 = (int) args[1]; execute(sender, arg1, arg2); }) .register();Optional arguments is also surprisingly difficult to implement and ends up causing more ambiguity and issues than explicitly stating everything manually.
Can I use it in annotation framework?
Can I use it in annotation framework?
No.
This feature, despite how useful it is, is not relatively high on priority at the moment. This is because this feature can be implemented using other methods, for example, something like this:
new CommandAPICommand("blah") .withArgument(new IntegerArgument("int")) .withOptionalArgument(new IntegerArgument("optional"), 10) .executes((sender, args) -> { int arg1 = (int) args[0]; int arg2 = (int) args[1]; // This is 10 if not existant }) .register(); Can already be implemented as this:
public void execute(CommandSender sender, int arg1, int arg2) { // Command logic here }
My reply: I can't understand this. :(
@SNWCreations
This code:
new CommandAPICommand("blah")
.withArgument(new IntegerArgument("int"))
.withOptionalArgument(new IntegerArgument("optional"), 10)
.executes((sender, args) -> {
int arg1 = (int) args[0];
int arg2 = (int) args[1]; // This is 10 if not existant
})
.register();
Is the same as this code:
new CommandAPICommand("blah")
.withArgument(new IntegerArgument("int"))
.executes((sender, args) -> {
int arg1 = (int) args[0];
execute(sender, arg1, 10);
})
.register();
new CommandAPICommand("blah")
.withArgument(new IntegerArgument("int"))
.withArgument(new IntegerArgument("optional"))
.executes((sender, args) -> {
int arg1 = (int) args[0];
int arg2 = (int) args[1];
execute(sender, arg1, arg2);
})
.register();
public void execute(CommandSender sender, int arg1, int arg2) {
// ...
}
@SNWCreations
This code:
new CommandAPICommand("blah") .withArgument(new IntegerArgument("int")) .withOptionalArgument(new IntegerArgument("optional"), 10) .executes((sender, args) -> { int arg1 = (int) args[0]; int arg2 = (int) args[1]; // This is 10 if not existant }) .register();Is the same as this code:
new CommandAPICommand("blah") .withArgument(new IntegerArgument("int")) .executes((sender, args) -> { int arg1 = (int) args[0]; execute(sender, arg1, 10); }) .register(); new CommandAPICommand("blah") .withArgument(new IntegerArgument("int")) .withArgument(new IntegerArgument("optional")) .executes((sender, args) -> { int arg1 = (int) args[0]; int arg2 = (int) args[1]; execute(sender, arg1, arg2); }) .register(); public void execute(CommandSender sender, int arg1, int arg2) { // ... }
Thanks! I got it.
It would be very good to get this implemented (or at least have made significant progress on this) for a 8.5.0 release due late July or early August.
After just over 2 years, this has now been implemented and merged into the upcoming 9.0.0 release.
After just over 2 years, this has now been implemented and merged into the upcoming 9.0.0 release.
I'm so happy to see this. :D
Implemented in 9.0.0.