BanPlayerCommands.java

net.minecraft.server.commands.BanPlayerCommands

信息

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

    TODO

字段/常量

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

      TODO

内部类/嵌套类型

构造器

方法

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

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

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

参数:

  • dispatcher: CommandDispatcher

说明:

TODO

private static int banPlayers(CommandSourceStack source, Collection<NameAndId> players, Component reason) @ L38

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

参数:

  • source: CommandSourceStack
  • players: Collection
  • reason: Component

说明:

TODO

代码

public class BanPlayerCommands {
    private static final SimpleCommandExceptionType ERROR_ALREADY_BANNED = new SimpleCommandExceptionType(Component.translatable("commands.ban.failed"));
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
        dispatcher.register(
            Commands.literal("ban")
                .requires(Commands.hasPermission(Commands.LEVEL_ADMINS))
                .then(
                    Commands.argument("targets", GameProfileArgument.gameProfile())
                        .executes(c -> banPlayers(c.getSource(), GameProfileArgument.getGameProfiles(c, "targets"), null))
                        .then(
                            Commands.argument("reason", MessageArgument.message())
                                .executes(
                                    c -> banPlayers(c.getSource(), GameProfileArgument.getGameProfiles(c, "targets"), MessageArgument.getMessage(c, "reason"))
                                )
                        )
                )
        );
    }
 
    private static int banPlayers(CommandSourceStack source, Collection<NameAndId> players, @Nullable Component reason) throws CommandSyntaxException {
        UserBanList list = source.getServer().getPlayerList().getBans();
        int count = 0;
 
        for (NameAndId player : players) {
            if (!list.isBanned(player)) {
                UserBanListEntry entry = new UserBanListEntry(player, null, source.getTextName(), null, reason == null ? null : reason.getString());
                list.add(entry);
                count++;
                source.sendSuccess(() -> Component.translatable("commands.ban.success", Component.literal(player.name()), entry.getReasonMessage()), true);
                ServerPlayer online = source.getServer().getPlayerList().getPlayer(player.id());
                if (online != null) {
                    online.connection.disconnect(Component.translatable("multiplayer.disconnect.banned"));
                }
            }
        }
 
        if (count == 0) {
            throw ERROR_ALREADY_BANNED.create();
        } else {
            return count;
        }
    }
}

引用的其他类

  • CommandSourceStack

    • 引用位置: 参数
  • Commands

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

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

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

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

    • 引用位置: 参数
  • UserBanListEntry

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