| 1 | package net.minecraft.server.jsonrpc.methods;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.Optional;
|
| 5 | import java.util.Set;
|
| 6 | import java.util.concurrent.CompletableFuture;
|
| 7 | import java.util.stream.Collectors;
|
| 8 | import net.minecraft.server.jsonrpc.api.PlayerDto;
|
| 9 | import net.minecraft.server.jsonrpc.internalapi.MinecraftApi;
|
| 10 | import net.minecraft.server.players.NameAndId;
|
| 11 | import net.minecraft.server.players.StoredUserEntry;
|
| 12 | import net.minecraft.server.players.UserWhiteListEntry;
|
| 13 | import net.minecraft.util.Util;
|
| 14 |
|
| 15 | public class AllowlistService {
|
| 16 | public static List<PlayerDto> get(final MinecraftApi minecraftApi) {
|
| 17 | return minecraftApi.allowListService().getEntries().stream().filter(p -> p.getUser() != null).map(u -> PlayerDto.from(u.getUser())).toList();
|
| 18 | }
|
| 19 |
|
| 20 | public static List<PlayerDto> add(final MinecraftApi minecraftApi, final List<PlayerDto> playerDtos, final ClientInfo clientInfo) {
|
| 21 | List<CompletableFuture<Optional<NameAndId>>> fetch = playerDtos.stream()
|
| 22 | .map(playerDto -> minecraftApi.playerListService().getUser(playerDto.id(), playerDto.name()))
|
| 23 | .toList();
|
| 24 |
|
| 25 | for (Optional<NameAndId> user : Util.sequence(fetch).join()) {
|
| 26 | user.ifPresent(nameAndId -> minecraftApi.allowListService().add(new UserWhiteListEntry(nameAndId), clientInfo));
|
| 27 | }
|
| 28 |
|
| 29 | return get(minecraftApi);
|
| 30 | }
|
| 31 |
|
| 32 | public static List<PlayerDto> clear(final MinecraftApi minecraftApi, final ClientInfo clientInfo) {
|
| 33 | minecraftApi.allowListService().clear(clientInfo);
|
| 34 | return get(minecraftApi);
|
| 35 | }
|
| 36 |
|
| 37 | public static List<PlayerDto> remove(final MinecraftApi minecraftApi, final List<PlayerDto> playerDtos, final ClientInfo clientInfo) {
|
| 38 | List<CompletableFuture<Optional<NameAndId>>> fetch = playerDtos.stream()
|
| 39 | .map(playerDto -> minecraftApi.playerListService().getUser(playerDto.id(), playerDto.name()))
|
| 40 | .toList();
|
| 41 |
|
| 42 | for (Optional<NameAndId> user : Util.sequence(fetch).join()) {
|
| 43 | user.ifPresent(nameAndId -> minecraftApi.allowListService().remove(nameAndId, clientInfo));
|
| 44 | }
|
| 45 |
|
| 46 | minecraftApi.allowListService().kickUnlistedPlayers(clientInfo);
|
| 47 | return get(minecraftApi);
|
| 48 | }
|
| 49 |
|
| 50 | public static List<PlayerDto> set(final MinecraftApi minecraftApi, final List<PlayerDto> playerDtos, final ClientInfo clientInfo) {
|
| 51 | List<CompletableFuture<Optional<NameAndId>>> fetch = playerDtos.stream()
|
| 52 | .map(playerDto -> minecraftApi.playerListService().getUser(playerDto.id(), playerDto.name()))
|
| 53 | .toList();
|
| 54 | Set<NameAndId> finalAllowList = Util.sequence(fetch).join().stream().flatMap(Optional::stream).collect(Collectors.toSet());
|
| 55 | Set<NameAndId> currentAllowList = minecraftApi.allowListService().getEntries().stream().map(StoredUserEntry::getUser).collect(Collectors.toSet());
|
| 56 | currentAllowList.stream().filter(user -> !finalAllowList.contains(user)).forEach(user -> minecraftApi.allowListService().remove(user, clientInfo));
|
| 57 | finalAllowList.stream()
|
| 58 | .filter(user -> !currentAllowList.contains(user))
|
| 59 | .forEach(user -> minecraftApi.allowListService().add(new UserWhiteListEntry(user), clientInfo));
|
| 60 | minecraftApi.allowListService().kickUnlistedPlayers(clientInfo);
|
| 61 | return get(minecraftApi);
|
| 62 | }
|
| 63 | }
|
| 64 |
|