KickCommand.java

net.minecraft.server.commands.KickCommand

信息

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

    TODO

字段/常量

  • ERROR_KICKING_OWNER

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

      TODO

  • ERROR_SINGLEPLAYER

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

      TODO

内部类/嵌套类型

构造器

方法

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

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

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

参数:

  • dispatcher: CommandDispatcher

说明:

TODO

private static int kickPlayers(CommandSourceStack source, Collection<ServerPlayer> players, Component reason) @ L37

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

参数:

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

说明:

TODO

代码

public class KickCommand {
    private static final SimpleCommandExceptionType ERROR_KICKING_OWNER = new SimpleCommandExceptionType(Component.translatable("commands.kick.owner.failed"));
    private static final SimpleCommandExceptionType ERROR_SINGLEPLAYER = new SimpleCommandExceptionType(
        Component.translatable("commands.kick.singleplayer.failed")
    );
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
        dispatcher.register(
            Commands.literal("kick")
                .requires(Commands.hasPermission(Commands.LEVEL_ADMINS))
                .then(
                    Commands.argument("targets", EntityArgument.players())
                        .executes(
                            c -> kickPlayers(c.getSource(), EntityArgument.getPlayers(c, "targets"), Component.translatable("multiplayer.disconnect.kicked"))
                        )
                        .then(
                            Commands.argument("reason", MessageArgument.message())
                                .executes(c -> kickPlayers(c.getSource(), EntityArgument.getPlayers(c, "targets"), MessageArgument.getMessage(c, "reason")))
                        )
                )
        );
    }
 
    private static int kickPlayers(CommandSourceStack source, Collection<ServerPlayer> players, Component reason) throws CommandSyntaxException {
        if (!source.getServer().isPublished()) {
            throw ERROR_SINGLEPLAYER.create();
        } else {
            int count = 0;
 
            for (ServerPlayer player : players) {
                if (!source.getServer().isSingleplayerOwner(player.nameAndId())) {
                    player.connection.disconnect(reason);
                    source.sendSuccess(() -> Component.translatable("commands.kick.success", player.getDisplayName(), reason), true);
                    count++;
                }
            }
 
            if (count == 0) {
                throw ERROR_KICKING_OWNER.create();
            } else {
                return count;
            }
        }
    }
}

引用的其他类

  • CommandSourceStack

    • 引用位置: 参数
  • Commands

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

    • 引用位置: 方法调用
    • 关联成员: EntityArgument.getPlayers(), EntityArgument.players()
  • MessageArgument

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

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

    • 引用位置: 参数