BanlistService.java

net.minecraft.server.jsonrpc.methods.BanlistService

信息

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

    TODO

字段/常量

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

      TODO

内部类/嵌套类型

  • net.minecraft.server.jsonrpc.methods.BanlistService.UserBan

    • 类型: record
    • 修饰符: private
    • 源码定位: L99
    • 说明:

      TODO

  • net.minecraft.server.jsonrpc.methods.BanlistService.UserBanDto

    • 类型: record
    • 修饰符: public
    • 源码定位: L113
    • 说明:

      TODO

构造器

方法

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

public static List<BanlistService.UserBanDto> get(MinecraftApi minecraftApi) @ L27

  • 方法名:get
  • 源码定位:L27
  • 返回类型:List<BanlistService.UserBanDto>
  • 修饰符:public static

参数:

  • minecraftApi: MinecraftApi

说明:

TODO

public static List<BanlistService.UserBanDto> add(MinecraftApi minecraftApi, List<BanlistService.UserBanDto> bans, ClientInfo clientInfo) @ L37

  • 方法名:add
  • 源码定位:L37
  • 返回类型:List<BanlistService.UserBanDto>
  • 修饰符:public static

参数:

  • minecraftApi: MinecraftApi
  • bans: List<BanlistService.UserBanDto>
  • clientInfo: ClientInfo

说明:

TODO

public static List<BanlistService.UserBanDto> clear(MinecraftApi minecraftApi, ClientInfo clientInfo) @ L56

  • 方法名:clear
  • 源码定位:L56
  • 返回类型:List<BanlistService.UserBanDto>
  • 修饰符:public static

参数:

  • minecraftApi: MinecraftApi
  • clientInfo: ClientInfo

说明:

TODO

public static List<BanlistService.UserBanDto> remove(MinecraftApi minecraftApi, List<PlayerDto> remove, ClientInfo clientInfo) @ L61

  • 方法名:remove
  • 源码定位:L61
  • 返回类型:List<BanlistService.UserBanDto>
  • 修饰符:public static

参数:

  • minecraftApi: MinecraftApi
  • remove: List
  • clientInfo: ClientInfo

说明:

TODO

public static List<BanlistService.UserBanDto> set(MinecraftApi minecraftApi, List<BanlistService.UserBanDto> bans, ClientInfo clientInfo) @ L75

  • 方法名:set
  • 源码定位:L75
  • 返回类型:List<BanlistService.UserBanDto>
  • 修饰符:public static

参数:

  • minecraftApi: MinecraftApi
  • bans: List<BanlistService.UserBanDto>
  • clientInfo: ClientInfo

说明:

TODO

代码

public class BanlistService {
    private static final String BAN_SOURCE = "Management server";
 
    public static List<BanlistService.UserBanDto> get(MinecraftApi minecraftApi) {
        return minecraftApi.banListService()
            .getUserBanEntries()
            .stream()
            .filter(p -> p.getUser() != null)
            .map(BanlistService.UserBan::from)
            .map(BanlistService.UserBanDto::from)
            .toList();
    }
 
    public static List<BanlistService.UserBanDto> add(MinecraftApi minecraftApi, List<BanlistService.UserBanDto> bans, ClientInfo clientInfo) {
        List<CompletableFuture<Optional<BanlistService.UserBan>>> fetch = bans.stream()
            .map(banx -> minecraftApi.playerListService().getUser(banx.player().id(), banx.player().name()).thenApply(u -> u.map(banx::toUserBan)))
            .toList();
 
        for (Optional<BanlistService.UserBan> ban : Util.sequence(fetch).join()) {
            if (!ban.isEmpty()) {
                BanlistService.UserBan userBan = ban.get();
                minecraftApi.banListService().addUserBan(userBan.toBanEntry(), clientInfo);
                ServerPlayer player = minecraftApi.playerListService().getPlayer(ban.get().player().id());
                if (player != null) {
                    player.connection.disconnect(Component.translatable("multiplayer.disconnect.banned"));
                }
            }
        }
 
        return get(minecraftApi);
    }
 
    public static List<BanlistService.UserBanDto> clear(MinecraftApi minecraftApi, ClientInfo clientInfo) {
        minecraftApi.banListService().clearUserBans(clientInfo);
        return get(minecraftApi);
    }
 
