Skip to content

Commit

Permalink
Simplified suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
4drian3d committed Apr 28, 2023
1 parent 7186c4d commit c36ac05
Showing 1 changed file with 4 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.context.ParsedArgument;
import com.mojang.brigadier.tree.LiteralCommandNode;
import com.velocitypowered.api.command.BrigadierCommand;
import com.velocitypowered.api.command.CommandManager;
Expand All @@ -21,8 +20,6 @@
import net.kyori.adventure.text.Component;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

public final class VelocityMainCommand {
@Inject
Expand Down Expand Up @@ -50,16 +47,14 @@ public void register() {
.requires(src -> src.hasPermission(CWPermission.ADMIN.permission()))
.then(RequiredArgumentBuilder.<CommandSource, String>argument("group", StringArgumentType.word())
.suggests((ctx, builder) -> {
groupSuggestions(plugin.getConfigCache(), ctx.getArguments().get("group"))
.forEach(builder::suggest);
plugin.getConfigCache().getGroupList().keySet().forEach(builder::suggest);
return builder.buildFuture();
})
.then(RequiredArgumentBuilder.<CommandSource, String>argument("command", StringArgumentType.word())
.suggests((ctx, builder) -> {
CWGroup group = plugin.getConfigCache().getGroupList().get(ctx.getArgument("group", String.class));
if (group == null) return builder.buildFuture();

String stringArgument = stringArgument(ctx.getArguments().get("command"));
for (String cmd : plugin.getServerCommands()) {
if (cmd.charAt(0) == '/')
cmd = cmd.substring(1);
Expand All @@ -69,8 +64,7 @@ public void register() {
cmd = cmdSplit[1];
}
if (group.getCommands().contains(cmd)) continue;
if (stringArgument == null || cmd.startsWith(stringArgument))
builder.suggest(cmd);
builder.suggest(cmd);
}
return builder.buildFuture();
})
Expand All @@ -93,19 +87,16 @@ public void register() {
.requires(src -> src.hasPermission(CWPermission.ADMIN.permission()))
.then(RequiredArgumentBuilder.<CommandSource, String>argument("group", StringArgumentType.word())
.suggests((ctx, builder) -> {
groupSuggestions(plugin.getConfigCache(), ctx.getArguments().get("group"))
.forEach(builder::suggest);
plugin.getConfigCache().getGroupList().keySet().forEach(builder::suggest);
return builder.buildFuture();
})
.then(RequiredArgumentBuilder.<CommandSource, String>argument("command", StringArgumentType.word())
.suggests((ctx, builder) -> {
CWGroup group = plugin.getConfigCache().getGroupList().get(ctx.getArgument("group", String.class));
if (group == null) return builder.buildFuture();

String stringArgument = stringArgument(ctx.getArguments().get("command"));
for (String s : group.getCommands()) {
if (stringArgument == null || s.startsWith(stringArgument))
builder.suggest(s);
builder.suggest(s);
}
return builder.buildFuture();
})
Expand Down Expand Up @@ -149,19 +140,4 @@ public void register() {
final BrigadierCommand command = new BrigadierCommand(node);
commandManager.register(commandManager.metaBuilder(command).plugin(plugin).build(), command);
}

private List<String> groupSuggestions(ConfigCache configCache, ParsedArgument<CommandSource, ?> argument) {
final String stringArgument = stringArgument(argument);
List<String> groups = new ArrayList<>();
for (String s : configCache.getGroupList().keySet()) {
if (stringArgument == null || s.startsWith(stringArgument))
groups.add(s);
}
return groups;
}

private String stringArgument(ParsedArgument<CommandSource, ?> argument) {
return argument == null
? null : (String) argument.getResult();
}
}

0 comments on commit c36ac05

Please sign in to comment.