File size: 4,047 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
32
33
34
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
package net.minecraft.server.commands;

import com.google.common.net.InetAddresses;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import java.util.List;
import javax.annotation.Nullable;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.MessageArgument;
import net.minecraft.commands.arguments.selector.EntitySelector;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.players.IpBanList;
import net.minecraft.server.players.IpBanListEntry;

public class BanIpCommands {
    private static final SimpleCommandExceptionType ERROR_INVALID_IP = new SimpleCommandExceptionType(Component.translatable("commands.banip.invalid"));
    private static final SimpleCommandExceptionType ERROR_ALREADY_BANNED = new SimpleCommandExceptionType(Component.translatable("commands.banip.failed"));

    public static void register(CommandDispatcher<CommandSourceStack> p_136528_) {
        p_136528_.register(
            Commands.literal("ban-ip")
                .requires(p_136532_ -> p_136532_.hasPermission(3))
                .then(
                    Commands.argument("target", StringArgumentType.word())
                        .executes(p_136538_ -> banIpOrName(p_136538_.getSource(), StringArgumentType.getString(p_136538_, "target"), null))
                        .then(
                            Commands.argument("reason", MessageArgument.message())
                                .executes(
                                    p_136530_ -> banIpOrName(
                                            p_136530_.getSource(),
                                            StringArgumentType.getString(p_136530_, "target"),
                                            MessageArgument.getMessage(p_136530_, "reason")
                                        )
                                )
                        )
                )
        );
    }

    private static int banIpOrName(CommandSourceStack p_136534_, String p_136535_, @Nullable Component p_136536_) throws CommandSyntaxException {
        if (InetAddresses.isInetAddress(p_136535_)) {
            return banIp(p_136534_, p_136535_, p_136536_);
        } else {
            ServerPlayer serverplayer = p_136534_.getServer().getPlayerList().getPlayerByName(p_136535_);
            if (serverplayer != null) {
                return banIp(p_136534_, serverplayer.getIpAddress(), p_136536_);
            } else {
                throw ERROR_INVALID_IP.create();
            }
        }
    }

    private static int banIp(CommandSourceStack p_136540_, String p_136541_, @Nullable Component p_136542_) throws CommandSyntaxException {
        IpBanList ipbanlist = p_136540_.getServer().getPlayerList().getIpBans();
        if (ipbanlist.isBanned(p_136541_)) {
            throw ERROR_ALREADY_BANNED.create();
        } else {
            List<ServerPlayer> list = p_136540_.getServer().getPlayerList().getPlayersWithAddress(p_136541_);
            IpBanListEntry ipbanlistentry = new IpBanListEntry(p_136541_, null, p_136540_.getTextName(), null, p_136542_ == null ? null : p_136542_.getString());
            ipbanlist.add(ipbanlistentry);
            p_136540_.sendSuccess(() -> Component.translatable("commands.banip.success", p_136541_, ipbanlistentry.getReason()), true);
            if (!list.isEmpty()) {
                p_136540_.sendSuccess(() -> Component.translatable("commands.banip.info", list.size(), EntitySelector.joinNames(list)), true);
            }

            for (ServerPlayer serverplayer : list) {
                serverplayer.connection.disconnect(Component.translatable("multiplayer.disconnect.ip_banned"));
            }

            return list.size();
        }
    }
}