PardonCommand .java

net.minecraft.server.commands.PardonCommand (class, 49 lines): members: ERROR_NOT_BANNED, register. strings: "commands.pardon.success", "pardon".

net.minecraft.server.commandsclass49 linesserver1.8 KB

API Surface (1)

void register(final CommandDispatcher<CommandSourceStack> dispatcher)

Fields & Constants (1)

ERROR_NOT_BANNED

String literals (4)

"commands.pardon.failed"
"pardon"
"targets"
"commands.pardon.success"

Full Source

PardonCommand.java49 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.UserBanList;
14
15
public class PardonCommand {
16
	private static final SimpleCommandExceptionType ERROR_NOT_BANNED = new SimpleCommandExceptionType(Component.translatable("commands.pardon.failed"));
17
18
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
19
		dispatcher.register(
20
			Commands.literal("pardon")
21
				.requires(Commands.hasPermission(Commands.LEVEL_ADMINS))
22
				.then(
23
					Commands.argument("targets", GameProfileArgument.gameProfile())
24
						.suggests((c, p) -> SharedSuggestionProvider.suggest(c.getSource().getServer().getPlayerList().getBans().getUserList(), p))
25
						.executes(c -> pardonPlayers(c.getSource(), GameProfileArgument.getGameProfiles(c, "targets")))
26
				)
27
		);
28
	}
29
30
	private static int pardonPlayers(final CommandSourceStack source, final Collection<NameAndId> players) throws CommandSyntaxException {
31
		UserBanList list = source.getServer().getPlayerList().getBans();
32
		int count = 0;
33
34
		for (NameAndId player : players) {
35
			if (list.isBanned(player)) {
36
				list.remove(player);
37
				count++;
38
				source.sendSuccess(() -> Component.translatable("commands.pardon.success", Component.literal(player.name())), true);
39
			}
40
		}
41
42
		if (count == 0) {
43
			throw ERROR_NOT_BANNED.create();
44
		} else {
45
			return count;
46
		}
47
	}
48
}
49

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