WorldOptionsScreen .java

net.minecraft.client.gui.screens.WorldOptionsScreen (class, 758 lines): members: GUEST_COMMAND_ACCESS_DISABLED_MULTIPLAYER_SCOPE_OFF, widget, portEdit.

net.minecraft.client.gui.screensclass758 linesclientextends Screen34.4 KB

Implements

API Surface (13)

void init()
boolean hasChanges()
void applyChanges(final @Nullable IntegratedServer singleplayerServer)
void repositionElements()
void extractMenuBackground(final GuiGraphicsExtractor graphics)
void extractRenderState(final GuiGraphicsExtractor graphics, final int xm, final int ym, final float a)
void onClose()
void onGamemasterPermissionChanged(final boolean hasGamemasterPermission)
void updatePersonalGameModeButton(final GameType mode)
void added()
void onDifficultyChanged()
void onDefaultGameModeChanged(final GameType mode)
WorldOptionsScreen.DifficultyButtons create(final Minecraft minecraft, final Level level, final WorldOptionsScreen screen)

Fields & Constants (72)

TITLE
GENERAL_TITLE
GAME_RULES
GAMERULES_DISABLED_TOOLTIP
GAMERULES_DISABLED_HARDCORE_TOOLTIP
DEFAULT_GAME_MODE
DEFAULT_GAME_MODE_TOOLTIP
PERSONAL_GAME_MODE
PERSONAL_GAME_MODE_TOOLTIP
GAME_MODE_DISABLED_OPERATOR_TOOLTIP
GAME_MODE_DISABLED_HARDCORE_TOOLTIP
ALLOW_COMMANDS
ALLOW_COMMANDS_TOOLTIP
ALLOW_COMMANDS_DISABLED_HARDCORE_TOOLTIP
ALLOW_COMMANDS_DISABLED_DEMO_TOOLTIP
RESTRICTIONS
MULTIPLAYER_TITLE
GUEST_COMMAND_ACCESS
GUEST_COMMAND_ACCESS_TOOLTIP
GUEST_COMMAND_ACCESS_DISABLED_MULTIPLAYER_SCOPE_OFF
GUEST_COMMAND_ACCESS_DISABLED_COMMANDS_OFF
FORCE_GAME_MODE
FORCE_GAME_MODE_ON_TOOLTIP
FORCE_GAME_MODE_OFF_TOOLTIP
FORCE_GAME_MODE_OFF_MULTIPLAYER_TOOLTIP
FORCE_GAME_MODE_OFF_HARDCORE_TOOLTIP
FORCE_GAME_MODE_OFF_COMMANDS_TOOLTIP
PORT_LOWER_BOUND
PORT_HIGHER_BOUND
PORT_INFO_TEXT
PORT_UNAVAILABLE
INVALID_PORT
DIFFICULTY_LOCK_TITLE
APPLY_CHANGES
layout
scrollArea
difficultyButtons
gameRulesButton
defaultGameModeButton
personalGameModeButton
initialDifficulty
wantedDifficulty
initialDifficultyLocked
wantedDifficultyLocked
initialDefaultGameMode
wantedDefaultGameMode
initialPersonalGameMode
wantedPersonalGameMode
initialAllowCommands
wantedAllowCommands
initialMultiplayerScope
wantedMultiplayerScope
port
portValid
portEdit
initialPort
initialGuestCommandAccess
wantedGuestCommandAccess

…and 14 more (see full source).

String literals (28)

"options.worldOptions.title"
"options.worldOptions.general.title"
"editGamerule.inGame.button"
"editGamerule.inGame.disabled.tooltip"
"editGamerule.inGame.disabled.hardcore.tooltip"
"options.worldOptions.game_mode"
"options.worldOptions.game_mode.tooltip"
"options.worldOptions.personal_game_mode"
"options.worldOptions.personal_game_mode.tooltip"
"options.worldOptions.game_mode.disabled.tooltip"
"selectWorld.allowCommands"
"options.worldOptions.allow_commands.tooltip"
"restrictions_screen.button"
"options.worldOptions.multiplayer.title"
"options.worldOptions.guest.command_access"
"options.worldOptions.guest.force_game_mode"
"lanServer.port"
"lanServer.port.unavailable"
"lanServer.port.invalid"
"difficulty.lock.title"
"menu.multiplayerOptions.applyChanges"
"difficulty.lock.question"
"menu.multiplayerOptions.lan"
"menu.multiplayerOptions.publish.stopped"

Full Source

