CommandAPI icon indicating copy to clipboard operation
CommandAPI copied to clipboard

Optional arguments

Open JorelAli opened this issue 5 years ago • 10 comments

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.

JorelAli avatar Nov 18 '20 00:11 JorelAli

Emmm, so is it really implemented? I like this feature, but I can't find it in v7.0 :(

SNWCreations avatar May 02 '22 03:05 SNWCreations

@SNWCreations No, this feature is not implemented yet

JorelAli avatar May 02 '22 03:05 JorelAli

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 avatar May 02 '22 03:05 SNWCreations

@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.

JorelAli avatar May 02 '22 03:05 JorelAli

@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?

SNWCreations avatar May 02 '22 03:05 SNWCreations

Can I use it in annotation framework?

No.

JorelAli avatar May 02 '22 03:05 JorelAli

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 avatar May 02 '22 03:05 SNWCreations

@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) {
  // ...
}

JorelAli avatar May 02 '22 04:05 JorelAli

@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.

SNWCreations avatar May 02 '22 05:05 SNWCreations

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.

JorelAli avatar Jul 11 '22 18:07 JorelAli

After just over 2 years, this has now been implemented and merged into the upcoming 9.0.0 release.

JorelAli avatar Jan 04 '23 21:01 JorelAli

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

SNWCreations avatar Jan 04 '23 23:01 SNWCreations

Implemented in 9.0.0.

JorelAli avatar Apr 23 '23 09:04 JorelAli