    public static List<BanlistService.UserBanDto> remove(MinecraftApi minecraftApi, List<PlayerDto> remove, ClientInfo clientInfo) {
        List<CompletableFuture<Optional<NameAndId>>> fetch = remove.stream()
            .map(playerDto -> minecraftApi.playerListService().getUser(playerDto.id(), playerDto.name()))
            .toList();
 
        for (Optional<NameAndId> user : Util.sequence(fetch).join()) {
            if (!user.isEmpty()) {
                minecraftApi.banListService().removeUserBan(user.get(), clientInfo);
            }
        }
 
        return get(minecraftApi);
    }
 
    public static List<BanlistService.UserBanDto> set(MinecraftApi minecraftApi, List<BanlistService.UserBanDto> bans, ClientInfo clientInfo) {
        List<CompletableFuture<Optional<BanlistService.UserBan>>> fetch = bans.stream()
            .map(ban -> minecraftApi.playerListService().getUser(ban.player().id(), ban.player().name()).thenApply(u -> u.map(ban::toUserBan)))
            .toList();
        Set<BanlistService.UserBan> finalAllowList = Util.sequence(fetch).join().stream().flatMap(Optional::stream).collect(Collectors.toSet());
        Set<BanlistService.UserBan> currentAllowList = minecraftApi.banListService()
            .getUserBanEntries()
            .stream()
            .filter(entry -> entry.getUser() != null)
            .map(BanlistService.UserBan::from)
            .collect(Collectors.toSet());
        currentAllowList.stream()
            .filter(ban -> !finalAllowList.contains(ban))
            .forEach(ban -> minecraftApi.banListService().removeUserBan(ban.player(), clientInfo));
        finalAllowList.stream().filter(ban -> !currentAllowList.contains(ban)).forEach(ban -> {
            minecraftApi.banListService().addUserBan(ban.toBanEntry(), clientInfo);
            ServerPlayer player = minecraftApi.playerListService().getPlayer(ban.player().id());
            if (player != null) {
                player.connection.disconnect(Component.translatable("multiplayer.disconnect.banned"));
            }
        });
        return get(minecraftApi);
    }
 
    private record UserBan(NameAndId player, @Nullable String reason, String source, Optional<Instant> expires) {
        private static BanlistService.UserBan from(UserBanListEntry entry) {
            return new BanlistService.UserBan(
                Objects.requireNonNull(entry.getUser()), entry.getReason(), entry.getSource(), Optional.ofNullable(entry.getExpires()).map(Date::toInstant)
            );
        }
 
        private UserBanListEntry toBanEntry() {
            return new UserBanListEntry(
                new NameAndId(this.player().id(), this.player().name()), null, this.source(), this.expires().map(Date::from).orElse(null), this.reason()
            );
        }
    }
 
    public record UserBanDto(PlayerDto player, Optional<String> reason, Optional<String> source, Optional<Instant> expires) {
        public static final MapCodec<BanlistService.UserBanDto> CODEC = RecordCodecBuilder.mapCodec(
            i -> i.group(
                    PlayerDto.CODEC.codec().fieldOf("player").forGetter(BanlistService.UserBanDto::player),
                    Codec.STRING.optionalFieldOf("reason").forGetter(BanlistService.UserBanDto::reason),
                    Codec.STRING.optionalFieldOf("source").forGetter(BanlistService.UserBanDto::source),
                    ExtraCodecs.INSTANT_ISO8601.optionalFieldOf("expires").forGetter(BanlistService.UserBanDto::expires)
                )
                .apply(i, BanlistService.UserBanDto::new)
        );
 
        private static BanlistService.UserBanDto from(BanlistService.UserBan ban) {
            return new BanlistService.UserBanDto(PlayerDto.from(ban.player()), Optional.ofNullable(ban.reason()), Optional.of(ban.source()), ban.expires());
        }
 
        public static BanlistService.UserBanDto from(UserBanListEntry entry) {
            return from(BanlistService.UserBan.from(entry));
        }
 
        private BanlistService.UserBan toUserBan(NameAndId nameAndId) {
            return new BanlistService.UserBan(nameAndId, this.reason().orElse(null), this.source().orElse("Management server"), this.expires());
        }
    }
}

引用的其他类

  • Component

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

    • 引用位置: 参数/方法调用
    • 关联成员: PlayerDto.from()
  • MinecraftApi

    • 引用位置: 参数
  • ClientInfo

    • 引用位置: 参数
  • NameAndId

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

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

    • 引用位置: 方法调用
    • 关联成员: Util.sequence()