| 1 | package com.mojang.realmsclient.gui.screens;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.realmsclient.util.task.RealmCreationTask;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.gui.components.Button;
|
| 8 | import net.minecraft.client.gui.components.EditBox;
|
| 9 | import net.minecraft.client.gui.components.StringWidget;
|
| 10 | import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
|
| 11 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 12 | import net.minecraft.client.gui.screens.worldselection.WorldSelectionList;
|
| 13 | import net.minecraft.network.chat.CommonComponents;
|
| 14 | import net.minecraft.network.chat.Component;
|
| 15 | import net.minecraft.realms.RealmsScreen;
|
| 16 | import net.minecraft.world.level.storage.LevelSummary;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 | import org.slf4j.Logger;
|
| 19 |
|
| 20 | @Environment(EnvType.CLIENT)
|
| 21 | public class RealmsSelectFileToUploadScreen extends RealmsScreen {
|
| 22 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 23 | public static final Component TITLE = Component.translatable("mco.upload.select.world.title");
|
| 24 | private static final Component UNABLE_TO_LOAD_WORLD = Component.translatable("selectWorld.unable_to_load");
|
| 25 | private final @Nullable RealmCreationTask realmCreationTask;
|
| 26 | private final RealmsResetWorldScreen lastScreen;
|
| 27 | private final long realmId;
|
| 28 | private final int slotId;
|
| 29 | private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this, 8 + 9 + 8 + 20 + 4, 33);
|
| 30 | protected @Nullable EditBox searchBox;
|
| 31 | private @Nullable WorldSelectionList list;
|
| 32 | private @Nullable Button uploadButton;
|
| 33 |
|
| 34 | public RealmsSelectFileToUploadScreen(
|
| 35 | final @Nullable RealmCreationTask realmCreationTask, final long realmId, final int slotId, final RealmsResetWorldScreen lastScreen
|
| 36 | ) {
|
| 37 | super(TITLE);
|
| 38 | this.realmCreationTask = realmCreationTask;
|
| 39 | this.lastScreen = lastScreen;
|
| 40 | this.realmId = realmId;
|
| 41 | this.slotId = slotId;
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public void init() {
|
| 46 | LinearLayout header = this.layout.addToHeader(LinearLayout.vertical().spacing(4));
|
| 47 | header.defaultCellSetting().alignHorizontallyCenter();
|
| 48 | header.addChild(new StringWidget(this.title, this.font));
|
| 49 | this.searchBox = header.addChild(new EditBox(this.font, this.width / 2 - 100, 22, 200, 20, this.searchBox, Component.translatable("selectWorld.search")));
|
| 50 | this.searchBox.setResponder(value -> {
|
| 51 | if (this.list != null) {
|
| 52 | this.list.updateFilter(value);
|
| 53 | }
|
| 54 | });
|
| 55 |
|
| 56 | try {
|
| 57 | this.list = this.layout
|
| 58 | .addToContents(
|
| 59 | new WorldSelectionList.Builder(this.minecraft, this)
|
| 60 | .width(this.width)
|
| 61 | .height(this.layout.getContentHeight())
|
| 62 | .filter(this.searchBox.getValue())
|
| 63 | .oldList(this.list)
|
| 64 | .uploadWorld()
|
| 65 | .onEntrySelect(this::updateButtonState)
|
| 66 | .onEntryInteract(this::upload)
|
| 67 | .build()
|
| 68 | );
|
| 69 | } catch (Exception e) {
|
| 70 | LOGGER.error("Couldn't load level list", e);
|
| 71 | this.minecraft.gui.setScreen(new RealmsGenericErrorScreen(UNABLE_TO_LOAD_WORLD, Component.nullToEmpty(e.getMessage()), this.lastScreen));
|
| 72 | return;
|
| 73 | }
|
| 74 |
|
| 75 | LinearLayout footer = this.layout.addToFooter(LinearLayout.horizontal().spacing(8));
|
| 76 | footer.defaultCellSetting().alignHorizontallyCenter();
|
| 77 | this.uploadButton = footer.addChild(
|
| 78 | Button.builder(Component.translatable("mco.upload.button.name"), button -> this.list.getSelectedOpt().ifPresent(this::upload)).build()
|
| 79 | );
|
| 80 | footer.addChild(Button.builder(CommonComponents.GUI_BACK, button -> this.onClose()).build());
|
| 81 | this.updateButtonState(null);
|
| 82 | RealmsSelectFileToUploadScreen var3 = this;
|
| 83 | this.layout.visitWidgets(x$0 -> var3.addRenderableWidget(x$0));
|
| 84 | this.repositionElements();
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | protected void repositionElements() {
|
| 89 | if (this.list != null) {
|
| 90 | this.list.updateSize(this.width, this.layout);
|
| 91 | }
|
| 92 |
|
| 93 | this.layout.arrangeElements();
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | protected void setInitialFocus() {
|
| 98 | this.setInitialFocus(this.searchBox);
|
| 99 | }
|
| 100 |
|
| 101 | private void updateButtonState(final @Nullable LevelSummary ignored) {
|
| 102 | if (this.list != null && this.uploadButton != null) {
|
| 103 | this.uploadButton.active = this.list.getSelected() != null;
|
| 104 | }
|
| 105 | }
|
| 106 |
|
| 107 | private void upload(final WorldSelectionList.WorldListEntry worldListEntry) {
|
| 108 | this.minecraft.gui.setScreen(new RealmsUploadScreen(this.realmCreationTask, this.realmId, this.slotId, this.lastScreen, worldListEntry.getLevelSummary()));
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public void onClose() {
|
| 113 | this.minecraft.gui.setScreen(this.lastScreen);
|
| 114 | }
|
| 115 | }
|
| 116 |
|