HelpCommand.java 4.44 KB
Newer Older
KingRainbow44's avatar
KingRainbow44 committed
1
2
package emu.grasscutter.command.commands;

方块君's avatar
方块君 committed
3
import emu.grasscutter.Grasscutter;
KingRainbow44's avatar
KingRainbow44 committed
4
5
6
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.command.CommandMap;
Melledy's avatar
Melledy committed
7
import emu.grasscutter.game.player.Player;
KingRainbow44's avatar
KingRainbow44 committed
8
9
10

import java.util.*;

11
12
import static emu.grasscutter.utils.Language.translate;

KingRainbow44's avatar
KingRainbow44 committed
13
14
15
16
17
@Command(label = "help", usage = "help [command]",
        description = "Sends the help message or shows information about a specified command")
public final class HelpCommand implements CommandHandler {

    @Override
AnimeGitB's avatar
AnimeGitB committed
18
    public void execute(Player player, Player targetPlayer, List<String> args) {
KingRainbow44's avatar
KingRainbow44 committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
        if (args.size() < 1) {
            HashMap<String, CommandHandler> handlers = CommandMap.getInstance().getHandlers();
            List<Command> annotations = new ArrayList<>();
            for (String key : handlers.keySet()) {
                Command annotation = handlers.get(key).getClass().getAnnotation(Command.class);

                if (!Arrays.asList(annotation.aliases()).contains(key)) {
                    if (player != null && !Objects.equals(annotation.permission(), "") && !player.getAccount().hasPermission(annotation.permission()))
                        continue;
                    annotations.add(annotation);
                }
            }

            SendAllHelpMessage(player, annotations);
        } else {
            String command = args.get(0);
            CommandHandler handler = CommandMap.getInstance().getHandler(command);
36
            StringBuilder builder = new StringBuilder(player == null ? "\n" + translate("commands.status.help") + " - " : translate("commands.status.help") + " - ").append(command).append(": \n");
KingRainbow44's avatar
KingRainbow44 committed
37
            if (handler == null) {
38
                builder.append(translate("commands.generic.command_exist_error"));
KingRainbow44's avatar
KingRainbow44 committed
39
40
41
42
            } else {
                Command annotation = handler.getClass().getAnnotation(Command.class);

                builder.append("   ").append(annotation.description()).append("\n");
43
                builder.append(translate("commands.help.usage")).append(annotation.usage());
KingRainbow44's avatar
KingRainbow44 committed
44
                if (annotation.aliases().length >= 1) {
45
                    builder.append("\n").append(translate("commands.help.aliases"));
KingRainbow44's avatar
KingRainbow44 committed
46
47
48
49
50
51
52
53
54
55
56
57
58
                    for (String alias : annotation.aliases()) {
                        builder.append(alias).append(" ");
                    }
                }
                if (player != null && !Objects.equals(annotation.permission(), "") && !player.getAccount().hasPermission(annotation.permission())) {
                    builder.append("\n Warning: You do not have permission to run this command.");
                }
            }

            CommandHandler.sendMessage(player, builder.toString());
        }
    }

59
    void SendAllHelpMessage(Player player, List<Command> annotations) {
KingRainbow44's avatar
KingRainbow44 committed
60
        if (player == null) {
61
            StringBuilder builder = new StringBuilder("\n" + translate("commands.help.available_commands") + "\n");
KingRainbow44's avatar
KingRainbow44 committed
62
63
64
            annotations.forEach(annotation -> {
                builder.append(annotation.label()).append("\n");
                builder.append("   ").append(annotation.description()).append("\n");
65
                builder.append(translate("commands.help.usage")).append(annotation.usage());
KingRainbow44's avatar
KingRainbow44 committed
66
                if (annotation.aliases().length >= 1) {
67
                    builder.append("\n").append(translate("commands.help.aliases"));
KingRainbow44's avatar
KingRainbow44 committed
68
69
70
71
72
73
74
75
76
77
                    for (String alias : annotation.aliases()) {
                        builder.append(alias).append(" ");
                    }
                }

                builder.append("\n");
            });

            CommandHandler.sendMessage(null, builder.toString());
        } else {
78
            CommandHandler.sendMessage(player, translate("commands.help.available_commands"));
KingRainbow44's avatar
KingRainbow44 committed
79
80
81
            annotations.forEach(annotation -> {
                StringBuilder builder = new StringBuilder(annotation.label()).append("\n");
                builder.append("   ").append(annotation.description()).append("\n");
82
                builder.append(translate("commands.help.usage")).append(annotation.usage());
KingRainbow44's avatar
KingRainbow44 committed
83
                if (annotation.aliases().length >= 1) {
84
                    builder.append("\n").append(translate("commands.help.aliases"));
KingRainbow44's avatar
KingRainbow44 committed
85
86
87
88
89
90
91
92
93
94
                    for (String alias : annotation.aliases()) {
                        builder.append(alias).append(" ");
                    }
                }

                CommandHandler.sendMessage(player, builder.toString());
            });
        }
    }
}