208 lines
		
	
	
	
		
			8.1 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			208 lines
		
	
	
	
		
			8.1 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
| package com.mojang.realmsclient.gui.screens.configuration;
 | |
| 
 | |
| import com.google.common.collect.Lists;
 | |
| import com.mojang.realmsclient.dto.RealmsServer;
 | |
| import com.mojang.realmsclient.dto.RealmsSlot;
 | |
| import com.mojang.realmsclient.dto.WorldTemplate;
 | |
| import com.mojang.realmsclient.gui.RealmsWorldSlotButton;
 | |
| import com.mojang.realmsclient.gui.screens.RealmsLongRunningMcoTaskScreen;
 | |
| import com.mojang.realmsclient.gui.screens.RealmsPopups;
 | |
| import com.mojang.realmsclient.gui.screens.RealmsResetWorldScreen;
 | |
| import com.mojang.realmsclient.gui.screens.RealmsSelectWorldTemplateScreen;
 | |
| import com.mojang.realmsclient.util.task.SwitchMinigameTask;
 | |
| import com.mojang.realmsclient.util.task.SwitchSlotTask;
 | |
| import java.util.List;
 | |
| import net.fabricmc.api.EnvType;
 | |
| import net.fabricmc.api.Environment;
 | |
| import net.minecraft.client.Minecraft;
 | |
| import net.minecraft.client.gui.components.Button;
 | |
| import net.minecraft.client.gui.components.tabs.GridLayoutTab;
 | |
| import net.minecraft.client.gui.layouts.GridLayout;
 | |
| import net.minecraft.client.gui.layouts.LayoutSettings;
 | |
| import net.minecraft.network.chat.Component;
 | |
| import org.jetbrains.annotations.Nullable;
 | |
| 
 | |
| @Environment(EnvType.CLIENT)
 | |
