minecraft-src/com/mojang/realmsclient/gui/screens/RealmsResetWorldScreen.java
2025-07-04 03:45:38 +03:00

296 lines
13 KiB
Java

package com.mojang.realmsclient.gui.screens;
import com.mojang.logging.LogUtils;
import com.mojang.realmsclient.RealmsMainScreen;
import com.mojang.realmsclient.client.RealmsClient;
import com.mojang.realmsclient.client.worldupload.RealmsCreateWorldFlow;
import com.mojang.realmsclient.dto.RealmsServer;
import com.mojang.realmsclient.dto.WorldTemplate;
import com.mojang.realmsclient.dto.WorldTemplatePaginatedList;
import com.mojang.realmsclient.dto.RealmsServer.WorldType;
import com.mojang.realmsclient.exception.RealmsServiceException;
import com.mojang.realmsclient.util.task.LongRunningTask;
import com.mojang.realmsclient.util.task.RealmCreationTask;
import com.mojang.realmsclient.util.task.ResettingTemplateWorldTask;
import com.mojang.realmsclient.util.task.SwitchSlotTask;
import java.util.ArrayList;
import java.util.List;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.StringWidget;
import net.minecraft.client.gui.components.Button.OnPress;
import net.minecraft.client.gui.layouts.GridLayout;
import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
import net.minecraft.client.gui.layouts.LayoutSettings;
import net.minecraft.client.gui.layouts.LinearLayout;
import net.minecraft.client.gui.layouts.SpacerElement;
import net.minecraft.client.gui.layouts.GridLayout.RowHelper;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.realms.RealmsScreen;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.ARGB;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
@Environment(EnvType.CLIENT)
public class RealmsResetWorldScreen extends RealmsScreen {
static final Logger LOGGER = LogUtils.getLogger();
private static final Component CREATE_REALM_TITLE = Component.translatable("mco.selectServer.create");
private static final Component CREATE_REALM_SUBTITLE = Component.translatable("mco.selectServer.create.subtitle");
private static final Component CREATE_WORLD_TITLE = Component.translatable("mco.configure.world.switch.slot");
private static final Component CREATE_WORLD_SUBTITLE = Component.translatable("mco.configure.world.switch.slot.subtitle");
private static final Component GENERATE_NEW_WORLD = Component.translatable("mco.reset.world.generate");
private static final Component RESET_WORLD_TITLE = Component.translatable("mco.reset.world.title");
private static final Component RESET_WORLD_SUBTITLE = Component.translatable("mco.reset.world.warning");
public static final Component CREATE_WORLD_RESET_TASK_TITLE = Component.translatable("mco.create.world.reset.title");
private static final Component RESET_WORLD_RESET_TASK_TITLE = Component.translatable("mco.reset.world.resetting.screen.title");
private static final Component WORLD_TEMPLATES_TITLE = Component.translatable("mco.reset.world.template");
private static final Component ADVENTURES_TITLE = Component.translatable("mco.reset.world.adventure");
private static final Component EXPERIENCES_TITLE = Component.translatable("mco.reset.world.experience");
private static final Component INSPIRATION_TITLE = Component.translatable("mco.reset.world.inspiration");
private final Screen lastScreen;
private final RealmsServer serverData;
private final Component subtitle;
private final int subtitleColor;
private final Component resetTaskTitle;
private static final ResourceLocation UPLOAD_LOCATION = ResourceLocation.withDefaultNamespace("textures/gui/realms/upload.png");
private static final ResourceLocation ADVENTURE_MAP_LOCATION = ResourceLocation.withDefaultNamespace("textures/gui/realms/adventure.png");
private static final ResourceLocation SURVIVAL_SPAWN_LOCATION = ResourceLocation.withDefaultNamespace("textures/gui/realms/survival_spawn.png");
private static final ResourceLocation NEW_WORLD_LOCATION = ResourceLocation.withDefaultNamespace("textures/gui/realms/new_world.png");
private static final ResourceLocation EXPERIENCE_LOCATION = ResourceLocation.withDefaultNamespace("textures/gui/realms/experience.png");
private static final ResourceLocation INSPIRATION_LOCATION = ResourceLocation.withDefaultNamespace("textures/gui/realms/inspiration.png");
WorldTemplatePaginatedList templates;
WorldTemplatePaginatedList adventuremaps;
WorldTemplatePaginatedList experiences;
WorldTemplatePaginatedList inspirations;
public final int slot;
@Nullable
private final RealmCreationTask realmCreationTask;
private final Runnable resetWorldRunnable;
private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this);
private RealmsResetWorldScreen(
Screen lastScreen,
RealmsServer serverData,
int slot,
Component title,
Component subtitle,
int subtitleColor,
Component resetTaskTitle,
Runnable resetWorldRunnable
) {
this(lastScreen, serverData, slot, title, subtitle, subtitleColor, resetTaskTitle, null, resetWorldRunnable);
}
public RealmsResetWorldScreen(
Screen lastScreen,
RealmsServer serverData,
int slot,
Component title,
Component subtitle,
int subtitleColor,
Component resetTaskTitle,
@Nullable RealmCreationTask realmCreationTask,
Runnable resetWorldRunnable
) {
super(title);
this.lastScreen = lastScreen;
this.serverData = serverData;
this.slot = slot;
this.subtitle = subtitle;
this.subtitleColor = subtitleColor;
this.resetTaskTitle = resetTaskTitle;
this.realmCreationTask = realmCreationTask;
this.resetWorldRunnable = resetWorldRunnable;
}
public static RealmsResetWorldScreen forNewRealm(Screen lastScreen, RealmsServer serverData, RealmCreationTask realmCreationTask, Runnable resetWorldRunnable) {
return new RealmsResetWorldScreen(
lastScreen,
serverData,
serverData.activeSlot,
CREATE_REALM_TITLE,
CREATE_REALM_SUBTITLE,
-6250336,
CREATE_WORLD_RESET_TASK_TITLE,
realmCreationTask,
resetWorldRunnable
);
}
public static RealmsResetWorldScreen forEmptySlot(Screen lastScreen, int slot, RealmsServer serverData, Runnable resetWorldRunnable) {
return new RealmsResetWorldScreen(
lastScreen, serverData, slot, CREATE_WORLD_TITLE, CREATE_WORLD_SUBTITLE, -6250336, CREATE_WORLD_RESET_TASK_TITLE, resetWorldRunnable
);
}
public static RealmsResetWorldScreen forResetSlot(Screen lastScreen, RealmsServer serverData, Runnable resetWorldRunnable) {
return new RealmsResetWorldScreen(
lastScreen, serverData, serverData.activeSlot, RESET_WORLD_TITLE, RESET_WORLD_SUBTITLE, -65536, RESET_WORLD_RESET_TASK_TITLE, resetWorldRunnable
);
}
@Override
public void init() {
LinearLayout linearLayout = this.layout.addToHeader(LinearLayout.vertical());
linearLayout.defaultCellSetting().padding(9 / 3);
linearLayout.addChild(new StringWidget(this.title, this.font), LayoutSettings::alignHorizontallyCenter);
linearLayout.addChild(new StringWidget(this.subtitle, this.font).setColor(this.subtitleColor), LayoutSettings::alignHorizontallyCenter);
(new Thread("Realms-reset-world-fetcher") {
public void run() {
RealmsClient realmsClient = RealmsClient.getOrCreate();
try {
WorldTemplatePaginatedList worldTemplatePaginatedList = realmsClient.fetchWorldTemplates(1, 10, WorldType.NORMAL);
WorldTemplatePaginatedList worldTemplatePaginatedList2 = realmsClient.fetchWorldTemplates(1, 10, WorldType.ADVENTUREMAP);
WorldTemplatePaginatedList worldTemplatePaginatedList3 = realmsClient.fetchWorldTemplates(1, 10, WorldType.EXPERIENCE);
WorldTemplatePaginatedList worldTemplatePaginatedList4 = realmsClient.fetchWorldTemplates(1, 10, WorldType.INSPIRATION);
RealmsResetWorldScreen.this.minecraft.execute(() -> {
RealmsResetWorldScreen.this.templates = worldTemplatePaginatedList;
RealmsResetWorldScreen.this.adventuremaps = worldTemplatePaginatedList2;
RealmsResetWorldScreen.this.experiences = worldTemplatePaginatedList3;
RealmsResetWorldScreen.this.inspirations = worldTemplatePaginatedList4;
});
} catch (RealmsServiceException var6) {
RealmsResetWorldScreen.LOGGER.error("Couldn't fetch templates in reset world", (Throwable)var6);
}
}
}).start();
GridLayout gridLayout = this.layout.addToContents(new GridLayout());
RowHelper rowHelper = gridLayout.createRowHelper(3);
rowHelper.defaultCellSetting().paddingHorizontal(16);
rowHelper.addChild(
new RealmsResetWorldScreen.FrameButton(
this.minecraft.font,
GENERATE_NEW_WORLD,
NEW_WORLD_LOCATION,
button -> RealmsCreateWorldFlow.createWorld(this.minecraft, this.lastScreen, this, this.slot, this.serverData, this.realmCreationTask)
)
);
rowHelper.addChild(
new RealmsResetWorldScreen.FrameButton(
this.minecraft.font,
RealmsSelectFileToUploadScreen.TITLE,
UPLOAD_LOCATION,
button -> this.minecraft.setScreen(new RealmsSelectFileToUploadScreen(this.realmCreationTask, this.serverData.id, this.slot, this))
)
);
rowHelper.addChild(
new RealmsResetWorldScreen.FrameButton(
this.minecraft.font,
WORLD_TEMPLATES_TITLE,
SURVIVAL_SPAWN_LOCATION,
button -> this.minecraft
.setScreen(new RealmsSelectWorldTemplateScreen(WORLD_TEMPLATES_TITLE, this::templateSelectionCallback, WorldType.NORMAL, this.templates))
)
);
rowHelper.addChild(SpacerElement.height(16), 3);
rowHelper.addChild(
new RealmsResetWorldScreen.FrameButton(
this.minecraft.font,
ADVENTURES_TITLE,
ADVENTURE_MAP_LOCATION,
button -> this.minecraft
.setScreen(new RealmsSelectWorldTemplateScreen(ADVENTURES_TITLE, this::templateSelectionCallback, WorldType.ADVENTUREMAP, this.adventuremaps))
)
);
rowHelper.addChild(
new RealmsResetWorldScreen.FrameButton(
this.minecraft.font,
EXPERIENCES_TITLE,
EXPERIENCE_LOCATION,
button -> this.minecraft
.setScreen(new RealmsSelectWorldTemplateScreen(EXPERIENCES_TITLE, this::templateSelectionCallback, WorldType.EXPERIENCE, this.experiences))
)
);
rowHelper.addChild(
new RealmsResetWorldScreen.FrameButton(
this.minecraft.font,
INSPIRATION_TITLE,
INSPIRATION_LOCATION,
button -> this.minecraft
.setScreen(new RealmsSelectWorldTemplateScreen(INSPIRATION_TITLE, this::templateSelectionCallback, WorldType.INSPIRATION, this.inspirations))
)
);
this.layout.addToFooter(Button.builder(CommonComponents.GUI_BACK, button -> this.onClose()).build());
this.layout.visitWidgets(guiEventListener -> {
AbstractWidget var10000 = this.addRenderableWidget(guiEventListener);
});
this.repositionElements();
}
@Override
protected void repositionElements() {
this.layout.arrangeElements();
}
@Override
public Component getNarrationMessage() {
return CommonComponents.joinForNarration(this.getTitle(), this.subtitle);
}
@Override
public void onClose() {
this.minecraft.setScreen(this.lastScreen);
}
private void templateSelectionCallback(@Nullable WorldTemplate template) {
this.minecraft.setScreen(this);
if (template != null) {
this.runResetTasks(new ResettingTemplateWorldTask(template, this.serverData.id, this.resetTaskTitle, this.resetWorldRunnable));
}
RealmsMainScreen.refreshServerList();
}
private void runResetTasks(LongRunningTask task) {
List<LongRunningTask> list = new ArrayList();
if (this.realmCreationTask != null) {
list.add(this.realmCreationTask);
}
if (this.slot != this.serverData.activeSlot) {
list.add(new SwitchSlotTask(this.serverData.id, this.slot, () -> {}));
}
list.add(task);
this.minecraft.setScreen(new RealmsLongRunningMcoTaskScreen(this.lastScreen, (LongRunningTask[])list.toArray(new LongRunningTask[0])));
}
@Environment(EnvType.CLIENT)
class FrameButton extends Button {
private static final ResourceLocation SLOT_FRAME_SPRITE = ResourceLocation.withDefaultNamespace("widget/slot_frame");
private static final int FRAME_SIZE = 60;
private static final int FRAME_WIDTH = 2;
private static final int IMAGE_SIZE = 56;
private final ResourceLocation image;
FrameButton(final Font font, final Component message, final ResourceLocation image, final OnPress onPress) {
super(0, 0, 60, 60 + 9, message, onPress, DEFAULT_NARRATION);
this.image = image;
}
@Override
public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
boolean bl = this.isHoveredOrFocused();
int i = -1;
if (bl) {
i = ARGB.colorFromFloat(1.0F, 0.56F, 0.56F, 0.56F);
}
int j = this.getX();
int k = this.getY();
guiGraphics.blit(RenderType::guiTextured, this.image, j + 2, k + 2, 0.0F, 0.0F, 56, 56, 56, 56, 56, 56, i);
guiGraphics.blitSprite(RenderType::guiTextured, SLOT_FRAME_SPRITE, j, k, 60, 60, i);
int l = bl ? -6250336 : -1;
guiGraphics.drawCenteredString(RealmsResetWorldScreen.this.font, this.getMessage(), j + 28, k - 14, l);
}
}
}