RealmsSlotOptionsScreen .java

com.mojang.realmsclient.gui.screens.configuration.RealmsSlotOptionsScreen (class): members: FORCE_GAME_MODE_BUTTON, SPAWN_PROTECTION_TEXT, defaultSlotName.

com.mojang.realmsclient.gui.screens.configurationclass230 linescommonextends RealmsScreen9.4 KB

API Surface (6)

void init()
void repositionElements()
void onClose()
Component getNarrationMessage()
void applyValue()
void updateMessage()

Fields & Constants (23)

DEFAULT_DIFFICULTY
DIFFICULTIES
DEFAULT_GAME_MODE
GAME_MODES
TITLE
WORLD_NAME_EDIT_LABEL
SPAWN_PROTECTION_TEXT
GAME_MODE_BUTTON
DIFFICULTY_BUTTON
FORCE_GAME_MODE_BUTTON
SPACING
layout
parentScreen
slot
worldType
defaultSlotName
spawnProtection
forceGameMode
difficulty
gameMode
worldName
warningHeader
spawnProtectionButton

String literals (9)

"mco.configure.world.buttons.options"
"mco.configure.world.edit.slot.name"
"mco.configure.world.spawnProtection"
"selectWorld.gameMode"
"options.difficulty"
"mco.configure.world.forceGameMode"
"mco.configure.world.edit.subscreen.adventuremap"
"mco.configure.world.edit.subscreen.inspiration"
"mco.configure.world.edit.subscreen.experience"

Full Source

RealmsSlotOptionsScreen.java230 lines · Minecraft 26.3 · verbatim
1
package com.mojang.realmsclient.gui.screens.configuration;
2
3
import com.google.common.collect.ImmutableList;
4
import com.mojang.realmsclient.dto.RealmsServer;
5
import com.mojang.realmsclient.dto.RealmsSlot;
6
import com.mojang.realmsclient.dto.RealmsWorldOptions;
7
import java.util.List;
8
import net.fabricmc.api.EnvType;
9
import net.fabricmc.api.Environment;
10
import net.minecraft.client.gui.components.AbstractSliderButton;
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.StringWidget;
15
import net.minecraft.client.gui.layouts.CommonLayouts;
16
import net.minecraft.client.gui.layouts.GridLayout;
17
import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
18
import net.minecraft.client.gui.layouts.LinearLayout;
19
import net.minecraft.network.chat.CommonComponents;
20
import net.minecraft.network.chat.Component;
21
import net.minecraft.realms.RealmsScreen;
22
import net.minecraft.util.Mth;
23
import net.minecraft.world.Difficulty;
24
import net.minecraft.world.level.GameType;
25
import org.jspecify.annotations.Nullable;
26
27
@Environment(EnvType.CLIENT)
28
public class RealmsSlotOptionsScreen extends RealmsScreen {
29
	private static final int DEFAULT_DIFFICULTY = 2;
30
	public static final List<Difficulty> DIFFICULTIES = ImmutableList.of(Difficulty.PEACEFUL, Difficulty.EASY, Difficulty.NORMAL, Difficulty.HARD);
31
	private static final int DEFAULT_GAME_MODE = 0;
32
	public static final List<GameType> GAME_MODES = ImmutableList.of(GameType.SURVIVAL, GameType.CREATIVE, GameType.ADVENTURE);
33
	private static final Component TITLE = Component.translatable("mco.configure.world.buttons.options");
34
	private static final Component WORLD_NAME_EDIT_LABEL = Component.translatable("mco.configure.world.edit.slot.name");
35
	private static final Component SPAWN_PROTECTION_TEXT = Component.translatable("mco.configure.world.spawnProtection");
36
	private static final Component GAME_MODE_BUTTON = Component.translatable("selectWorld.gameMode");
37
	private static final Component DIFFICULTY_BUTTON = Component.translatable("options.difficulty");
38
	private static final Component FORCE_GAME_MODE_BUTTON = Component.translatable("mco.configure.world.forceGameMode");
39
	private static final int SPACING = 8;
40
	private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this);
41
	private final RealmsConfigureWorldScreen parentScreen;
42
	private final RealmsSlot slot;
43
	private final RealmsServer.WorldType worldType;
44
	private final String defaultSlotName;
45
	private int spawnProtection;
46
	private boolean forceGameMode;
47
	private Difficulty difficulty;
48
	private GameType gameMode;
49
	private String worldName;
