BanCommand.java 1.99 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 = "ban <@player> [time] [reason]",
Yazawazi's avatar
Yazawazi committed
14
    description = "commands.ban.description",
15
16
    permission = "server.ban",
    targetRequirement = Command.TargetRequirement.PLAYER
Yazawazi's avatar
Yazawazi committed
17
18
19
)
public final class BanCommand implements CommandHandler {

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

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

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

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

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

45
46
47
48
49
50
51
52
53
54
55
56
        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
57
58
        }

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