WorldOptionsScreen.java758 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.screens;
2
3
import java.util.function.Consumer;
4
import net.fabricmc.api.EnvType;
5
import net.fabricmc.api.Environment;
6
import net.minecraft.ChatFormatting;
7
import net.minecraft.client.Minecraft;
8
import net.minecraft.client.gui.GuiGraphicsExtractor;
9
import net.minecraft.client.gui.components.AbstractSelectionList;
10
import net.minecraft.client.gui.components.AbstractWidget;
11
import net.minecraft.client.gui.components.Button;
12
import net.minecraft.client.gui.components.CycleButton;
13
import net.minecraft.client.gui.components.EditBox;
14
import net.minecraft.client.gui.components.FocusableTextWidget;
15
import net.minecraft.client.gui.components.LockIconButton;
16
import net.minecraft.client.gui.components.PopupScreen;
17
import net.minecraft.client.gui.components.ScrollableLayout;
18
import net.minecraft.client.gui.components.Tooltip;
19
import net.minecraft.client.gui.layouts.EqualSpacingLayout;
20
import net.minecraft.client.gui.layouts.GridLayout;
21
import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
22
import net.minecraft.client.gui.layouts.LayoutElement;
23
import net.minecraft.client.gui.layouts.LinearLayout;
24
import net.minecraft.client.gui.screens.multiplayer.RestrictionsScreen;
25
import net.minecraft.client.gui.screens.options.HasDifficultyReaction;
26
import net.minecraft.client.gui.screens.options.HasGamemasterPermissionReaction;
27
import net.minecraft.client.gui.screens.options.InWorldGameRulesScreen;
28
import net.minecraft.client.renderer.RenderPipelines;
29
import net.minecraft.client.server.IntegratedServer;
30
import net.minecraft.network.chat.CommonComponents;
31
import net.minecraft.network.chat.Component;
32
import net.minecraft.network.chat.ComponentUtils;
33
import net.minecraft.network.protocol.game.ServerboundChangeDifficultyPacket;
34
import net.minecraft.network.protocol.game.ServerboundLockDifficultyPacket;
35
import net.minecraft.server.MinecraftServer.MultiplayerScope;
36
import net.minecraft.server.permissions.Permissions;
37
import net.minecraft.util.HttpUtil;
38
import net.minecraft.world.Difficulty;
39
import net.minecraft.world.level.GameType;
40
import net.minecraft.world.level.Level;
41
import org.jspecify.annotations.Nullable;
42
43
@Environment(EnvType.CLIENT)
44
public class WorldOptionsScreen extends Screen implements HasGamemasterPermissionReaction, HasDifficultyReaction {
45
	private static final Component TITLE = Component.translatable("options.worldOptions.title");
46
	private static final Component GENERAL_TITLE = Component.translatable("options.worldOptions.general.title")
47
		.withStyle(ChatFormatting.UNDERLINE, ChatFormatting.BOLD);
48
	private static final Component GAME_RULES = Component.translatable("editGamerule.inGame.button");
49
	private static final Tooltip GAMERULES_DISABLED_TOOLTIP = Tooltip.create(Component.translatable("editGamerule.inGame.disabled.tooltip"));
50
	private static final Tooltip GAMERULES_DISABLED_HARDCORE_TOOLTIP = Tooltip.create(Component.translatable("editGamerule.inGame.disabled.hardcore.tooltip"));
51
	private static final Component DEFAULT_GAME_MODE = Component.translatable("options.worldOptions.game_mode");
52
	private static final Tooltip DEFAULT_GAME_MODE_TOOLTIP = Tooltip.create(Component.translatable("options.worldOptions.game_mode.tooltip"));
53
	private static final Component PERSONAL_GAME_MODE = Component.translatable("options.worldOptions.personal_game_mode");
54
	private static final Tooltip PERSONAL_GAME_MODE_TOOLTIP = Tooltip.create(Component.translatable("options.worldOptions.personal_game_mode.tooltip"));
55
	private static final Tooltip GAME_MODE_DISABLED_OPERATOR_TOOLTIP = Tooltip.create(
56
		Component.translatable("options.worldOptions.game_mode.disabled.operator.tooltip")
57
	);
58
	private static final Tooltip GAME_MODE_DISABLED_HARDCORE_TOOLTIP = Tooltip.create(Component.translatable("options.worldOptions.game_mode.disabled.tooltip"));
59
	private static final Component ALLOW_COMMANDS = Component.translatable("selectWorld.allowCommands");
60
	private static final Tooltip ALLOW_COMMANDS_TOOLTIP = Tooltip.create(Component.translatable("options.worldOptions.allow_commands.tooltip"));
61
	private static final Tooltip ALLOW_COMMANDS_DISABLED_HARDCORE_TOOLTIP = Tooltip.create(
62
		Component.translatable("options.worldOptions.allow_commands.disabled.tooltip")
63
	);
64
	private static final Tooltip ALLOW_COMMANDS_DISABLED_DEMO_TOOLTIP = Tooltip.create(
65
		Component.translatable("options.worldOptions.allow_commands.disabled.demo.tooltip")
66
	);
67
	private static final Component RESTRICTIONS = Component.translatable("restrictions_screen.button");
68
	private static final Component MULTIPLAYER_TITLE = Component.translatable("options.worldOptions.multiplayer.title")
69
		.withStyle(ChatFormatting.UNDERLINE, ChatFormatting.BOLD);
70
	private static final Component GUEST_COMMAND_ACCESS = Component.translatable("options.worldOptions.guest.command_access");
71
	private static final Tooltip GUEST_COMMAND_ACCESS_TOOLTIP = Tooltip.create(Component.translatable("options.worldOptions.guest.command_access.tooltip"));
72
	private static final Tooltip GUEST_COMMAND_ACCESS_DISABLED_MULTIPLAYER_SCOPE_OFF = Tooltip.create(
73
		Component.translatable("options.worldOptions.guest.command_access.disabled.scope.tooltip")
74
	);
75
	private static final Tooltip GUEST_COMMAND_ACCESS_DISABLED_COMMANDS_OFF = Tooltip.create(
76
		Component.translatable("options.worldOptions.guest.command_access.disabled.commands.tooltip")
77
	);
78
	private static final Component FORCE_GAME_MODE = Component.translatable("options.worldOptions.guest.force_game_mode");
79
	private static final Tooltip FORCE_GAME_MODE_ON_TOOLTIP = Tooltip.create(Component.translatable("options.worldOptions.guest.force_game_mode.on.tooltip"));
80
	private static final Tooltip FORCE_GAME_MODE_OFF_TOOLTIP = Tooltip.create(Component.translatable("options.worldOptions.guest.force_game_mode.off.tooltip"));
81
	private static final Tooltip FORCE_GAME_MODE_OFF_MULTIPLAYER_TOOLTIP = Tooltip.create(
82
		Component.translatable("options.worldOptions.guest.force_game_mode.off.scope.tooltip")
83
	);
84
	private static final Tooltip FORCE_GAME_MODE_OFF_HARDCORE_TOOLTIP = Tooltip.create(
85
		Component.translatable("options.worldOptions.guest.force_game_mode.off.hardcore.tooltip")
86
	);
87
	private static final Tooltip FORCE_GAME_MODE_OFF_COMMANDS_TOOLTIP = Tooltip.create(
88
		Component.translatable("options.worldOptions.guest.force_game_mode.off.commands.tooltip")
89
	);
90
	private static final int PORT_LOWER_BOUND = 1024;
91
	private static final int PORT_HIGHER_BOUND = 65535;
92
	private static final Component PORT_INFO_TEXT = Component.translatable("lanServer.port");
93
	private static final Component PORT_UNAVAILABLE = Component.translatable("lanServer.port.unavailable", 1024, 65535);
94
	private static final Component INVALID_PORT = Component.translatable("lanServer.port.invalid", 1024, 65535);
95
	private static final Component DIFFICULTY_LOCK_TITLE = Component.translatable("difficulty.lock.title");
96
	private static final Component APPLY_CHANGES = Component.translatable("menu.multiplayerOptions.applyChanges");
97
	private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this);
