| 1 | package net.minecraft.network.protocol.status;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.DataResult;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.nio.charset.StandardCharsets;
|
| 7 | import java.util.Base64;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Optional;
|
| 10 | import net.minecraft.SharedConstants;
|
| 11 | import net.minecraft.WorldVersion;
|
| 12 | import net.minecraft.network.chat.CommonComponents;
|
| 13 | import net.minecraft.network.chat.Component;
|
| 14 | import net.minecraft.network.chat.ComponentSerialization;
|
| 15 | import net.minecraft.server.players.NameAndId;
|
| 16 |
|
| 17 | public record ServerStatus(
|
| 18 | Component description,
|
| 19 | Optional<ServerStatus.Players> players,
|
| 20 | Optional<ServerStatus.Version> version,
|
| 21 | Optional<ServerStatus.Favicon> favicon,
|
| 22 | boolean enforcesSecureChat
|
| 23 | ) {
|
| 24 | public static final Codec<ServerStatus> CODEC = RecordCodecBuilder.create(
|
| 25 | i -> i.group(
|
| 26 | ComponentSerialization.CODEC.lenientOptionalFieldOf("description", CommonComponents.EMPTY).forGetter(ServerStatus::description),
|
| 27 | ServerStatus.Players.CODEC.lenientOptionalFieldOf("players").forGetter(ServerStatus::players),
|
| 28 | ServerStatus.Version.CODEC.lenientOptionalFieldOf("version").forGetter(ServerStatus::version),
|
| 29 | ServerStatus.Favicon.CODEC.lenientOptionalFieldOf("favicon").forGetter(ServerStatus::favicon),
|
| 30 | Codec.BOOL.lenientOptionalFieldOf("enforcesSecureChat", false).forGetter(ServerStatus::enforcesSecureChat)
|
| 31 | )
|
| 32 | .apply(i, ServerStatus::new)
|
| 33 | );
|
| 34 |
|
| 35 | public record Favicon(byte[] iconBytes) {
|
| 36 | private static final String PREFIX = "data:image/png;base64,";
|
| 37 | public static final Codec<ServerStatus.Favicon> CODEC = Codec.STRING.comapFlatMap(string -> {
|
| 38 | if (!string.startsWith("data:image/png;base64,")) {
|
| 39 | return DataResult.error(() -> "Unknown format");
|
| 40 | }
|
| 41 |
|
| 42 | try {
|
| 43 | String base64 = string.substring("data:image/png;base64,".length()).replaceAll("\n", "");
|
| 44 | byte[] iconBytes = Base64.getDecoder().decode(base64.getBytes(StandardCharsets.UTF_8));
|
| 45 | return DataResult.success(new ServerStatus.Favicon(iconBytes));
|
| 46 | } catch (IllegalArgumentException e) {
|
| 47 | return DataResult.error(() -> "Malformed base64 server icon");
|
| 48 | }
|
| 49 | }, favicon -> "data:image/png;base64," + new String(Base64.getEncoder().encode(favicon.iconBytes), StandardCharsets.UTF_8));
|
| 50 | }
|
| 51 |
|
| 52 | public record Players(int max, int online, List<NameAndId> sample) {
|
| 53 | public static final Codec<ServerStatus.Players> CODEC = RecordCodecBuilder.create(
|
| 54 | i -> i.group(
|
| 55 | Codec.INT.fieldOf("max").forGetter(ServerStatus.Players::max),
|
| 56 | Codec.INT.fieldOf("online").forGetter(ServerStatus.Players::online),
|
| 57 | NameAndId.CODEC.listOf().lenientOptionalFieldOf("sample", List.of()).forGetter(ServerStatus.Players::sample)
|
| 58 | )
|
| 59 | .apply(i, ServerStatus.Players::new)
|
| 60 | );
|
| 61 | }
|
| 62 |
|
| 63 | public record Version(String name, int protocol) {
|
| 64 | public static final Codec<ServerStatus.Version> CODEC = RecordCodecBuilder.create(
|
| 65 | i -> i.group(Codec.STRING.fieldOf("name").forGetter(ServerStatus.Version::name), Codec.INT.fieldOf("protocol").forGetter(ServerStatus.Version::protocol))
|
| 66 | .apply(i, ServerStatus.Version::new)
|
| 67 | );
|
| 68 |
|
| 69 | public static ServerStatus.Version current() {
|
| 70 | WorldVersion version = SharedConstants.getCurrentVersion();
|
| 71 | return new ServerStatus.Version(version.name(), version.protocolVersion());
|
| 72 | }
|
| 73 | }
|
| 74 | }
|
| 75 |
|