HelpCommand.java 4.69 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;

13
@Command(label = "help", usage = "help [command]")
KingRainbow44's avatar
KingRainbow44 committed
14
15
public final class HelpCommand implements CommandHandler {

16
17
18
19
20
    @Override
    public String description() {
        return translate("commands.help.description");
    }

KingRainbow44's avatar
KingRainbow44 committed
21
    @Override
AnimeGitB's avatar
AnimeGitB committed
22
    public void execute(Player player, Player targetPlayer, List<String> args) {
KingRainbow44's avatar
KingRainbow44 committed
23
24
        if (args.size() < 1) {
            HashMap<String, CommandHandler> handlers = CommandMap.getInstance().getHandlers();
25
            HashMap<Command, CommandHandler> annotations = new HashMap<>();
KingRainbow44's avatar
KingRainbow44 committed
26
27
28
29
30
31
            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;
32
                    annotations.put(annotation, handlers.get(key));
KingRainbow44's avatar
KingRainbow44 committed
33
34
35
36
37
38
39
                }
            }

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

方块君's avatar
方块君 committed
46
                builder.append("   ").append(handler.description() == null ? annotation.description(): handler.description()).append("\n");
47
                builder.append(translate("commands.help.usage")).append(annotation.usage());
KingRainbow44's avatar
KingRainbow44 committed
48
                if (annotation.aliases().length >= 1) {
49
                    builder.append("\n").append(translate("commands.help.aliases"));
KingRainbow44's avatar
KingRainbow44 committed
50
51
52
53
54
55
56
57
58
59
60
61
62
                    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());
        }
    }

63
    void SendAllHelpMessage(Player player, HashMap<Command, CommandHandler> annotations) {
KingRainbow44's avatar
KingRainbow44 committed
64
        if (player == null) {
65
            StringBuilder builder = new StringBuilder("\n" + translate("commands.help.available_commands") + "\n");
66
            annotations.forEach((annotation, handler) -> {
KingRainbow44's avatar
KingRainbow44 committed
67
                builder.append(annotation.label()).append("\n");
方块君's avatar
方块君 committed
68
                builder.append("   ").append(handler.description() == null ? annotation.description() : handler.description()).append("\n");
69
                builder.append(translate("commands.help.usage")).append(annotation.usage());
KingRainbow44's avatar
KingRainbow44 committed
70
                if (annotation.aliases().length >= 1) {
71
                    builder.append("\n").append(translate("commands.help.aliases"));
KingRainbow44's avatar
KingRainbow44 committed
72
73
74
75
76
77
78
79
80
81
                    for (String alias : annotation.aliases()) {
                        builder.append(alias).append(" ");
                    }
                }

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

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

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