98
	private @Nullable ScrollableLayout scrollArea;
99
	private final WorldOptionsScreen.DifficultyButtons difficultyButtons;
100
	private @Nullable Button gameRulesButton;
101
	private @Nullable CycleButton<GameType> defaultGameModeButton;
102
	private @Nullable CycleButton<GameType> personalGameModeButton;
103
	private @Nullable Difficulty initialDifficulty;
104
	private @Nullable Difficulty wantedDifficulty;
105
	private @Nullable Boolean initialDifficultyLocked;
106
	private @Nullable Boolean wantedDifficultyLocked;
107
	private @Nullable GameType initialDefaultGameMode;
108
	private @Nullable GameType wantedDefaultGameMode;
109
	private @Nullable GameType initialPersonalGameMode;
110
	private @Nullable GameType wantedPersonalGameMode;
111
	private @Nullable Boolean initialAllowCommands;
112
	private @Nullable Boolean wantedAllowCommands;
113
	private @Nullable MultiplayerScope initialMultiplayerScope;
114
	private @Nullable MultiplayerScope wantedMultiplayerScope;
115
	private int port = HttpUtil.getAvailablePort();
116
	private boolean portValid = true;
117
	private @Nullable EditBox portEdit;
118
	private int initialPort;
119
	private @Nullable Boolean initialGuestCommandAccess;
120
	private @Nullable Boolean wantedGuestCommandAccess;
121
	private @Nullable CycleButton<Boolean> guestCommandAccessButton;
122
	private @Nullable Boolean initialForceGameMode;
123
	private @Nullable Boolean wantedForceGameMode;
124
	private @Nullable CycleButton<Boolean> forceGameModeButton;
125
	private final Screen lastScreen;
126
	private final Level level;
127
	private @Nullable Button applyChanges;
128
129
	public WorldOptionsScreen(final Screen lastScreen, final Level level) {
130
		super(TITLE);
131
		this.lastScreen = lastScreen;
132
		this.level = level;
133
		this.difficultyButtons = WorldOptionsScreen.DifficultyButtons.create(this.minecraft, level, this);
134
	}
135
136
	@Override
137
	protected void init() {
138
		this.layout.addTitleHeader(TITLE, this.font);
139
		IntegratedServer singleplayerServer = this.minecraft.getSingleplayerServer();
140
		LinearLayout content = LinearLayout.vertical().spacing(8);
141
		content.defaultCellSetting().padding(8).alignHorizontallyCenter().alignVerticallyTop();
142
		this.scrollArea = this.layout.addToContents(new ScrollableLayout(this.minecraft, content, this.layout.getContentHeight()));
143
		this.generalOptions(content, singleplayerServer);
144
		if (singleplayerServer != null) {
145
			this.multiplayerOptions(content, singleplayerServer);
146
		}
147
148
		this.applyChanges = Button.builder(
149
				APPLY_CHANGES,
150
				var2x -> {
151
					if (this.wantedDifficultyLocked != this.initialDifficultyLocked) {
152
						Component difficultyDisplayName = this.wantedDifficulty != null ? this.wantedDifficulty.getDisplayName() : this.level.getDifficulty().getDisplayName();
153
						this.minecraft
154
							.gui
155
							.setScreen(
156
								new PopupScreen.Builder(this, DIFFICULTY_LOCK_TITLE)
157
									.addMessage(Component.translatable("difficulty.lock.question", difficultyDisplayName))
158
									.addButton(CommonComponents.GUI_YES, var2xx -> {
159
										this.applyChanges(singleplayerServer);
160
										this.minecraft.gui.setScreen(this.lastScreen);
161
									})
162
									.addButton(CommonComponents.GUI_NO, var1x -> this.minecraft.gui.setScreen(this))
163
									.build()
164
							);
165
					} else {
166
						this.applyChanges(singleplayerServer);
167
						this.minecraft.gui.setScreen(this.lastScreen);
168
					}
169
				}
170
			)
171
			.build();
172
		this.applyChanges.active = false;
173
		LinearLayout footer = this.layout.addToFooter(LinearLayout.horizontal().spacing(8));
174
		footer.addChild(this.applyChanges);
175
		footer.addChild(Button.builder(CommonComponents.GUI_CANCEL, var1x -> this.onClose()).build());
176
		this.layout.visitWidgets(this::addRenderableWidget);
177
		this.repositionElements();
178
	}
179
180
	private FocusableTextWidget createCategoryHeader(final Component title) {
181
		return FocusableTextWidget.builder(title, this.font).alwaysShowBorder(false).backgroundFill(FocusableTextWidget.BackgroundFill.ON_FOCUS).build();
182
	}
