| 1 | package com.mojang.realmsclient.gui.screens.configuration;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.realmsclient.dto.RealmsServer;
|
| 5 | import com.mojang.realmsclient.dto.WorldTemplate;
|
| 6 | import com.mojang.realmsclient.gui.RealmsWorldSlotButton;
|
| 7 | import com.mojang.realmsclient.gui.screens.RealmsLongRunningMcoTaskScreen;
|
| 8 | import com.mojang.realmsclient.gui.screens.RealmsPopups;
|
| 9 | import com.mojang.realmsclient.gui.screens.RealmsResetWorldScreen;
|
| 10 | import com.mojang.realmsclient.gui.screens.RealmsSelectWorldTemplateScreen;
|
| 11 | import com.mojang.realmsclient.util.task.SwitchMinigameTask;
|
| 12 | import com.mojang.realmsclient.util.task.SwitchSlotTask;
|
| 13 | import java.util.List;
|
| 14 | import net.fabricmc.api.EnvType;
|
| 15 | import net.fabricmc.api.Environment;
|
| 16 | import net.minecraft.client.Minecraft;
|
| 17 | import net.minecraft.client.gui.components.Button;
|
| 18 | import net.minecraft.client.gui.components.tabs.GridLayoutTab;
|
| 19 | import net.minecraft.client.gui.layouts.GridLayout;
|
| 20 | import net.minecraft.client.gui.layouts.LayoutSettings;
|
| 21 | import net.minecraft.network.chat.Component;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 |
|
| 24 | @Environment(EnvType.CLIENT)
|
| 25 | public class RealmsWorldsTab extends GridLayoutTab implements RealmsConfigurationTab {
|
| 26 | static final Component TITLE = Component.translatable("mco.configure.worlds.title");
|
| 27 | private final RealmsConfigureWorldScreen configurationScreen;
|
| 28 | private final Minecraft minecraft;
|
| 29 | private RealmsServer serverData;
|
| 30 | private final Button optionsButton;
|
| 31 | private final Button backupButton;
|
| 32 | private final Button resetWorldButton;
|
| 33 | private final List<RealmsWorldSlotButton> slotButtonList = Lists.newArrayList();
|
| 34 |
|
| 35 | public RealmsWorldsTab(final RealmsConfigureWorldScreen configurationScreen, final Minecraft minecraft, final RealmsServer serverData) {
|
| 36 | super(TITLE);
|
| 37 | this.configurationScreen = configurationScreen;
|
| 38 | this.minecraft = minecraft;
|
| 39 | this.serverData = serverData;
|
| 40 | GridLayout.RowHelper helper = this.layout.spacing(20).createRowHelper(1);
|
| 41 | GridLayout.RowHelper slots = new GridLayout().spacing(16).createRowHelper(4);
|
| 42 | this.slotButtonList.clear();
|
| 43 |
|
| 44 | for (int i = 1; i < 5; i++) {
|
| 45 | this.slotButtonList.add(slots.addChild(this.createSlotButton(i), LayoutSettings.defaults().alignVerticallyBottom()));
|
| 46 | }
|
| 47 |
|
| 48 | helper.addChild(slots.getGrid());
|
| 49 | GridLayout.RowHelper buttons = new GridLayout().spacing(8).createRowHelper(1);
|
| 50 | this.optionsButton = buttons.addChild(
|
| 51 | Button.builder(
|
| 52 | Component.translatable("mco.configure.world.buttons.options"),
|
| 53 | var3 -> minecraft.gui
|
| 54 | .setScreen(
|
| 55 | new RealmsSlotOptionsScreen(configurationScreen, serverData.slots.get(serverData.activeSlot).copy(), serverData.worldType, serverData.activeSlot)
|
| 56 | )
|
| 57 | )
|
| 58 | .bounds(0, 0, 150, 20)
|
| 59 | .build()
|
| 60 | );
|
| 61 | this.backupButton = buttons.addChild(
|
| 62 | Button.builder(
|
| 63 | Component.translatable("mco.configure.world.backup"),
|
| 64 | var3 -> minecraft.gui.setScreen(new RealmsBackupScreen(configurationScreen, serverData.copy(), serverData.activeSlot))
|
| 65 | )
|
| 66 | .bounds(0, 0, 150, 20)
|
| 67 | .build()
|
| 68 | );
|
| 69 | this.resetWorldButton = buttons.addChild(Button.builder(Component.empty(), button -> this.resetButtonPressed()).bounds(0, 0, 150, 20).build());
|
| 70 | helper.addChild(buttons.getGrid(), LayoutSettings.defaults().alignHorizontallyCenter());
|
| 71 | this.backupButton.active = true;
|
| 72 | this.updateData(serverData);
|
| 73 | }
|
| 74 |
|
| 75 | private void resetButtonPressed() {
|
| 76 | if (this.isMinigame()) {
|
| 77 | this.minecraft
|
| 78 | .gui
|
| 79 | .setScreen(
|
| 80 | new RealmsSelectWorldTemplateScreen(
|
| 81 | Component.translatable("mco.template.title.minigame"), this::templateSelectionCallback, RealmsServer.WorldType.MINIGAME, null
|
| 82 | )
|
| 83 | );
|
| 84 | } else {
|
| 85 | this.minecraft
|
| 86 | .gui
|
| 87 | .setScreen(
|
| 88 | RealmsResetWorldScreen.forResetSlot(
|
| 89 | this.configurationScreen,
|
| 90 | this.serverData.copy(),
|
| 91 | () -> this.minecraft.execute(() -> this.minecraft.gui.setScreen(this.configurationScreen.getNewScreen()))
|
| 92 | )
|
| 93 | );
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | private void templateSelectionCallback(final @Nullable WorldTemplate worldTemplate) {
|
| 98 | if (worldTemplate != null && WorldTemplate.WorldTemplateType.MINIGAME == worldTemplate.type()) {
|
| 99 | this.configurationScreen.stateChanged();
|
| 100 | RealmsConfigureWorldScreen newScreen = this.configurationScreen.getNewScreen();
|
| 101 | this.minecraft.gui.setScreen(new RealmsLongRunningMcoTaskScreen(newScreen, new SwitchMinigameTask(this.serverData.id, worldTemplate, newScreen)));
|
| 102 | } else {
|
| 103 | this.minecraft.gui.setScreen(this.configurationScreen);
|
| 104 | }
|
| 105 | }
|
| 106 |
|
| 107 | private boolean isMinigame() {
|
| 108 | return this.serverData.isMinigameActive();
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public void onSelected(final RealmsServer serverData) {
|
| 113 | this.updateData(serverData);
|
| 114 | }
|
| 115 |
|
| 116 | @Override
|
| 117 | public void updateData(final RealmsServer serverData) {
|
| 118 | this.serverData = serverData;
|
| 119 | this.optionsButton.active = !serverData.expired && !this.isMinigame();
|
| 120 | this.resetWorldButton.active = !serverData.expired;
|
| 121 | if (this.isMinigame()) {
|
| 122 | this.resetWorldButton.setMessage(Component.translatable("mco.configure.world.buttons.switchminigame"));
|
| 123 | } else {
|
| 124 | boolean emptySlot = serverData.slots.containsKey(serverData.activeSlot) && serverData.slots.get(serverData.activeSlot).options.empty;
|
| 125 | if (emptySlot) {
|
| 126 | this.resetWorldButton.setMessage(Component.translatable("mco.configure.world.buttons.newworld"));
|
| 127 | } else {
|
| 128 | this.resetWorldButton.setMessage(Component.translatable("mco.configure.world.buttons.resetworld"));
|
| 129 | }
|
| 130 | }
|
| 131 |
|
| 132 | this.backupButton.active = !this.isMinigame();
|
| 133 |
|
| 134 | for (RealmsWorldSlotButton realmsWorldSlotButton : this.slotButtonList) {
|
| 135 | RealmsWorldSlotButton.State state = realmsWorldSlotButton.setServerData(serverData);
|
| 136 | if (state.activeSlot) {
|
| 137 | realmsWorldSlotButton.setSize(80, 80);
|
| 138 | } else {
|
| 139 | realmsWorldSlotButton.setSize(50, 50);
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | this.slotButtonList.get(serverData.activeSlot - 1).updateSlotState(serverData);
|
| 144 | }
|
| 145 |
|
| 146 | private RealmsWorldSlotButton createSlotButton(final int i) {
|
| 147 | return new RealmsWorldSlotButton(this.configurationScreen, 0, 0, 80, 80, i, this.serverData, button -> {
|
| 148 | RealmsWorldSlotButton.State state = ((RealmsWorldSlotButton)button).getState();
|
| 149 | switch (state.action) {
|
| 150 | case SWITCH_SLOT:
|
| 151 | if (state.minigame) {
|
| 152 | this.switchToMinigame();
|
| 153 | } else if (state.empty) {
|
| 154 | this.switchToEmptySlot(i, this.serverData);
|
| 155 | } else {
|
| 156 | this.switchToFullSlot(i, this.serverData);
|
| 157 | }
|
| 158 | case NOTHING:
|
| 159 | return;
|
| 160 | default:
|
| 161 | throw new IllegalStateException("Unknown action " + state.action);
|
| 162 | }
|
| 163 | });
|
| 164 | }
|
| 165 |
|
| 166 | private void switchToMinigame() {
|
| 167 | RealmsSelectWorldTemplateScreen screen = new RealmsSelectWorldTemplateScreen(
|
| 168 | Component.translatable("mco.template.title.minigame"),
|
| 169 | this::templateSelectionCallback,
|
| 170 | RealmsServer.WorldType.MINIGAME,
|
| 171 | null,
|
| 172 | List.of(
|
| 173 | Component.translatable("mco.minigame.world.info.line1").withColor(-4539718), Component.translatable("mco.minigame.world.info.line2").withColor(-4539718)
|
| 174 | )
|
| 175 | );
|
| 176 | this.minecraft.gui.setScreen(screen);
|
| 177 | }
|
| 178 |
|
| 179 | private void switchToFullSlot(final int selectedSlot, final RealmsServer serverData) {
|
| 180 | this.minecraft
|
| 181 | .gui
|
| 182 | .setScreen(
|
| 183 | RealmsPopups.infoPopupScreen(
|
| 184 | this.configurationScreen,
|
| 185 | Component.translatable("mco.configure.world.slot.switch.question.line1"),
|
| 186 | popup -> {
|
| 187 | RealmsConfigureWorldScreen newScreen = this.configurationScreen.getNewScreen();
|
| 188 | this.configurationScreen.stateChanged();
|
| 189 | this.minecraft
|
| 190 | .gui
|
| 191 | .setScreen(
|
| 192 | new RealmsLongRunningMcoTaskScreen(
|
| 193 | newScreen, new SwitchSlotTask(serverData.id, selectedSlot, () -> this.minecraft.execute(() -> this.minecraft.gui.setScreen(newScreen)))
|
| 194 | )
|
| 195 | );
|
| 196 | }
|
| 197 | )
|
| 198 | );
|
| 199 | }
|
| 200 |
|
| 201 | private void switchToEmptySlot(final int selectedSlot, final RealmsServer serverData) {
|
| 202 | this.minecraft
|
| 203 | .gui
|
| 204 | .setScreen(
|
| 205 | RealmsPopups.infoPopupScreen(
|
| 206 | this.configurationScreen,
|
| 207 | Component.translatable("mco.configure.world.slot.switch.question.line1"),
|
| 208 | var3 -> {
|
| 209 | this.configurationScreen.stateChanged();
|
| 210 | RealmsResetWorldScreen resetWorldScreen = RealmsResetWorldScreen.forEmptySlot(
|
| 211 | this.configurationScreen,
|
| 212 | selectedSlot,
|
| 213 | serverData,
|
| 214 | () -> this.minecraft.execute(() -> this.minecraft.gui.setScreen(this.configurationScreen.getNewScreen()))
|
| 215 | );
|
| 216 | this.minecraft.gui.setScreen(resetWorldScreen);
|
| 217 | }
|
| 218 | )
|
| 219 | );
|
| 220 | }
|
| 221 | }
|
| 222 |
|