| class RealmsWorldsTab extends GridLayoutTab implements RealmsConfigurationTab {
 | |
| 	static final Component TITLE = Component.translatable("mco.configure.worlds.title");
 | |
| 	private final RealmsConfigureWorldScreen configurationScreen;
 | |
| 	private final Minecraft minecraft;
 | |
| 	private RealmsServer serverData;
 | |
| 	private final Button optionsButton;
 | |
| 	private final Button backupButton;
 | |
| 	private final Button resetWorldButton;
 | |
| 	private final List<RealmsWorldSlotButton> slotButtonList = Lists.<RealmsWorldSlotButton>newArrayList();
 | |
| 
 | |
| 	RealmsWorldsTab(RealmsConfigureWorldScreen configurationScreen, Minecraft minecraft, RealmsServer serverData) {
 | |
| 		super(TITLE);
 | |
| 		this.configurationScreen = configurationScreen;
 | |
| 		this.minecraft = minecraft;
 | |
| 		this.serverData = serverData;
 | |
| 		GridLayout.RowHelper rowHelper = this.layout.spacing(20).createRowHelper(1);
 | |
| 		GridLayout.RowHelper rowHelper2 = new GridLayout().spacing(16).createRowHelper(4);
 | |
| 		this.slotButtonList.clear();
 | |
| 
 | |
| 		for (int i = 1; i < 5; i++) {
 | |
| 			this.slotButtonList.add((RealmsWorldSlotButton)rowHelper2.addChild(this.createSlotButton(i), LayoutSettings.defaults().alignVerticallyBottom()));
 | |
| 		}
 | |
| 
 | |
| 		rowHelper.addChild(rowHelper2.getGrid());
 | |
| 		GridLayout.RowHelper rowHelper3 = new GridLayout().spacing(8).createRowHelper(1);
 | |
| 		this.optionsButton = rowHelper3.addChild(
 | |
| 			Button.builder(
 | |
| 					Component.translatable("mco.configure.world.buttons.options"),
 | |
| 					button -> minecraft.setScreen(
 | |
| 						new RealmsSlotOptionsScreen(
 | |
| 							configurationScreen, ((RealmsSlot)serverData.slots.get(serverData.activeSlot)).clone(), serverData.worldType, serverData.activeSlot
 | |
| 						)
 | |
| 					)
 | |
| 				)
 | |
| 				.bounds(0, 0, 150, 20)
 | |
| 				.build()
 | |
| 		);
 | |
| 		this.backupButton = rowHelper3.addChild(
 | |
| 			Button.builder(
 | |
| 					Component.translatable("mco.configure.world.backup"),
 | |
| 					button -> minecraft.setScreen(new RealmsBackupScreen(configurationScreen, serverData.clone(), serverData.activeSlot))
 | |
| 				)
 | |
| 				.bounds(0, 0, 150, 20)
 | |
| 				.build()
 | |
| 		);
 | |
| 		this.resetWorldButton = rowHelper3.addChild(Button.builder(Component.empty(), button -> this.resetButtonPressed()).bounds(0, 0, 150, 20).build());
 | |
| 		rowHelper.addChild(rowHelper3.getGrid(), LayoutSettings.defaults().alignHorizontallyCenter());
 | |
| 		this.backupButton.active = true;
 | |
| 		this.updateData(serverData);
 | |
| 	}
 | |
| 
 | |
| 	private void resetButtonPressed() {
 | |
| 		if (this.isMinigame()) {
 | |
| 			this.minecraft
 | |
| 				.setScreen(
 | |
| 					new RealmsSelectWorldTemplateScreen(
 | |
| 						Component.translatable("mco.template.title.minigame"), this::templateSelectionCallback, RealmsServer.WorldType.MINIGAME
 | |
| 					)
 | |
| 				);
 | |
| 		} else {
 | |
| 			this.minecraft
 | |
| 				.setScreen(
 | |
| 					RealmsResetWorldScreen.forResetSlot(
 | |
| 						this.configurationScreen, this.serverData.clone(), () -> this.minecraft.execute(() -> this.minecraft.setScreen(this.configurationScreen.getNewScreen()))
 | |
| 					)
 | |
| 				);
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	private void templateSelectionCallback(@Nullable WorldTemplate template) {
 | |
| 		if (template != null && WorldTemplate.WorldTemplateType.MINIGAME == template.type) {
 | |
| 			this.configurationScreen.stateChanged();
 | |
| 			RealmsConfigureWorldScreen realmsConfigureWorldScreen = this.configurationScreen.getNewScreen();
 | |
| 			this.minecraft
 | |
| 				.setScreen(new RealmsLongRunningMcoTaskScreen(realmsConfigureWorldScreen, new SwitchMinigameTask(this.serverData.id, template, realmsConfigureWorldScreen)));
 | |
| 		} else {
 | |
| 			this.minecraft.setScreen(this.configurationScreen);
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	private boolean isMinigame() {
 | |
| 		return this.serverData.isMinigameActive();
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	public void onSelected(RealmsServer server) {
 | |
| 		this.updateData(server);
 | |
| 	}
 | |
| 
 | |
| 	@Override
 | |
| 	public void updateData(RealmsServer server) {
 | |
| 		this.serverData = server;
 | |
| 		this.optionsButton.active = !server.expired && !this.isMinigame();
 | |
| 		this.resetWorldButton.active = !server.expired;
 | |
| 		if (this.isMinigame()) {
 | |
| 			this.resetWorldButton.setMessage(Component.translatable("mco.configure.world.buttons.switchminigame"));
 | |
| 		} else {
 | |
| 			boolean bl = server.slots.containsKey(server.activeSlot) && ((RealmsSlot)server.slots.get(server.activeSlot)).options.empty;
 | |
| 			if (bl) {
 | |
| 				this.resetWorldButton.setMessage(Component.translatable("mco.configure.world.buttons.newworld"));
 | |
| 			} else {
 | |
| 				this.resetWorldButton.setMessage(Component.translatable("mco.configure.world.buttons.resetworld"));
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		this.backupButton.active = !this.isMinigame();
 | |
| 
 | |
| 		for (RealmsWorldSlotButton realmsWorldSlotButton : this.slotButtonList) {
 | |
| 			RealmsWorldSlotButton.State state = realmsWorldSlotButton.setServerData(server);
 | |
| 			if (state.activeSlot) {
 | |
| 				realmsWorldSlotButton.setSize(80, 80);
 | |
| 			} else {
 | |
| 				realmsWorldSlotButton.setSize(50, 50);
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	private RealmsWorldSlotButton createSlotButton(int slotIndex) {
 | |
| 		return new RealmsWorldSlotButton(0, 0, 80, 80, slotIndex, this.serverData, button -> {
 | |
| 			RealmsWorldSlotButton.State state = ((RealmsWorldSlotButton)button).getState();
 | |
| 			switch (state.action) {
 | |
| 				case SWITCH_SLOT:
 | |
| 					if (state.minigame) {
 | |
| 						this.switchToMinigame();
 | |
| 					} else if (state.empty) {
 | |
| 						this.switchToEmptySlot(slotIndex, this.serverData);
 | |
| 					} else {
 | |
| 						this.switchToFullSlot(slotIndex, this.serverData);
 | |
| 					}
 | |
| 				case NOTHING:
 | |
| 					return;
 | |
| 				default:
 | |
| 					throw new IllegalStateException("Unknown action " + state.action);
 | |
| 			}
 | |
| 		});
 | |
| 	}
 | |
| 
 | |
| 	private void switchToMinigame() {
 | |
| 		RealmsSelectWorldTemplateScreen realmsSelectWorldTemplateScreen = new RealmsSelectWorldTemplateScreen(
 | |
| 			Component.translatable("mco.template.title.minigame"), this::templateSelectionCallback, RealmsServer.WorldType.MINIGAME
 | |
| 		);
 | |
| 		realmsSelectWorldTemplateScreen.setWarning(Component.translatable("mco.minigame.world.info.line1"), Component.translatable("mco.minigame.world.info.line2"));
 | |
| 		this.minecraft.setScreen(realmsSelectWorldTemplateScreen);
 | |
| 	}
 | |
| 
 | |
| 	private void switchToFullSlot(int slotIndex, RealmsServer serverData) {
 | |
| 		this.minecraft
 | |
| 			.setScreen(
 | |
| 				RealmsPopups.infoPopupScreen(
 | |
| 					this.configurationScreen,
 | |
| 					Component.translatable("mco.configure.world.slot.switch.question.line1"),
 | |
| 					popupScreen -> {
 | |
| 						RealmsConfigureWorldScreen realmsConfigureWorldScreen = this.configurationScreen.getNewScreen();
 | |
| 						this.configurationScreen.stateChanged();
 | |
| 						this.minecraft
 | |
| 							.setScreen(
 | |
| 								new RealmsLongRunningMcoTaskScreen(
 | |
| 									realmsConfigureWorldScreen,
 | |
| 									new SwitchSlotTask(serverData.id, slotIndex, () -> this.minecraft.execute(() -> this.minecraft.setScreen(realmsConfigureWorldScreen)))
 | |
| 								)
 | |
| 							);
 | |
| 					}
 | |
| 				)
 | |
| 			);
 | |
| 	}
 | |
| 
 | |
| 	private void switchToEmptySlot(int slotIndex, RealmsServer serverData) {
 | |
| 		this.minecraft
 | |
| 			.setScreen(
 | |
| 				RealmsPopups.infoPopupScreen(
 | |
| 					this.configurationScreen,
 | |
| 					Component.translatable("mco.configure.world.slot.switch.question.line1"),
 | |
| 					popupScreen -> {
 | |
| 						this.configurationScreen.stateChanged();
 | |
| 						RealmsResetWorldScreen realmsResetWorldScreen = RealmsResetWorldScreen.forEmptySlot(
 | |
| 							this.configurationScreen, slotIndex, serverData, () -> this.minecraft.execute(() -> this.minecraft.setScreen(this.configurationScreen.getNewScreen()))
 | |
| 						);
 | |
| 						this.minecraft.setScreen(realmsResetWorldScreen);
 | |
| 					}
 | |
| 				)
 | |
| 			);
 | |
| 	}
 | |
| }
 |