| 1 | package com.mojang.realmsclient.gui.screens;
|
| 2 |
|
| 3 | import com.mojang.realmsclient.gui.RealmsDataFetcher;
|
| 4 | import com.mojang.realmsclient.gui.RealmsHeader;
|
| 5 | import com.mojang.realmsclient.gui.screens.RealmsPdpScreen.PdpHeaderEntry.Subtitle;
|
| 6 | import com.mojang.realmsclient.gui.task.DataFetcher;
|
| 7 | import java.util.ArrayList;
|
| 8 | import java.util.List;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 | import net.minecraft.ChatFormatting;
|
| 12 | import net.minecraft.client.Minecraft;
|
| 13 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 14 | import net.minecraft.client.gui.components.Button;
|
| 15 | import net.minecraft.client.gui.components.ObjectSelectionList;
|
| 16 | import net.minecraft.client.gui.components.RealmsButton;
|
| 17 | import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
|
| 18 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 19 | import net.minecraft.client.gui.screens.ConfirmLinkScreen;
|
| 20 | import net.minecraft.client.gui.screens.Screen;
|
| 21 | import net.minecraft.client.renderer.RenderPipelines;
|
| 22 | import net.minecraft.network.chat.CommonComponents;
|
| 23 | import net.minecraft.network.chat.Component;
|
| 24 | import net.minecraft.realms.RealmsScreen;
|
| 25 | import net.minecraft.resources.Identifier;
|
| 26 | import net.minecraft.util.CommonLinks;
|
| 27 | import org.jspecify.annotations.Nullable;
|
| 28 |
|
| 29 | @Environment(EnvType.CLIENT)
|
| 30 | public class RealmsPdpScreen extends RealmsScreen {
|
| 31 | private static final Component TITLE = Component.translatable("mco.selectServer.purchase");
|
| 32 | private static final Component TRY_FOR_FREE = Component.translatable("mco.pdp.tryForFree");
|
| 33 | private static final Component TRIAL_TITLE = Component.translatable("mco.pdp.trial.title").withStyle(ChatFormatting.UNDERLINE);
|
| 34 | private static final Component TRIAL_DESCRIPTION = Component.translatable("mco.pdp.trial.description");
|
| 35 | private static final Component FRIENDS_TITLE = Component.translatable("mco.pdp.friends.title").withStyle(ChatFormatting.BOLD);
|
| 36 | private static final Component FRIENDS_DESCRIPTION = Component.translatable("mco.pdp.friends.description");
|
| 37 | private static final Component MINIGAMES_TITLE = Component.translatable("mco.pdp.minigames.title").withStyle(ChatFormatting.BOLD);
|
| 38 | private static final Component MINIGAMES_DESCRIPTION = Component.translatable("mco.pdp.minigames.description");
|
| 39 | private static final Component PRIVATE_SERVER_TITLE = Component.translatable("mco.pdp.private.title").withStyle(ChatFormatting.BOLD);
|
| 40 | private static final Component PRIVATE_SERVER_BACKUPS = Component.translatable("mco.pdp.private.backups");
|
| 41 | private static final Component PRIVATE_SERVER_EASY_TO_MANAGE = Component.translatable("mco.pdp.private.easyToManage");
|
| 42 | private static final Component PRIVATE_SERVER_WORLD_SLOTS = Component.translatable("mco.pdp.private.worldSlots");
|
| 43 | private static final Component PRIVATE_SERVER_SECURE = Component.translatable("mco.pdp.private.secure");
|
| 44 | private static final Identifier FRIENDS_IMAGE = Identifier.withDefaultNamespace("textures/gui/realms/friends.png");
|
| 45 | private static final Identifier MINIGAMES_IMAGE = Identifier.withDefaultNamespace("textures/gui/realms/minigames.png");
|
| 46 | private static final Identifier PRIVATE_SERVER_IMAGE = Identifier.withDefaultNamespace("textures/gui/realms/private_server.png");
|
| 47 | private static final Identifier TRIAL_AVAILABLE_SPRITE = Identifier.withDefaultNamespace("icon/trial_available");
|
| 48 | private static final int HEADER_HEIGHT = 44;
|
| 49 | private static final int ROW_WIDTH = 290;
|
| 50 | private static final int IMAGE_WIDTH = 80;
|
| 51 | private static final int IMAGE_HEIGHT = 45;
|
| 52 | private static final int SPACING = 16;
|
| 53 | private static final int TEXT_WIDTH = 182;
|
| 54 | private static final int HEADER_BUTTON_SPACING = 4;
|
| 55 | private final Screen lastScreen;
|
| 56 | private boolean trialAvailable;
|
| 57 | private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this);
|
| 58 | private final RealmsHeader header;
|
| 59 | private final @Nullable Runnable openRealmsScreenWhenAvailable;
|
| 60 | private RealmsPdpScreen.@Nullable PdpList pdpList;
|
| 61 | private DataFetcher.@Nullable Subscription dataSubscription;
|
| 62 | private boolean realmsAvailable;
|
| 63 | private boolean redirectedToRealms;
|
| 64 |
|
| 65 | public RealmsPdpScreen(final Screen lastScreen, final boolean trialAvailable, final Runnable openJoinRealmScreen) {
|
| 66 | this(lastScreen, trialAvailable, openJoinRealmScreen, null);
|
| 67 | }
|
| 68 |
|
| 69 | public RealmsPdpScreen(
|
| 70 | final Screen lastScreen, final boolean trialAvailable, final Runnable openJoinRealmScreen, final @Nullable Runnable openRealmsScreenWhenAvailable
|
| 71 | ) {
|
| 72 | super(TITLE);
|
| 73 | this.lastScreen = lastScreen;
|
| 74 | this.trialAvailable = trialAvailable;
|
| 75 | this.openRealmsScreenWhenAvailable = openRealmsScreenWhenAvailable;
|
| 76 | this.header = new RealmsHeader(this, openJoinRealmScreen);
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | public void init() {
|
| 81 | this.layout.removeChildren();
|
| 82 | this.layout.setHeaderHeight(44);
|
| 83 | this.layout.addToHeader(this.header.createLayout(realmsLogo(), 44, 4));
|
| 84 | this.dataSubscription = this.initDataFetcher(this.minecraft.realmsDataFetcher());
|
| 85 | this.pdpList = this.layout.addToContents(new RealmsPdpScreen.PdpList(this.minecraft));
|
| 86 | LinearLayout footer = this.layout.addToFooter(LinearLayout.horizontal().spacing(8));
|
| 87 | if (this.trialAvailable) {
|
| 88 | footer.addChild(new RealmsButton(0, 0, 150, 20, TRY_FOR_FREE, ConfirmLinkScreen.confirmLink(this, CommonLinks.START_REALMS_TRIAL)));
|
| 89 | } else {
|
| 90 | footer.addChild(Button.builder(TITLE, ConfirmLinkScreen.confirmLink(this, CommonLinks.BUY_REALMS)).build());
|
| 91 | }
|
| 92 |
|
| 93 | footer.addChild(Button.builder(CommonComponents.GUI_BACK, var1x -> this.onClose()).build());
|
| 94 | this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
|
| 95 | this.repositionElements();
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | public void tick() {
|
| 100 | super.tick();
|
| 101 | boolean previousTrialAvailable = this.trialAvailable;
|
| 102 | if (this.dataSubscription != null) {
|
| 103 | this.dataSubscription.tick();
|
| 104 | }
|
| 105 |
|
| 106 | if (previousTrialAvailable != this.trialAvailable && this.minecraft.gui.screen() == this) {
|
| 107 | this.rebuildWidgets();
|
| 108 | } else {
|
| 109 | if (!this.redirectedToRealms && this.realmsAvailable && this.openRealmsScreenWhenAvailable != null && this.minecraft.gui.screen() == this) {
|
| 110 | this.redirectedToRealms = true;
|
| 111 | this.openRealmsScreenWhenAvailable.run();
|
| 112 | }
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | private DataFetcher.Subscription initDataFetcher(final RealmsDataFetcher dataSource) {
|
| 117 | DataFetcher.Subscription result = dataSource.dataFetcher.createSubscription();
|
| 118 | if (this.openRealmsScreenWhenAvailable != null) {
|
| 119 | result.subscribe(
|
| 120 | dataSource.serverListUpdateTask,
|
| 121 | serverListData -> this.realmsAvailable = !serverListData.serverList().isEmpty() || !serverListData.availableSnapshotServers().isEmpty()
|
| 122 | );
|
| 123 | }
|
| 124 |
|
| 125 | result.subscribe(dataSource.pendingInvitesTask, this.header::setPendingInvites);
|
| 126 | result.subscribe(dataSource.trialAvailabilityTask, trialAvailable -> this.trialAvailable = trialAvailable);
|
| 127 | result.subscribe(dataSource.newsTask, news -> {
|
| 128 | dataSource.newsManager.updateUnreadNews(news);
|
| 129 | this.header.setNews(dataSource.newsManager.newsLink(), dataSource.newsManager.hasUnreadNews());
|
| 130 | });
|
| 131 | return result;
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | protected void repositionElements() {
|
| 136 | if (this.pdpList != null) {
|
| 137 | this.pdpList.updateSize(this.width, this.layout);
|
| 138 | }
|
| 139 |
|
| 140 | this.layout.arrangeElements();
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | public void onClose() {
|
| 145 | this.minecraft.gui.setScreen(this.lastScreen);
|
| 146 | }
|
| 147 |
|
| 148 | @Environment(EnvType.CLIENT)
|
| 149 | private abstract static class AbstractPdpEntry extends ObjectSelectionList.Entry<RealmsPdpScreen.AbstractPdpEntry> {
|
| 150 | protected final Component title;
|
| 151 |
|
| 152 | public AbstractPdpEntry(final Component title) {
|
| 153 | this.title = title;
|
| 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | @Environment(EnvType.CLIENT)
|
| 158 | private class PdpFeatureEntry extends RealmsPdpScreen.AbstractPdpEntry {
|
| 159 | private final Identifier image;
|
| 160 | private final List<Component> descriptions;
|
| 161 |
|
| 162 | private PdpFeatureEntry(final Identifier image, final Component title, final Component... descriptions) {
|
| 163 | super(title);
|
| 164 | this.image = image;
|
| 165 | this.descriptions = List.of(descriptions);
|
| 166 | }
|
| 167 |
|
| 168 | private int entryHeight() {
|
| 169 | return Math.max(45, this.textHeight()) + 16;
|
| 170 | }
|
| 171 |
|
| 172 | private int textHeight() {
|
| 173 | int height = RealmsPdpScreen.this.font.wordWrapHeight(this.title, 182) + 4;
|
| 174 |
|
| 175 | for (Component description : this.descriptions) {
|
| 176 | height += 2;
|
| 177 | height += RealmsPdpScreen.this.font.wordWrapHeight(description, 182);
|
| 178 | }
|
| 179 |
|
| 180 | return height;
|
| 181 | }
|
| 182 |
|
| 183 | @Override
|
| 184 | public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
|
| 185 | int contentY = this.getContentY() + 4;
|
| 186 | graphics.blit(RenderPipelines.GUI_TEXTURED, this.image, this.getContentX() + 4, contentY, 0.0F, 0.0F, 80, 45, 80, 45);
|
| 187 | int textY = graphics.textWithWordWrap(RealmsPdpScreen.this.font, this.title, this.titleX(), contentY, 182, -1) + 4;
|
| 188 |
|
| 189 | for (Component description : this.descriptions) {
|
| 190 | textY = graphics.textWithWordWrap(RealmsPdpScreen.this.font, description, this.titleX(), textY, 182, -1) + 2;
|
| 191 | }
|
| 192 | }
|
| 193 |
|
| 194 | @Override
|
| 195 | public Component getNarration() {
|
| 196 | List<Component> lines = new ArrayList<>(this.descriptions.size() + 1);
|
| 197 | lines.add(this.title);
|
| 198 | lines.addAll(this.descriptions);
|
| 199 | return Component.translatable("narrator.select", CommonComponents.joinLines(lines));
|
| 200 | }
|
| 201 |
|
| 202 | private int titleX() {
|
| 203 | return this.getContentX() + 4 + 80 + 16;
|
| 204 | }
|
| 205 | }
|
| 206 |
|
| 207 | @Environment(EnvType.CLIENT)
|
| 208 | private class PdpHeaderEntry extends RealmsPdpScreen.AbstractPdpEntry {
|
| 209 | private static final int DESCRIPTION_ICON_SIZE = 8;
|
| 210 | private final @Nullable Subtitle subtitle;
|
| 211 |
|
| 212 | public PdpHeaderEntry(final Component title, final @Nullable Subtitle subtitle) {
|
| 213 | super(title);
|
| 214 | this.subtitle = subtitle;
|
| 215 | }
|
| 216 |
|
| 217 | @Override
|
| 218 | public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
|
| 219 | int textY = graphics.textWithWordWrap(RealmsPdpScreen.this.font, this.title, this.titleX(), this.getContentY() + 4, 182, -1) + 4;
|
| 220 | if (this.subtitle != null) {
|
| 221 | int iconX = this.getContentXMiddle() - Math.min(182, RealmsPdpScreen.this.font.width(this.subtitle.subTitle)) / 2 - 12;
|
| 222 | int iconY = textY + (9 - 8) / 2;
|
| 223 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, this.subtitle.icon, iconX, iconY, 8, 8);
|
| 224 | int subtitleX = this.getContentXMiddle() - Math.min(182, RealmsPdpScreen.this.font.width(this.subtitle.subTitle)) / 2;
|
| 225 | graphics.textWithWordWrap(RealmsPdpScreen.this.font, this.subtitle.subTitle, subtitleX, textY, 182, -1);
|
| 226 | }
|
| 227 | }
|
| 228 |
|
| 229 | private int entryHeight() {
|
| 230 | return this.textHeight() + 16;
|
| 231 | }
|
| 232 |
|
| 233 | private int textHeight() {
|
| 234 | int height = RealmsPdpScreen.this.font.wordWrapHeight(this.title, 182) + 4;
|
| 235 | if (this.subtitle != null) {
|
| 236 | height += 2;
|
| 237 | height += RealmsPdpScreen.this.font.wordWrapHeight(this.subtitle.subTitle, 182);
|
| 238 | }
|
| 239 |
|
| 240 | return height;
|
| 241 | }
|
| 242 |
|
| 243 | @Override
|
| 244 | public Component getNarration() {
|
| 245 | List<Component> lines = new ArrayList<>();
|
| 246 | lines.add(this.title);
|
| 247 | if (this.subtitle != null) {
|
| 248 | lines.add(this.subtitle.subTitle);
|
| 249 | }
|
| 250 |
|
| 251 | return Component.translatable("narrator.select", CommonComponents.joinLines(lines));
|
| 252 | }
|
| 253 |
|
| 254 | private int titleX() {
|
| 255 | return this.getContentXMiddle() - Math.min(182, RealmsPdpScreen.this.font.width(this.title)) / 2;
|
| 256 | }
|
| 257 | }
|
| 258 |
|
| 259 | @Environment(EnvType.CLIENT)
|
| 260 | private class PdpList extends ObjectSelectionList<RealmsPdpScreen.AbstractPdpEntry> {
|
| 261 | private PdpList(final Minecraft minecraft) {
|
| 262 | super(minecraft, RealmsPdpScreen.this.width, RealmsPdpScreen.this.layout.getContentHeight(), RealmsPdpScreen.this.layout.getHeaderHeight(), 45);
|
| 263 | if (RealmsPdpScreen.this.trialAvailable) {
|
| 264 | this.addHeader(RealmsPdpScreen.TRIAL_TITLE, new Subtitle(RealmsPdpScreen.TRIAL_DESCRIPTION, RealmsPdpScreen.TRIAL_AVAILABLE_SPRITE));
|
| 265 | } else {
|
| 266 | this.addHeader(RealmsPdpScreen.TRIAL_TITLE, null);
|
| 267 | }
|
| 268 |
|
| 269 | this.addFeature(RealmsPdpScreen.FRIENDS_IMAGE, RealmsPdpScreen.FRIENDS_TITLE, RealmsPdpScreen.FRIENDS_DESCRIPTION);
|
| 270 | this.addFeature(RealmsPdpScreen.MINIGAMES_IMAGE, RealmsPdpScreen.MINIGAMES_TITLE, RealmsPdpScreen.MINIGAMES_DESCRIPTION);
|
| 271 | this.addFeature(
|
| 272 | RealmsPdpScreen.PRIVATE_SERVER_IMAGE,
|
| 273 | RealmsPdpScreen.PRIVATE_SERVER_TITLE,
|
| 274 | RealmsPdpScreen.PRIVATE_SERVER_BACKUPS,
|
| 275 | RealmsPdpScreen.PRIVATE_SERVER_EASY_TO_MANAGE,
|
| 276 | RealmsPdpScreen.PRIVATE_SERVER_WORLD_SLOTS,
|
| 277 | RealmsPdpScreen.PRIVATE_SERVER_SECURE
|
| 278 | );
|
| 279 | }
|
| 280 |
|
| 281 | private void addHeader(final Component title, final @Nullable Subtitle subTitle) {
|
| 282 | RealmsPdpScreen.PdpHeaderEntry entry = RealmsPdpScreen.this.new PdpHeaderEntry(title, subTitle);
|
| 283 | this.addEntry(entry, entry.entryHeight());
|
| 284 | }
|
| 285 |
|
| 286 | private void addFeature(final Identifier image, final Component title, final Component... descriptions) {
|
| 287 | RealmsPdpScreen.PdpFeatureEntry entry = RealmsPdpScreen.this.new PdpFeatureEntry(image, title, descriptions);
|
| 288 | this.addEntry(entry, entry.entryHeight());
|
| 289 | }
|
| 290 |
|
| 291 | @Override
|
| 292 | public int getRowWidth() {
|
| 293 | return 290;
|
| 294 | }
|
| 295 | }
|
| 296 | }
|
| 297 |
|