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

import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
6
import emu.grasscutter.command.Command.TargetRequirement;
KingRainbow44's avatar
KingRainbow44 committed
7
import emu.grasscutter.game.Account;
Melledy's avatar
Melledy committed
8
import emu.grasscutter.game.player.Player;
KingRainbow44's avatar
KingRainbow44 committed
9
10
11

import java.util.List;

12
13
import static emu.grasscutter.utils.Language.translate;

14
15
16
17
18
19
@Command(label = "permission", usage = {
    "add <permission>",
    "remove <permission>",
    "clear",
    "list"
}, permission = "permission", targetRequirement = TargetRequirement.PLAYER)
KingRainbow44's avatar
KingRainbow44 committed
20
21
22
public final class PermissionCommand implements CommandHandler {

    @Override
AnimeGitB's avatar
AnimeGitB committed
23
    public void execute(Player sender, Player targetPlayer, List<String> args) {
24
        if (args.isEmpty() || args.size() > 2) {
25
            sendUsageMessage(sender);
KingRainbow44's avatar
KingRainbow44 committed
26
27
28
            return;
        }

github-actions's avatar
github-actions committed
29
        if (!Grasscutter.getPermissionHandler().EnablePermissionCommand()) {
4Benj_'s avatar
4Benj_ committed
30
31
32
33
            CommandHandler.sendTranslatedMessage(sender, "commands.generic.permission_error");
            return;
        }

KingRainbow44's avatar
KingRainbow44 committed
34
        String action = args.get(0);
35
36
37
38
        String permission = "";
        if (args.size() > 1) {
            permission = args.get(1);
        }
KingRainbow44's avatar
KingRainbow44 committed
39

AnimeGitB's avatar
AnimeGitB committed
40
        Account account = targetPlayer.getAccount();
KingRainbow44's avatar
KingRainbow44 committed
41
        if (account == null) {
Secretboy's avatar
Secretboy committed
42
            CommandHandler.sendMessage(sender, translate(sender, "commands.permission.account_error"));
KingRainbow44's avatar
KingRainbow44 committed
43
44
45
46
47
            return;
        }

        switch (action) {
            default:
48
                sendUsageMessage(sender);
KingRainbow44's avatar
KingRainbow44 committed
49
50
                break;
            case "add":
51
52
53
                if (permission.isEmpty()) {
                    sendUsageMessage(sender);
                } else if (account.addPermission(permission)) {
Secretboy's avatar
Secretboy committed
54
55
                    CommandHandler.sendMessage(sender, translate(sender, "commands.permission.add"));
                } else CommandHandler.sendMessage(sender, translate(sender, "commands.permission.has_error"));
KingRainbow44's avatar
KingRainbow44 committed
56
57
58
                break;
            case "remove":
                if (account.removePermission(permission)) {
Secretboy's avatar
Secretboy committed
59
60
                    CommandHandler.sendMessage(sender, translate(sender, "commands.permission.remove"));
                } else CommandHandler.sendMessage(sender, translate(sender, "commands.permission.not_have_error"));
KingRainbow44's avatar
KingRainbow44 committed
61
                break;
62
63
64
65
66
67
68
            case "clear":
                account.clearPermission();
                CommandHandler.sendMessage(sender, translate(sender, "commands.permission.remove"));
                break;
            case "list":
                CommandHandler.sendMessage(sender, String.join("\n", account.getPermissions()));
                break;
KingRainbow44's avatar
KingRainbow44 committed
69
70
71
72
        }

        account.save();
    }
Secretboy's avatar
Secretboy committed
73
}