| 1 | package net.minecraft.server.commands;
|
| 2 |
|
| 3 | import com.google.common.net.InetAddresses;
|
| 4 | import com.mojang.brigadier.CommandDispatcher;
|
| 5 | import com.mojang.brigadier.arguments.StringArgumentType;
|
| 6 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 7 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 8 | import net.minecraft.commands.CommandSourceStack;
|
| 9 | import net.minecraft.commands.Commands;
|
| 10 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 11 | import net.minecraft.network.chat.Component;
|
| 12 | import net.minecraft.server.players.IpBanList;
|
| 13 |
|
| 14 | public class PardonIpCommand {
|
| 15 | private static final SimpleCommandExceptionType ERROR_INVALID = new SimpleCommandExceptionType(Component.translatable("commands.pardonip.invalid"));
|
| 16 | private static final SimpleCommandExceptionType ERROR_NOT_BANNED = new SimpleCommandExceptionType(Component.translatable("commands.pardonip.failed"));
|
| 17 |
|
| 18 | public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
|
| 19 | dispatcher.register(
|
| 20 | Commands.literal("pardon-ip")
|
| 21 | .requires(Commands.hasPermission(Commands.LEVEL_ADMINS))
|
| 22 | .then(
|
| 23 | Commands.argument("target", StringArgumentType.word())
|
| 24 | .suggests((c, p) -> SharedSuggestionProvider.suggest(c.getSource().getServer().getPlayerList().getIpBans().getUserList(), p))
|
| 25 | .executes(c -> unban(c.getSource(), StringArgumentType.getString(c, "target")))
|
| 26 | )
|
| 27 | );
|
| 28 | }
|
| 29 |
|
| 30 | private static int unban(final CommandSourceStack source, final String ip) throws CommandSyntaxException {
|
| 31 | if (!InetAddresses.isInetAddress(ip)) {
|
| 32 | throw ERROR_INVALID.create();
|
| 33 | }
|
| 34 |
|
| 35 | IpBanList bans = source.getServer().getPlayerList().getIpBans();
|
| 36 | if (!bans.isBanned(ip)) {
|
| 37 | throw ERROR_NOT_BANNED.create();
|
| 38 | }
|
| 39 |
|
| 40 | bans.remove(ip);
|
| 41 | source.sendSuccess(() -> Component.translatable("commands.pardonip.success", ip), true);
|
| 42 | return 1;
|
| 43 | }
|
| 44 | }
|
| 45 |
|