| 1 | package net.minecraft.client.multiplayer;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import java.io.IOException;
|
| 7 | import java.util.Collections;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Optional;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import net.minecraft.SharedConstants;
|
| 13 | import net.minecraft.nbt.CompoundTag;
|
| 14 | import net.minecraft.network.chat.Component;
|
| 15 | import net.minecraft.network.protocol.status.ServerStatus.Players;
|
| 16 | import net.minecraft.util.ExtraCodecs;
|
| 17 | import net.minecraft.util.PngInfo;
|
| 18 | import org.jspecify.annotations.Nullable;
|
| 19 | import org.slf4j.Logger;
|
| 20 |
|
| 21 | @Environment(EnvType.CLIENT)
|
| 22 | public class ServerData {
|
| 23 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 24 | private static final int MAX_ICON_SIZE = 1024;
|
| 25 | public String name;
|
| 26 | public String ip;
|
| 27 | public Component status;
|
| 28 | public Component motd;
|
| 29 | public @Nullable Players players;
|
| 30 | public long ping;
|
| 31 | public int protocol = SharedConstants.getCurrentVersion().protocolVersion();
|
| 32 | public Component version = Component.literal(SharedConstants.getCurrentVersion().name());
|
| 33 | public List<Component> playerList = Collections.emptyList();
|
| 34 | private ServerData.ServerPackStatus packStatus = ServerData.ServerPackStatus.PROMPT;
|
| 35 | private byte @Nullable [] iconBytes;
|
| 36 | private ServerData.Type type;
|
| 37 | private int acceptedCodeOfConduct;
|
| 38 | private ServerData.State state = ServerData.State.INITIAL;
|
| 39 |
|
| 40 | public ServerData(final String name, final String ip, final ServerData.Type type) {
|
| 41 | this.name = name;
|
| 42 | this.ip = ip;
|
| 43 | this.type = type;
|
| 44 | }
|
| 45 |
|
| 46 | public CompoundTag write() {
|
| 47 | CompoundTag tag = new CompoundTag();
|
| 48 | tag.putString("name", this.name);
|
| 49 | tag.putString("ip", this.ip);
|
| 50 | tag.storeNullable("icon", ExtraCodecs.BASE64_STRING, this.iconBytes);
|
| 51 | tag.store(ServerData.ServerPackStatus.FIELD_CODEC, this.packStatus);
|
| 52 | if (this.acceptedCodeOfConduct != 0) {
|
| 53 | tag.putInt("acceptedCodeOfConduct", this.acceptedCodeOfConduct);
|
| 54 | }
|
| 55 |
|
| 56 | return tag;
|
| 57 | }
|
| 58 |
|
| 59 | public ServerData.ServerPackStatus getResourcePackStatus() {
|
| 60 | return this.packStatus;
|
| 61 | }
|
| 62 |
|
| 63 | public void setResourcePackStatus(final ServerData.ServerPackStatus packStatus) {
|
| 64 | this.packStatus = packStatus;
|
| 65 | }
|
| 66 |
|
| 67 | public static ServerData read(final CompoundTag tag) {
|
| 68 | ServerData server = new ServerData(tag.getStringOr("name", ""), tag.getStringOr("ip", ""), ServerData.Type.OTHER);
|
| 69 | server.setIconBytes(tag.read("icon", ExtraCodecs.BASE64_STRING).orElse(null));
|
| 70 | server.setResourcePackStatus(tag.read(ServerData.ServerPackStatus.FIELD_CODEC).orElse(ServerData.ServerPackStatus.PROMPT));
|
| 71 | server.acceptedCodeOfConduct = tag.getIntOr("acceptedCodeOfConduct", 0);
|
| 72 | return server;
|
| 73 | }
|
| 74 |
|
| 75 | public byte @Nullable [] getIconBytes() {
|
| 76 | return this.iconBytes;
|
| 77 | }
|
| 78 |
|
| 79 | public void setIconBytes(final byte @Nullable [] iconBytes) {
|
| 80 | this.iconBytes = iconBytes;
|
| 81 | }
|
| 82 |
|
| 83 | public boolean isLan() {
|
| 84 | return this.type == ServerData.Type.LAN;
|
| 85 | }
|
| 86 |
|
| 87 | public boolean isRealm() {
|
| 88 | return this.type == ServerData.Type.REALM;
|
| 89 | }
|
| 90 |
|
| 91 | public ServerData.Type type() {
|
| 92 | return this.type;
|
| 93 | }
|
| 94 |
|
| 95 | public boolean hasAcceptedCodeOfConduct(final String codeOfConduct) {
|
| 96 | return this.acceptedCodeOfConduct == codeOfConduct.hashCode();
|
| 97 | }
|
| 98 |
|
| 99 | public void acceptCodeOfConduct(final String codeOfConduct) {
|
| 100 | this.acceptedCodeOfConduct = codeOfConduct.hashCode();
|
| 101 | }
|
| 102 |
|
| 103 | public void clearCodeOfConduct() {
|
| 104 | this.acceptedCodeOfConduct = 0;
|
| 105 | }
|
| 106 |
|
| 107 | public void copyNameIconFrom(final ServerData other) {
|
| 108 | this.ip = other.ip;
|
| 109 | this.name = other.name;
|
| 110 | this.iconBytes = other.iconBytes;
|
| 111 | }
|
| 112 |
|
| 113 | public void copyFrom(final ServerData other) {
|
| 114 | this.copyNameIconFrom(other);
|
| 115 | this.setResourcePackStatus(other.getResourcePackStatus());
|
| 116 | this.type = other.type;
|
| 117 | }
|
| 118 |
|
| 119 | public ServerData.State state() {
|
| 120 | return this.state;
|
| 121 | }
|
| 122 |
|
| 123 | public void setState(final ServerData.State state) {
|
| 124 | this.state = state;
|
| 125 | }
|
| 126 |
|
| 127 | public static byte @Nullable [] validateIcon(final byte @Nullable [] bytes) {
|
| 128 | if (bytes != null) {
|
| 129 | try {
|
| 130 | PngInfo iconInfo = PngInfo.fromBytes(bytes);
|
| 131 | if (iconInfo.width() <= 1024 && iconInfo.height() <= 1024) {
|
| 132 | return bytes;
|
| 133 | }
|
| 134 | } catch (IOException e) {
|
| 135 | LOGGER.warn("Failed to decode server icon", e);
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | return null;
|
| 140 | }
|
| 141 |
|
| 142 | @Environment(EnvType.CLIENT)
|
| 143 | public enum ServerPackStatus {
|
| 144 | ENABLED("enabled"),
|
| 145 | DISABLED("disabled"),
|
| 146 | PROMPT("prompt");
|
| 147 |
|
| 148 | public static final MapCodec<ServerData.ServerPackStatus> FIELD_CODEC = Codec.BOOL
|
| 149 | .optionalFieldOf("acceptTextures")
|
| 150 | .xmap(acceptTextures -> acceptTextures.<ServerData.ServerPackStatus>map(b -> b ? ENABLED : DISABLED).orElse(PROMPT), status -> {
|
| 151 | return switch (status) {
|
| 152 | case ENABLED -> Optional.of(true);
|
| 153 | case DISABLED -> Optional.of(false);
|
| 154 | case PROMPT -> Optional.empty();
|
| 155 | };
|
| 156 | });
|
| 157 | private final Component name;
|
| 158 |
|
| 159 | ServerPackStatus(final String name) {
|
| 160 | this.name = Component.translatable("manageServer.resourcePack." + name);
|
| 161 | }
|
| 162 |
|
| 163 | public Component getName() {
|
| 164 | return this.name;
|
| 165 | }
|
| 166 | }
|
| 167 |
|
| 168 | @Environment(EnvType.CLIENT)
|
| 169 | public enum State {
|
| 170 | INITIAL,
|
| 171 | PINGING,
|
| 172 | UNREACHABLE,
|
| 173 | INCOMPATIBLE,
|
| 174 | SUCCESSFUL;
|
| 175 | }
|
| 176 |
|
| 177 | @Environment(EnvType.CLIENT)
|
| 178 | public enum Type {
|
| 179 | LAN,
|
| 180 | REALM,
|
| 181 | OTHER;
|
| 182 | }
|
| 183 | }
|
| 184 |
|