BanPlayerCommands .java

net.minecraft.server.commands.BanPlayerCommands (class, 60 lines): members: ERROR_ALREADY_BANNED, register. strings: "commands.ban.success", "reason".

net.minecraft.server.commandsclass60 linesserver2.4 KB

API Surface (1)

void register(final CommandDispatcher<CommandSourceStack> dispatcher)

Fields & Constants (1)

ERROR_ALREADY_BANNED

String literals (5)

"commands.ban.failed"
"targets"
"reason"
"commands.ban.success"
"multiplayer.disconnect.banned"

Full Source

BanPlayerCommands.java60 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.commands;
2
3
import com.mojang.brigadier.CommandDispatcher;
4
import com.mojang.brigadier.exceptions.CommandSyntaxException;
5
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
6
import java.util.Collection;
7
import net.minecraft.commands.CommandSourceStack;
8
import net.minecraft.commands.Commands;
9
import net.minecraft.commands.arguments.GameProfileArgument;
10
import net.minecraft.commands.arguments.MessageArgument;
11
import net.minecraft.network.chat.Component;
12
import net.minecraft.server.level.ServerPlayer;
13
import net.minecraft.server.players.NameAndId;
14
import net.minecraft.server.players.UserBanList;
15
import net.minecraft.server.players.UserBanListEntry;
16
import org.jspecify.annotations.Nullable;
17
18
public class BanPlayerCommands {
19
	private static final SimpleCommandExceptionType ERROR_ALREADY_BANNED = new SimpleCommandExceptionType(Component.translatable("commands.ban.failed"));
20
21
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
22
		dispatcher.register(
23
			Commands.literal("ban")
24
				.requires(Commands.hasPermission(Commands.LEVEL_ADMINS))
25
				.then(
26
					Commands.argument("targets", GameProfileArgument.gameProfile())
27
						.executes(c -> banPlayers(c.getSource(), GameProfileArgument.getGameProfiles(c, "targets"), null))
28
						.then(
29
							Commands.argument("reason", MessageArgument.message())
30
								.executes(c -> banPlayers(c.getSource(), GameProfileArgument.getGameProfiles(c, "targets"), MessageArgument.getMessage(c, "reason")))
31
						)
32
				)
33
		);
34
	}
35
36
	private static int banPlayers(final CommandSourceStack source, final Collection<NameAndId> players, final @Nullable Component reason) throws CommandSyntaxException {
37
		UserBanList list = source.getServer().getPlayerList().getBans();
38
		int count = 0;
39
40
		for (NameAndId player : players) {
41
			if (!list.isBanned(player)) {
42
				UserBanListEntry entry = new UserBanListEntry(player, null, source.getTextName(), null, reason == null ? null : reason.getString());
43
				list.add(entry);
44
				count++;
45
				source.sendSuccess(() -> Component.translatable("commands.ban.success", Component.literal(player.name()), entry.getReasonMessage()), true);
46
				ServerPlayer online = source.getServer().getPlayerList().getPlayer(player.id());
47
				if (online != null) {
48
					online.connection.disconnect(Component.translatable("multiplayer.disconnect.banned"));
49
				}
50
			}
51
		}
52
53
		if (count == 0) {
54
			throw ERROR_ALREADY_BANNED.create();
55
		} else {
56
			return count;
57
		}
58
	}
59
}
60

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