HelpCommand.java 3.87 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
        Account account = (player == null) ? null : player.getAccount();
AnimeGitB's avatar
AnimeGitB committed
49
        var commandMap = CommandMap.getInstance();
50
51
52
        List<String> commands = new ArrayList<>();
        List<String> commands_no_permission = new ArrayList<>();
        if (args.isEmpty()) {
AnimeGitB's avatar
AnimeGitB committed
53
            commandMap.getHandlers().forEach((key, command) -> {
54
55
56
57
58
                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
59
                }
AnimeGitB's avatar
AnimeGitB committed
60
            });
61
            CommandHandler.sendTranslatedMessage(player, "commands.help.available_commands");
KingRainbow44's avatar
KingRainbow44 committed
62
        } else {
63
            String command_str = args.remove(0).toLowerCase();
AnimeGitB's avatar
AnimeGitB committed
64
            CommandHandler command = commandMap.getHandler(command_str);
65
66
            if (command == null) {
                CommandHandler.sendTranslatedMessage(player, "commands.generic.command_exist_error");
AnimeGitB's avatar
AnimeGitB committed
67
                CommandHandler.sendMessage(player, "Command: " + command_str);
68
                return;
KingRainbow44's avatar
KingRainbow44 committed
69
            } else {
70
71
72
73
74
                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
75
76
77
                }
            }
        }
AnimeGitB's avatar
AnimeGitB committed
78
79
80
        final String suf = "\n\t" + translate(player, "commands.help.warn_player_has_no_permission");
        commands.forEach(s -> CommandHandler.sendMessage(player, s));
        commands_no_permission.forEach(s -> CommandHandler.sendMessage(player, s + suf));
KingRainbow44's avatar
KingRainbow44 committed
81
82
    }
}