WhitelistCommand.java

net.minecraft.server.commands.WhitelistCommand

信息

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

    TODO

字段/常量

  • ERROR_ALREADY_ENABLED

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

      TODO

  • ERROR_ALREADY_DISABLED

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

      TODO

  • ERROR_ALREADY_WHITELISTED

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

      TODO

  • ERROR_NOT_WHITELISTED

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

      TODO

内部类/嵌套类型

构造器

方法

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

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

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

参数:

  • dispatcher: CommandDispatcher

说明:

TODO

private static int reload(CommandSourceStack source) @ L71

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

参数:

  • source: CommandSourceStack

说明:

TODO

private static int addPlayers(CommandSourceStack source, Collection<NameAndId> targets) @ L78

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

参数:

  • source: CommandSourceStack
  • targets: Collection

说明:

TODO

private static int removePlayers(CommandSourceStack source, Collection<NameAndId> targets) @ L98

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

参数:

  • source: CommandSourceStack
  • targets: Collection

说明:

TODO

private static int enableWhitelist(CommandSourceStack source) @ L119

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

参数:

  • source: CommandSourceStack

说明:

TODO

private static int disableWhitelist(CommandSourceStack source) @ L130

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

参数:

  • source: CommandSourceStack

说明:

TODO

private static int showList(CommandSourceStack source) @ L140

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

参数:

  • source: CommandSourceStack

说明:

TODO

代码

public class WhitelistCommand {
    private static final SimpleCommandExceptionType ERROR_ALREADY_ENABLED = new SimpleCommandExceptionType(
        Component.translatable("commands.whitelist.alreadyOn")
    );
    private static final SimpleCommandExceptionType ERROR_ALREADY_DISABLED = new SimpleCommandExceptionType(
        Component.translatable("commands.whitelist.alreadyOff")
    );
    private static final SimpleCommandExceptionType ERROR_ALREADY_WHITELISTED = new SimpleCommandExceptionType(
        Component.translatable("commands.whitelist.add.failed")
    );
    private static final SimpleCommandExceptionType ERROR_NOT_WHITELISTED = new SimpleCommandExceptionType(
        Component.translatable("commands.whitelist.remove.failed")
    );
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
        dispatcher.register(
            Commands.literal("whitelist")
                .requires(Commands.hasPermission(Commands.LEVEL_ADMINS))
                .then(Commands.literal("on").executes(c -> enableWhitelist(c.getSource())))
                .then(Commands.literal("off").executes(c -> disableWhitelist(c.getSource())))
                .then(Commands.literal("list").executes(c -> showList(c.getSource())))
                .then(
                    Commands.literal("add")
                        .then(
                            Commands.argument("targets", GameProfileArgument.gameProfile())
                                .suggests(
                                    (c, p) -> {
                                        PlayerList list = c.getSource().getServer().getPlayerList();
                                        return SharedSuggestionProvider.suggest(
                                            list.getPlayers()
                                                .stream()
                                                .map(Player::nameAndId)
                                                .filter(nameAndId -> !list.getWhiteList().isWhiteListed(nameAndId))
                                                .map(NameAndId::name),
                                            p
                                        );
                                    }
                                )
                                .executes(c -> addPlayers(c.getSource(), GameProfileArgument.getGameProfiles(c, "targets")))
                        )
                )
                .then(
                    Commands.literal("remove")
                        .then(
                            Commands.argument("targets", GameProfileArgument.gameProfile())
                                .suggests((c, p) -> SharedSuggestionProvider.suggest(c.getSource().getServer().getPlayerList().getWhiteListNames(), p))
                                .executes(c -> removePlayers(c.getSource(), GameProfileArgument.getGameProfiles(c, "targets")))
                        )
                )
                .then(Commands.literal("reload").executes(c -> reload(c.getSource())))
        );
    }
 
    private static int reload(CommandSourceStack source) {
        source.getServer().getPlayerList().reloadWhiteList();
        source.sendSuccess(() -> Component.translatable("commands.whitelist.reloaded"), true);
        source.getServer().kickUnlistedPlayers();
        return 1;
    }
 
    private static int addPlayers(CommandSourceStack source, Collection<NameAndId> targets) throws CommandSyntaxException {
        UserWhiteList list = source.getServer().getPlayerList().getWhiteList();
        int success = 0;
 
        for (NameAndId target : targets) {
            if (!list.isWhiteListed(target)) {
                UserWhiteListEntry entry = new UserWhiteListEntry(target);
                list.add(entry);
                source.sendSuccess(() -> Component.translatable("commands.whitelist.add.success", Component.literal(target.name())), true);
                success++;
            }
        }
 
        if (success == 0) {
            throw ERROR_ALREADY_WHITELISTED.create();
        } else {
            return success;
        }
    }
 
    private static int removePlayers(CommandSourceStack source, Collection<NameAndId> targets) throws CommandSyntaxException {
        UserWhiteList list = source.getServer().getPlayerList().getWhiteList();
        int success = 0;
 
        for (NameAndId target : targets) {
            if (list.isWhiteListed(target)) {
                UserWhiteListEntry entry = new UserWhiteListEntry(target);
                list.remove(entry);
                source.sendSuccess(() -> Component.translatable("commands.whitelist.remove.success", Component.literal(target.name())), true);
                success++;
            }
        }
 
        if (success == 0) {
            throw ERROR_NOT_WHITELISTED.create();
        } else {
            source.getServer().kickUnlistedPlayers();
            return success;
        }
    }
 
    private static int enableWhitelist(CommandSourceStack source) throws CommandSyntaxException {
        if (source.getServer().isUsingWhitelist()) {
            throw ERROR_ALREADY_ENABLED.create();
        } else {
            source.getServer().setUsingWhitelist(true);
            source.sendSuccess(() -> Component.translatable("commands.whitelist.enabled"), true);
            source.getServer().kickUnlistedPlayers();
            return 1;
        }
    }
 
    private static int disableWhitelist(CommandSourceStack source) throws CommandSyntaxException {
        if (!source.getServer().isUsingWhitelist()) {
            throw ERROR_ALREADY_DISABLED.create();
        } else {
            source.getServer().setUsingWhitelist(false);
            source.sendSuccess(() -> Component.translatable("commands.whitelist.disabled"), true);
            return 1;
        }
    }
 
    private static int showList(CommandSourceStack source) {
        String[] list = source.getServer().getPlayerList().getWhiteListNames();
        if (list.length == 0) {
            source.sendSuccess(() -> Component.translatable("commands.whitelist.none"), false);
        } else {
            source.sendSuccess(() -> Component.translatable("commands.whitelist.list", list.length, String.join(", ", list)), false);
        }
 
        return list.length;
    }
}

引用的其他类

  • CommandSourceStack

    • 引用位置: 参数
  • Commands

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

    • 引用位置: 方法调用
    • 关联成员: SharedSuggestionProvider.suggest()
  • GameProfileArgument

    • 引用位置: 方法调用
    • 关联成员: GameProfileArgument.gameProfile(), GameProfileArgument.getGameProfiles()
  • Component

    • 引用位置: 方法调用
    • 关联成员: Component.literal(), Component.translatable()
  • NameAndId

    • 引用位置: 参数
  • UserWhiteListEntry

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