| 1 | package com.mojang.realmsclient.gui.screens;
|
| 2 |
|
| 3 | import com.mojang.realmsclient.RealmsAvailability;
|
| 4 | import com.mojang.realmsclient.dto.RealmsNotification;
|
| 5 | import com.mojang.realmsclient.gui.RealmsDataFetcher;
|
| 6 | import com.mojang.realmsclient.gui.task.DataFetcher;
|
| 7 | import java.util.Objects;
|
| 8 | import java.util.concurrent.CompletableFuture;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 | import net.minecraft.client.GameNarrator;
|
| 12 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 13 | import net.minecraft.client.gui.screens.TitleScreen;
|
| 14 | import net.minecraft.client.renderer.RenderPipelines;
|
| 15 | import net.minecraft.realms.RealmsScreen;
|
| 16 | import net.minecraft.resources.Identifier;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 |
|
| 19 | @Environment(EnvType.CLIENT)
|
| 20 | public class RealmsNotificationsScreen extends RealmsScreen {
|
| 21 | private static final Identifier UNSEEN_NOTIFICATION_SPRITE = Identifier.withDefaultNamespace("icon/unseen_notification");
|
| 22 | private static final Identifier NEWS_SPRITE = Identifier.withDefaultNamespace("icon/news");
|
| 23 | private static final Identifier INVITE_SPRITE = Identifier.withDefaultNamespace("icon/invite");
|
| 24 | private static final Identifier TRIAL_AVAILABLE_SPRITE = Identifier.withDefaultNamespace("icon/trial_available");
|
| 25 | private final CompletableFuture<Boolean> validClient = RealmsAvailability.get().thenApply(result -> result.type() == RealmsAvailability.Type.SUCCESS);
|
| 26 | private DataFetcher.@Nullable Subscription realmsDataSubscription;
|
| 27 | private RealmsNotificationsScreen.@Nullable DataFetcherConfiguration currentConfiguration;
|
| 28 | private volatile int numberOfPendingInvites;
|
| 29 | private static boolean trialAvailable;
|
| 30 | private static boolean hasUnreadNews;
|
| 31 | private static boolean hasUnseenNotifications;
|
| 32 | private final RealmsNotificationsScreen.DataFetcherConfiguration showAll = new RealmsNotificationsScreen.DataFetcherConfiguration() {
|
| 33 | @Override
|
| 34 | public DataFetcher.Subscription initDataFetcher(final RealmsDataFetcher dataSource) {
|
| 35 | DataFetcher.Subscription result = dataSource.dataFetcher.createSubscription();
|
| 36 | RealmsNotificationsScreen.this.addNewsAndInvitesSubscriptions(dataSource, result);
|
| 37 | RealmsNotificationsScreen.this.addNotificationsSubscriptions(dataSource, result);
|
| 38 | return result;
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | public boolean showOldNotifications() {
|
| 43 | return true;
|
| 44 | }
|
| 45 | };
|
| 46 | private final RealmsNotificationsScreen.DataFetcherConfiguration onlyNotifications = new RealmsNotificationsScreen.DataFetcherConfiguration() {
|
| 47 | @Override
|
| 48 | public DataFetcher.Subscription initDataFetcher(final RealmsDataFetcher dataSource) {
|
| 49 | DataFetcher.Subscription result = dataSource.dataFetcher.createSubscription();
|
| 50 | RealmsNotificationsScreen.this.addNotificationsSubscriptions(dataSource, result);
|
| 51 | return result;
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public boolean showOldNotifications() {
|
| 56 | return false;
|
| 57 | }
|
| 58 | };
|
| 59 |
|
| 60 | public RealmsNotificationsScreen() {
|
| 61 | super(GameNarrator.NO_TITLE);
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | public void init() {
|
| 66 | if (this.realmsDataSubscription != null) {
|
| 67 | this.realmsDataSubscription.forceUpdate();
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | public void added() {
|
| 73 | super.added();
|
| 74 | this.minecraft.realmsDataFetcher().notificationsTask.reset();
|
| 75 | }
|
| 76 |
|
| 77 | private RealmsNotificationsScreen.@Nullable DataFetcherConfiguration getConfiguration() {
|
| 78 | boolean realmsEnabled = this.inTitleScreen() && this.validClient.getNow(false);
|
| 79 | if (!realmsEnabled) {
|
| 80 | return null;
|
| 81 | } else {
|
| 82 | return this.getRealmsNotificationsEnabled() ? this.showAll : this.onlyNotifications;
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | public void tick() {
|
| 88 | RealmsNotificationsScreen.DataFetcherConfiguration dataFetcherConfiguration = this.getConfiguration();
|
| 89 | if (!Objects.equals(this.currentConfiguration, dataFetcherConfiguration)) {
|
| 90 | this.currentConfiguration = dataFetcherConfiguration;
|
| 91 | if (this.currentConfiguration != null) {
|
| 92 | this.realmsDataSubscription = this.currentConfiguration.initDataFetcher(this.minecraft.realmsDataFetcher());
|
| 93 | } else {
|
| 94 | this.realmsDataSubscription = null;
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | if (this.realmsDataSubscription != null) {
|
| 99 | this.realmsDataSubscription.tick();
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 | private boolean getRealmsNotificationsEnabled() {
|
| 104 | return this.minecraft.options.realmsNotifications().get();
|
| 105 | }
|
| 106 |
|
| 107 | private boolean inTitleScreen() {
|
| 108 | return this.minecraft.gui.screen() instanceof TitleScreen;
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int xm, final int ym, final float a) {
|
| 113 | super.extractRenderState(graphics, xm, ym, a);
|
| 114 | if (this.validClient.getNow(false)) {
|
| 115 | this.extractIcons(graphics);
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | @Override
|
| 120 | public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 121 | }
|
| 122 |
|
| 123 | private void extractIcons(final GuiGraphicsExtractor graphics) {
|
| 124 | int pendingInvitesCount = this.numberOfPendingInvites;
|
| 125 | int spacing = 24;
|
| 126 | int topPos = this.height / 4 + 48;
|
| 127 | int buttonRight = this.width / 2 + 100;
|
| 128 | int baseY = topPos + 48 + 2;
|
| 129 | int iconRight = buttonRight - 3;
|
| 130 | if (hasUnseenNotifications) {
|
| 131 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, UNSEEN_NOTIFICATION_SPRITE, iconRight - 12, baseY + 3, 10, 10);
|
| 132 | iconRight -= 16;
|
| 133 | }
|
| 134 |
|
| 135 | if (this.currentConfiguration != null && this.currentConfiguration.showOldNotifications()) {
|
| 136 | if (hasUnreadNews) {
|
| 137 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, NEWS_SPRITE, iconRight - 14, baseY + 1, 14, 14);
|
| 138 | iconRight -= 16;
|
| 139 | }
|
| 140 |
|
| 141 | if (pendingInvitesCount != 0) {
|
| 142 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, INVITE_SPRITE, iconRight - 14, baseY + 1, 14, 14);
|
| 143 | iconRight -= 16;
|
| 144 | }
|
| 145 |
|
| 146 | if (trialAvailable) {
|
| 147 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, TRIAL_AVAILABLE_SPRITE, iconRight - 10, baseY + 4, 8, 8);
|
| 148 | }
|
| 149 | }
|
| 150 | }
|
| 151 |
|
| 152 | private void addNewsAndInvitesSubscriptions(final RealmsDataFetcher dataSource, final DataFetcher.Subscription result) {
|
| 153 | result.subscribe(dataSource.pendingInvitesTask, value -> this.numberOfPendingInvites = value);
|
| 154 | result.subscribe(dataSource.trialAvailabilityTask, value -> trialAvailable = value);
|
| 155 | result.subscribe(dataSource.newsTask, value -> {
|
| 156 | dataSource.newsManager.updateUnreadNews(value);
|
| 157 | hasUnreadNews = dataSource.newsManager.hasUnreadNews();
|
| 158 | });
|
| 159 | }
|
| 160 |
|
| 161 | private void addNotificationsSubscriptions(final RealmsDataFetcher dataSource, final DataFetcher.Subscription result) {
|
| 162 | result.subscribe(dataSource.notificationsTask, notifications -> {
|
| 163 | hasUnseenNotifications = false;
|
| 164 |
|
| 165 | for (RealmsNotification notification : notifications) {
|
| 166 | if (!notification.seen()) {
|
| 167 | hasUnseenNotifications = true;
|
| 168 | break;
|
| 169 | }
|
| 170 | }
|
| 171 | });
|
| 172 | }
|
| 173 |
|
| 174 | @Environment(EnvType.CLIENT)
|
| 175 | private interface DataFetcherConfiguration {
|
| 176 | DataFetcher.Subscription initDataFetcher(RealmsDataFetcher realmsDataFetcher);
|
| 177 |
|
| 178 | boolean showOldNotifications();
|
| 179 | }
|
| 180 | }
|
| 181 |
|