| 1 | package com.mojang.realmsclient.gui.screens;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.realmsclient.RealmsMainScreen;
|
| 5 | import com.mojang.realmsclient.client.RealmsClient;
|
| 6 | import com.mojang.realmsclient.client.worldupload.RealmsCreateWorldFlow;
|
| 7 | import com.mojang.realmsclient.dto.RealmsServer;
|
| 8 | import com.mojang.realmsclient.dto.WorldTemplate;
|
| 9 | import com.mojang.realmsclient.dto.WorldTemplatePaginatedList;
|
| 10 | import com.mojang.realmsclient.exception.RealmsServiceException;
|
| 11 | import com.mojang.realmsclient.util.task.LongRunningTask;
|
| 12 | import com.mojang.realmsclient.util.task.RealmCreationTask;
|
| 13 | import com.mojang.realmsclient.util.task.ResettingTemplateWorldTask;
|
| 14 | import com.mojang.realmsclient.util.task.SwitchSlotTask;
|
| 15 | import java.util.ArrayList;
|
| 16 | import java.util.List;
|
| 17 | import net.fabricmc.api.EnvType;
|
| 18 | import net.fabricmc.api.Environment;
|
| 19 | import net.minecraft.client.gui.Font;
|
| 20 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 21 | import net.minecraft.client.gui.components.Button;
|
| 22 | import net.minecraft.client.gui.components.StringWidget;
|
| 23 | import net.minecraft.client.gui.layouts.GridLayout;
|
| 24 | import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
|
| 25 | import net.minecraft.client.gui.layouts.LayoutSettings;
|
| 26 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 27 | import net.minecraft.client.gui.layouts.SpacerElement;
|
| 28 | import net.minecraft.client.gui.screens.Screen;
|
| 29 | import net.minecraft.client.renderer.RenderPipelines;
|
| 30 | import net.minecraft.network.chat.CommonComponents;
|
| 31 | import net.minecraft.network.chat.Component;
|
| 32 | import net.minecraft.realms.RealmsScreen;
|
| 33 | import net.minecraft.resources.Identifier;
|
| 34 | import net.minecraft.util.ARGB;
|
| 35 | import org.jspecify.annotations.Nullable;
|
| 36 | import org.slf4j.Logger;
|
| 37 |
|
| 38 | @Environment(EnvType.CLIENT)
|
| 39 | public class RealmsResetWorldScreen extends RealmsScreen {
|
| 40 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 41 | private static final Component CREATE_REALM_TITLE = Component.translatable("mco.selectServer.create");
|
| 42 | private static final Component CREATE_REALM_SUBTITLE = Component.translatable("mco.selectServer.create.subtitle").withColor(-6250336);
|
| 43 | private static final Component CREATE_WORLD_TITLE = Component.translatable("mco.configure.world.switch.slot");
|
| 44 | private static final Component CREATE_WORLD_SUBTITLE = Component.translatable("mco.configure.world.switch.slot.subtitle").withColor(-6250336);
|
| 45 | private static final Component GENERATE_NEW_WORLD = Component.translatable("mco.reset.world.generate");
|
| 46 | private static final Component RESET_WORLD_TITLE = Component.translatable("mco.reset.world.title");
|
| 47 | private static final Component RESET_WORLD_SUBTITLE = Component.translatable("mco.reset.world.warning").withColor(-65536);
|
| 48 | public static final Component CREATE_WORLD_RESET_TASK_TITLE = Component.translatable("mco.create.world.reset.title");
|
| 49 | private static final Component RESET_WORLD_RESET_TASK_TITLE = Component.translatable("mco.reset.world.resetting.screen.title");
|
| 50 | private static final Component WORLD_TEMPLATES_TITLE = Component.translatable("mco.reset.world.template");
|
| 51 | private static final Component ADVENTURES_TITLE = Component.translatable("mco.reset.world.adventure");
|
| 52 | private static final Component EXPERIENCES_TITLE = Component.translatable("mco.reset.world.experience");
|
| 53 | private static final Component INSPIRATION_TITLE = Component.translatable("mco.reset.world.inspiration");
|
| 54 | private final Screen lastScreen;
|
| 55 | private final RealmsServer serverData;
|
| 56 | private final Component subtitle;
|
| 57 | private final Component resetTaskTitle;
|
| 58 | private static final Identifier UPLOAD_LOCATION = Identifier.withDefaultNamespace("textures/gui/realms/upload.png");
|
| 59 | private static final Identifier ADVENTURE_MAP_LOCATION = Identifier.withDefaultNamespace("textures/gui/realms/adventure.png");
|
| 60 | private static final Identifier SURVIVAL_SPAWN_LOCATION = Identifier.withDefaultNamespace("textures/gui/realms/survival_spawn.png");
|
| 61 | private static final Identifier NEW_WORLD_LOCATION = Identifier.withDefaultNamespace("textures/gui/realms/new_world.png");
|
| 62 | private static final Identifier EXPERIENCE_LOCATION = Identifier.withDefaultNamespace("textures/gui/realms/experience.png");
|
| 63 | private static final Identifier INSPIRATION_LOCATION = Identifier.withDefaultNamespace("textures/gui/realms/inspiration.png");
|
| 64 | private WorldTemplatePaginatedList templates;
|
| 65 | private WorldTemplatePaginatedList adventuremaps;
|
| 66 | private WorldTemplatePaginatedList experiences;
|
| 67 | private WorldTemplatePaginatedList inspirations;
|
| 68 | public final int slot;
|
| 69 | private final @Nullable RealmCreationTask realmCreationTask;
|
| 70 | private final Runnable resetWorldRunnable;
|
| 71 | private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this);
|
| 72 |
|
| 73 | private RealmsResetWorldScreen(
|
| 74 | final Screen lastScreen,
|
| 75 | final RealmsServer serverData,
|
| 76 | final int slot,
|
| 77 | final Component title,
|
| 78 | final Component subtitle,
|
| 79 | final Component resetTaskTitle,
|
| 80 | final Runnable resetWorldRunnable
|
| 81 | ) {
|
| 82 | this(lastScreen, serverData, slot, title, subtitle, resetTaskTitle, null, resetWorldRunnable);
|
| 83 | }
|
| 84 |
|
| 85 | public RealmsResetWorldScreen(
|
| 86 | final Screen lastScreen,
|
| 87 | final RealmsServer serverData,
|
| 88 | final int slot,
|
| 89 | final Component title,
|
| 90 | final Component subtitle,
|
| 91 | final Component resetTaskTitle,
|
| 92 | final @Nullable RealmCreationTask realmCreationTask,
|
| 93 | final Runnable resetWorldRunnable
|
| 94 | ) {
|
| 95 | super(title);
|
| 96 | this.lastScreen = lastScreen;
|
| 97 | this.serverData = serverData;
|
| 98 | this.slot = slot;
|
| 99 | this.subtitle = subtitle;
|
| 100 | this.resetTaskTitle = resetTaskTitle;
|
| 101 | this.realmCreationTask = realmCreationTask;
|
| 102 | this.resetWorldRunnable = resetWorldRunnable;
|
| 103 | }
|
| 104 |
|
| 105 | public static RealmsResetWorldScreen forNewRealm(
|
| 106 | final Screen lastScreen, final RealmsServer serverData, final RealmCreationTask realmCreationTask, final Runnable resetWorldRunnable
|
| 107 | ) {
|
| 108 | return new RealmsResetWorldScreen(
|
| 109 | lastScreen,
|
| 110 | serverData,
|
| 111 | serverData.activeSlot,
|
| 112 | CREATE_REALM_TITLE,
|
| 113 | CREATE_REALM_SUBTITLE,
|
| 114 | CREATE_WORLD_RESET_TASK_TITLE,
|
| 115 | realmCreationTask,
|
| 116 | resetWorldRunnable
|
| 117 | );
|
| 118 | }
|
| 119 |
|
| 120 | public static RealmsResetWorldScreen forEmptySlot(final Screen lastScreen, final int slot, final RealmsServer serverData, final Runnable resetWorldRunnable) {
|
| 121 | return new RealmsResetWorldScreen(lastScreen, serverData, slot, CREATE_WORLD_TITLE, CREATE_WORLD_SUBTITLE, CREATE_WORLD_RESET_TASK_TITLE, resetWorldRunnable);
|
| 122 | }
|
| 123 |
|
| 124 | public static RealmsResetWorldScreen forResetSlot(final Screen lastScreen, final RealmsServer serverData, final Runnable resetWorldRunnable) {
|
| 125 | return new RealmsResetWorldScreen(
|
| 126 | lastScreen, serverData, serverData.activeSlot, RESET_WORLD_TITLE, RESET_WORLD_SUBTITLE, RESET_WORLD_RESET_TASK_TITLE, resetWorldRunnable
|
| 127 | );
|
| 128 | }
|
| 129 |
|
| 130 | @Override
|
| 131 | public void init() {
|
| 132 | LinearLayout header = this.layout.addToHeader(LinearLayout.vertical());
|
| 133 | header.defaultCellSetting().padding(9 / 3);
|
| 134 | header.addChild(new StringWidget(this.title, this.font), LayoutSettings::alignHorizontallyCenter);
|
| 135 | header.addChild(new StringWidget(this.subtitle, this.font), LayoutSettings::alignHorizontallyCenter);
|
| 136 | (new Thread("Realms-reset-world-fetcher") {
|
| 137 | @Override
|
| 138 | public void run() {
|
| 139 | RealmsClient client = RealmsClient.getOrCreate();
|
| 140 |
|
| 141 | try {
|
| 142 | WorldTemplatePaginatedList templates = client.fetchWorldTemplates(1, 10, RealmsServer.WorldType.NORMAL);
|
| 143 | WorldTemplatePaginatedList adventuremaps = client.fetchWorldTemplates(1, 10, RealmsServer.WorldType.ADVENTUREMAP);
|
| 144 | WorldTemplatePaginatedList experiences = client.fetchWorldTemplates(1, 10, RealmsServer.WorldType.EXPERIENCE);
|
| 145 | WorldTemplatePaginatedList inspirations = client.fetchWorldTemplates(1, 10, RealmsServer.WorldType.INSPIRATION);
|
| 146 | RealmsResetWorldScreen.this.minecraft.execute(() -> {
|
| 147 | RealmsResetWorldScreen.this.templates = templates;
|
| 148 | RealmsResetWorldScreen.this.adventuremaps = adventuremaps;
|
| 149 | RealmsResetWorldScreen.this.experiences = experiences;
|
| 150 | RealmsResetWorldScreen.this.inspirations = inspirations;
|
| 151 | });
|
| 152 | } catch (RealmsServiceException e) {
|
| 153 | RealmsResetWorldScreen.LOGGER.error("Couldn't fetch templates in reset world", e);
|
| 154 | }
|
| 155 | }
|
| 156 | }).start();
|
| 157 | GridLayout grid = this.layout.addToContents(new GridLayout());
|
| 158 | GridLayout.RowHelper helper = grid.createRowHelper(3);
|
| 159 | helper.defaultCellSetting().paddingHorizontal(16);
|
| 160 | helper.addChild(
|
| 161 | new RealmsResetWorldScreen.FrameButton(
|
| 162 | this.minecraft.font,
|
| 163 | GENERATE_NEW_WORLD,
|
| 164 | NEW_WORLD_LOCATION,
|
| 165 | button -> RealmsCreateWorldFlow.createWorld(this.minecraft, this.lastScreen, this, this.slot, this.serverData, this.realmCreationTask)
|
| 166 | )
|
| 167 | );
|
| 168 | helper.addChild(
|
| 169 | new RealmsResetWorldScreen.FrameButton(
|
| 170 | this.minecraft.font,
|
| 171 | RealmsSelectFileToUploadScreen.TITLE,
|
| 172 | UPLOAD_LOCATION,
|
| 173 | var1x -> this.minecraft.gui.setScreen(new RealmsSelectFileToUploadScreen(this.realmCreationTask, this.serverData.id, this.slot, this))
|
| 174 | )
|
| 175 | );
|
| 176 | helper.addChild(
|
| 177 | new RealmsResetWorldScreen.FrameButton(
|
| 178 | this.minecraft.font,
|
| 179 | WORLD_TEMPLATES_TITLE,
|
| 180 | SURVIVAL_SPAWN_LOCATION,
|
| 181 | var1x -> this.minecraft
|
| 182 | .gui
|
| 183 | .setScreen(new RealmsSelectWorldTemplateScreen(WORLD_TEMPLATES_TITLE, this::templateSelectionCallback, RealmsServer.WorldType.NORMAL, this.templates))
|
| 184 | )
|
| 185 | );
|
| 186 | helper.addChild(SpacerElement.height(16), 3);
|
| 187 | helper.addChild(
|
| 188 | new RealmsResetWorldScreen.FrameButton(
|
| 189 | this.minecraft.font,
|
| 190 | ADVENTURES_TITLE,
|
| 191 | ADVENTURE_MAP_LOCATION,
|
| 192 | var1x -> this.minecraft
|
| 193 | .gui
|
| 194 | .setScreen(new RealmsSelectWorldTemplateScreen(ADVENTURES_TITLE, this::templateSelectionCallback, RealmsServer.WorldType.ADVENTUREMAP, this.adventuremaps))
|
| 195 | )
|
| 196 | );
|
| 197 | helper.addChild(
|
| 198 | new RealmsResetWorldScreen.FrameButton(
|
| 199 | this.minecraft.font,
|
| 200 | EXPERIENCES_TITLE,
|
| 201 | EXPERIENCE_LOCATION,
|
| 202 | var1x -> this.minecraft
|
| 203 | .gui
|
| 204 | .setScreen(new RealmsSelectWorldTemplateScreen(EXPERIENCES_TITLE, this::templateSelectionCallback, RealmsServer.WorldType.EXPERIENCE, this.experiences))
|
| 205 | )
|
| 206 | );
|
| 207 | helper.addChild(
|
| 208 | new RealmsResetWorldScreen.FrameButton(
|
| 209 | this.minecraft.font,
|
| 210 | INSPIRATION_TITLE,
|
| 211 | INSPIRATION_LOCATION,
|
| 212 | var1x -> this.minecraft
|
| 213 | .gui
|
| 214 | .setScreen(new RealmsSelectWorldTemplateScreen(INSPIRATION_TITLE, this::templateSelectionCallback, RealmsServer.WorldType.INSPIRATION, this.inspirations))
|
| 215 | )
|
| 216 | );
|
| 217 | this.layout.addToFooter(Button.builder(CommonComponents.GUI_BACK, button -> this.onClose()).build());
|
| 218 | this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
|
| 219 | this.repositionElements();
|
| 220 | }
|
| 221 |
|
| 222 | @Override
|
| 223 | protected void repositionElements() {
|
| 224 | this.layout.arrangeElements();
|
| 225 | }
|
| 226 |
|
| 227 | @Override
|
| 228 | public Component getNarrationMessage() {
|
| 229 | return CommonComponents.joinForNarration(this.getTitle(), this.subtitle);
|
| 230 | }
|
| 231 |
|
| 232 | @Override
|
| 233 | public void onClose() {
|
| 234 | this.minecraft.gui.setScreen(this.lastScreen);
|
| 235 | }
|
| 236 |
|
| 237 | private void templateSelectionCallback(final @Nullable WorldTemplate template) {
|
| 238 | this.minecraft.gui.setScreen(this);
|
| 239 | if (template != null) {
|
| 240 | this.runResetTasks(new ResettingTemplateWorldTask(template, this.serverData.id, this.resetTaskTitle, this.resetWorldRunnable));
|
| 241 | }
|
| 242 |
|
| 243 | RealmsMainScreen.refreshServerList();
|
| 244 | }
|
| 245 |
|
| 246 | private void runResetTasks(final LongRunningTask resetTask) {
|
| 247 | List<LongRunningTask> tasks = new ArrayList<>();
|
| 248 | if (this.realmCreationTask != null) {
|
| 249 | tasks.add(this.realmCreationTask);
|
| 250 | }
|
| 251 |
|
| 252 | if (this.slot != this.serverData.activeSlot) {
|
| 253 | tasks.add(new SwitchSlotTask(this.serverData.id, this.slot, () -> {}));
|
| 254 | }
|
| 255 |
|
| 256 | tasks.add(resetTask);
|
| 257 | this.minecraft.gui.setScreen(new RealmsLongRunningMcoTaskScreen(this.lastScreen, tasks.toArray(new LongRunningTask[0])));
|
| 258 | }
|
| 259 |
|
| 260 | @Environment(EnvType.CLIENT)
|
| 261 | private class FrameButton extends Button {
|
| 262 | private static final Identifier SLOT_FRAME_SPRITE = Identifier.withDefaultNamespace("widget/slot_frame");
|
| 263 | private static final int FRAME_SIZE = 60;
|
| 264 | private static final int FRAME_WIDTH = 2;
|
| 265 | private static final int IMAGE_SIZE = 56;
|
| 266 | private final Identifier image;
|
| 267 |
|
| 268 | private FrameButton(final Font font, final Component text, final Identifier image, final Button.OnPress onPress) {
|
| 269 | super(0, 0, 60, 60 + 9, text, onPress, DEFAULT_NARRATION);
|
| 270 | this.image = image;
|
| 271 | }
|
| 272 |
|
| 273 | @Override
|
| 274 | public void extractContents(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 275 | boolean hoveredOrFocused = this.isHoveredOrFocused();
|
| 276 | int color = -1;
|
| 277 | if (hoveredOrFocused) {
|
| 278 | color = ARGB.colorFromFloat(1.0F, 0.56F, 0.56F, 0.56F);
|
| 279 | }
|
| 280 |
|
| 281 | int x = this.getX();
|
| 282 | int y = this.getY();
|
| 283 | graphics.blit(RenderPipelines.GUI_TEXTURED, this.image, x + 2, y + 2, 0.0F, 0.0F, 56, 56, 56, 56, 56, 56, color);
|
| 284 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, SLOT_FRAME_SPRITE, x, y, 60, 60, color);
|
| 285 | int textColor = hoveredOrFocused ? -6250336 : -1;
|
| 286 | graphics.centeredText(RealmsResetWorldScreen.this.font, this.getMessage(), x + 28, y - 14, textColor);
|
| 287 | }
|
| 288 | }
|
| 289 | }
|
| 290 |
|