BanCommand.java 2.7 KB
Newer Older
Yazawazi's avatar
Yazawazi committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package emu.grasscutter.command.commands;

import java.util.List;

import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.Account;
import emu.grasscutter.game.player.Player;

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

@Command(
    label = "ban",
    usage = "ban <player> [time] [reason]",
    description = "commands.ban.description",
    targetRequirement = Command.TargetRequirement.NONE
)
public final class BanCommand implements CommandHandler {

    private boolean banAccount(int uid, int time, String reason) {
        Player player = Grasscutter.getGameServer().getPlayerByUid(uid, true);

        if (player == null) {
            return false;
        }

        Account account = player.getAccount();
        if (account == null) {
            account = DatabaseHelper.getAccountByPlayerId(uid);
Yazawazi's avatar
Yazawazi committed
32
33
34
            if (account == null) {
                return false;
            }
Yazawazi's avatar
Yazawazi committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
        }

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

        Player banUser = Grasscutter.getGameServer().getPlayerByUid(uid);

        if (banUser != null) {
            banUser.getSession().close();
        }
        return true;
    }

    @Override
    public void execute(Player sender, Player targetPlayer, List<String> args) {
        if (args.size() < 1) {
            CommandHandler.sendMessage(sender, translate(sender, "commands.ban.command_usage"));
            return;
        }

        int uid = 0;
        int time = 2051190000;
        String reason = "Reason not specified.";

        if (args.size() >= 1) {
            try {
                uid = Integer.parseInt(args.get(0));
            } catch (NumberFormatException ignored) {
                CommandHandler.sendMessage(sender, translate(sender, "commands.ban.invalid_player_id"));
                return;
            }
        }

        if (args.size() >= 2) {
            try {
                time = Integer.parseInt(args.get(1));
            } catch (NumberFormatException ignored) {
                CommandHandler.sendMessage(sender, translate(sender, "commands.ban.invalid_time"));
                return;
            }
        }

        if (args.size() >= 3) {
            reason = args.get(2);
        }

        if (banAccount(uid, time, reason)) {
            CommandHandler.sendMessage(sender, translate(sender, "commands.ban.success"));
        } else {
            CommandHandler.sendMessage(sender, translate(sender, "commands.ban.failure"));
        }
    }
}