feat: improve help command and add clickable page navigation

This commit is contained in:
cattyn
2026-04-20 19:51:16 +03:00
parent fcb7dc420f
commit fa9c868feb
2 changed files with 79 additions and 26 deletions
@@ -5,18 +5,27 @@ 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.OyVey;
import me.alpha432.oyvey.features.commands.Command;
import me.alpha432.oyvey.manager.CommandManager;
import me.alpha432.oyvey.util.chat.ChatUtil;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentUtils;
import net.minecraft.network.chat.MutableComponent;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import static me.alpha432.oyvey.features.commands.MessageSignatures.GENERAL;
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.get;
import static me.alpha432.oyvey.features.commands.argument.NumberArgumentType.number;
import static me.alpha432.oyvey.util.chat.SimpleSignature.fromInt;
import static me.alpha432.oyvey.util.TextUtil.text;
import static me.alpha432.oyvey.util.chat.ChatUtil.prefix;
import static me.alpha432.oyvey.util.chat.ChatUtil.withPrefix;
public class HelpCommand extends Command {
private static final int ITEMS_PER_PAGE = 5;
@@ -36,27 +45,33 @@ public class HelpCommand extends Command {
}
private int helpSpecific(CommandContext<CommandManager> ctx) {
int index = 0;
Command command = getCommand(ctx, "command_name");
sendMessage("Usages for %s:", fromInt(index++), command.getName());
MutableComponent text = Component.empty();
text.append(withPrefix("Usages for %s:\n", command.getName()));
ParseResults<CommandManager> results = ctx.getSource().getDispatcher()
.parse(command.getName(), ctx.getSource());
if (results.getContext().getNodes().isEmpty()) {
return success("No usages available");
}
text.append(withPrefix("No usages available"));
} else {
Map<CommandNode<CommandManager>, String> smartUsages = ctx.getSource().getDispatcher().getSmartUsage(
Iterables.getLast(results.getContext().getNodes()).getNode(), ctx.getSource());
List<Component> usages = new ArrayList<>();
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", fromInt(index++), results.getReader().getString(), usage);
for (String usage : smartUsages.values()) {
usages.add(text(" {gray}.%s %s", command.getName(), usage));
}
text.append(ComponentUtils.formatList(usages, Component.literal("\n")));
}
ChatUtil.sendClientSideMessage(text, GENERAL);
return success();
}
private int helpCommands(CommandContext<CommandManager> ctx, int page) {
int index = 0;
List<Command> commands = ctx.getSource().getCommands()
.stream().filter(Command::isShown).toList();
@@ -69,18 +84,38 @@ public class HelpCommand extends Command {
List<Command> paginated = commands.subList((pageIndex - 1) * ITEMS_PER_PAGE,
Math.min(commands.size(), pageIndex * ITEMS_PER_PAGE));
sendMessage("Commands (%s):", fromInt(index++), commands.size());
MutableComponent text = Component.empty();
text.append(withPrefix("Commands (%s): \n", 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", fromInt(index++),
joiner, command.getDescription());
String aliases = String.join(", ", command.getAliases());
String description = command.getDescription();
text.append(withPrefix("{dark_gray} %s: {reset}\n %s\n", aliases, description));
}
return success("Page %s/%s", pageIndex, pages);
text.append(buildPages(pageIndex, pages));
ChatUtil.sendClientSideMessage(text, GENERAL);
return success();
}
private static Component buildPages(int index, int pages) {
String command = OyVey.commandManager.getCommandPrefix() + "help ";
MutableComponent text = prefix();
boolean hasPages = pages > 1;
if (hasPages && index != 1)
text.append(suggest(Component.literal(" <"), command + (index - 1)));
text.append(text(" Page %d/%d", index, pages));
if (hasPages && index != pages)
text.append(suggest(Component.literal(" > "), command + (index + 1)));
return text;
}
private static Component suggest(MutableComponent component, String command) {
return component.withStyle(s -> s.withClickEvent(new ClickEvent.SuggestCommand(command.formatted())));
}
}
@@ -3,24 +3,42 @@ package me.alpha432.oyvey.util.chat;
import me.alpha432.oyvey.OyVey;
import me.alpha432.oyvey.ducks.render.IChatComponent;
import me.alpha432.oyvey.features.commands.Command;
import me.alpha432.oyvey.util.TextUtil;
import net.minecraft.ChatFormatting;
import net.minecraft.client.GuiMessage;
import net.minecraft.client.GuiMessageTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MessageSignature;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.Style;
import static me.alpha432.oyvey.util.traits.Util.mc;
public class ChatUtil {
public static void sendMessage(Component message, Signature identifier) {
sendClientSideMessage(Component.empty()
public static MutableComponent prefix() {
return withPrefix((Component) null);
}
public static MutableComponent withPrefix(String string) {
return withPrefix(string, new Object[0]);
}
public static MutableComponent withPrefix(String string, Object... o) {
return withPrefix(TextUtil.text(string, o));
}
public static MutableComponent withPrefix(Component component) {
MutableComponent text = Component.empty()
.setStyle(Style.EMPTY.withColor(ChatFormatting.GRAY))
.append("<")
.append(Component.literal("<"))
.append(getClientNameComponent())
.append(">")
.append(" ")
.append(message), identifier);
.append(Component.literal(">"));
if (component != null) text.append(" ").append(component);
return text;
}
public static void sendMessage(Component message, Signature identifier) {
sendClientSideMessage(withPrefix(message), identifier);
}
public static void sendClientSideMessage(Component message, Signature sig) {