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

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

    @Override
AnimeGitB's avatar
AnimeGitB committed
17
    public void execute(Player player, Player targetPlayer, List<String> args) {
KingRainbow44's avatar
KingRainbow44 committed
18
19
        if (args.size() < 1) {
            HashMap<String, CommandHandler> handlers = CommandMap.getInstance().getHandlers();
方块君's avatar
方块君 committed
20
            List<Command> annotations = new ArrayList<>();
KingRainbow44's avatar
KingRainbow44 committed
21
22
23
24
25
26
            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;
方块君's avatar
方块君 committed
27
                    annotations.add(annotation);
KingRainbow44's avatar
KingRainbow44 committed
28
29
30
31
32
33
34
                }
            }

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

方块君's avatar
方块君 committed
41
                builder.append("   ").append(translate(annotation.description())).append("\n");
42
                builder.append(translate("commands.help.usage")).append(annotation.usage());
KingRainbow44's avatar
KingRainbow44 committed
43
                if (annotation.aliases().length >= 1) {
44
                    builder.append("\n").append(translate("commands.help.aliases"));
KingRainbow44's avatar
KingRainbow44 committed
45
46
47
48
49
50
51
52
53
54
55
56
57
                    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());
        }
    }

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

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

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

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