| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.google.common.collect.Iterables;
|
| 4 | import com.mojang.brigadier.CommandDispatcher;
|
| 5 | import com.mojang.brigadier.ParseResults;
|
| 6 | import com.mojang.brigadier.arguments.StringArgumentType;
|
| 7 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 8 | import com.mojang.brigadier.tree.CommandNode;
|
| 9 | import java.util.Map;
|
| 10 | import net.minecraft.commands.CommandSourceStack;
|
| 11 | import net.minecraft.commands.Commands;
|
| 12 | import net.minecraft.network.chat.Component;
|
| 13 |
|
| 14 | public class HelpCommand {
|
| 15 | private static final SimpleCommandExceptionType ERROR_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.help.failed"));
|
| 16 |
|
| 17 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 18 | dispatcher.register(Commands.literal("help").executes(s -> {
|
| 19 | Map<CommandNode<CommandSourceStack>, String> usage = dispatcher.getSmartUsage(dispatcher.getRoot(), s.getSource());
|
| 20 |
|
| 21 | for (String line : usage.values()) {
|
| 22 | s.getSource().sendSuccess(() -> Component.literal("/" + line), false);
|
| 23 | }
|
| 24 |
|
| 25 | return usage.size();
|
| 26 | }).then(Commands.argument("command", StringArgumentType.greedyString()).executes(s -> {
|
| 27 | ParseResults<CommandSourceStack> command = dispatcher.parse(StringArgumentType.getString(s, "command"), s.getSource());
|
| 28 | if (command.getContext().getNodes().isEmpty()) {
|
| 29 | throw ERROR_FAILED.create();
|
| 30 | }
|
| 31 |
|
| 32 | Map<CommandNode<CommandSourceStack>, String> usage = dispatcher.getSmartUsage(Iterables.getLast(command.getContext().getNodes()).getNode(), s.getSource());
|
| 33 |
|
| 34 | for (String line : usage.values()) {
|
| 35 | s.getSource().sendSuccess(() -> Component.literal("/" + command.getReader().getString() + " " + line), false);
|
| 36 | }
|
| 37 |
|
| 38 | return usage.size();
|
| 39 | })));
|
| 40 | }
|
| 41 | }
|
| 42 |
|