File size: 2,641 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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.context.CommandContext;
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(CommandDispatcher<CommandSourceStack> p_137788_) {
        p_137788_.register(
            Commands.literal("help")
                .executes(p_288460_ -> {
                    Map<CommandNode<CommandSourceStack>, String> map = p_137788_.getSmartUsage(p_137788_.getRoot(), p_288460_.getSource());

                    for (String s : map.values()) {
                        p_288460_.getSource().sendSuccess(() -> Component.literal("/" + s), false);
                    }

                    return map.size();
                })
                .then(
                    Commands.argument("command", StringArgumentType.greedyString())
                        .executes(
                            p_288458_ -> {
                                ParseResults<CommandSourceStack> parseresults = p_137788_.parse(
                                    StringArgumentType.getString(p_288458_, "command"), p_288458_.getSource()
                                );
                                if (parseresults.getContext().getNodes().isEmpty()) {
                                    throw ERROR_FAILED.create();
                                } else {
                                    Map<CommandNode<CommandSourceStack>, String> map = p_137788_.getSmartUsage(
                                        Iterables.getLast(parseresults.getContext().getNodes()).getNode(), p_288458_.getSource()
                                    );

                                    for (String s : map.values()) {
                                        p_288458_.getSource().sendSuccess(() -> Component.literal("/" + parseresults.getReader().getString() + " " + s), false);
                                    }

                                    return map.size();
                                }
                            }
                        )
                )
        );
    }
}