package net.minecraft.server.commands; import com.google.common.collect.Iterables; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.ParseResults; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; import com.mojang.brigadier.tree.CommandNode; import java.util.Map; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; import net.minecraft.network.chat.Component; public class HelpCommand { private static final SimpleCommandExceptionType ERROR_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.help.failed")); public static void register(final CommandDispatcher dispatcher) { dispatcher.register(Commands.literal("help").executes(s -> { Map, String> usage = dispatcher.getSmartUsage(dispatcher.getRoot(), s.getSource()); for (String line : usage.values()) { s.getSource().sendSuccess(() -> Component.literal("/" + line), false); } return usage.size(); }).then(Commands.argument("command", StringArgumentType.greedyString()).executes(s -> { ParseResults command = dispatcher.parse(StringArgumentType.getString(s, "command"), s.getSource()); if (command.getContext().getNodes().isEmpty()) { throw ERROR_FAILED.create(); } Map, String> usage = dispatcher.getSmartUsage(Iterables.getLast(command.getContext().getNodes()).getNode(), s.getSource()); for (String line : usage.values()) { s.getSource().sendSuccess(() -> Component.literal("/" + command.getReader().getString() + " " + line), false); } return usage.size(); }))); } }