| 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 DeOpCommands {
|
| 16 | private static final SimpleCommandExceptionType ERROR_NOT_OP = new SimpleCommandExceptionType(Component.translatable("commands.deop.failed"));
|
| 17 |
|
| 18 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 19 | dispatcher.register(
|
| 20 | Commands.literal("deop")
|
| 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().getOpNames(), p))
|
| 25 | .executes(c -> deopPlayers(c.getSource(), GameProfileArgument.getGameProfiles(c, "targets")))
|
| 26 | )
|
| 27 | );
|
| 28 | }
|
| 29 |
|
| 30 | private static int deopPlayers(final CommandSourceStack source, final Collection<NameAndId> players) throws CommandSyntaxException {
|
| 31 | PlayerList list = source.getServer().getPlayerList();
|
| 32 | int count = 0;
|
| 33 |
|
| 34 | for (NameAndId player : players) {
|
| 35 | if (list.isOp(player)) {
|
| 36 | list.deop(player);
|
| 37 | count++;
|
| 38 | source.sendSuccess(() -> Component.translatable("commands.deop.success", player.name()), true);
|
| 39 | }
|
| 40 | }
|
| 41 |
|
| 42 | if (count == 0) {
|
| 43 | throw ERROR_NOT_OP.create();
|
| 44 | }
|
| 45 |
|
| 46 | source.getServer().kickUnlistedPlayers();
|
| 47 | return count;
|
| 48 | }
|
| 49 | }
|
| 50 |
|