RealmsConfigureWorldScreen .java

com.mojang.realmsclient.gui.screens.configuration.RealmsConfigureWorldScreen (class): members: getNewScreenWithKnownData, tierConfigurationFetched, desc.

com.mojang.realmsclient.gui.screens.configurationclass440 linescommonextends RealmsScreen16.4 KB

API Surface (21)

void init()
int getContentHeight()
int getHeaderHeight()
Screen getLastScreen()
Screen createErrorScreen(final RealmsServiceException exception)
void repositionElements()
void extractRenderState(final GuiGraphicsExtractor graphics, final int xm, final int ym, final float a)
boolean keyPressed(final KeyEvent event)
void extractMenuBackground(final GuiGraphicsExtractor graphics)
void onClose()
void fetchRegionData()
void fetchServerData(final long realmId)
void fetchSubscription(final long realmId)
void saveSlotSettings(final RealmsSlot slot)
boolean saveSettings(final String name, final String desc, final RegionSelectionPreference preference, final @Nullable RealmsRegion region, final @Nullable Integer renderDistance, final @Nullable Integer simulationDistance)
void openTheWorld(final boolean join)
void closeTheWorld()
void stateChanged()
boolean invitePlayer(final long serverId, final String name)
RealmsConfigureWorldScreen getNewScreen()
RealmsConfigureWorldScreen getNewScreenWithKnownData(final RealmsServer serverData)

Fields & Constants (21)

LOGGER
PLAY_TEXT
FOOTER_SEPARATOR_HEIGHT
lastScreen
serverData
regions
subscription
tierConfiguration
tierConfigurationFetched
regionServiceQuality
serverId
stateChanged
tabManager
playButton
tabNavigationBar
layout
name
desc
preference
region
renderDistance

String literals (10)

"mco.selectServer.play"
"mco.configure.world.loading"
"Couldn't get realms region data"
"Couldn't get own world"
"Couldn't get subscription"
"Couldn't get Realm tier configuration"
"mco.configure.world.settings.expired"
"Couldn't save slot settings"
"Couldn't save settings"
"Couldn't invite user"

Full Source

RealmsConfigureWorldScreen.java440 lines · Minecraft 26.3 · verbatim
1
package com.mojang.realmsclient.gui.screens.configuration;
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.RealmsError;
7
import com.mojang.realmsclient.dto.PlayerInfo;
8
import com.mojang.realmsclient.dto.PreferredRegionsDto;
9
import com.mojang.realmsclient.dto.RealmTierConfigurationDto;
10
import com.mojang.realmsclient.dto.RealmsRegion;
11
import com.mojang.realmsclient.dto.RealmsServer;
12
import com.mojang.realmsclient.dto.RealmsSlot;
13
import com.mojang.realmsclient.dto.RegionDataDto;
14
import com.mojang.realmsclient.dto.RegionSelectionPreference;
15
import com.mojang.realmsclient.dto.RegionSelectionPreferenceDto;
16
import com.mojang.realmsclient.dto.ServiceQuality;
17
import com.mojang.realmsclient.dto.Subscription;
18
import com.mojang.realmsclient.exception.RealmsServiceException;
19
import com.mojang.realmsclient.gui.screens.RealmsGenericErrorScreen;
20
import com.mojang.realmsclient.gui.screens.RealmsLongRunningMcoTaskScreen;
21
import com.mojang.realmsclient.util.RealmsUtil;
22
import com.mojang.realmsclient.util.task.CloseServerTask;
23
import com.mojang.realmsclient.util.task.OpenServerTask;
24
import java.util.LinkedHashMap;
25
import java.util.List;
26
import java.util.Map;
27
import java.util.Objects;
28
import net.fabricmc.api.EnvType;
29
import net.fabricmc.api.Environment;
30
import net.minecraft.client.gui.GuiGraphicsExtractor;
31
import net.minecraft.client.gui.components.Button;
32
import net.minecraft.client.gui.components.Tooltip;
33
import net.minecraft.client.gui.components.tabs.LoadingTab;
34
import net.minecraft.client.gui.components.tabs.MenuTabBar;
35
import net.minecraft.client.gui.components.tabs.Tab;
36
import net.minecraft.client.gui.components.tabs.TabManager;
37
import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
38
import net.minecraft.client.gui.layouts.LinearLayout;
39
import net.minecraft.client.gui.navigation.ScreenRectangle;
40
import net.minecraft.client.gui.screens.Screen;
41
import net.minecraft.client.gui.screens.worldselection.CreateWorldScreen;
42
import net.minecraft.client.input.KeyEvent;
43
import net.minecraft.client.renderer.RenderPipelines;
44
import net.minecraft.network.chat.CommonComponents;
45
import net.minecraft.network.chat.Component;
46
import net.minecraft.realms.RealmsScreen;
47
import net.minecraft.util.StringUtil;
48
import org.jspecify.annotations.Nullable;
49
import org.slf4j.Logger;
50
51
@Environment(EnvType.CLIENT)
52
public class RealmsConfigureWorldScreen extends RealmsScreen {
53
	private static final Logger LOGGER = LogUtils.getLogger();
54
	private static final Component PLAY_TEXT = Component.translatable("mco.selectServer.play");
55
	static final int FOOTER_SEPARATOR_HEIGHT = 2;
56
	private final RealmsMainScreen lastScreen;
57
	private @Nullable RealmsServer serverData;
58
	private @Nullable PreferredRegionsDto regions;
59
	private @Nullable Subscription subscription;
60
	private @Nullable RealmTierConfigurationDto tierConfiguration;
61
	private boolean tierConfigurationFetched;
62
	private final Map<RealmsRegion, ServiceQuality> regionServiceQuality = new LinkedHashMap<>();
63
	private final long serverId;
64
	private boolean stateChanged;
65
	private final TabManager tabManager = new TabManager(
66
		x$0 -> this.addRenderableWidget(x$0), x$0 -> this.removeWidget(x$0), this::onTabSelected, this::onTabDeselected
67
	);
68
	private @Nullable Button playButton;
69
	private @Nullable MenuTabBar tabNavigationBar;
70
	final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this);
