BanCommand.java 1.93 KB
Newer Older
Yazawazi's avatar
Yazawazi committed
1
2
3
4
5
6
7
8
package emu.grasscutter.command.commands;

import java.util.List;

import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.Account;
import emu.grasscutter.game.player.Player;
9
import emu.grasscutter.server.game.GameSession;
Yazawazi's avatar
Yazawazi committed
10
11
12

@Command(
    label = "ban",
13
    usage = {"[<time> [<reason>]]"},
14
15
    permission = "server.ban",
    targetRequirement = Command.TargetRequirement.PLAYER
Yazawazi's avatar
Yazawazi committed
16
17
18
)
public final class BanCommand implements CommandHandler {

19
20
    private boolean banAccount(Player targetPlayer, int time, String reason) {
        Account account = targetPlayer.getAccount();
Yazawazi's avatar
Yazawazi committed
21
22

        if (account == null) {
23
            return false;
Yazawazi's avatar
Yazawazi committed
24
25
26
27
28
29
30
31
        }

        account.setBanReason(reason);
        account.setBanEndTime(time);
        account.setBanStartTime((int) System.currentTimeMillis() / 1000);
        account.setBanned(true);
        account.save();

32
33
34
        GameSession session = targetPlayer.getSession();
        if (session != null) {
            session.close();
Yazawazi's avatar
Yazawazi committed
35
36
37
38
39
40
41
42
43
        }
        return true;
    }

    @Override
    public void execute(Player sender, Player targetPlayer, List<String> args) {
        int time = 2051190000;
        String reason = "Reason not specified.";

44
45
46
47
48
49
50
51
52
53
54
55
        switch (args.size()) {
            case 2:
                reason = args.get(1);  // Fall-through
            case 1:
                try {
                    time = Integer.parseInt(args.get(0));
                } catch (NumberFormatException ignored) {
                    CommandHandler.sendTranslatedMessage(sender, "commands.ban.invalid_time");
                    return;
                }  // Fall-through, unimportant
            default:
                break;
Yazawazi's avatar
Yazawazi committed
56
57
        }

58
59
        if (banAccount(targetPlayer, time, reason)) {
            CommandHandler.sendTranslatedMessage(sender, "commands.ban.success");
Yazawazi's avatar
Yazawazi committed
60
        } else {
61
            CommandHandler.sendTranslatedMessage(sender, "commands.ban.failure");
Yazawazi's avatar
Yazawazi committed
62
63
        }
    }
github-actions's avatar
github-actions committed
64
}