183
184
	private void generalOptions(final LinearLayout content, final @Nullable IntegratedServer singleplayerServer) {
185
		GridLayout grid = content.addChild(new GridLayout());
186
		GridLayout.RowHelper rowHelper = grid.columnSpacing(8).rowSpacing(4).createRowHelper(2);
187
		rowHelper.defaultCellSetting().alignHorizontallyCenter();
188
		rowHelper.addChild(this.createCategoryHeader(GENERAL_TITLE), 2);
189
		boolean host = singleplayerServer != null && this.minecraft.player != null && singleplayerServer.isSingleplayerOwner(this.minecraft.player.nameAndId());
190
		if (host) {
191
			this.wantedDefaultGameMode = singleplayerServer.getWorldData().getGameType();
192
			this.initialDefaultGameMode = this.wantedDefaultGameMode;
193
			this.defaultGameModeButton = rowHelper.addChild(
194
				this.createGameModeButton(
195
					singleplayerServer, DEFAULT_GAME_MODE, DEFAULT_GAME_MODE_TOOLTIP, this.wantedDefaultGameMode, value -> this.wantedDefaultGameMode = value
196
				),
197
				2
198
			);
199
			this.wantedPersonalGameMode = singleplayerServer.getPersonalGameMode();
200
			this.initialPersonalGameMode = this.wantedPersonalGameMode;
201
			if (this.wantedPersonalGameMode != null) {
202
				this.personalGameModeButton = rowHelper.addChild(
203
					this.createGameModeButton(
204
						singleplayerServer, PERSONAL_GAME_MODE, PERSONAL_GAME_MODE_TOOLTIP, this.wantedPersonalGameMode, value -> this.wantedPersonalGameMode = value
205
					),
206
					2
207
				);
208
			}
209
210
			this.wantedAllowCommands = singleplayerServer.getWorldData().isAllowCommands();
211
			this.initialAllowCommands = this.wantedAllowCommands;
212
			rowHelper.addChild(this.createAllowCommandsButton(singleplayerServer));
213
		}
214
215
		rowHelper.addChild(this.difficultyButtons.layout());
216
		this.gameRulesButton = rowHelper.addChild(this.createGameRulesButton(singleplayerServer));
217
		rowHelper.addChild(this.createRestrictionsButton());
218
	}
219
220
	private Button createGameRulesButton(final @Nullable IntegratedServer singleplayerServer) {
221
		Button gameRulesButton = Button.builder(GAME_RULES, var1 -> {
222
			if (this.minecraft.player != null) {
223
				this.minecraft.gui.setScreen(new InWorldGameRulesScreen(this.minecraft.player.connection, var1x -> this.minecraft.gui.setScreen(this), this));
224
			}
225
		}).build();
226
		this.updateButton(gameRulesButton, singleplayerServer, null, GAMERULES_DISABLED_TOOLTIP, GAMERULES_DISABLED_HARDCORE_TOOLTIP);
227
		return gameRulesButton;
228
	}
229
230
	private CycleButton<GameType> createGameModeButton(
231
		final IntegratedServer singleplayerServer,
232
		final Component buttonName,
233
		final Tooltip tooltip,
234
		final GameType defaultValue,
235
		final Consumer<GameType> gameTypeToChange
236
	) {
237
		CycleButton<GameType> gameModeButton = CycleButton.builder(GameType::getShortDisplayName, defaultValue)
238
			.withValues(GameType.values())
239
			.withTooltip(var1 -> tooltip)
240
			.create(0, 0, 308, 20, buttonName, (var2, value) -> {
241
				gameTypeToChange.accept(value);
242
				this.updateApplyChangesActiveState();
243
			});
244
		this.updateButton(gameModeButton, singleplayerServer, tooltip, GAME_MODE_DISABLED_OPERATOR_TOOLTIP, GAME_MODE_DISABLED_HARDCORE_TOOLTIP);
245
		return gameModeButton;
246
	}
247
248
	private CycleButton<Boolean> createAllowCommandsButton(final IntegratedServer singleplayerServer) {
249
		CycleButton<Boolean> allowCommandsButton = CycleButton.onOffBuilder(singleplayerServer.getWorldData().isAllowCommands())
250
			.withTooltip(var0 -> ALLOW_COMMANDS_TOOLTIP)
251
			.create(ALLOW_COMMANDS, (var2x, allowCommands) -> {
252
				this.wantedAllowCommands = allowCommands;
253
				this.updateGuestCommandAccessButton(singleplayerServer);
254
				this.updatePermissionDependentButtons(singleplayerServer, allowCommands, false);
255
				this.updateApplyChangesActiveState();
256
			});
257
		if (singleplayerServer.isDemo()) {
258
			allowCommandsButton.active = false;
259
			allowCommandsButton.setTooltip(ALLOW_COMMANDS_DISABLED_DEMO_TOOLTIP);
260
		} else if (!singleplayerServer.isHardcore() || this.minecraft.player != null && this.minecraft.player.permissions().hasPermission(Permissions.COMMANDS_OWNER)
261
			)
262
		 {
263
			allowCommandsButton.setTooltip(ALLOW_COMMANDS_TOOLTIP);
264
		} else {
265
			allowCommandsButton.active = false;
266
			allowCommandsButton.setTooltip(ALLOW_COMMANDS_DISABLED_HARDCORE_TOOLTIP);
267
		}
268
269
		return allowCommandsButton;
270
	}
271
272
	private void updateButton(
273
		final @Nullable AbstractWidget widget,
274
		final @Nullable IntegratedServer singleplayerServer,
275
		final @Nullable Tooltip tooltip,
276
		final Tooltip disabledTooltip,
277
		final Tooltip hardcoreTooltip
278
	) {
279
		if (widget != null) {
280
			boolean hardcore = singleplayerServer != null && singleplayerServer.isHardcore();
281
			boolean shouldButtonsBeActive = this.minecraft.player != null
282
				&& (this.wantedAllowCommands == null ? this.minecraft.player.permissions().hasPermission(Permissions.COMMANDS_GAMEMASTER) : this.wantedAllowCommands);
283
			widget.active = !hardcore && shouldButtonsBeActive;
284
			widget.setTooltip(hardcore ? hardcoreTooltip : (shouldButtonsBeActive ? tooltip : disabledTooltip));
285
		}
286
	}
287
288
	private Button createRestrictionsButton() {
289
		return Button.builder(RESTRICTIONS, var1 -> {
290
			if (this.minecraft.player != null) {
291
				this.minecraft.gui.setScreen(new RestrictionsScreen(this, this.minecraft.player.chatAbilities()));
292
			}
293
		}).build();
294
	}