71
72
	public RealmsConfigureWorldScreen(
73
		final RealmsMainScreen lastScreen, final long serverId, final @Nullable RealmsServer serverData, final @Nullable PreferredRegionsDto regions
74
	) {
75
		super(Component.empty());
76
		this.lastScreen = lastScreen;
77
		this.serverId = serverId;
78
		this.serverData = serverData;
79
		this.regions = regions;
80
	}
81
82
	public RealmsConfigureWorldScreen(final RealmsMainScreen lastScreen, final long serverId) {
83
		this(lastScreen, serverId, null, null);
84
	}
85
86
	@Override
87
	public void init() {
88
		if (this.serverData == null) {
89
			this.fetchServerData(this.serverId);
90
		}
91
92
		if (this.regions == null) {
93
			this.fetchRegionData();
94
		}
95
96
		if (this.subscription == null) {
97
			this.fetchSubscription(this.serverId);
98
		}
99
100
		if (!this.tierConfigurationFetched) {
101
			this.fetchTierConfiguration(this.serverId);
102
		}
103
104
		Component loadingTitle = Component.translatable("mco.configure.world.loading");
105
		this.tabNavigationBar = MenuTabBar.builder(this.tabManager, this.width)
106
			.addTabs(
107
				new LoadingTab(this.getFont(), RealmsWorldsTab.TITLE, loadingTitle),
108
				new LoadingTab(this.getFont(), RealmsPlayersTab.TITLE, loadingTitle),
109
				new LoadingTab(this.getFont(), RealmsSubscriptionTab.TITLE, loadingTitle),
110
				new LoadingTab(this.getFont(), RealmsSettingsTab.TITLE, loadingTitle)
111
			)
112
			.build();
113
		this.tabNavigationBar.setTabActiveState(3, false);
114
		this.addRenderableWidget(this.tabNavigationBar);
115
		LinearLayout footer = this.layout.addToFooter(LinearLayout.horizontal().spacing(8));
116
		this.playButton = footer.addChild(Button.builder(PLAY_TEXT, button -> {
117
			this.onClose();
118
			RealmsMainScreen.play(this.serverData, this);
119
		}).width(150).build());
120
		this.playButton.active = false;
121
		footer.addChild(Button.builder(CommonComponents.GUI_BACK, button -> this.onClose()).build());
122
		this.layout.visitWidgets(button -> {
123
			button.setTabOrderGroup(1);
124
			this.addRenderableWidget(button);
125
		});
126
		this.tabNavigationBar.selectTab(0, false);
127
		this.repositionElements();
128
		if (this.serverData != null && this.regions != null && this.subscription != null) {
129
			this.onRealmsDataFetched();
130
		}
131
	}
132
133
	private void onTabSelected(final Tab tab) {
134
		if (this.serverData != null && tab instanceof RealmsConfigurationTab configurationTab) {
135
			configurationTab.onSelected(this.serverData);
136
		}
137
	}
138
139
	private void onTabDeselected(final Tab tab) {
140
		if (this.serverData != null && tab instanceof RealmsConfigurationTab configurationTab) {
141
			configurationTab.onDeselected(this.serverData);
142
		}
143
	}
