| 1 | package net.minecraft.server.jsonrpc.methods;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.time.Instant;
|
| 7 | import java.util.Date;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Objects;
|
| 10 | import java.util.Optional;
|
| 11 | import java.util.Set;
|
| 12 | import java.util.concurrent.CompletableFuture;
|
| 13 | import java.util.stream.Collectors;
|
| 14 | import net.minecraft.network.chat.Component;
|
| 15 | import net.minecraft.server.jsonrpc.api.PlayerDto;
|
| 16 | import net.minecraft.server.jsonrpc.internalapi.MinecraftApi;
|
| 17 | import net.minecraft.server.level.ServerPlayer;
|
| 18 | import net.minecraft.server.players.NameAndId;
|
| 19 | import net.minecraft.server.players.UserBanListEntry;
|
| 20 | import net.minecraft.util.ExtraCodecs;
|
| 21 | import net.minecraft.util.Util;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 |
|
| 24 | public class BanlistService {
|
| 25 | private static final String BAN_SOURCE = "Management server";
|
| 26 |
|
| 27 | public static List<BanlistService.UserBanDto> get(final MinecraftApi minecraftApi) {
|
| 28 | return minecraftApi.banListService()
|
| 29 | .getUserBanEntries()
|
| 30 | .stream()
|
| 31 | .filter(p -> p.getUser() != null)
|
| 32 | .map(BanlistService.UserBan::from)
|
| 33 | .map(BanlistService.UserBanDto::from)
|
| 34 | .toList();
|
| 35 | }
|
| 36 |
|
| 37 | public static List<BanlistService.UserBanDto> add(final MinecraftApi minecraftApi, final List<BanlistService.UserBanDto> bans, final ClientInfo clientInfo) {
|
| 38 | List<CompletableFuture<Optional<BanlistService.UserBan>>> fetch = bans.stream()
|
| 39 | .map(banx -> minecraftApi.playerListService().getUser(banx.player().id(), banx.player().name()).thenApply(u -> u.map(banx::toUserBan)))
|
| 40 | .toList();
|
| 41 |
|
| 42 | for (Optional<BanlistService.UserBan> ban : Util.sequence(fetch).join()) {
|
| 43 | if (!ban.isEmpty()) {
|
| 44 | BanlistService.UserBan userBan = ban.get();
|
| 45 | minecraftApi.banListService().addUserBan(userBan.toBanEntry(), clientInfo);
|
| 46 | ServerPlayer player = minecraftApi.playerListService().getPlayer(ban.get().player().id());
|
| 47 | if (player != null) {
|
| 48 | player.connection.disconnect(Component.translatable("multiplayer.disconnect.banned"));
|
| 49 | }
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | return get(minecraftApi);
|
| 54 | }
|
| 55 |
|
| 56 | public static List<BanlistService.UserBanDto> clear(final MinecraftApi minecraftApi, final ClientInfo clientInfo) {
|
| 57 | minecraftApi.banListService().clearUserBans(clientInfo);
|
| 58 | return get(minecraftApi);
|
| 59 | }
|
| 60 |
|
| 61 | public static List<BanlistService.UserBanDto> remove(final MinecraftApi minecraftApi, final List<PlayerDto> remove, final ClientInfo clientInfo) {
|
| 62 | List<CompletableFuture<Optional<NameAndId>>> fetch = remove.stream()
|
| 63 | .map(playerDto -> minecraftApi.playerListService().getUser(playerDto.id(), playerDto.name()))
|
| 64 | .toList();
|
| 65 |
|
| 66 | for (Optional<NameAndId> user : Util.sequence(fetch).join()) {
|
| 67 | if (!user.isEmpty()) {
|
| 68 | minecraftApi.banListService().removeUserBan(user.get(), clientInfo);
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | return get(minecraftApi);
|
| 73 | }
|
| 74 |
|
| 75 | public static List<BanlistService.UserBanDto> set(final MinecraftApi minecraftApi, final List<BanlistService.UserBanDto> bans, final ClientInfo clientInfo) {
|
| 76 | List<CompletableFuture<Optional<BanlistService.UserBan>>> fetch = bans.stream()
|
| 77 | .map(ban -> minecraftApi.playerListService().getUser(ban.player().id(), ban.player().name()).thenApply(u -> u.map(ban::toUserBan)))
|
| 78 | .toList();
|
| 79 | Set<BanlistService.UserBan> finalAllowList = Util.sequence(fetch).join().stream().flatMap(Optional::stream).collect(Collectors.toSet());
|
| 80 | Set<BanlistService.UserBan> currentAllowList = minecraftApi.banListService()
|
| 81 | .getUserBanEntries()
|
| 82 | .stream()
|
| 83 | .filter(entry -> entry.getUser() != null)
|
| 84 | .map(BanlistService.UserBan::from)
|
| 85 | .collect(Collectors.toSet());
|
| 86 | currentAllowList.stream().filter(ban -> !finalAllowList.contains(ban)).forEach(ban -> minecraftApi.banListService().removeUserBan(ban.player(), clientInfo));
|
| 87 | finalAllowList.stream().filter(ban -> !currentAllowList.contains(ban)).forEach(ban -> {
|
| 88 | minecraftApi.banListService().addUserBan(ban.toBanEntry(), clientInfo);
|
| 89 | ServerPlayer player = minecraftApi.playerListService().getPlayer(ban.player().id());
|
| 90 | if (player != null) {
|
| 91 | player.connection.disconnect(Component.translatable("multiplayer.disconnect.banned"));
|
| 92 | }
|
| 93 | });
|
| 94 | return get(minecraftApi);
|
| 95 | }
|
| 96 |
|
| 97 | private record UserBan(NameAndId player, @Nullable String reason, String source, Optional<Instant> expires) {
|
| 98 | private static BanlistService.UserBan from(final UserBanListEntry entry) {
|
| 99 | return new BanlistService.UserBan(
|
| 100 | Objects.requireNonNull(entry.getUser()), entry.getReason(), entry.getSource(), Optional.ofNullable(entry.getExpires()).map(Date::toInstant)
|
| 101 | );
|
| 102 | }
|
| 103 |
|
| 104 | private UserBanListEntry toBanEntry() {
|
| 105 | return new UserBanListEntry(
|
| 106 | new NameAndId(this.player().id(), this.player().name()), null, this.source(), this.expires().map(Date::from).orElse(null), this.reason()
|
| 107 | );
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | public record UserBanDto(PlayerDto player, Optional<String> reason, Optional<String> source, Optional<Instant> expires) {
|
| 112 | public static final MapCodec<BanlistService.UserBanDto> CODEC = RecordCodecBuilder.mapCodec(
|
| 113 | i -> i.group(
|
| 114 | PlayerDto.CODEC.codec().fieldOf("player").forGetter(BanlistService.UserBanDto::player),
|
| 115 | Codec.STRING.optionalFieldOf("reason").forGetter(BanlistService.UserBanDto::reason),
|
| 116 | Codec.STRING.optionalFieldOf("source").forGetter(BanlistService.UserBanDto::source),
|
| 117 | ExtraCodecs.INSTANT_ISO8601.optionalFieldOf("expires").forGetter(BanlistService.UserBanDto::expires)
|
| 118 | )
|
| 119 | .apply(i, BanlistService.UserBanDto::new)
|
| 120 | );
|
| 121 |
|
| 122 | private static BanlistService.UserBanDto from(final BanlistService.UserBan ban) {
|
| 123 | return new BanlistService.UserBanDto(PlayerDto.from(ban.player()), Optional.ofNullable(ban.reason()), Optional.of(ban.source()), ban.expires());
|
| 124 | }
|
| 125 |
|
| 126 | public static BanlistService.UserBanDto from(final UserBanListEntry entry) {
|
| 127 | return from(BanlistService.UserBan.from(entry));
|
| 128 | }
|
| 129 |
|
| 130 | private BanlistService.UserBan toUserBan(final NameAndId nameAndId) {
|
| 131 | return new BanlistService.UserBan(nameAndId, this.reason().orElse(null), this.source().orElse("Management server"), this.expires());
|
| 132 | }
|
| 133 | }
|
| 134 | }
|
| 135 |
|