refactor: make help command use method references

This commit is contained in:
cattyn
2026-02-02 23:50:14 +03:00
parent 2c49e88216
commit 3a8c7ab805
3 changed files with 48 additions and 48 deletions
@@ -91,7 +91,7 @@ public class CommandModule extends Command {
builder.then(literal(setting.getName().toLowerCase())
.then(argument("value", number(type, minMax(setting.getMin(), setting.getMax())))
.executes((ctx) -> {
setting.setValue(getNumber(type, ctx, "value"));
setting.setValue(get(type, ctx, "value"));
return settingChangeReturn(setting);
})));
}
@@ -49,7 +49,7 @@ public record NumberArgumentType<T extends Number>(Class<T> type, Constraint<T>
};
}
public static <T extends Number> T getNumber(Class<T> type, CommandContext<?> ctx, String name) {
public static <T extends Number> T get(Class<T> type, CommandContext<?> ctx, String name) {
return ctx.getArgument(name, type);
}
@@ -3,6 +3,7 @@ package me.alpha432.oyvey.features.commands.impl;
import com.google.common.collect.Iterables;
import com.mojang.brigadier.ParseResults;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.tree.CommandNode;
import me.alpha432.oyvey.features.commands.Command;
import me.alpha432.oyvey.manager.CommandManager;
@@ -13,7 +14,7 @@ import java.util.StringJoiner;
import static me.alpha432.oyvey.features.commands.argument.CommandArgumentType.command;
import static me.alpha432.oyvey.features.commands.argument.CommandArgumentType.getCommand;
import static me.alpha432.oyvey.features.commands.argument.NumberArgumentType.getNumber;
import static me.alpha432.oyvey.features.commands.argument.NumberArgumentType.get;
import static me.alpha432.oyvey.features.commands.argument.NumberArgumentType.number;
public class HelpCommand extends Command {
@@ -27,57 +28,56 @@ public class HelpCommand extends Command {
@Override
public void createArgumentBuilder(LiteralArgumentBuilder<CommandManager> builder) {
builder.then(argument("page", number(Integer.class))
.executes((ctx) ->
helpCommand(getNumber(Integer.class, ctx, "page")).run(ctx)))
.executes((ctx) -> helpCommands(ctx, get(Integer.class, ctx, "page")))
.then(argument("command_name", command())
.executes((ctx) -> {
Command command = getCommand(ctx, "command_name");
sendMessage("Usages for %s:", command.getName());
ParseResults<CommandManager> results = ctx.getSource().getDispatcher()
.parse(command.getName(), ctx.getSource());
if (results.getContext().getNodes().isEmpty()) {
return success("No usages available");
}
Map<CommandNode<CommandManager>, String> smartUsages = ctx.getSource().getDispatcher().getSmartUsage(
Iterables.getLast(results.getContext().getNodes()).getNode(), ctx.getSource());
for (String usage : smartUsages.values()) {
sendMessage(".%s %s", results.getReader().getString(), usage);
}
return success();
}))
.executes((ctx) -> helpCommand(1).run(ctx));
.executes(this::helpSpecific))
.executes((ctx) -> helpCommands(ctx, 1)));
}
private com.mojang.brigadier.Command<CommandManager> helpCommand(int page) {
return (ctx) -> {
List<Command> commands = ctx.getSource().getCommands()
.stream().filter(Command::isShown).toList();
private int helpSpecific(CommandContext<CommandManager> ctx) {
Command command = getCommand(ctx, "command_name");
sendMessage("Usages for %s:", command.getName());
int pageIndex = page;
int pages = (int) Math.ceil((double) commands.size() / ITEMS_PER_PAGE);
if (pageIndex > pages) {
pageIndex = 1;
ParseResults<CommandManager> results = ctx.getSource().getDispatcher()
.parse(command.getName(), ctx.getSource());
if (results.getContext().getNodes().isEmpty()) {
return success("No usages available");
}
Map<CommandNode<CommandManager>, String> smartUsages = ctx.getSource().getDispatcher().getSmartUsage(
Iterables.getLast(results.getContext().getNodes()).getNode(), ctx.getSource());
for (String usage : smartUsages.values()) {
sendMessage(".%s %s", results.getReader().getString(), usage);
}
return success();
}
private int helpCommands(CommandContext<CommandManager> ctx, int page) {
List<Command> commands = ctx.getSource().getCommands()
.stream().filter(Command::isShown).toList();
int pageIndex = page;
int pages = (int) Math.ceil((double) commands.size() / ITEMS_PER_PAGE);
if (pageIndex > pages) {
pageIndex = 1;
}
List<Command> paginated = commands.subList((pageIndex - 1) * ITEMS_PER_PAGE,
Math.min(commands.size(), pageIndex * ITEMS_PER_PAGE));
sendMessage("Commands (%s):", commands.size());
for (Command command : paginated) {
StringJoiner joiner = new StringJoiner(", ");
for (String alias : command.getAliases()) {
joiner.add(alias);
}
List<Command> paginated = commands.subList((pageIndex - 1) * ITEMS_PER_PAGE,
Math.min(commands.size(), pageIndex * ITEMS_PER_PAGE));
sendMessage("{dark_gray} %s: {reset}\n %s",
joiner, command.getDescription());
}
sendMessage("Commands (%s):", commands.size());
for (Command command : paginated) {
StringJoiner joiner = new StringJoiner(", ");
for (String alias : command.getAliases()) {
joiner.add(alias);
}
sendMessage("{dark_gray} %s: {reset}\n %s",
joiner, command.getDescription());
}
return success("Page %s/%s", pageIndex, pages);
};
return success("Page %s/%s", pageIndex, pages);
}
}