144
145
	public int getContentHeight() {
146
		return this.layout.getContentHeight();
147
	}
148
149
	public int getHeaderHeight() {
150
		return this.layout.getHeaderHeight();
151
	}
152
153
	public Screen getLastScreen() {
154
		return this.lastScreen;
155
	}
156
157
	public Screen createErrorScreen(final RealmsServiceException exception) {
158
		return new RealmsGenericErrorScreen(exception, this.lastScreen);
159
	}
160
161
	@Override
162
	public void repositionElements() {
163
		if (this.tabNavigationBar != null) {
164
			this.tabNavigationBar.arrangeElements(this.width);
165
			int tabAreaTop = this.tabNavigationBar.getRectangle().bottom();
166
			ScreenRectangle tabArea = new ScreenRectangle(0, tabAreaTop, this.width, this.height - this.layout.getFooterHeight() - tabAreaTop);
167
			this.tabManager.setTabArea(tabArea);
168
			this.layout.setHeaderHeight(tabAreaTop);
169
			this.layout.arrangeElements();
170
		}
171
	}
172
173
	private void updateButtonStates() {
174
		if (this.serverData != null && this.playButton != null) {
175
			this.playButton.active = this.serverData.shouldPlayButtonBeActive();
176
			if (!this.playButton.active && this.serverData.state == RealmsServer.State.CLOSED) {
177
				this.playButton.setTooltip(Tooltip.create(RealmsServer.WORLD_CLOSED_COMPONENT));
178
			}
179
		}
180
	}
181
182
	@Override
183
	public void extractRenderState(final GuiGraphicsExtractor graphics, final int xm, final int ym, final float a) {
184
		super.extractRenderState(graphics, xm, ym, a);
185
		graphics.blit(RenderPipelines.GUI_TEXTURED, Screen.FOOTER_SEPARATOR, 0, this.height - this.layout.getFooterHeight() - 2, 0.0F, 0.0F, this.width, 2, 32, 2);
186
	}
187
188
	@Override
189
	public boolean keyPressed(final KeyEvent event) {
190
		return this.tabNavigationBar.keyPressed(event) ? true : super.keyPressed(event);
191
	}
192
193
	@Override
194
	protected void extractMenuBackground(final GuiGraphicsExtractor graphics) {
195
		graphics.blit(RenderPipelines.GUI_TEXTURED, CreateWorldScreen.TAB_HEADER_BACKGROUND, 0, 0, 0.0F, 0.0F, this.width, this.layout.getHeaderHeight(), 16, 16);
196
		this.extractMenuBackground(graphics, 0, this.layout.getHeaderHeight(), this.width, this.height);
197
	}
198
199
	@Override
200
	public void onClose() {
201
		if (this.serverData != null && this.tabManager.getCurrentTab() instanceof RealmsConfigurationTab tab) {
202
			tab.onDeselected(this.serverData);
203
		}
204
205
		this.minecraft.gui.setScreen(this.lastScreen);
206
		if (this.stateChanged) {
207
			this.lastScreen.resetScreen();
208
		}
209
	}
210
211
	public void fetchRegionData() {
212
		RealmsUtil.supplyAsync(
213
				RealmsClient::getPreferredRegionSelections, RealmsUtil.openScreenAndLogOnFailure(this::createErrorScreen, "Couldn't get realms region data")
214
			)
215
			.thenAcceptAsync(regions -> {
216
				this.regions = regions;
217
				this.onRealmsDataFetched();
218
			}, this.minecraft);
219
	}
220
221
	public void fetchServerData(final long realmId) {
222
		RealmsUtil.<RealmsServer>supplyAsync(
223
				client -> client.getOwnRealm(realmId), RealmsUtil.openScreenAndLogOnFailure(this::createErrorScreen, "Couldn't get own world")
224
			)
225
			.thenAcceptAsync(serverData -> {
226
				this.serverData = serverData;
227
				this.onRealmsDataFetched();
228
			}, this.minecraft);
229
	}
230
231
	public void fetchSubscription(final long realmId) {
232
		RealmsUtil.<Subscription>supplyAsync(client -> client.subscriptionFor(realmId), e -> {
233
			LOGGER.error("Couldn't get subscription", e);
234
			this.minecraft.execute(() -> {
235
				if (this.minecraft.gui.screen() instanceof RealmsConfigureWorldScreen) {
236
					this.minecraft.gui.setScreen(this.createErrorScreen(e));
237
				}
238
			});
239
		}).thenAcceptAsync(subscription -> {
240
			this.subscription = subscription;
241
			this.onRealmsDataFetched();
242
		}, this.minecraft);
243
	}
