HelpCommand.java 3.74 KB
Newer Older
KingRainbow44's avatar
KingRainbow44 committed
1
2
3
4
5
package emu.grasscutter.command.commands;

import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.command.CommandMap;
Melledy's avatar
Melledy committed
6
import emu.grasscutter.game.player.Player;
KingRainbow44's avatar
KingRainbow44 committed
7
8
9

import java.util.*;

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

12
@Command(label = "help", usage = "help [command]", description = "commands.help.description", targetRequirement = Command.TargetRequirement.NONE)
KingRainbow44's avatar
KingRainbow44 committed
13
14
public final class HelpCommand implements CommandHandler {

Luke H-W's avatar
Luke H-W committed
15
16
17
18
19
20
21
22
23
24
    private void createCommand(StringBuilder builder, Player player, Command annotation) {
        builder.append("\n").append(annotation.label()).append(" - ").append(translate(player, annotation.description()));
        builder.append("\n\t").append(translate(player, "commands.help.usage"));
        if (annotation.aliases().length >= 1) {
            builder.append("\n\t").append(translate(player, "commands.help.aliases"));
            for (String alias : annotation.aliases()) {
                builder.append(alias).append(" ");
            }
        }
        builder.append("\n\t").append(translate(player, "commands.help.tip_need_permission"));
github-actions's avatar
github-actions committed
25
        if (annotation.permission().isEmpty() || annotation.permission().isBlank()) {
Luke H-W's avatar
Luke H-W committed
26
27
28
29
30
            builder.append(translate(player, "commands.help.tip_need_no_permission"));
        } else {
            builder.append(annotation.permission());
        }

github-actions's avatar
github-actions committed
31
        if (!annotation.permissionTargeted().isEmpty() && !annotation.permissionTargeted().isBlank()) {
Luke H-W's avatar
Luke H-W committed
32
33
34
35
36
            String permissionTargeted = annotation.permissionTargeted();
            builder.append(" ").append(translate(player, "commands.help.tip_permission_targeted", permissionTargeted));
        }
    }

KingRainbow44's avatar
KingRainbow44 committed
37
    @Override
AnimeGitB's avatar
AnimeGitB committed
38
    public void execute(Player player, Player targetPlayer, List<String> args) {
KingRainbow44's avatar
KingRainbow44 committed
39
40
        if (args.size() < 1) {
            HashMap<String, CommandHandler> handlers = CommandMap.getInstance().getHandlers();
方块君's avatar
方块君 committed
41
            List<Command> annotations = new ArrayList<>();
KingRainbow44's avatar
KingRainbow44 committed
42
43
44
45
46
47
            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
48
                    annotations.add(annotation);
KingRainbow44's avatar
KingRainbow44 committed
49
50
51
52
53
54
55
                }
            }

            SendAllHelpMessage(player, annotations);
        } else {
            String command = args.get(0);
            CommandHandler handler = CommandMap.getInstance().getHandler(command);
Luke H-W's avatar
Luke H-W committed
56
            StringBuilder builder = new StringBuilder("");
KingRainbow44's avatar
KingRainbow44 committed
57
            if (handler == null) {
Secretboy's avatar
Secretboy committed
58
                builder.append(translate(player, "commands.generic.command_exist_error"));
KingRainbow44's avatar
KingRainbow44 committed
59
60
61
            } else {
                Command annotation = handler.getClass().getAnnotation(Command.class);

Luke H-W's avatar
Luke H-W committed
62
                this.createCommand(builder, player, annotation);
63

KingRainbow44's avatar
KingRainbow44 committed
64
                if (player != null && !Objects.equals(annotation.permission(), "") && !player.getAccount().hasPermission(annotation.permission())) {
Luke H-W's avatar
Luke H-W committed
65
                    builder.append("\n\t").append(translate(player, "commands.help.warn_player_has_no_permission"));
KingRainbow44's avatar
KingRainbow44 committed
66
67
68
69
70
71
72
                }
            }

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

方块君's avatar
方块君 committed
73
    void SendAllHelpMessage(Player player, List<Command> annotations) {
Luke H-W's avatar
Luke H-W committed
74
75
76
77
78
        StringBuilder builder = new StringBuilder(translate(player, "commands.help.available_commands"));
        annotations.forEach(annotation -> {
            this.createCommand(builder, player, annotation);
            builder.append("\n");
        });
KingRainbow44's avatar
KingRainbow44 committed
79

Luke H-W's avatar
Luke H-W committed
80
        CommandHandler.sendMessage(player, builder.toString());
KingRainbow44's avatar
KingRainbow44 committed
81
82
    }
}