295
296
	private void multiplayerOptions(final LinearLayout content, final IntegratedServer singleplayerServer) {
297
		GridLayout grid = content.addChild(new GridLayout());
298
		grid.defaultCellSetting().alignHorizontallyCenter();
299
		GridLayout.RowHelper helper = grid.columnSpacing(8).rowSpacing(4).createRowHelper(2);
300
		helper.addChild(this.createCategoryHeader(MULTIPLAYER_TITLE), 2);
301
		this.wantedMultiplayerScope = singleplayerServer.getMultiplayerScope();
302
		this.initialMultiplayerScope = this.wantedMultiplayerScope;
303
		helper.addChild(
304
			CycleButton.onOffBuilder(singleplayerServer.getMultiplayerScope() == MultiplayerScope.LAN)
305
				.withTooltip(value -> Tooltip.create(value ? MultiplayerScope.LAN.getTooltip() : MultiplayerScope.OFF.getTooltip()))
306
				.create(Component.translatable("menu.multiplayerOptions.lan"), (var2, value) -> {
307
					this.wantedMultiplayerScope = value ? MultiplayerScope.LAN : MultiplayerScope.OFF;
308
					this.updateGuestCommandAccessButton(singleplayerServer);
309
					this.updatePortControlsState();
310
					this.updateApplyChangesActiveState();
311
				})
312
		);
313
		if (this.initialMultiplayerScope == MultiplayerScope.LAN) {
314
			this.port = singleplayerServer.getPort();
315
			this.initialPort = this.port;
316
		}
317
318
		this.portEdit = new EditBox(this.font, PORT_INFO_TEXT);
319
		this.portEdit.setResponder(value -> {
320
			this.setPortError(this.portEdit, this.tryParsePort(value));
321
			this.portEdit.setHint(Component.literal(String.valueOf(this.port)));
322
			this.updateApplyChangesActiveState();
323
		});
324
		this.portEdit.setTooltip(Tooltip.create(PORT_INFO_TEXT));
325
		if (this.initialMultiplayerScope == MultiplayerScope.LAN) {
326
			this.portEdit.setValue(String.valueOf(this.port));
327
		}
328
329
		helper.addChild(this.portEdit);
330
		this.updatePortControlsState();
331
		this.wantedGuestCommandAccess = singleplayerServer.getGuestCommandAccess();
332
		this.initialGuestCommandAccess = this.wantedGuestCommandAccess;
333
		this.guestCommandAccessButton = helper.addChild(
334
			CycleButton.onOffBuilder(this.initialGuestCommandAccess).withTooltip(var0 -> GUEST_COMMAND_ACCESS_TOOLTIP).create(GUEST_COMMAND_ACCESS, (var2, value) -> {
335
				this.wantedGuestCommandAccess = value;
336
				this.updateForceGameModeButton(singleplayerServer);
337
				this.updateApplyChangesActiveState();
338
			})
339
		);
340
		this.wantedForceGameMode = singleplayerServer.forceGameMode();
341
		this.initialForceGameMode = this.wantedForceGameMode;
342
		this.forceGameModeButton = helper.addChild(
343
			CycleButton.onOffBuilder(this.initialForceGameMode)
344
				.withTooltip(value -> value ? FORCE_GAME_MODE_ON_TOOLTIP : FORCE_GAME_MODE_OFF_TOOLTIP)
345
				.create(FORCE_GAME_MODE, (var1, value) -> {
346
					this.wantedForceGameMode = value;
347
					this.updateApplyChangesActiveState();
348
				})
349
		);
350
		this.updateGuestCommandAccessButton(singleplayerServer);
351
	}
352
353
	private void updateGuestCommandAccessButton(final IntegratedServer singleplayerServer) {
354
		if (this.guestCommandAccessButton != null) {
355
			boolean lanScope = this.wantedMultiplayerScope == MultiplayerScope.LAN;
356
			boolean allowCommands = Boolean.TRUE.equals(this.wantedAllowCommands);
357
			Tooltip tooltip;
358
			if (!lanScope) {
359
				this.wantedGuestCommandAccess = false;
360
				tooltip = GUEST_COMMAND_ACCESS_DISABLED_MULTIPLAYER_SCOPE_OFF;
361
			} else if (!allowCommands) {
362
				this.wantedGuestCommandAccess = false;
363
				tooltip = GUEST_COMMAND_ACCESS_DISABLED_COMMANDS_OFF;
364
			} else {
365
				this.wantedGuestCommandAccess = singleplayerServer.getGuestCommandAccess();
366
				tooltip = GUEST_COMMAND_ACCESS_TOOLTIP;
367
			}
368
369
			this.guestCommandAccessButton.setValue(this.wantedGuestCommandAccess);
370
			this.guestCommandAccessButton.setTooltip(tooltip);
371
			this.guestCommandAccessButton.active = lanScope && allowCommands;
372
			this.updateForceGameModeButton(singleplayerServer);
373
		}
374
	}
375
376
	private void updateForceGameModeButton(final IntegratedServer singleplayerServer) {
377
		if (this.forceGameModeButton != null) {
378
			boolean lanScope = this.wantedMultiplayerScope == MultiplayerScope.LAN;
379
			boolean guestCommandAccess = Boolean.TRUE.equals(this.wantedGuestCommandAccess);
380
			Tooltip tooltip;
381
			if (singleplayerServer.isHardcore()) {
382
				this.wantedForceGameMode = true;
383
				tooltip = FORCE_GAME_MODE_OFF_HARDCORE_TOOLTIP;
384
			} else if (!lanScope) {
385
				this.wantedForceGameMode = true;
386
				tooltip = FORCE_GAME_MODE_OFF_MULTIPLAYER_TOOLTIP;
387
			} else if (guestCommandAccess) {
388
				this.wantedForceGameMode = false;
389
				tooltip = FORCE_GAME_MODE_OFF_COMMANDS_TOOLTIP;
390
			} else {
391
				this.wantedForceGameMode = singleplayerServer.forceGameMode();
392
				tooltip = this.wantedForceGameMode ? FORCE_GAME_MODE_ON_TOOLTIP : FORCE_GAME_MODE_OFF_TOOLTIP;
393
			}
394
395
			this.forceGameModeButton.setValue(this.wantedForceGameMode);
396
			this.forceGameModeButton.setTooltip(tooltip);
397
			this.forceGameModeButton.active = lanScope && !guestCommandAccess && !singleplayerServer.isHardcore();
398
		}
399
	}
