| 1 | package net.minecraft.server.jsonrpc.methods;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Optional;
|
| 7 | import net.minecraft.network.chat.Component;
|
| 8 | import net.minecraft.network.protocol.status.ServerStatus;
|
| 9 | import net.minecraft.server.jsonrpc.api.PlayerDto;
|
| 10 | import net.minecraft.server.jsonrpc.internalapi.MinecraftApi;
|
| 11 | import net.minecraft.server.level.ServerPlayer;
|
| 12 |
|
| 13 | public class ServerStateService {
|
| 14 | public static ServerStateService.ServerState status(final MinecraftApi minecraftApi) {
|
| 15 | return !minecraftApi.serverStateService().isReady()
|
| 16 | ? ServerStateService.ServerState.NOT_STARTED
|
| 17 | : new ServerStateService.ServerState(true, PlayerService.get(minecraftApi), ServerStatus.Version.current());
|
| 18 | }
|
| 19 |
|
| 20 | public static boolean save(final MinecraftApi minecraftApi, final boolean flush, final ClientInfo clientInfo) {
|
| 21 | return minecraftApi.serverStateService().saveEverything(true, flush, true, clientInfo);
|
| 22 | }
|
| 23 |
|
| 24 | public static boolean stop(final MinecraftApi minecraftApi, final ClientInfo clientInfo) {
|
| 25 | minecraftApi.submit(() -> minecraftApi.serverStateService().halt(false, clientInfo));
|
| 26 | return true;
|
| 27 | }
|
| 28 |
|
| 29 | public static boolean systemMessage(final MinecraftApi minecraftApi, final ServerStateService.SystemMessage systemMessage, final ClientInfo clientInfo) {
|
| 30 | Component component = systemMessage.message().asComponent().orElse(null);
|
| 31 | if (component == null) {
|
| 32 | return false;
|
| 33 | }
|
| 34 |
|
| 35 | if (systemMessage.receivingPlayers().isPresent()) {
|
| 36 | if (systemMessage.receivingPlayers().get().isEmpty()) {
|
| 37 | return false;
|
| 38 | }
|
| 39 |
|
| 40 | for (PlayerDto playerDto : systemMessage.receivingPlayers().get()) {
|
| 41 | ServerPlayer player;
|
| 42 | if (playerDto.id().isPresent()) {
|
| 43 | player = minecraftApi.playerListService().getPlayer(playerDto.id().get());
|
| 44 | } else {
|
| 45 | if (!playerDto.name().isPresent()) {
|
| 46 | continue;
|
| 47 | }
|
| 48 |
|
| 49 | player = minecraftApi.playerListService().getPlayerByName(playerDto.name().get());
|
| 50 | }
|
| 51 |
|
| 52 | if (player != null) {
|
| 53 | player.sendSystemMessage(component, systemMessage.overlay());
|
| 54 | }
|
| 55 | }
|
| 56 | } else {
|
| 57 | minecraftApi.serverStateService().broadcastSystemMessage(component, systemMessage.overlay(), clientInfo);
|
| 58 | }
|
| 59 |
|
| 60 | return true;
|
| 61 | }
|
| 62 |
|
| 63 | public record ServerState(boolean started, List<PlayerDto> players, ServerStatus.Version version) {
|
| 64 | public static final Codec<ServerStateService.ServerState> CODEC = RecordCodecBuilder.create(
|
| 65 | i -> i.group(
|
| 66 | Codec.BOOL.fieldOf("started").forGetter(ServerStateService.ServerState::started),
|
| 67 | PlayerDto.CODEC.codec().listOf().lenientOptionalFieldOf("players", List.of()).forGetter(ServerStateService.ServerState::players),
|
| 68 | ServerStatus.Version.CODEC.fieldOf("version").forGetter(ServerStateService.ServerState::version)
|
| 69 | )
|
| 70 | .apply(i, ServerStateService.ServerState::new)
|
| 71 | );
|
| 72 | public static final ServerStateService.ServerState NOT_STARTED = new ServerStateService.ServerState(false, List.of(), ServerStatus.Version.current());
|
| 73 | }
|
| 74 |
|
| 75 | public record SystemMessage(Message message, boolean overlay, Optional<List<PlayerDto>> receivingPlayers) {
|
| 76 | public static final Codec<ServerStateService.SystemMessage> CODEC = RecordCodecBuilder.create(
|
| 77 | i -> i.group(
|
| 78 | Message.CODEC.fieldOf("message").forGetter(ServerStateService.SystemMessage::message),
|
| 79 | Codec.BOOL.fieldOf("overlay").forGetter(ServerStateService.SystemMessage::overlay),
|
| 80 | PlayerDto.CODEC.codec().listOf().lenientOptionalFieldOf("receivingPlayers").forGetter(ServerStateService.SystemMessage::receivingPlayers)
|
| 81 | )
|
| 82 | .apply(i, ServerStateService.SystemMessage::new)
|
| 83 | );
|
| 84 | }
|
| 85 | }
|
| 86 |
|