OpCommand .java

net.minecraft.server.commands.OpCommand (class): members: ERROR_ALREADY_OP, register. strings: "commands.op.success", "commands.op.failed", "targets".

net.minecraft.server.commandsclass56 linesserver1.9 KB

API Surface (1)

void register(final CommandDispatcher<CommandSourceStack> dispatcher)

Fields & Constants (1)

ERROR_ALREADY_OP

String literals (3)

"commands.op.failed"
"targets"
"commands.op.success"

Full Source

OpCommand.java56 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
15
public class OpCommand {
16
	private static final SimpleCommandExceptionType ERROR_ALREADY_OP = new SimpleCommandExceptionType(Component.translatable("commands.op.failed"));
17
18
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
19
		dispatcher.register(
20
			Commands.literal("op")
21
				.requires(Commands.hasPermission(Commands.LEVEL_ADMINS))
22
				.then(
23
					Commands.argument("targets", GameProfileArgument.gameProfile())
24
						.suggests(
25
							(c, p) -> {
26
								PlayerList list = c.getSource().getServer().getPlayerList();
27
								return SharedSuggestionProvider.suggest(
28
									list.getPlayers().stream().filter(player -> !list.isOp(player.nameAndId())).map(pl -> pl.getGameProfile().name()), p
29
								);
30
							}
31
						)
32
						.executes(c -> opPlayers(c.getSource(), GameProfileArgument.getGameProfiles(c, "targets")))
33
				)
34
		);
35
	}
36
37
	private static int opPlayers(final CommandSourceStack source, final Collection<NameAndId> players) throws CommandSyntaxException {
38
		PlayerList list = source.getServer().getPlayerList();
39
		int count = 0;
40
41
		for (NameAndId player : players) {
42
			if (!list.isOp(player)) {
43
				list.op(player);
44
				count++;
45
				source.sendSuccess(() -> Component.translatable("commands.op.success", player.name()), true);
46
			}
47
		}
48
49
		if (count == 0) {
50
			throw ERROR_ALREADY_OP.create();
51
		} else {
52
			return count;
53
		}
54
	}
55
}
56

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