| 1 | package com.mojang.realmsclient.gui;
|
| 2 |
|
| 3 | import com.mojang.realmsclient.RealmsMainScreen;
|
| 4 | import com.mojang.realmsclient.client.RealmsClient;
|
| 5 | import com.mojang.realmsclient.dto.RealmsNews;
|
| 6 | import com.mojang.realmsclient.dto.RealmsNotification;
|
| 7 | import com.mojang.realmsclient.dto.RealmsServer;
|
| 8 | import com.mojang.realmsclient.dto.RealmsServerPlayerLists;
|
| 9 | import com.mojang.realmsclient.gui.task.DataFetcher;
|
| 10 | import com.mojang.realmsclient.gui.task.RepeatedDelayStrategy;
|
| 11 | import com.mojang.realmsclient.util.RealmsPersistence;
|
| 12 | import java.time.Duration;
|
| 13 | import java.util.List;
|
| 14 | import java.util.concurrent.TimeUnit;
|
| 15 | import net.fabricmc.api.EnvType;
|
| 16 | import net.fabricmc.api.Environment;
|
| 17 | import net.minecraft.util.Util;
|
| 18 |
|
| 19 | @Environment(EnvType.CLIENT)
|
| 20 | public class RealmsDataFetcher {
|
| 21 | public final DataFetcher dataFetcher = new DataFetcher(Util.nonCriticalIoPool(), TimeUnit.MILLISECONDS, Util.timeSource());
|
| 22 | private final List<DataFetcher.Task<?>> tasks;
|
| 23 | public final DataFetcher.Task<List<RealmsNotification>> notificationsTask;
|
| 24 | public final DataFetcher.Task<RealmsDataFetcher.ServerListData> serverListUpdateTask;
|
| 25 | public final DataFetcher.Task<Integer> pendingInvitesTask;
|
| 26 | public final DataFetcher.Task<Boolean> trialAvailabilityTask;
|
| 27 | public final DataFetcher.Task<RealmsNews> newsTask;
|
| 28 | public final DataFetcher.Task<RealmsServerPlayerLists> onlinePlayersTask;
|
| 29 | public final RealmsNewsManager newsManager = new RealmsNewsManager(new RealmsPersistence());
|
| 30 |
|
| 31 | public RealmsDataFetcher(final RealmsClient realmsClient) {
|
| 32 | this.serverListUpdateTask = this.dataFetcher
|
| 33 | .createTask(
|
| 34 | "server list",
|
| 35 | () -> {
|
| 36 | com.mojang.realmsclient.dto.RealmsServerList realmsServerList = realmsClient.listRealms();
|
| 37 | return RealmsMainScreen.isSnapshot()
|
| 38 | ? new RealmsDataFetcher.ServerListData(realmsServerList.servers(), realmsClient.listSnapshotEligibleRealms())
|
| 39 | : new RealmsDataFetcher.ServerListData(realmsServerList.servers(), List.of());
|
| 40 | },
|
| 41 | Duration.ofSeconds(60L),
|
| 42 | RepeatedDelayStrategy.CONSTANT
|
| 43 | );
|
| 44 | this.pendingInvitesTask = this.dataFetcher
|
| 45 | .createTask("pending invite count", realmsClient::pendingInvitesCount, Duration.ofSeconds(10L), RepeatedDelayStrategy.exponentialBackoff(360));
|
| 46 | this.trialAvailabilityTask = this.dataFetcher
|
| 47 | .createTask("trial availablity", realmsClient::trialAvailable, Duration.ofSeconds(60L), RepeatedDelayStrategy.exponentialBackoff(60));
|
| 48 | this.newsTask = this.dataFetcher.createTask("unread news", realmsClient::getNews, Duration.ofMinutes(5L), RepeatedDelayStrategy.CONSTANT);
|
| 49 | this.notificationsTask = this.dataFetcher.createTask("notifications", realmsClient::getNotifications, Duration.ofMinutes(5L), RepeatedDelayStrategy.CONSTANT);
|
| 50 | this.onlinePlayersTask = this.dataFetcher.createTask("online players", realmsClient::getLiveStats, Duration.ofSeconds(10L), RepeatedDelayStrategy.CONSTANT);
|
| 51 | this.tasks = List.of(
|
| 52 | this.notificationsTask, this.serverListUpdateTask, this.pendingInvitesTask, this.trialAvailabilityTask, this.newsTask, this.onlinePlayersTask
|
| 53 | );
|
| 54 | }
|
| 55 |
|
| 56 | public List<DataFetcher.Task<?>> getTasks() {
|
| 57 | return this.tasks;
|
| 58 | }
|
| 59 |
|
| 60 | @Environment(EnvType.CLIENT)
|
| 61 | public record ServerListData(List<RealmsServer> serverList, List<RealmsServer> availableSnapshotServers) {
|
| 62 | }
|
| 63 | }
|
| 64 |
|