| 1 | package com.mojang.realmsclient.dto;
|
| 2 |
|
| 3 | import com.google.gson.TypeAdapter;
|
| 4 | import com.google.gson.stream.JsonReader;
|
| 5 | import com.google.gson.stream.JsonWriter;
|
| 6 | import com.mojang.logging.LogUtils;
|
| 7 | import java.io.IOException;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import org.jspecify.annotations.Nullable;
|
| 11 | import org.slf4j.Logger;
|
| 12 |
|
| 13 | @Environment(EnvType.CLIENT)
|
| 14 | public enum RealmsRegion {
|
| 15 | AUSTRALIA_EAST("AustraliaEast", "realms.configuration.region.australia_east"),
|
| 16 | AUSTRALIA_SOUTHEAST("AustraliaSoutheast", "realms.configuration.region.australia_southeast"),
|
| 17 | BRAZIL_SOUTH("BrazilSouth", "realms.configuration.region.brazil_south"),
|
| 18 | CANADA_CENTRAL("CanadaCentral", "realms.configuration.region.canada_central"),
|
| 19 | CENTRAL_INDIA("CentralIndia", "realms.configuration.region.central_india"),
|
| 20 | CENTRAL_US("CentralUs", "realms.configuration.region.central_us"),
|
| 21 | EAST_ASIA("EastAsia", "realms.configuration.region.east_asia"),
|
| 22 | EAST_US("EastUs", "realms.configuration.region.east_us"),
|
| 23 | EAST_US_2("EastUs2", "realms.configuration.region.east_us_2"),
|
| 24 | FRANCE_CENTRAL("FranceCentral", "realms.configuration.region.france_central"),
|
| 25 | JAPAN_EAST("JapanEast", "realms.configuration.region.japan_east"),
|
| 26 | JAPAN_WEST("JapanWest", "realms.configuration.region.japan_west"),
|
| 27 | KOREA_CENTRAL("KoreaCentral", "realms.configuration.region.korea_central"),
|
| 28 | MEXICO_CENTRAL("MexicoCentral", "realms.configuration.region.mexico_central"),
|
| 29 | NORTH_CENTRAL_US("NorthCentralUs", "realms.configuration.region.north_central_us"),
|
| 30 | NORTH_EUROPE("NorthEurope", "realms.configuration.region.north_europe"),
|
| 31 | SOUTH_CENTRAL_US("SouthCentralUs", "realms.configuration.region.south_central_us"),
|
| 32 | SOUTHEAST_ASIA("SoutheastAsia", "realms.configuration.region.southeast_asia"),
|
| 33 | SOUTH_AFRICA_NORTH("SouthAfricaNorth", "realms.configuration.region.south_africa_north"),
|
| 34 | SWEDEN_CENTRAL("SwedenCentral", "realms.configuration.region.sweden_central"),
|
| 35 | UAE_NORTH("UAENorth", "realms.configuration.region.uae_north"),
|
| 36 | UK_SOUTH("UKSouth", "realms.configuration.region.uk_south"),
|
| 37 | WEST_CENTRAL_US("WestCentralUs", "realms.configuration.region.west_central_us"),
|
| 38 | WEST_EUROPE("WestEurope", "realms.configuration.region.west_europe"),
|
| 39 | WEST_US("WestUs", "realms.configuration.region.west_us"),
|
| 40 | WEST_US_2("WestUs2", "realms.configuration.region.west_us_2"),
|
| 41 | WEST_US_3("WestUs3", "realms.configuration.region.west_us_3"),
|
| 42 | INVALID_REGION("invalid", "");
|
| 43 |
|
| 44 | public final String nameId;
|
| 45 | public final String translationKey;
|
| 46 |
|
| 47 | RealmsRegion(final String nameId, final String translationKey) {
|
| 48 | this.nameId = nameId;
|
| 49 | this.translationKey = translationKey;
|
| 50 | }
|
| 51 |
|
| 52 | public static @Nullable RealmsRegion findByNameId(final String nameIdStr) {
|
| 53 | for (RealmsRegion value : values()) {
|
| 54 | if (value.nameId.equals(nameIdStr)) {
|
| 55 | return value;
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | return null;
|
| 60 | }
|
| 61 |
|
| 62 | @Environment(EnvType.CLIENT)
|
| 63 | public static class RealmsRegionJsonAdapter extends TypeAdapter<RealmsRegion> {
|
| 64 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 65 |
|
| 66 | public void write(final JsonWriter jsonWriter, final RealmsRegion realmsRegion) throws IOException {
|
| 67 | jsonWriter.value(realmsRegion.nameId);
|
| 68 | }
|
| 69 |
|
| 70 | public RealmsRegion read(final JsonReader jsonReader) throws IOException {
|
| 71 | String nameId = jsonReader.nextString();
|
| 72 | RealmsRegion realmsRegion = RealmsRegion.findByNameId(nameId);
|
| 73 | if (realmsRegion == null) {
|
| 74 | LOGGER.warn("Unsupported RealmsRegion {}", nameId);
|
| 75 | return RealmsRegion.INVALID_REGION;
|
| 76 | } else {
|
| 77 | return realmsRegion;
|
| 78 | }
|
| 79 | }
|
| 80 | }
|
| 81 | }
|
| 82 |
|