50
	private @Nullable StringWidget warningHeader;
51
	private RealmsSlotOptionsScreen.@Nullable SettingsSlider spawnProtectionButton;
52
53
	public RealmsSlotOptionsScreen(
54
		final RealmsConfigureWorldScreen configureWorldScreen, final RealmsSlot slot, final RealmsServer.WorldType worldType, final int activeSlot
55
	) {
56
		super(TITLE);
57
		this.parentScreen = configureWorldScreen;
58
		this.slot = slot;
59
		this.worldType = worldType;
60
		this.difficulty = findByIndex(DIFFICULTIES, slot.options.difficulty, 2);
61
		this.gameMode = findByIndex(GAME_MODES, slot.options.gameMode, 0);
62
		this.defaultSlotName = slot.options.getDefaultSlotName(activeSlot);
63
		this.setWorldName(slot.options.getSlotName(activeSlot));
64
		if (worldType == RealmsServer.WorldType.NORMAL) {
65
			this.spawnProtection = slot.options.spawnProtection;
66
			this.forceGameMode = slot.options.forceGameMode;
67
		} else {
68
			this.spawnProtection = 0;
69
			this.forceGameMode = false;
70
		}
71
	}
72
73
	@Override
74
	public void init() {
75
		LinearLayout header = this.layout.addToHeader(LinearLayout.vertical().spacing(8));
76
		header.defaultCellSetting().alignHorizontallyCenter();
77
		header.addChild(new StringWidget(TITLE, this.minecraft.font));
78
79
		Component warning = switch (this.worldType) {
80
			case ADVENTUREMAP -> Component.translatable("mco.configure.world.edit.subscreen.adventuremap").withColor(-65536);
81
			case INSPIRATION -> Component.translatable("mco.configure.world.edit.subscreen.inspiration").withColor(-65536);
82
			case EXPERIENCE -> Component.translatable("mco.configure.world.edit.subscreen.experience").withColor(-65536);
83
			default -> null;
84
		};
85
		if (warning != null) {
86
			this.layout.setHeaderHeight(41 + 9 + 8);
87
			this.warningHeader = header.addChild(new StringWidget(warning, this.font));
88
		}
89
90
		GridLayout contentGrid = this.layout.addToContents(new GridLayout().spacing(8));
91
		contentGrid.defaultCellSetting().alignHorizontallyCenter();
92
		GridLayout.RowHelper rowHelper = contentGrid.createRowHelper(2);
93
		EditBox worldNameEdit = new EditBox(this.minecraft.font, 0, 0, 150, 20, null, WORLD_NAME_EDIT_LABEL);
94
		worldNameEdit.setValue(this.worldName);
95
		worldNameEdit.setResponder(this::setWorldName);
96
		rowHelper.addChild(CommonLayouts.labeledElement(this.font, worldNameEdit, WORLD_NAME_EDIT_LABEL), 2);
97
		CycleButton<Difficulty> difficultyCycleButton = rowHelper.addChild(
98
			CycleButton.builder(Difficulty::getDisplayName, this.difficulty)
99
				.withValues(DIFFICULTIES)
100
				.create(0, 0, 150, 20, DIFFICULTY_BUTTON, (var1x, value) -> this.difficulty = value)
101
		);
102
		CycleButton<GameType> gameTypeCycleButton = rowHelper.addChild(
103
			CycleButton.builder(GameType::getShortDisplayName, this.gameMode)
104
				.withValues(GAME_MODES)
105
				.create(0, 0, 150, 20, GAME_MODE_BUTTON, (var1x, value) -> this.gameMode = value)
106
		);
107
		CycleButton<Boolean> forceGameModeButton = rowHelper.addChild(
108
			CycleButton.onOffBuilder(this.forceGameMode).create(0, 0, 150, 20, FORCE_GAME_MODE_BUTTON, (var1x, value) -> this.forceGameMode = value)
109
		);
110
		this.spawnProtectionButton = rowHelper.addChild(new RealmsSlotOptionsScreen.SettingsSlider(0, 0, 150, this.spawnProtection, 0.0F, 16.0F));
111
		if (this.worldType != RealmsServer.WorldType.NORMAL) {
112
			this.spawnProtectionButton.active = false;
113
			forceGameModeButton.active = false;
114
		}
115
116
		if (this.slot.isHardcore()) {
117
			difficultyCycleButton.active = false;
118
			gameTypeCycleButton.active = false;
119
			forceGameModeButton.active = false;
120
		}
121
122
		LinearLayout footer = this.layout.addToFooter(LinearLayout.horizontal().spacing(8));
123
		footer.addChild(Button.builder(CommonComponents.GUI_CONTINUE, var1x -> this.saveSettings()).build());
124
		footer.addChild(Button.builder(CommonComponents.GUI_CANCEL, var1x -> this.onClose()).build());
125
		this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
126
		this.repositionElements();
127
	}
