BanIpCommands.java

net.minecraft.server.commands.BanIpCommands

信息

  • 全限定名:net.minecraft.server.commands.BanIpCommands
  • 类型:public class
  • 包:net.minecraft.server.commands
  • 源码路径:src/main/java/net/minecraft/server/commands/BanIpCommands.java
  • 起始行号:L19
  • 职责:

    TODO

字段/常量

  • ERROR_INVALID_IP

    • 类型: SimpleCommandExceptionType
    • 修饰符: private static final
    • 源码定位: L20
    • 说明:

      TODO

  • ERROR_ALREADY_BANNED

    • 类型: SimpleCommandExceptionType
    • 修饰符: private static final
    • 源码定位: L21
    • 说明:

      TODO

内部类/嵌套类型

构造器

方法

下面的方法块按源码顺序生成。

public static void register(CommandDispatcher<CommandSourceStack> dispatcher) @ L23

  • 方法名:register
  • 源码定位:L23
  • 返回类型:void
  • 修饰符:public static

参数:

  • dispatcher: CommandDispatcher

说明:

TODO

private static int banIpOrName(CommandSourceStack source, String target, Component reason) @ L38

  • 方法名:banIpOrName
  • 源码定位:L38
  • 返回类型:int
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • target: String
  • reason: Component

说明:

TODO

private static int banIp(CommandSourceStack source, String ip, Component reason) @ L51

  • 方法名:banIp
  • 源码定位:L51
  • 返回类型:int
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • ip: String
  • reason: Component

说明:

TODO

代码

public class BanIpCommands {
    private static final SimpleCommandExceptionType ERROR_INVALID_IP = new SimpleCommandExceptionType(Component.translatable("commands.banip.invalid"));
    private static final SimpleCommandExceptionType ERROR_ALREADY_BANNED = new SimpleCommandExceptionType(Component.translatable("commands.banip.failed"));
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
        dispatcher.register(
            Commands.literal("ban-ip")
                .requires(Commands.hasPermission(Commands.LEVEL_ADMINS))
                .then(
                    Commands.argument("target", StringArgumentType.word())
                        .executes(c -> banIpOrName(c.getSource(), StringArgumentType.getString(c, "target"), null))
                        .then(
                            Commands.argument("reason", MessageArgument.message())
                                .executes(c -> banIpOrName(c.getSource(), StringArgumentType.getString(c, "target"), MessageArgument.getMessage(c, "reason")))
                        )
                )
        );
    }
 
    private static int banIpOrName(CommandSourceStack source, String target, @Nullable Component reason) throws CommandSyntaxException {
        if (InetAddresses.isInetAddress(target)) {
            return banIp(source, target, reason);
        } else {
            ServerPlayer player = source.getServer().getPlayerList().getPlayerByName(target);
            if (player != null) {
                return banIp(source, player.getIpAddress(), reason);
            } else {
                throw ERROR_INVALID_IP.create();
            }
        }
    }
 
    private static int banIp(CommandSourceStack source, String ip, @Nullable Component reason) throws CommandSyntaxException {
        IpBanList list = source.getServer().getPlayerList().getIpBans();
        if (list.isBanned(ip)) {
            throw ERROR_ALREADY_BANNED.create();
        } else {
            List<ServerPlayer> players = source.getServer().getPlayerList().getPlayersWithAddress(ip);
            IpBanListEntry entry = new IpBanListEntry(ip, null, source.getTextName(), null, reason == null ? null : reason.getString());
            list.add(entry);
            source.sendSuccess(() -> Component.translatable("commands.banip.success", ip, entry.getReasonMessage()), true);
            if (!players.isEmpty()) {
                source.sendSuccess(() -> Component.translatable("commands.banip.info", players.size(), EntitySelector.joinNames(players)), true);
            }
 
            for (ServerPlayer player : players) {
                player.connection.disconnect(Component.translatable("multiplayer.disconnect.ip_banned"));
            }
 
            return players.size();
        }
    }
}

引用的其他类

  • CommandSourceStack

    • 引用位置: 参数
  • Commands

    • 引用位置: 方法调用
    • 关联成员: Commands.argument(), Commands.hasPermission(), Commands.literal()
  • MessageArgument

    • 引用位置: 方法调用
    • 关联成员: MessageArgument.getMessage(), MessageArgument.message()
  • EntitySelector

    • 引用位置: 方法调用
    • 关联成员: EntitySelector.joinNames()
  • Component

    • 引用位置: 参数/方法调用
    • 关联成员: Component.translatable()
  • IpBanListEntry

    • 引用位置: 构造调用
    • 关联成员: IpBanListEntry()