|
| 1 | +/* |
| 2 | + * This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client). |
| 3 | + * Copyright (c) Meteor Development. |
| 4 | + */ |
| 5 | + |
| 6 | +package meteordevelopment.meteorclient.commands.commands; |
| 7 | + |
| 8 | +import com.mojang.brigadier.builder.LiteralArgumentBuilder; |
| 9 | +import com.mojang.brigadier.tree.CommandNode; |
| 10 | +import meteordevelopment.meteorclient.commands.Command; |
| 11 | +import meteordevelopment.meteorclient.commands.Commands; |
| 12 | +import meteordevelopment.meteorclient.commands.arguments.CommandArgumentType; |
| 13 | +import meteordevelopment.meteorclient.utils.player.ChatUtils; |
| 14 | +import net.minecraft.command.CommandSource; |
| 15 | +import net.minecraft.text.MutableText; |
| 16 | +import net.minecraft.text.Text; |
| 17 | +import net.minecraft.util.Formatting; |
| 18 | + |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +public class HelpCommand extends Command { |
| 22 | + public HelpCommand() { |
| 23 | + super("help", "Shows you what a command does."); |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public void build(LiteralArgumentBuilder<CommandSource> builder) { |
| 28 | + builder.then(argument("command", CommandArgumentType.create()).executes(context -> { |
| 29 | + showHelp(CommandArgumentType.get(context)); |
| 30 | + return SINGLE_SUCCESS; |
| 31 | + })); |
| 32 | + |
| 33 | + builder.executes(context -> { |
| 34 | + showHelp(this); |
| 35 | + return SINGLE_SUCCESS; |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + private void showHelp(Command cmd) { |
| 40 | + MutableText msg = Text.literal(""); |
| 41 | + msg.append(Text.literal("Help for ").formatted(Formatting.GRAY).append(Text.literal(cmd.getName()).formatted(Formatting.YELLOW))); |
| 42 | + msg.append(Text.literal("\n ")).append(Text.literal("Description: ").formatted(Formatting.GRAY).append(Text.literal(cmd.getDescription()).formatted(Formatting.WHITE))); |
| 43 | + |
| 44 | + if (!cmd.getAliases().isEmpty()) { |
| 45 | + msg.append(Text.literal("\n ")).append(Text.literal("Aliases: ").formatted(Formatting.GRAY)); |
| 46 | + msg.append(Text.literal(String.join(", ", cmd.getAliases())).formatted(Formatting.AQUA)); |
| 47 | + } |
| 48 | + |
| 49 | + msg.append(getUsageText(cmd)); |
| 50 | + ChatUtils.sendMsg(msg); |
| 51 | + } |
| 52 | + |
| 53 | + private MutableText getUsageText(Command cmd) { |
| 54 | + CommandSource source = mc.getNetworkHandler().getCommandSource(); |
| 55 | + CommandNode<CommandSource> root = Commands.DISPATCHER.getRoot(); |
| 56 | + CommandNode<CommandSource> node = root.getChild(cmd.getName()); |
| 57 | + |
| 58 | + MutableText usagesText = Text.literal(""); |
| 59 | + |
| 60 | + if (node != null) { |
| 61 | + Map<CommandNode<CommandSource>, String> usages = Commands.DISPATCHER.getSmartUsage(node, source); |
| 62 | + |
| 63 | + for (String usage : usages.values()) { |
| 64 | + usagesText.append(Text.literal("\n " + cmd + " ").formatted(Formatting.GREEN)).append(Text.literal(usage).formatted(Formatting.GREEN)); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + if (usagesText.getString().isEmpty()) { |
| 69 | + usagesText.append(Text.literal("\n " + cmd).formatted(Formatting.GREEN)); |
| 70 | + } |
| 71 | + |
| 72 | + return Text.literal("\n Usage:").formatted(Formatting.GRAY).append(usagesText); |
| 73 | + } |
| 74 | +} |
0 commit comments