400
401
	protected boolean hasChanges() {
402
		return this.hasSettingsChanges() && (!this.portIsRequired() || this.portValid);
403
	}
404
405
	protected void applyChanges(final @Nullable IntegratedServer singleplayerServer) {
406
		if (this.wantedDifficulty != null && this.wantedDifficulty != this.initialDifficulty && this.minecraft.getConnection() != null) {
407
			this.minecraft.getConnection().send(new ServerboundChangeDifficultyPacket(this.wantedDifficulty));
408
		}
409
410
		if (this.wantedDifficultyLocked != null && this.wantedDifficultyLocked != this.initialDifficultyLocked && this.minecraft.getConnection() != null) {
411
			this.minecraft.getConnection().send(new ServerboundLockDifficultyPacket(true));
412
			this.difficultyButtons.lockButton.setLocked(true);
413
			WorldOptionsScreen.DifficultyButtons.updateDifficultyButtonsState(
414
				this.minecraft, this.level, this.difficultyButtons.difficultyButton, this.difficultyButtons.lockButton
415
			);
416
		}
417
418
		if (singleplayerServer != null) {
419
			if (this.wantedAllowCommands != null && this.wantedAllowCommands != this.initialAllowCommands) {
420
				singleplayerServer.setWorldAllowCommands(this.wantedAllowCommands);
421
			}
422
423
			if (this.wantedForceGameMode != null && this.wantedForceGameMode != this.initialForceGameMode) {
424
				singleplayerServer.setForceGameMode(this.wantedForceGameMode);
425
			}
426
427
			if (this.wantedDefaultGameMode != null && this.wantedDefaultGameMode != this.initialDefaultGameMode) {
428
				singleplayerServer.setWorldGameType(this.wantedDefaultGameMode);
429
			}
430
431
			if (this.wantedPersonalGameMode != null && this.wantedPersonalGameMode != this.initialPersonalGameMode) {
432
				singleplayerServer.setPersonalGameType(this.wantedPersonalGameMode);
433
			}
434
435
			if (this.wantedGuestCommandAccess != null && this.wantedGuestCommandAccess != this.initialGuestCommandAccess) {
436
				singleplayerServer.setGuestCommandAccess(this.wantedGuestCommandAccess);
437
			}
438
439
			if (this.wantedMultiplayerScope != this.initialMultiplayerScope || this.lanPortChanged()) {
440
				this.changeMultiplayerScope(singleplayerServer);
441
			}
442
		}
443
	}
444
445
	private void updateApplyChangesActiveState() {
446
		if (this.applyChanges != null) {
447
			this.applyChanges.active = this.hasChanges();
448
		}
449
	}
450
451
	@Override
452
	protected void repositionElements() {
453
		this.scrollArea.arrangeElements();
454
		this.scrollArea.setMaxHeight(this.layout.getContentHeight());
455
		this.scrollArea.setMinHeight(this.layout.getContentHeight());
456
		this.layout.arrangeElements();
457
		this.scrollArea.setPosition(this.width / 2 - this.scrollArea.getWidth() / 2, this.layout.getHeaderHeight());
458
	}
459
460
	@Override
461
	protected void extractMenuBackground(final GuiGraphicsExtractor graphics) {
462
		super.extractMenuBackground(graphics);
463
		graphics.blit(
464
			RenderPipelines.GUI_TEXTURED,
465
			AbstractSelectionList.INWORLD_MENU_LIST_BACKGROUND,
466
			this.layout.getX(),
467
			this.layout.getHeaderHeight(),
468
			this.width,
469
			this.height - this.layout.getFooterHeight() + (int)this.scrollArea.getScrollAmount(),
470
			this.width,
471
			this.layout.getContentHeight(),
472
			32,
473
			32
474
		);
475
	}
476
477
	@Override
478
	public void extractRenderState(final GuiGraphicsExtractor graphics, final int xm, final int ym, final float a) {
479
		super.extractRenderState(graphics, xm, ym, a);
480
		graphics.blit(
481
			RenderPipelines.GUI_TEXTURED, Screen.INWORLD_HEADER_SEPARATOR, this.layout.getX(), this.layout.getHeaderHeight() - 2, 0.0F, 0.0F, this.width, 2, 32, 2
482
		);
483
		graphics.blit(
484
			RenderPipelines.GUI_TEXTURED,
485
			Screen.INWORLD_FOOTER_SEPARATOR,
486
			this.layout.getX(),
487
			this.height - this.layout.getFooterHeight(),
488
			0.0F,
489
			0.0F,
490
			this.width,
491
			2,
492
			32,
493
			2
494
		);
495
	}
496
497
	@Override
498
	public void onClose() {
499
		this.minecraft.gui.setScreen(this.lastScreen);
500
	}
501
502
	@Override
503
	public void onGamemasterPermissionChanged(final boolean hasGamemasterPermission) {
504
		IntegratedServer singleplayerServer = this.minecraft.getSingleplayerServer();
505
		this.updatePermissionDependentButtons(singleplayerServer, hasGamemasterPermission, true);
506
		if (!hasGamemasterPermission && !this.minecraft.hasSingleplayerServer()) {
507
			this.minecraft.gui.setScreen(this.lastScreen);
508
			if (this.minecraft.gui.screen() instanceof HasGamemasterPermissionReaction screen) {
509
				screen.onGamemasterPermissionChanged(hasGamemasterPermission);
510
			}
511
		}
512
	}
