BanIpCommands .java

net.minecraft.server.commands.BanIpCommands (class, 72 lines): members: ERROR_INVALID_IP, ERROR_ALREADY_BANNED, register. strings: "commands.banip.failed".

net.minecraft.server.commandsclass72 linesserver3.1 KB

API Surface (1)

void register(final CommandDispatcher<CommandSourceStack> dispatcher)

Fields & Constants (2)

ERROR_INVALID_IP
ERROR_ALREADY_BANNED

String literals (8)

"commands.banip.invalid"
"commands.banip.failed"
"ban-ip"
"target"
"reason"
"commands.banip.success"
"commands.banip.info"
"multiplayer.disconnect.ip_banned"

Full Source

BanIpCommands.java72 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.commands;
2
3
import com.google.common.net.InetAddresses;
4
import com.mojang.brigadier.CommandDispatcher;
5
import com.mojang.brigadier.arguments.StringArgumentType;
6
import com.mojang.brigadier.exceptions.CommandSyntaxException;
7
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
8
import java.util.List;
9
import net.minecraft.commands.CommandSourceStack;
10
import net.minecraft.commands.Commands;
11
import net.minecraft.commands.arguments.MessageArgument;
12
import net.minecraft.commands.arguments.selector.EntitySelector;
13
import net.minecraft.network.chat.Component;
14
import net.minecraft.server.level.ServerPlayer;
15
import net.minecraft.server.players.IpBanList;
16
import net.minecraft.server.players.IpBanListEntry;
17
import org.jspecify.annotations.Nullable;
18
19
public class BanIpCommands {
20
	private static final SimpleCommandExceptionType ERROR_INVALID_IP = new SimpleCommandExceptionType(Component.translatable("commands.banip.invalid"));
21
	private static final SimpleCommandExceptionType ERROR_ALREADY_BANNED = new SimpleCommandExceptionType(Component.translatable("commands.banip.failed"));
22
23
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
24
		dispatcher.register(
25
			Commands.literal("ban-ip")
26
				.requires(Commands.hasPermission(Commands.LEVEL_ADMINS))
27
				.then(
28
					Commands.argument("target", StringArgumentType.word())
29
						.executes(c -> banIpOrName(c.getSource(), StringArgumentType.getString(c, "target"), null))
30
						.then(
31
							Commands.argument("reason", MessageArgument.message())
32
								.executes(c -> banIpOrName(c.getSource(), StringArgumentType.getString(c, "target"), MessageArgument.getMessage(c, "reason")))
33
						)
34
				)
35
		);
36
	}
37
38
	private static int banIpOrName(final CommandSourceStack source, final String target, final @Nullable Component reason) throws CommandSyntaxException {
39
		if (InetAddresses.isInetAddress(target)) {
40
			return banIp(source, target, reason);
41
		} else {
42
			ServerPlayer player = source.getServer().getPlayerList().getPlayerByName(target);
43
			if (player != null) {
44
				return banIp(source, player.getIpAddress(), reason);
45
			} else {
46
				throw ERROR_INVALID_IP.create();
47
			}
48
		}
49
	}
50
51
	private static int banIp(final CommandSourceStack source, final String ip, final @Nullable Component reason) throws CommandSyntaxException {
52
		IpBanList list = source.getServer().getPlayerList().getIpBans();
53
		if (list.isBanned(ip)) {
54
			throw ERROR_ALREADY_BANNED.create();
55
		}
56
57
		List<ServerPlayer> players = source.getServer().getPlayerList().getPlayersWithAddress(ip);
58
		IpBanListEntry entry = new IpBanListEntry(ip, null, source.getTextName(), null, reason == null ? null : reason.getString());
59
		list.add(entry);
60
		source.sendSuccess(() -> Component.translatable("commands.banip.success", ip, entry.getReasonMessage()), true);
61
		if (!players.isEmpty()) {
62
			source.sendSuccess(() -> Component.translatable("commands.banip.info", players.size(), EntitySelector.joinNames(players)), true);
63
		}
64
65
		for (ServerPlayer player : players) {
66
			player.connection.disconnect(Component.translatable("multiplayer.disconnect.ip_banned"));
67
		}
68
69
		return players.size();
70
	}
71
}
72

Decompiled Minecraft 26.3 source of BanIpCommands (net/minecraft/server/commands/BanIpCommands.java) with Mojang official mappings, stored verbatim on the site.