WhitelistCommand .java

net.minecraft.server.commands.WhitelistCommand (class): members: ERROR_ALREADY_WHITELISTED, ERROR_ALREADY_DISABLED, ERROR_ALREADY_ENABLED, register.

net.minecraft.server.commandsclass144 linesserver5.6 KB

API Surface (1)

void register(final CommandDispatcher<CommandSourceStack> dispatcher)

Fields & Constants (4)

ERROR_ALREADY_ENABLED
ERROR_ALREADY_DISABLED
ERROR_ALREADY_WHITELISTED
ERROR_NOT_WHITELISTED

String literals (16)

"commands.whitelist.alreadyOn"
"commands.whitelist.alreadyOff"
"commands.whitelist.add.failed"
"commands.whitelist.remove.failed"
"whitelist"
"list"
"targets"
"remove"
"reload"
"commands.whitelist.reloaded"
"commands.whitelist.add.success"
"commands.whitelist.remove.success"
"commands.whitelist.enabled"
"commands.whitelist.disabled"
"commands.whitelist.none"
"commands.whitelist.list"

Full Source

WhitelistCommand.java144 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.SharedSuggestionProvider;
10
import net.minecraft.commands.arguments.GameProfileArgument;
11
import net.minecraft.network.chat.Component;
12
import net.minecraft.server.players.NameAndId;
13
import net.minecraft.server.players.PlayerList;
14
import net.minecraft.server.players.UserWhiteList;
15
import net.minecraft.server.players.UserWhiteListEntry;
16
import net.minecraft.world.entity.player.Player;
17
18
public class WhitelistCommand {
19
	private static final SimpleCommandExceptionType ERROR_ALREADY_ENABLED = new SimpleCommandExceptionType(Component.translatable("commands.whitelist.alreadyOn"));
20
	private static final SimpleCommandExceptionType ERROR_ALREADY_DISABLED = new SimpleCommandExceptionType(
21
		Component.translatable("commands.whitelist.alreadyOff")
22
	);
23
	private static final SimpleCommandExceptionType ERROR_ALREADY_WHITELISTED = new SimpleCommandExceptionType(
24
		Component.translatable("commands.whitelist.add.failed")
25
	);
26
	private static final SimpleCommandExceptionType ERROR_NOT_WHITELISTED = new SimpleCommandExceptionType(
27
		Component.translatable("commands.whitelist.remove.failed")
28
	);
29
30
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
31
		dispatcher.register(
32
			Commands.literal("whitelist")
33
				.requires(Commands.hasPermission(Commands.LEVEL_ADMINS))
34
				.then(Commands.literal("on").executes(c -> enableWhitelist(c.getSource())))
35
				.then(Commands.literal("off").executes(c -> disableWhitelist(c.getSource())))
36
				.then(Commands.literal("list").executes(c -> showList(c.getSource())))
37
				.then(
38
					Commands.literal("add")
39
						.then(
40
							Commands.argument("targets", GameProfileArgument.gameProfile())
41
								.suggests(
42
									(c, p) -> {
43
										PlayerList list = c.getSource().getServer().getPlayerList();
44
										return SharedSuggestionProvider.suggest(
45
											list.getPlayers().stream().map(Player::nameAndId).filter(nameAndId -> !list.getWhiteList().isWhiteListed(nameAndId)).map(NameAndId::name), p
46
										);
47
									}
48
								)
49
								.executes(c -> addPlayers(c.getSource(), GameProfileArgument.getGameProfiles(c, "targets")))
50
						)
51
				)
52
				.then(
53
					Commands.literal("remove")
54
						.then(
55
							Commands.argument("targets", GameProfileArgument.gameProfile())
56
								.suggests((c, p) -> SharedSuggestionProvider.suggest(c.getSource().getServer().getPlayerList().getWhiteListNames(), p))
57
								.executes(c -> removePlayers(c.getSource(), GameProfileArgument.getGameProfiles(c, "targets")))
58
						)
59
				)
60
				.then(Commands.literal("reload").executes(c -> reload(c.getSource())))
61
		);
62
	}
63
64
	private static int reload(final CommandSourceStack source) {
65
		source.getServer().getPlayerList().reloadWhiteList();
66
		source.sendSuccess(() -> Component.translatable("commands.whitelist.reloaded"), true);
67
		source.getServer().kickUnlistedPlayers();
68
		return 1;
69
	}
70
71
	private static int addPlayers(final CommandSourceStack source, final Collection<NameAndId> targets) throws CommandSyntaxException {
72
		UserWhiteList list = source.getServer().getPlayerList().getWhiteList();
73
		int success = 0;
74
75
		for (NameAndId target : targets) {
76
			if (!list.isWhiteListed(target)) {
77
				UserWhiteListEntry entry = new UserWhiteListEntry(target);
78
				list.add(entry);
79
				source.sendSuccess(() -> Component.translatable("commands.whitelist.add.success", Component.literal(target.name())), true);
80
				success++;
81
			}
82
		}
83
84
		if (success == 0) {
85
			throw ERROR_ALREADY_WHITELISTED.create();
86
		} else {
87
			return success;
88
		}
89
	}
90
91
	private static int removePlayers(final CommandSourceStack source, final Collection<NameAndId> targets) throws CommandSyntaxException {
92
		UserWhiteList list = source.getServer().getPlayerList().getWhiteList();
93
		int success = 0;
94
95
		for (NameAndId target : targets) {
96
			if (list.isWhiteListed(target)) {
97
				UserWhiteListEntry entry = new UserWhiteListEntry(target);
98
				list.remove(entry);
99
				source.sendSuccess(() -> Component.translatable("commands.whitelist.remove.success", Component.literal(target.name())), true);
100
				success++;
101
			}
102
		}
103
104
		if (success == 0) {
105
			throw ERROR_NOT_WHITELISTED.create();
106
		}
107
108
		source.getServer().kickUnlistedPlayers();
109
		return success;
110
	}
111
112
	private static int enableWhitelist(final CommandSourceStack source) throws CommandSyntaxException {
113
		if (source.getServer().isUsingWhitelist()) {
114
			throw ERROR_ALREADY_ENABLED.create();
115
		}
116
117
		source.getServer().setUsingWhitelist(true);
118
		source.sendSuccess(() -> Component.translatable("commands.whitelist.enabled"), true);
119
		source.getServer().kickUnlistedPlayers();
120
		return 1;
121
	}
122
123
	private static int disableWhitelist(final CommandSourceStack source) throws CommandSyntaxException {
124
		if (!source.getServer().isUsingWhitelist()) {
125
			throw ERROR_ALREADY_DISABLED.create();
126
		}
127
128
		source.getServer().setUsingWhitelist(false);
129
		source.sendSuccess(() -> Component.translatable("commands.whitelist.disabled"), true);
130
		return 1;
131
	}
132
133
	private static int showList(final CommandSourceStack source) {
134
		String[] list = source.getServer().getPlayerList().getWhiteListNames();
135
		if (list.length == 0) {
136
			source.sendSuccess(() -> Component.translatable("commands.whitelist.none"), false);
137
		} else {
138
			source.sendSuccess(() -> Component.translatable("commands.whitelist.list", list.length, String.join(", ", list)), false);
139
		}
140
141
		return list.length;
142
	}
143
}
144

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