513
514
	private void updatePermissionDependentButtons(
515
		final @Nullable IntegratedServer singleplayerServer, final boolean allowCommands, final boolean affectGameRulesButton
516
	) {
517
		if (!allowCommands) {
518
			if (this.defaultGameModeButton != null && this.personalGameModeButton != null && this.initialDefaultGameMode != null) {
519
				this.wantedDefaultGameMode = this.initialDefaultGameMode;
520
				this.defaultGameModeButton.setValue(this.wantedDefaultGameMode);
521
				this.wantedPersonalGameMode = this.wantedDefaultGameMode;
522
				this.personalGameModeButton.setValue(this.wantedDefaultGameMode);
523
			}
524
		} else if (this.personalGameModeButton != null && this.wantedPersonalGameMode != null) {
525
			this.personalGameModeButton.setValue(this.wantedPersonalGameMode);
526
		}
527
528
		if (affectGameRulesButton) {
529
			this.updateButton(this.gameRulesButton, singleplayerServer, null, GAMERULES_DISABLED_TOOLTIP, GAMERULES_DISABLED_HARDCORE_TOOLTIP);
530
		}
531
532
		this.updateButton(
533
			this.defaultGameModeButton, singleplayerServer, DEFAULT_GAME_MODE_TOOLTIP, GAME_MODE_DISABLED_OPERATOR_TOOLTIP, GAME_MODE_DISABLED_HARDCORE_TOOLTIP
534
		);
535
		this.updateButton(
536
			this.personalGameModeButton, singleplayerServer, PERSONAL_GAME_MODE_TOOLTIP, GAME_MODE_DISABLED_OPERATOR_TOOLTIP, GAME_MODE_DISABLED_HARDCORE_TOOLTIP
537
		);
538
		this.difficultyButtons.refresh(this.minecraft, this);
539
	}
540
541
	public void updatePersonalGameModeButton(final GameType mode) {
542
		if (this.personalGameModeButton != null) {
543
			this.initialPersonalGameMode = mode;
544
			this.personalGameModeButton.setValue(mode);
545
		}
546
	}
547
548
	@Override
549
	public void added() {
550
		this.difficultyButtons.refresh(this.minecraft, this);
551
	}
552
553
	@Override
554
	public void onDifficultyChanged() {
555
		this.difficultyButtons.refresh(this.minecraft, this, true);
556
	}
557
558
	public void onDefaultGameModeChanged(final GameType mode) {
559
		if (this.defaultGameModeButton != null) {
560
			this.initialDefaultGameMode = mode;
561
			this.defaultGameModeButton.setValue(mode);
562
		}
563
	}
564
565
	private boolean portIsRequired() {
566
		return this.wantedMultiplayerScope == MultiplayerScope.LAN;
567
	}
568
569
	private boolean hasSettingsChanges() {
570
		return this.wantedDifficulty != this.initialDifficulty
571
			|| this.wantedDifficultyLocked != this.initialDifficultyLocked
572
			|| this.wantedDefaultGameMode != this.initialDefaultGameMode
573
			|| this.wantedPersonalGameMode != this.initialPersonalGameMode
574
			|| this.wantedAllowCommands != this.initialAllowCommands
575
			|| this.wantedMultiplayerScope != this.initialMultiplayerScope
576
			|| this.wantedGuestCommandAccess != this.initialGuestCommandAccess
577
			|| this.wantedForceGameMode != this.initialForceGameMode
578
			|| this.lanPortChanged();
579
	}
580
581
	private boolean lanPortChanged() {
582
		return this.wantedMultiplayerScope == MultiplayerScope.LAN && this.initialMultiplayerScope == MultiplayerScope.LAN && this.port != this.initialPort;
583
	}
584
585
	private void changeMultiplayerScope(final IntegratedServer singleplayerServer) {
586
		if (this.wantedMultiplayerScope != null) {
587
			if (singleplayerServer.unpublishServer()) {
588
				this.sendPublishMessage(Component.translatable("menu.multiplayerOptions.publish.stopped"));
589
			}
590
591
			if (this.wantedMultiplayerScope != MultiplayerScope.OFF) {
592
				this.publish(singleplayerServer, this.wantedMultiplayerScope);
593
			}
594
595
			this.minecraft.getPlayerSocialManager().getPresenceHandler().tryUpdatePresence();
596
		}
597
	}
598
599
	private void publish(final IntegratedServer singleplayerServer, final MultiplayerScope scope) {
600
		if (!singleplayerServer.publishServer(scope, this.port)) {
601
			this.sendPublishMessage(Component.translatable("commands.publish.failed"));
602
		} else {
603
			Component message = scope == MultiplayerScope.LAN
604
				? Component.translatable("menu.multiplayerOptions.publish.started.lan", ComponentUtils.copyOnClickText(String.valueOf(this.port)))
605
				: Component.translatable("menu.multiplayerOptions.publish.started.online");
606
			this.sendPublishMessage(message);
607
		}
608
	}
609
610
	private void sendPublishMessage(final Component message) {
611
		this.minecraft.gui.hud.getChat().addClientSystemMessage(message);
612
		this.minecraft.getNarrator().saySystemQueued(message);
613
		this.minecraft.updateTitle();
614
	}
615
616
	private void updatePortControlsState() {
617
		boolean lanWanted = this.wantedMultiplayerScope == MultiplayerScope.LAN;
618
		if (this.portEdit != null) {
619
			String desired = lanWanted ? (this.initialMultiplayerScope == MultiplayerScope.LAN ? String.valueOf(this.initialPort) : "") : "";
620
			if (!this.portEdit.getValue().equals(desired)) {
621
				this.portEdit.setValue(desired);
622
			}
623
624
			this.portEdit.setEditable(lanWanted);
625
			this.portEdit.active = lanWanted;
626
			this.portEdit.setHint(lanWanted ? Component.literal(String.valueOf(this.port)) : PORT_INFO_TEXT);
627
			if (!lanWanted) {
628
				this.portEdit.setFocused(false);
629
				this.setPortError(this.portEdit, null);
630
			}
631
		}
632
	}
633
634
	private @Nullable Component tryParsePort(final String value) {
635
		if (value.isBlank()) {
636
			this.port = HttpUtil.getAvailablePort();
637
			return null;
638
		}
639
640
		try {
641
			int parsed = Integer.parseInt(value);
642
			if (parsed < 1024 || parsed > 65535) {
643
				return INVALID_PORT;
644
			}
645
646
			if (parsed != this.initialPort && !HttpUtil.isPortAvailable(parsed)) {
647
				return PORT_UNAVAILABLE;
648
			}
649
650
			this.port = parsed;
651
			return null;
652
		} catch (NumberFormatException e) {
653
			this.port = HttpUtil.getAvailablePort();
654
			return INVALID_PORT;
655
		}
656
	}
