minecraft-src/com/mojang/realmsclient/dto/ServiceQuality.java
2025-09-18 12:27:44 +00:00

68 lines
1.7 KiB
Java

package com.mojang.realmsclient.dto;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.mojang.logging.LogUtils;
import java.io.IOException;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
@Environment(EnvType.CLIENT)
public enum ServiceQuality {
GREAT(1, "icon/ping_5"),
GOOD(2, "icon/ping_4"),
OKAY(3, "icon/ping_3"),
POOR(4, "icon/ping_2"),
UNKNOWN(5, "icon/ping_unknown");
final int value;
private final ResourceLocation icon;
private ServiceQuality(final int value, final String icon) {
this.value = value;
this.icon = ResourceLocation.withDefaultNamespace(icon);
}
@Nullable
public static ServiceQuality byValue(int value) {
for (ServiceQuality serviceQuality : values()) {
if (serviceQuality.getValue() == value) {
return serviceQuality;
}
}
return null;
}
public int getValue() {
return this.value;
}
public ResourceLocation getIcon() {
return this.icon;
}
@Environment(EnvType.CLIENT)
public static class RealmsServiceQualityJsonAdapter extends TypeAdapter<ServiceQuality> {
private static final Logger LOGGER = LogUtils.getLogger();
public void write(JsonWriter writer, ServiceQuality serviceQuality) throws IOException {
writer.value((long)serviceQuality.value);
}
public ServiceQuality read(JsonReader reader) throws IOException {
int i = reader.nextInt();
ServiceQuality serviceQuality = ServiceQuality.byValue(i);
if (serviceQuality == null) {
LOGGER.warn("Unsupported ServiceQuality {}", i);
return ServiceQuality.UNKNOWN;
} else {
return serviceQuality;
}
}
}
}