244
245
	private void fetchTierConfiguration(final long realmId) {
246
		RealmsUtil.<RealmTierConfigurationDto>supplyAsync(
247
				client -> client.getRealmTierConfiguration(realmId), RealmsUtil.openScreenAndLogOnFailure(this::createErrorScreen, "Couldn't get Realm tier configuration")
248
			)
249
			.thenAcceptAsync(tierConfiguration -> {
250
				this.tierConfiguration = tierConfiguration;
251
				this.tierConfigurationFetched = true;
252
				this.onRealmsDataFetched();
253
			}, this.minecraft);
254
	}
255
256
	private void onRealmsDataFetched() {
257
		if (this.serverData != null && this.regions != null && this.subscription != null && this.tierConfigurationFetched) {
258
			this.regionServiceQuality.clear();
259
260
			for (RegionDataDto region : this.regions.regionData()) {
261
				if (region.region() != RealmsRegion.INVALID_REGION) {
262
					this.regionServiceQuality.put(region.region(), region.serviceQuality());
263
				}
264
			}
265
266
			int focusedTabIndex = -1;
267
			if (this.tabNavigationBar != null) {
268
				focusedTabIndex = this.tabNavigationBar.getTabs().indexOf(this.tabManager.getCurrentTab());
269
			}
270
271
			if (this.tabNavigationBar != null) {
272
				this.removeWidget(this.tabNavigationBar);
273
			}
274
275
			this.tabNavigationBar = this.addRenderableWidget(
276
				MenuTabBar.builder(this.tabManager, this.width)
277
					.addTabs(
278
						new RealmsWorldsTab(this, Objects.requireNonNull(this.minecraft), this.serverData),
279
						new RealmsPlayersTab(this, this.minecraft, this.serverData),
280
						new RealmsSubscriptionTab(this, this.minecraft, this.serverData, this.subscription),
281
						new RealmsSettingsTab(this, this.minecraft, this.serverData, this.tierConfiguration, this.regionServiceQuality)
282
					)
283
					.build()
284
			);
285
			this.setFocused(this.tabNavigationBar);
286
			if (focusedTabIndex != -1) {
287
				this.tabNavigationBar.selectTab(focusedTabIndex, false);
288
			}
289
290
			this.tabNavigationBar.setTabActiveState(3, !this.serverData.expired);
291
			if (this.serverData.expired) {
292
				this.tabNavigationBar.setTabTooltip(3, Tooltip.create(Component.translatable("mco.configure.world.settings.expired")));
293
			} else {
294
				this.tabNavigationBar.setTabTooltip(3, null);
295
			}
296
297
			this.updateButtonStates();
298
			this.repositionElements();
299
		}
300
	}
301
302
	public void saveSlotSettings(final RealmsSlot slot) {
303
		RealmsSlot oldSlot = this.serverData.slots.get(this.serverData.activeSlot);
304
		slot.options.templateId = oldSlot.options.templateId;
305
		slot.options.templateImage = oldSlot.options.templateImage;
306
		RealmsClient client = RealmsClient.getOrCreate();
307
308
		try {
309
			if (this.serverData.activeSlot != slot.slotId) {
310
				throw new RealmsServiceException(RealmsError.CustomError.configurationError());
311
			}
312
313
			client.updateSlot(this.serverData.id, slot.slotId, slot.options, slot.settings);
314
			this.serverData.slots.put(this.serverData.activeSlot, slot);
315
			if (slot.options.gameMode != oldSlot.options.gameMode || slot.isHardcore() != oldSlot.isHardcore()) {
316
				RealmsMainScreen.refreshServerList();
317
			}
318
319
			this.stateChanged();
320
		} catch (RealmsServiceException e) {
321
			LOGGER.error("Couldn't save slot settings", e);
322
			this.minecraft.gui.setScreen(new RealmsGenericErrorScreen(e, this));
323
			return;
324
		}
325
326
		this.minecraft.gui.setScreen(this);
327
	}
