How to change the choice argument during the game runtime

[Français]
Je voudrait savoir comment changer pendant que le serveur tourne l’argument GenericArguments.choices
Merci a ceux qui m’aideront

[English with Google Translate]
I would like to know how to change while the server is running the GenericArguments.choices argument.
Thanks to those who will help me

The CommandSpec is immutable and therefore it cannot be changed. My question is why are you requiring to do so?

The workaround would be to deregister the command and then reregister the command with the new arguments

[Google translate]
I have a list of stations (too long to explain) that all have a name that is random because there are a lot of them. And I want us to be able to choose a station (with the name) to get information about that station.

And how to deregister the command ?

As the argument is always a station name, im not sure how changing the argument will help.

As for deregistering.

When you register a command, it will return a CommandMapping object. You need that to deregister the command

CommandMapping mapping;
Sponge.getCommandManager().removeMapping(mapping);

Thank you, it works !

No, do not do that. There is a native way to do this in the API without having to do this - there is a dynamic choices argument that was designed for this exact situation.

https://jd.spongepowered.org/7.2.0/org/spongepowered/api/command/args/GenericArguments.html#choices-org.spongepowered.api.text.Text-java.util.function.Supplier-java.util.function.Function-

The Supplier<Collection<String>> will return the valid strings that can be selected each time the command parameter is invoked. So, your supplier could point to the list of stations. The Function<String, ?> converts the selected string into the object that would be put into the CommandContext, if you just want the string, you could just use Function::Identity or the lambda x -> x.

You could also create your own CommandElement if you want the choice to depend on the CommandSource that is invoking the command - how to do that is available on our Docs - Analyse des Arguments — Documentation Sponge 7.2.0.

Deregistering a command is the wrong answer for this, and is always the wrong answer unless the command is not useful any more.

1 Like

Thank you for this clarification !