PlayerService.java

net.minecraft.server.jsonrpc.methods.PlayerService

信息

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

    TODO

字段/常量

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

      TODO

内部类/嵌套类型

  • net.minecraft.server.jsonrpc.methods.PlayerService.KickDto
    • 类型: record
    • 修饰符: public
    • 源码定位: L44
    • 说明:

      TODO

构造器

方法

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

public static List<PlayerDto> get(MinecraftApi minecraftApi) @ L17

  • 方法名:get
  • 源码定位:L17
  • 返回类型:List
  • 修饰符:public static

参数:

  • minecraftApi: MinecraftApi

说明:

TODO

public static List<PlayerDto> kick(MinecraftApi minecraftApi, List<PlayerService.KickDto> kick, ClientInfo clientInfo) @ L21

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

参数:

  • minecraftApi: MinecraftApi
  • kick: List<PlayerService.KickDto>
  • clientInfo: ClientInfo

说明:

TODO

private static ServerPlayer getServerPlayer(MinecraftApi minecraftApi, PlayerDto playerDto) @ L36

  • 方法名:getServerPlayer
  • 源码定位:L36
  • 返回类型:ServerPlayer
  • 修饰符:private static

参数:

  • minecraftApi: MinecraftApi
  • playerDto: PlayerDto

说明:

TODO

代码

public class PlayerService {
    private static final Component DEFAULT_KICK_MESSAGE = Component.translatable("multiplayer.disconnect.kicked");
 
    public static List<PlayerDto> get(MinecraftApi minecraftApi) {
        return minecraftApi.playerListService().getPlayers().stream().map(PlayerDto::from).toList();
    }
 
    public static List<PlayerDto> kick(MinecraftApi minecraftApi, List<PlayerService.KickDto> kick, ClientInfo clientInfo) {
        List<PlayerDto> kicked = new ArrayList<>();
 
        for (PlayerService.KickDto kickDto : kick) {
            ServerPlayer serverPlayer = getServerPlayer(minecraftApi, kickDto.player());
            if (serverPlayer != null) {
                minecraftApi.playerListService().remove(serverPlayer, clientInfo);
                serverPlayer.connection.disconnect(kickDto.message.flatMap(Message::asComponent).orElse(DEFAULT_KICK_MESSAGE));
                kicked.add(kickDto.player());
            }
        }
 
        return kicked;
    }
 
    private static @Nullable ServerPlayer getServerPlayer(MinecraftApi minecraftApi, PlayerDto playerDto) {
        if (playerDto.id().isPresent()) {
            return minecraftApi.playerListService().getPlayer(playerDto.id().get());
        } else {
            return playerDto.name().isPresent() ? minecraftApi.playerListService().getPlayerByName(playerDto.name().get()) : null;
        }
    }
 
    public record KickDto(PlayerDto player, Optional<Message> message) {
        public static final MapCodec<PlayerService.KickDto> CODEC = RecordCodecBuilder.mapCodec(
            i -> i.group(
                    PlayerDto.CODEC.codec().fieldOf("player").forGetter(PlayerService.KickDto::player),
                    Message.CODEC.optionalFieldOf("message").forGetter(PlayerService.KickDto::message)
                )
                .apply(i, PlayerService.KickDto::new)
        );
    }
}

引用的其他类