328
329
	public boolean saveSettings(
330
		final String name,
331
		final String desc,
332
		final RegionSelectionPreference preference,
333
		final @Nullable RealmsRegion region,
334
		final @Nullable Integer renderDistance,
335
		final @Nullable Integer simulationDistance
336
	) {
337
		String description = StringUtil.isBlank(desc) ? "" : desc;
338
		String finalName = StringUtil.isBlank(name) ? "" : name;
339
		RealmsRegion regionSelection = preference == RegionSelectionPreference.MANUAL ? region : null;
340
		RegionSelectionPreferenceDto currentRegionSelection = this.serverData.regionSelectionPreference;
341
		boolean configurationChanged = !Objects.equals(finalName, this.serverData.name)
342
			|| !Objects.equals(description, this.serverData.motd)
343
			|| currentRegionSelection == null
344
			|| preference != currentRegionSelection.regionSelectionPreference
345
			|| regionSelection != currentRegionSelection.preferredRegion;
346
		if (!configurationChanged && renderDistance == null && simulationDistance == null) {
347
			return true;
348
		}
349
350
		RealmsClient client = RealmsClient.getOrCreate();
351
352
		try {
353
			RegionSelectionPreferenceDto regionSelectionPreference = new RegionSelectionPreferenceDto(preference, regionSelection);
354
			if (configurationChanged) {
355
				RealmsSlot realmsSlot = this.serverData.slots.get(this.serverData.activeSlot);
356
				client.updateConfiguration(
357
					this.serverData.id, finalName, description, regionSelectionPreference, realmsSlot.slotId, realmsSlot.options, realmsSlot.settings
358
				);
359
			}
360
361
			if (renderDistance != null) {
362
				client.updateRenderDistance(this.serverData.id, renderDistance);
363
			}
364
365
			if (simulationDistance != null) {
366
				client.updateSimulationDistance(this.serverData.id, simulationDistance);
367
			}
368
369
			if (configurationChanged) {
370
				this.serverData.regionSelectionPreference = regionSelectionPreference;
371
				this.serverData.name = name;
372
				this.serverData.motd = description;
373
			}
374
375
			this.stateChanged();
376
		} catch (RealmsServiceException e) {
377
			LOGGER.error("Couldn't save settings", e);
378
			this.minecraft.gui.setScreen(new RealmsGenericErrorScreen(e, this));
379
			return false;
380
		}
381
382
		this.minecraft.gui.setScreen(this);
383
		return true;
384
	}
385
386
	public void openTheWorld(final boolean join) {
387
		RealmsConfigureWorldScreen screenWithKnownData = this.getNewScreenWithKnownData(this.serverData);
388
		this.minecraft
389
			.gui
390
			.setScreen(new RealmsLongRunningMcoTaskScreen(this.getNewScreen(), new OpenServerTask(this.serverData, screenWithKnownData, join, this.minecraft)));
391
	}
392
393
	public void closeTheWorld() {
394
		RealmsConfigureWorldScreen screenWithKnownData = this.getNewScreenWithKnownData(this.serverData);
395
		this.minecraft.gui.setScreen(new RealmsLongRunningMcoTaskScreen(this.getNewScreen(), new CloseServerTask(this.serverData, screenWithKnownData)));
396
	}
397
398
	public void stateChanged() {
399
		this.stateChanged = true;
400
		if (this.tabNavigationBar != null) {
401
			for (Tab child : this.tabNavigationBar.getTabs()) {
402
				if (child instanceof RealmsConfigurationTab tab) {
403
					tab.updateData(this.serverData);
404
				}
405
			}
406
		}
407
	}
408
409
	public boolean invitePlayer(final long serverId, final String name) {
410
		RealmsClient client = RealmsClient.getOrCreate();
411
412
		try {
413
			List<PlayerInfo> players = client.invite(serverId, name);
414
			if (this.serverData != null) {
415
				this.serverData.players = players;
416
			} else {
417
				this.serverData = client.getOwnRealm(serverId);
418
			}
419
420
			this.stateChanged();
421
			return true;
422
		} catch (RealmsServiceException e) {
423
			LOGGER.error("Couldn't invite user", e);
424
			return false;
425
		}
426
	}
427
428
	public RealmsConfigureWorldScreen getNewScreen() {
429
		RealmsConfigureWorldScreen realmsConfigureWorldScreen = new RealmsConfigureWorldScreen(this.lastScreen, this.serverId);
430
		realmsConfigureWorldScreen.stateChanged = this.stateChanged;
431
		return realmsConfigureWorldScreen;
432
	}
433
434
	public RealmsConfigureWorldScreen getNewScreenWithKnownData(final RealmsServer serverData) {
435
		RealmsConfigureWorldScreen realmsConfigureWorldScreen = new RealmsConfigureWorldScreen(this.lastScreen, this.serverId, serverData, this.regions);
436
		realmsConfigureWorldScreen.stateChanged = this.stateChanged;
437
		return realmsConfigureWorldScreen;
438
	}
439
}
440

Decompiled Minecraft 26.3 source of RealmsConfigureWorldScreen (com/mojang/realmsclient/gui/screens/configuration/RealmsConfigureWorldScreen.java) with Mojang official mappings, stored verbatim on the site.