| 1 | package com.mojang.realmsclient.gui.screens;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import com.mojang.realmsclient.RealmsMainScreen;
|
| 6 | import com.mojang.realmsclient.client.RealmsClient;
|
| 7 | import com.mojang.realmsclient.dto.RealmsServer;
|
| 8 | import com.mojang.realmsclient.dto.RealmsSlot;
|
| 9 | import com.mojang.realmsclient.dto.WorldDownload;
|
| 10 | import com.mojang.realmsclient.exception.RealmsServiceException;
|
| 11 | import com.mojang.realmsclient.gui.RealmsWorldSlotButton;
|
| 12 | import com.mojang.realmsclient.util.RealmsTextureManager;
|
| 13 | import com.mojang.realmsclient.util.RealmsUtil;
|
| 14 | import com.mojang.realmsclient.util.task.OpenServerTask;
|
| 15 | import com.mojang.realmsclient.util.task.SwitchSlotTask;
|
| 16 | import java.util.List;
|
| 17 | import java.util.Map.Entry;
|
| 18 | import java.util.stream.Collectors;
|
| 19 | import java.util.stream.Stream;
|
| 20 | import net.fabricmc.api.EnvType;
|
| 21 | import net.fabricmc.api.Environment;
|
| 22 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 23 | import net.minecraft.client.gui.components.Button;
|
| 24 | import net.minecraft.client.gui.screens.Screen;
|
| 25 | import net.minecraft.client.renderer.RenderPipelines;
|
| 26 | import net.minecraft.network.chat.CommonComponents;
|
| 27 | import net.minecraft.network.chat.Component;
|
| 28 | import net.minecraft.network.chat.ComponentUtils;
|
| 29 | import net.minecraft.realms.RealmsScreen;
|
| 30 | import net.minecraft.resources.Identifier;
|
| 31 | import net.minecraft.util.ARGB;
|
| 32 | import net.minecraft.util.Mth;
|
| 33 | import org.jspecify.annotations.Nullable;
|
| 34 | import org.slf4j.Logger;
|
| 35 |
|
| 36 | @Environment(EnvType.CLIENT)
|
| 37 | public class RealmsBrokenWorldScreen extends RealmsScreen {
|
| 38 | private static final Identifier SLOT_FRAME_SPRITE = Identifier.withDefaultNamespace("widget/slot_frame");
|
| 39 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 40 | private static final int DEFAULT_BUTTON_WIDTH = 80;
|
| 41 | private final Screen lastScreen;
|
| 42 | private @Nullable RealmsServer serverData;
|
| 43 | private final long serverId;
|
| 44 | private final Component[] message = new Component[]{
|
| 45 | Component.translatable("mco.brokenworld.message.line1"), Component.translatable("mco.brokenworld.message.line2")
|
| 46 | };
|
| 47 | private int leftX;
|
| 48 | private final List<Integer> slotsThatHasBeenDownloaded = Lists.newArrayList();
|
| 49 | private int animTick;
|
| 50 |
|
| 51 | public RealmsBrokenWorldScreen(final Screen lastScreen, final long serverId, final boolean isMinigame) {
|
| 52 | super(isMinigame ? Component.translatable("mco.brokenworld.minigame.title") : Component.translatable("mco.brokenworld.title"));
|
| 53 | this.lastScreen = lastScreen;
|
| 54 | this.serverId = serverId;
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public void init() {
|
| 59 | this.leftX = this.width / 2 - 150;
|
| 60 | this.addRenderableWidget(Button.builder(CommonComponents.GUI_BACK, button -> this.onClose()).bounds((this.width - 150) / 2, row(13) - 5, 150, 20).build());
|
| 61 | if (this.serverData == null) {
|
| 62 | this.fetchServerData(this.serverId);
|
| 63 | } else {
|
| 64 | this.addButtons();
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public Component getNarrationMessage() {
|
| 70 | return ComponentUtils.formatList(Stream.concat(Stream.of(this.title), Stream.of(this.message)).collect(Collectors.toList()), CommonComponents.SPACE);
|
| 71 | }
|
| 72 |
|
| 73 | private void addButtons() {
|
| 74 | for (Entry<Integer, RealmsSlot> entry : this.serverData.slots.entrySet()) {
|
| 75 | int slot = entry.getKey();
|
| 76 | boolean canPlay = slot != this.serverData.activeSlot || this.serverData.isMinigameActive();
|
| 77 | Button playOrDownloadButton;
|
| 78 | if (canPlay) {
|
| 79 | playOrDownloadButton = Button.builder(
|
| 80 | Component.translatable("mco.brokenworld.play"),
|
| 81 | var2x -> this.minecraft
|
| 82 | .gui
|
| 83 | .setScreen(new RealmsLongRunningMcoTaskScreen(this.lastScreen, new SwitchSlotTask(this.serverData.id, slot, this::doSwitchOrReset)))
|
| 84 | )
|
| 85 | .bounds(this.getFramePositionX(slot), row(8), 80, 20)
|
| 86 | .build();
|
| 87 | playOrDownloadButton.active = !this.serverData.slots.get(slot).options.empty;
|
| 88 | } else {
|
| 89 | playOrDownloadButton = Button.builder(
|
| 90 | Component.translatable("mco.brokenworld.download"),
|
| 91 | button -> this.minecraft
|
| 92 | .gui
|
| 93 | .setScreen(
|
| 94 | RealmsPopups.infoPopupScreen(this, Component.translatable("mco.configure.world.restore.download.question.line1"), var2x -> this.downloadWorld(slot))
|
| 95 | )
|
| 96 | )
|
| 97 | .bounds(this.getFramePositionX(slot), row(8), 80, 20)
|
| 98 | .build();
|
| 99 | }
|
| 100 |
|
| 101 | if (this.slotsThatHasBeenDownloaded.contains(slot)) {
|
| 102 | playOrDownloadButton.active = false;
|
| 103 | playOrDownloadButton.setMessage(Component.translatable("mco.brokenworld.downloaded"));
|
| 104 | }
|
| 105 |
|
| 106 | this.addRenderableWidget(playOrDownloadButton);
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | public void tick() {
|
| 112 | this.animTick++;
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int xm, final int ym, final float a) {
|
| 117 | super.extractRenderState(graphics, xm, ym, a);
|
| 118 | graphics.centeredText(this.font, this.title, this.width / 2, 17, -1);
|
| 119 |
|
| 120 | for (int i = 0; i < this.message.length; i++) {
|
| 121 | graphics.centeredText(this.font, this.message[i], this.width / 2, row(-1) + 3 + i * 12, -6250336);
|
| 122 | }
|
| 123 |
|
| 124 | if (this.serverData != null) {
|
| 125 | for (Entry<Integer, RealmsSlot> entry : this.serverData.slots.entrySet()) {
|
| 126 | if (entry.getValue().options.templateImage != null && entry.getValue().options.templateId != -1L) {
|
| 127 | this.extractSlotFrame(
|
| 128 | graphics,
|
| 129 | this.getFramePositionX(entry.getKey()),
|
| 130 | row(1) + 5,
|
| 131 | xm,
|
| 132 | ym,
|
| 133 | this.serverData.activeSlot == entry.getKey() && !this.isMinigame(),
|
| 134 | entry.getValue().options.getSlotName(entry.getKey()),
|
| 135 | entry.getKey(),
|
| 136 | entry.getValue().options.templateId,
|
| 137 | entry.getValue().options.templateImage,
|
| 138 | entry.getValue().options.empty
|
| 139 | );
|
| 140 | } else {
|
| 141 | this.extractSlotFrame(
|
| 142 | graphics,
|
| 143 | this.getFramePositionX(entry.getKey()),
|
| 144 | row(1) + 5,
|
| 145 | xm,
|
| 146 | ym,
|
| 147 | this.serverData.activeSlot == entry.getKey() && !this.isMinigame(),
|
| 148 | entry.getValue().options.getSlotName(entry.getKey()),
|
| 149 | entry.getKey(),
|
| 150 | -1L,
|
| 151 | null,
|
| 152 | entry.getValue().options.empty
|
| 153 | );
|
| 154 | }
|
| 155 | }
|
| 156 | }
|
| 157 | }
|
| 158 |
|
| 159 | private int getFramePositionX(final int i) {
|
| 160 | return this.leftX + (i - 1) * 110;
|
| 161 | }
|
| 162 |
|
| 163 | public Screen createErrorScreen(final RealmsServiceException exception) {
|
| 164 | return new RealmsGenericErrorScreen(exception, this.lastScreen);
|
| 165 | }
|
| 166 |
|
| 167 | private void fetchServerData(final long realmId) {
|
| 168 | RealmsUtil.<RealmsServer>supplyAsync(
|
| 169 | client -> client.getOwnRealm(realmId), RealmsUtil.openScreenAndLogOnFailure(this::createErrorScreen, "Couldn't get own world")
|
| 170 | )
|
| 171 | .thenAcceptAsync(serverData -> {
|
| 172 | this.serverData = serverData;
|
| 173 | this.addButtons();
|
| 174 | }, this.minecraft);
|
| 175 | }
|
| 176 |
|
| 177 | public void doSwitchOrReset() {
|
| 178 | new Thread(
|
| 179 | () -> {
|
| 180 | RealmsClient client = RealmsClient.getOrCreate();
|
| 181 | if (this.serverData.state == RealmsServer.State.CLOSED) {
|
| 182 | this.minecraft
|
| 183 | .execute(() -> this.minecraft.gui.setScreen(new RealmsLongRunningMcoTaskScreen(this, new OpenServerTask(this.serverData, this, true, this.minecraft))));
|
| 184 | } else {
|
| 185 | try {
|
| 186 | RealmsServer ownRealm = client.getOwnRealm(this.serverId);
|
| 187 | this.minecraft.execute(() -> RealmsMainScreen.play(ownRealm, this));
|
| 188 | } catch (RealmsServiceException e) {
|
| 189 | LOGGER.error("Couldn't get own world", e);
|
| 190 | this.minecraft.execute(() -> this.minecraft.gui.setScreen(this.createErrorScreen(e)));
|
| 191 | }
|
| 192 | }
|
| 193 | },
|
| 194 | "Realms open server"
|
| 195 | )
|
| 196 | .start();
|
| 197 | }
|
| 198 |
|
| 199 | private void downloadWorld(final int slotId) {
|
| 200 | RealmsClient client = RealmsClient.getOrCreate();
|
| 201 |
|
| 202 | try {
|
| 203 | WorldDownload worldDownload = client.requestDownloadInfo(this.serverData.id, slotId);
|
| 204 | RealmsDownloadLatestWorldScreen downloadScreen = new RealmsDownloadLatestWorldScreen(this, worldDownload, this.serverData.getWorldName(slotId), result -> {
|
| 205 | if (result) {
|
| 206 | this.slotsThatHasBeenDownloaded.add(slotId);
|
| 207 | this.clearWidgets();
|
| 208 | this.addButtons();
|
| 209 | } else {
|
| 210 | this.minecraft.gui.setScreen(this);
|
| 211 | }
|
| 212 | });
|
| 213 | this.minecraft.gui.setScreen(downloadScreen);
|
| 214 | } catch (RealmsServiceException e) {
|
| 215 | LOGGER.error("Couldn't download world data", e);
|
| 216 | this.minecraft.gui.setScreen(new RealmsGenericErrorScreen(e, this));
|
| 217 | }
|
| 218 | }
|
| 219 |
|
| 220 | @Override
|
| 221 | public void onClose() {
|
| 222 | this.minecraft.gui.setScreen(this.lastScreen);
|
| 223 | }
|
| 224 |
|
| 225 | private boolean isMinigame() {
|
| 226 | return this.serverData != null && this.serverData.isMinigameActive();
|
| 227 | }
|
| 228 |
|
| 229 | private void extractSlotFrame(
|
| 230 | final GuiGraphicsExtractor graphics,
|
| 231 | final int x,
|
| 232 | final int y,
|
| 233 | final int xm,
|
| 234 | final int ym,
|
| 235 | final boolean active,
|
| 236 | final String text,
|
| 237 | final int i,
|
| 238 | final long imageId,
|
| 239 | final @Nullable String image,
|
| 240 | final boolean empty
|
| 241 | ) {
|
| 242 | Identifier texture;
|
| 243 | if (empty) {
|
| 244 | texture = RealmsWorldSlotButton.EMPTY_SLOT_LOCATION;
|
| 245 | } else if (image != null && imageId != -1L) {
|
| 246 | texture = RealmsTextureManager.worldTemplate(String.valueOf(imageId), image);
|
| 247 | } else if (i == 1) {
|
| 248 | texture = RealmsWorldSlotButton.DEFAULT_WORLD_SLOT_1;
|
| 249 | } else if (i == 2) {
|
| 250 | texture = RealmsWorldSlotButton.DEFAULT_WORLD_SLOT_2;
|
| 251 | } else if (i == 3) {
|
| 252 | texture = RealmsWorldSlotButton.DEFAULT_WORLD_SLOT_3;
|
| 253 | } else {
|
| 254 | texture = RealmsTextureManager.worldTemplate(String.valueOf(this.serverData.minigameId), this.serverData.minigameImage);
|
| 255 | }
|
| 256 |
|
| 257 | if (active) {
|
| 258 | float c = 0.9F + 0.1F * Mth.cos(this.animTick * 0.2F);
|
| 259 | graphics.blit(RenderPipelines.GUI_TEXTURED, texture, x + 3, y + 3, 0.0F, 0.0F, 74, 74, 74, 74, 74, 74, ARGB.colorFromFloat(1.0F, c, c, c));
|
| 260 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, SLOT_FRAME_SPRITE, x, y, 80, 80);
|
| 261 | } else {
|
| 262 | int color = ARGB.colorFromFloat(1.0F, 0.56F, 0.56F, 0.56F);
|
| 263 | graphics.blit(RenderPipelines.GUI_TEXTURED, texture, x + 3, y + 3, 0.0F, 0.0F, 74, 74, 74, 74, 74, 74, color);
|
| 264 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, SLOT_FRAME_SPRITE, x, y, 80, 80, color);
|
| 265 | }
|
| 266 |
|
| 267 | graphics.centeredText(this.font, text, x + 40, y + 66, -1);
|
| 268 | }
|
| 269 | }
|
| 270 |
|