HelpCommand.java 3.76 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;
6
import emu.grasscutter.game.Account;
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 = {"[<command>]"}, targetRequirement = Command.TargetRequirement.NONE)
KingRainbow44's avatar
KingRainbow44 committed
14
public final class HelpCommand implements CommandHandler {
15
    private final boolean SHOW_COMMANDS_WITHOUT_PERMISSIONS = false;  // TODO: Make this into a server config key
KingRainbow44's avatar
KingRainbow44 committed
16

17
18
19
20
21
22
23
24
25
    private String createCommand(Player player, CommandHandler command, List<String> args) {
        StringBuilder builder = new StringBuilder(command.getLabel())
            .append(" - ")
            .append(command.getDescriptionString(player))
            .append("\n\t")
            .append(command.getUsageString(player, args.toArray(new String[0])));

        Command annotation = command.getClass().getAnnotation(Command.class);
        if (annotation.aliases().length > 0) {
Luke H-W's avatar
Luke H-W committed
26
27
28
29
30
            builder.append("\n\t").append(translate(player, "commands.help.aliases"));
            for (String alias : annotation.aliases()) {
                builder.append(alias).append(" ");
            }
        }
31

Luke H-W's avatar
Luke H-W committed
32
        builder.append("\n\t").append(translate(player, "commands.help.tip_need_permission"));
33
        if (!annotation.permission().isEmpty()) {
Luke H-W's avatar
Luke H-W committed
34
            builder.append(annotation.permission());
35
36
        } else {
            builder.append(translate(player, "commands.help.tip_need_no_permission"));
Luke H-W's avatar
Luke H-W committed
37
38
        }

39
        if (!annotation.permissionTargeted().isEmpty()) {
Luke H-W's avatar
Luke H-W committed
40
41
42
            String permissionTargeted = annotation.permissionTargeted();
            builder.append(" ").append(translate(player, "commands.help.tip_permission_targeted", permissionTargeted));
        }
43
        return builder.toString();
Luke H-W's avatar
Luke H-W committed
44
45
    }

KingRainbow44's avatar
KingRainbow44 committed
46
    @Override
AnimeGitB's avatar
AnimeGitB committed
47
    public void execute(Player player, Player targetPlayer, List<String> args) {
48
49
50
51
52
        Account account = (player == null) ? null : player.getAccount();
        Map<String, CommandHandler> handlers = CommandMap.getInstance().getHandlers();
        List<String> commands = new ArrayList<>();
        List<String> commands_no_permission = new ArrayList<>();
        if (args.isEmpty()) {
KingRainbow44's avatar
KingRainbow44 committed
53
            for (String key : handlers.keySet()) {
54
55
56
57
58
59
                CommandHandler command = handlers.get(key);
                Command annotation = command.getClass().getAnnotation(Command.class);
                if (player == null || account.hasPermission(annotation.permission())) {
                    commands.add(createCommand(player, command, args));
                } else if (SHOW_COMMANDS_WITHOUT_PERMISSIONS) {
                    commands_no_permission.add(createCommand(player, command, args));
KingRainbow44's avatar
KingRainbow44 committed
60
61
62
                }
            }
        } else {
63
64
65
66
67
            String command_str = args.remove(0);
            CommandHandler command = handlers.get(command_str);
            if (command == null) {
                CommandHandler.sendTranslatedMessage(player, "commands.generic.command_exist_error");
                return;
KingRainbow44's avatar
KingRainbow44 committed
68
            } else {
69
70
71
72
73
                Command annotation = command.getClass().getAnnotation(Command.class);
                if (player == null || account.hasPermission(annotation.permission())) {
                    commands.add(createCommand(player, command, args));
                } else {
                    commands_no_permission.add(createCommand(player, command, args));
KingRainbow44's avatar
KingRainbow44 committed
74
75
76
                }
            }
        }
77
78
79
80
        for (String s : commands)
            CommandHandler.sendMessage(player, s);
        for (String s : commands_no_permission)
            CommandHandler.sendMessage(player, s + "\n\t" + translate(player, "commands.help.warn_player_has_no_permission"));
KingRainbow44's avatar
KingRainbow44 committed
81
82
    }
}