
setTabCompletion arguments start at positon 2
Closed this issue ยท 7 comments
What steps will reproduce the problem?
Used code from wiki website:
AddonCommand myCommand = new AddonCommand(TownyCommandAddonAPI.CommandType.TOWN, "mycommand", new UpgradeCommandExecutor());
myCommand.setTabCompletion(0, Arrays.asList("suggestions", "for", "first", "argument"));
myCommand.setTabCompletion(1, Arrays.asList("suggestions", "for", "second", "argument"));
TownyCommandAddonAPI.addSubCommand(myCommand);
When I use the sub-command, the completion list is not displayed until the 3rd argument.
I think the problem arises from this line . The -2 is usefull for commands like "/town set" (CommandType.TOWN_SET) but not for commands like "/town" (CommandType.TOWN) or "/nation".
What is the expected output?
The completion should start after the subcommand. If CommandTypes are used with their own arguments, the developer could start at index 1 for the completion.
Towny version
towny-0.101.1.12
Server version
paper-1.21.4-231
I have never run across this because I have always put a onTabComplete method into my command classes, and not used the method described in the wiki.
Please test the same issue using this jar: https://github.com/TownyAdvanced/Towny/actions/runs/15230675152/artifacts/3191213368
Please test the TOWN and TOWN_SET CommandTypes to make sure I did it right.
This made no difference.
The debugger shows that "precendingArgs" is 2 for CommandType "TOWN".
But it should be 1 (as correctly defined in the code). But TownyCommandAddonAPI.args is static. Therefore, I think it always has the last added value.
I am also not sure that TOWNYADMIN_TOWN and TOWNYADMIN_NATION should be 3. Isn't it the "/townyadmin town" and "/townyadmin nation" command?
I am also not sure that TOWNYADMIN_TOWN and TOWNYADMIN_NATION should be 3. Isn't it the "/townyadmin town" and "/townyadmin nation" command?
These ones are special as the town and nation name end up becoming the first arg, so that it can be used for things.
Try out this jar and see if it fixes the args variable: https://github.com/TownyAdvanced/Towny/actions/runs/15231967699/artifacts/3191477295
CommandType.TOWN is correct now, but TOWN_SET is not working.
The parameter "args" of getTabCompletion() gets only the additional input after the towny prefefined arguments starting with the sub command. Towny itself cuts the predefined arguments for TOWN and TOWN_SET and all others already. So the extension "RESIDENT(1) ..." in "Potentially fix the tab completer in in #7834" seem to be not necessary.
So it looks like the bug fix would simply be:
return command().tabCompletions.get(args.length - 1) == null ? Collections.emptyList() : command().tabCompletions.get(args.length - 1);
instead of
return command().tabCompletions.get(args.length - 2) == null ? Collections.emptyList() : command().tabCompletions.get(args.length - 2);
EDIT: I can now confirm. I tried with index -1 for the compensation:
myCommand.setTabCompletion(-1, Arrays.asList("suggestions", "for", "first", "argument"));
This works for all CommandTypes except TOWNYADMIN_TOWN and TOWNYADMIN_NATION. Since the town name is included in the Towny arguments here, this and this probably needs to be changed to args[3]
Here's another test jar: https://github.com/TownyAdvanced/Towny/actions/runs/15241589394/artifacts/3193787584