128
129
	@Override
130
	protected void repositionElements() {
131
		this.layout.arrangeElements();
132
	}
133
134
	@Override
135
	public void onClose() {
136
		this.minecraft.gui.setScreen(this.parentScreen);
137
	}
138
139
	private static <T> T findByIndex(final List<T> values, final int index, final int defaultIndex) {
140
		return index >= 0 && index < values.size() ? values.get(index) : values.get(defaultIndex);
141
	}
142
143
	private static <T> int findIndex(final List<T> values, final T value, final int defaultIndex) {
144
		int result = values.indexOf(value);
145
		return result == -1 ? defaultIndex : result;
146
	}
147
148
	@Override
149
	public Component getNarrationMessage() {
150
		return this.warningHeader == null ? super.getNarrationMessage() : CommonComponents.joinForNarration(this.getTitle(), this.warningHeader.getMessage());
151
	}
152
153
	private void setWorldName(final String value) {
154
		if (value.equals(this.defaultSlotName)) {
155
			this.worldName = "";
156
		} else {
157
			this.worldName = value;
158
		}
159
	}
160
161
	private void saveSettings() {
162
		int difficultyId = findIndex(DIFFICULTIES, this.difficulty, 2);
163
		int gameModeId = findIndex(GAME_MODES, this.gameMode, 0);
164
		if (this.worldType != RealmsServer.WorldType.ADVENTUREMAP
165
			&& this.worldType != RealmsServer.WorldType.EXPERIENCE
166
			&& this.worldType != RealmsServer.WorldType.INSPIRATION) {
167
			this.parentScreen
168
				.saveSlotSettings(
169
					new RealmsSlot(
170
						this.slot.slotId,
171
						new RealmsWorldOptions(
172
							this.spawnProtection, difficultyId, gameModeId, this.forceGameMode, this.worldName, this.slot.options.version, this.slot.options.compatibility
173
						),
174
						this.slot.settings
175
					)
176
				);
177
		} else {
178
			this.parentScreen
179
				.saveSlotSettings(
180
					new RealmsSlot(
181
						this.slot.slotId,
182
						new RealmsWorldOptions(
183
							this.slot.options.spawnProtection,
184
							difficultyId,
185
							gameModeId,
186
							this.slot.options.forceGameMode,
187
							this.worldName,
188
							this.slot.options.version,
189
							this.slot.options.compatibility
190
						),
191
						this.slot.settings
192
					)
193
				);
194
		}
195
	}
196
197
	@Environment(EnvType.CLIENT)
198
	private class SettingsSlider extends AbstractSliderButton {
199
		private final double minValue;
200
		private final double maxValue;
201
202
		public SettingsSlider(final int x, final int y, final int width, final int currentValue, final float minValue, final float maxValue) {
203
			super(x, y, width, 20, CommonComponents.EMPTY, 0.0);
204
			this.minValue = minValue;
205
			this.maxValue = maxValue;
206
			this.value = (Mth.clamp(currentValue, minValue, maxValue) - minValue) / (maxValue - minValue);
207
			this.updateMessage();
208
		}
209
210
		@Override
211
		public void applyValue() {
212
			if (RealmsSlotOptionsScreen.this.spawnProtectionButton.active) {
213
				RealmsSlotOptionsScreen.this.spawnProtection = (int)Mth.lerp(Mth.clamp(this.value, 0.0, 1.0), this.minValue, this.maxValue);
214
			}
215
		}
216
217
		@Override
218
		protected void updateMessage() {
219
			this.setMessage(
220
				CommonComponents.optionNameValue(
221
					RealmsSlotOptionsScreen.SPAWN_PROTECTION_TEXT,
222
					RealmsSlotOptionsScreen.this.spawnProtection == 0
223
						? CommonComponents.OPTION_OFF
224
						: Component.literal(String.valueOf(RealmsSlotOptionsScreen.this.spawnProtection))
225
				)
226
			);
227
		}
228
	}
229
}
230

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