| 1 | package com.mojang.realmsclient.util;
|
| 2 |
|
| 3 | import com.google.gson.annotations.SerializedName;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import com.mojang.realmsclient.dto.GuardedSerializer;
|
| 6 | import com.mojang.realmsclient.dto.ReflectionBasedSerialization;
|
| 7 | import java.net.URI;
|
| 8 | import java.nio.charset.StandardCharsets;
|
| 9 | import java.nio.file.Files;
|
| 10 | import java.nio.file.NoSuchFileException;
|
| 11 | import java.nio.file.Path;
|
| 12 | import net.fabricmc.api.EnvType;
|
| 13 | import net.fabricmc.api.Environment;
|
| 14 | import net.minecraft.client.Minecraft;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 | import org.slf4j.Logger;
|
| 17 |
|
| 18 | @Environment(EnvType.CLIENT)
|
| 19 | public class RealmsPersistence {
|
| 20 | private static final String FILE_NAME = "realms_persistence.json";
|
| 21 | private static final GuardedSerializer GSON = new GuardedSerializer();
|
| 22 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 23 |
|
| 24 | public RealmsPersistence.RealmsPersistenceData read() {
|
| 25 | return readFile();
|
| 26 | }
|
| 27 |
|
| 28 | public void save(final RealmsPersistence.RealmsPersistenceData data) {
|
| 29 | writeFile(data);
|
| 30 | }
|
| 31 |
|
| 32 | public static RealmsPersistence.RealmsPersistenceData readFile() {
|
| 33 | Path file = getPathToData();
|
| 34 |
|
| 35 | try {
|
| 36 | String contents = Files.readString(file, StandardCharsets.UTF_8);
|
| 37 | RealmsPersistence.RealmsPersistenceData realmsPersistenceData = GSON.fromJson(contents, RealmsPersistence.RealmsPersistenceData.class);
|
| 38 | if (realmsPersistenceData != null) {
|
| 39 | return realmsPersistenceData;
|
| 40 | }
|
| 41 | } catch (NoSuchFileException var3) {
|
| 42 | } catch (Exception e) {
|
| 43 | LOGGER.warn("Failed to read Realms storage {}", file, e);
|
| 44 | }
|
| 45 |
|
| 46 | return new RealmsPersistence.RealmsPersistenceData();
|
| 47 | }
|
| 48 |
|
| 49 | public static void writeFile(final RealmsPersistence.RealmsPersistenceData data) {
|
| 50 | Path file = getPathToData();
|
| 51 |
|
| 52 | try {
|
| 53 | Files.writeString(file, GSON.toJson(data), StandardCharsets.UTF_8);
|
| 54 | } catch (Exception var3) {
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | private static Path getPathToData() {
|
| 59 | return Minecraft.getInstance().gameDirectory.toPath().resolve("realms_persistence.json");
|
| 60 | }
|
| 61 |
|
| 62 | @Environment(EnvType.CLIENT)
|
| 63 | public static class RealmsPersistenceData implements ReflectionBasedSerialization {
|
| 64 | @SerializedName("newsLink")
|
| 65 | public @Nullable URI newsLink;
|
| 66 | @SerializedName("hasUnreadNews")
|
| 67 | public boolean hasUnreadNews;
|
| 68 | }
|
| 69 | }
|
| 70 |
|