657
658
	private void setPortError(final EditBox portEdit, final @Nullable Component errorMessage) {
659
		this.portValid = errorMessage == null;
660
		if (errorMessage == null) {
661
			portEdit.setTextColor(-2039584);
662
			portEdit.setTooltip(Tooltip.create(PORT_INFO_TEXT));
663
		} else {
664
			portEdit.setTextColor(-2142128);
665
			portEdit.setTooltip(Tooltip.create(errorMessage));
666
		}
667
	}
668
669
	@Environment(EnvType.CLIENT)
670
	private record DifficultyButtons(LayoutElement layout, CycleButton<Difficulty> difficultyButton, LockIconButton lockButton, Level level) {
671
		private static final Component DIFFICULTY_TITLE = Component.translatable("options.difficulty");
672
		private static final Tooltip DIFFICULTY_DISABLED_HARDCORE_TOOLTIP = Tooltip.create(
673
			Component.translatable("options.worldOptions.difficulty.disabled.hardcore.tooltip")
674
		);
675
		private static final Tooltip DIFFICULTY_DISABLED_LOCKED_TOOLTIP = Tooltip.create(
676
			Component.translatable("options.worldOptions.difficulty.disabled.locked.tooltip")
677
		);
678
		private static final Tooltip DIFFICULTY_DISABLED_OPERATOR_TOOLTIP = Tooltip.create(
679
			Component.translatable("options.worldOptions.difficulty.disabled.operator.tooltip")
680
		);
681
682
		public static WorldOptionsScreen.DifficultyButtons create(final Minecraft minecraft, final Level level, final WorldOptionsScreen screen) {
683
			screen.wantedDifficulty = level.getDifficulty();
684
			screen.initialDifficulty = screen.wantedDifficulty;
685
			CycleButton<Difficulty> difficultyButton = CycleButton.builder(Difficulty::getDisplayName, level.getDifficulty())
686
				.withValues(Difficulty.values())
687
				.create(0, 0, 150, 20, DIFFICULTY_TITLE, (var1, value) -> {
688
					screen.wantedDifficulty = value;
689
					screen.updateApplyChangesActiveState();
690
				});
691
			screen.wantedDifficultyLocked = isDifficultyLocked(level);
692
			screen.initialDifficultyLocked = screen.wantedDifficultyLocked;
693
			LockIconButton lockButton = new LockIconButton(0, 0, button -> {
694
				if (button instanceof LockIconButton lockIconButton) {
695
					lockIconButton.setLocked(!screen.wantedDifficultyLocked);
696
				}
697
698
				screen.wantedDifficultyLocked = !screen.wantedDifficultyLocked;
699
				screen.updateApplyChangesActiveState();
700
			});
701
			difficultyButton.setWidth(difficultyButton.getWidth() - lockButton.getWidth());
702
			lockButton.setLocked(isDifficultyLocked(level));
703
			updateDifficultyButtonsState(minecraft, level, difficultyButton, lockButton);
704
			EqualSpacingLayout linearLayout = new EqualSpacingLayout(150, 0, EqualSpacingLayout.Orientation.HORIZONTAL);
705
			linearLayout.addChild(difficultyButton);
706
			linearLayout.addChild(lockButton);
707
			return new WorldOptionsScreen.DifficultyButtons(linearLayout, difficultyButton, lockButton, level);
708
		}
709
710
		private void refresh(final Minecraft minecraft, final WorldOptionsScreen worldOptionsScreen) {
711
			this.refresh(minecraft, worldOptionsScreen, false);
712
		}
713
714
		private void refresh(final Minecraft minecraft, final WorldOptionsScreen worldOptionsScreen, final boolean forceLevelDifficulty) {
715
			this.difficultyButton
716
				.setValue(
717
					forceLevelDifficulty
718
						? this.level.getDifficulty()
719
						: (worldOptionsScreen.wantedDifficulty != null ? worldOptionsScreen.wantedDifficulty : this.level.getDifficulty())
720
				);
721
			this.lockButton.setLocked(worldOptionsScreen.wantedDifficultyLocked != null ? worldOptionsScreen.wantedDifficultyLocked : isDifficultyLocked(this.level));
722
			updateDifficultyButtonsState(minecraft, this.level, this.difficultyButton, this.lockButton);
723
		}
724
725
		private static void updateDifficultyButtonsState(
726
			final Minecraft minecraft, final Level level, final CycleButton<Difficulty> difficultyButton, final LockIconButton lockButton
727
		) {
728
			if (minecraft.player != null
729
				&& (
730
					minecraft.player.permissions().hasPermission(Permissions.COMMANDS_GAMEMASTER)
731
						|| minecraft.getSingleplayerServer() != null && minecraft.getSingleplayerServer().isSingleplayerOwner(minecraft.player.nameAndId())
732
				)) {
733
				if (level.getLevelData().isDifficultyLocked()) {
734
					lockButton.active = false;
735
					difficultyButton.active = false;
736
					difficultyButton.setTooltip(DIFFICULTY_DISABLED_LOCKED_TOOLTIP);
737
				} else if (level.getLevelData().isHardcore()) {
738
					lockButton.active = false;
739
					difficultyButton.active = false;
740
					difficultyButton.setTooltip(DIFFICULTY_DISABLED_HARDCORE_TOOLTIP);
741
				} else {
742
					lockButton.active = true;
743
					difficultyButton.active = true;
744
					difficultyButton.setTooltip(null);
745
				}
746
			} else {
747
				lockButton.active = false;
748
				difficultyButton.active = false;
749
				difficultyButton.setTooltip(DIFFICULTY_DISABLED_OPERATOR_TOOLTIP);
750
			}
751
		}
752
753
		private static boolean isDifficultyLocked(final Level level) {
754
			return level.getLevelData().isDifficultyLocked() || level.getLevelData().isHardcore();
755
		}
756
	}
757
}
758

Decompiled Minecraft 26.3 source of WorldOptionsScreen (net/minecraft/client/gui/screens/WorldOptionsScreen.java) with Mojang official mappings, stored verbatim on the site.