WorldCreationUiState .java

net.minecraft.client.gui.screens.worldselection.WorldCreationUiState (class): members: tryUpdateDataConfiguration, setGenerateStructures, SelectedGameMode.

net.minecraft.client.gui.screens.worldselectionclass332 linesclient11.5 KB

API Surface (33)

void addListener(final Consumer<WorldCreationUiState> action)
void onChanged()
void setName(final String name)
String getName()
String getTargetFolder()
void setGameMode(final WorldCreationUiState.SelectedGameMode gameMode)
WorldCreationUiState.SelectedGameMode getGameMode()
void setDifficulty(final Difficulty difficulty)
Difficulty getDifficulty()
boolean isHardcore()
void setAllowCommands(final boolean allowCommands)
boolean isAllowCommands()
void setSeed(final String seed)
String getSeed()
void setGenerateStructures(final boolean generateStructures)
boolean isGenerateStructures()
void setBonusChest(final boolean bonusChest)
boolean isBonusChest()
void setSettings(final WorldCreationContext settings)
WorldCreationContext getSettings()
void updateDimensions(final WorldCreationContext.DimensionsUpdater modifier)
boolean tryUpdateDataConfiguration(final WorldDataConfiguration newConfig)
boolean isDebug()
void setWorldType(final WorldCreationUiState.WorldTypeEntry worldType)
WorldCreationUiState.WorldTypeEntry getWorldType()
List<WorldCreationUiState.WorldTypeEntry> getNormalPresetList()
List<WorldCreationUiState.WorldTypeEntry> getAltPresetList()
void setGameRules(final GameRules gameRules)
GameRules getGameRules()
Component getInfo()
record WorldTypeEntry(@Nullable Holder<WorldPreset> preset)
Component describePreset()
boolean isAmplified()

Fields & Constants (17)

DEFAULT_WORLD_NAME
listeners
name
gameMode
difficulty
allowCommands
seed
generateStructures
bonusChest
savesFolder
targetFolder
settings
worldType
normalPresetList
altPresetList
gameRules
preset

String literals (12)

"selectWorld.newWorld"
"Could not create save folder"
"World"
"Could not create world folder"
"survival"
"hardcore"
"creative"
"spectator"
"selectWorld.gameMode."
".info"
"generator.custom"
"generator"

Full Source

WorldCreationUiState.java332 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.screens.worldselection;
2
3
import java.io.IOException;
4
import java.nio.file.Files;
5
import java.nio.file.Path;
6
import java.util.ArrayList;
7
import java.util.List;
8
import java.util.Optional;
9
import java.util.OptionalLong;
10
import java.util.function.Consumer;
11
import net.fabricmc.api.EnvType;
12
import net.fabricmc.api.Environment;
13
import net.minecraft.core.Holder;
14
import net.minecraft.core.Registry;
15
import net.minecraft.core.registries.Registries;
16
import net.minecraft.network.chat.Component;
17
import net.minecraft.resources.ResourceKey;
18
import net.minecraft.tags.TagKey;
19
import net.minecraft.tags.WorldPresetTags;
20
import net.minecraft.util.FileUtil;
21
import net.minecraft.world.Difficulty;
22
import net.minecraft.world.level.GameType;
23
import net.minecraft.world.level.WorldDataConfiguration;
24
import net.minecraft.world.level.gamerules.GameRules;
25
import net.minecraft.world.level.levelgen.WorldOptions;
26
import net.minecraft.world.level.levelgen.presets.WorldPreset;
27
import net.minecraft.world.level.levelgen.presets.WorldPresets;
28
import net.minecraft.world.level.storage.LevelStorageException;
29
import org.jspecify.annotations.Nullable;
30
31
@Environment(EnvType.CLIENT)
32
public class WorldCreationUiState {
33
	private static final Component DEFAULT_WORLD_NAME = Component.translatable("selectWorld.newWorld");
34
	private final List<Consumer<WorldCreationUiState>> listeners = new ArrayList<>();
35
	private String name = DEFAULT_WORLD_NAME.getString();
36
	private WorldCreationUiState.SelectedGameMode gameMode = WorldCreationUiState.SelectedGameMode.SURVIVAL;
37
	private Difficulty difficulty = Difficulty.NORMAL;
38
	private @Nullable Boolean allowCommands;
39
	private String seed;
40
	private boolean generateStructures;
41
	private boolean bonusChest;
42
	private final Path savesFolder;
43
	private String targetFolder;
44
	private WorldCreationContext settings;
45
	private WorldCreationUiState.WorldTypeEntry worldType;
46
	private final List<WorldCreationUiState.WorldTypeEntry> normalPresetList = new ArrayList<>();
47
	private final List<WorldCreationUiState.WorldTypeEntry> altPresetList = new ArrayList<>();
48
	private GameRules gameRules;
49
50
	public WorldCreationUiState(
51
		final Path savesFolder, final WorldCreationContext settings, final Optional<ResourceKey<WorldPreset>> preset, final OptionalLong seed
52
	) {
53
		this.savesFolder = savesFolder;
54
		this.settings = settings;
55
		this.worldType = new WorldCreationUiState.WorldTypeEntry(findPreset(settings, preset).orElse(null));
56
		this.updatePresetLists();
57
		this.seed = seed.isPresent() ? Long.toString(seed.getAsLong()) : "";
58
		this.generateStructures = settings.options().generateStructures();
59
		this.bonusChest = settings.options().generateBonusChest();
60
		this.targetFolder = this.findResultFolder(this.name);
61
		this.gameMode = settings.initialWorldCreationOptions().selectedGameMode();
62
		this.gameRules = new GameRules(settings.dataConfiguration().enabledFeatures());
63
		this.gameRules.setAll(settings.initialWorldCreationOptions().gameRuleOverwrites(), null);
64
		Optional.ofNullable(settings.initialWorldCreationOptions().flatLevelPreset())
65
			.flatMap(key -> settings.worldgenLoadContext().lookup(Registries.FLAT_LEVEL_GENERATOR_PRESET).flatMap(registry -> registry.get(key)))
66
			.map(reference -> reference.value().settings())
67
			.ifPresent(generatorSettings -> this.updateDimensions(PresetEditor.flatWorldConfigurator(generatorSettings)));
68
	}
69
70
	public void addListener(final Consumer<WorldCreationUiState> action) {
71
		this.listeners.add(action);
72
	}
73
74
	public void onChanged() {
75
		boolean bonusChest = this.isBonusChest();
76
		if (bonusChest != this.settings.options().generateBonusChest()) {
77
			this.settings = this.settings.withOptions(options -> options.withBonusChest(bonusChest));
78
		}
79
80
		boolean generateStructures = this.isGenerateStructures();
81
		if (generateStructures != this.settings.options().generateStructures()) {
82
			this.settings = this.settings.withOptions(options -> options.withStructures(generateStructures));
83
		}
84
85
		for (Consumer<WorldCreationUiState> listener : this.listeners) {
86
			listener.accept(this);
87
		}
88
	}
89
90
	public void setName(final String name) {
91
		this.name = name;
92
		this.targetFolder = this.findResultFolder(name);
93
		this.onChanged();
94
	}
95
96
	private String findResultFolder(final String name) {
97
		if (!Files.exists(this.savesFolder)) {
98
			try {
99
				Files.createDirectory(this.savesFolder);
100
			} catch (IOException e) {
101
				throw new LevelStorageException(Component.literal("Could not create save folder"), e);
102
			}
103
		}
104
105
		String trimmedName = name.trim();
106
107
		try {
108
			return FileUtil.findAvailableName(this.savesFolder, !trimmedName.isEmpty() ? trimmedName : DEFAULT_WORLD_NAME.getString(), "");
109
		} catch (Exception var5) {
110
			try {
111
				return FileUtil.findAvailableName(this.savesFolder, "World", "");
112
			} catch (IOException e) {
113
				throw new LevelStorageException(Component.literal("Could not create world folder"), e);
114
			}
115
		}
116
	}
117
118
	public String getName() {
119
		return this.name;
120
	}
121
122
	public String getTargetFolder() {
123
		return this.targetFolder;
124
	}
125
126
	public void setGameMode(final WorldCreationUiState.SelectedGameMode gameMode) {
127
		this.gameMode = gameMode;
128
		this.onChanged();
129
	}
130
131
	public WorldCreationUiState.SelectedGameMode getGameMode() {
132
		return this.isDebug() ? WorldCreationUiState.SelectedGameMode.DEBUG : this.gameMode;
133
	}
134
135
	public void setDifficulty(final Difficulty difficulty) {
136
		this.difficulty = difficulty;
137
		this.onChanged();
138
	}
139
140
	public Difficulty getDifficulty() {
141
		return this.isHardcore() ? Difficulty.HARD : this.difficulty;
142
	}
143
144
	public boolean isHardcore() {
145
		return this.getGameMode() == WorldCreationUiState.SelectedGameMode.HARDCORE;
146
	}
147
148
	public void setAllowCommands(final boolean allowCommands) {
149
		this.allowCommands = allowCommands;
150
		this.onChanged();
151
	}
152
153
	public boolean isAllowCommands() {
154
		if (this.isDebug()) {
155
			return true;
156
		} else if (this.isHardcore()) {
157
			return false;
158
		} else {
159
			return this.allowCommands == null ? this.getGameMode() == WorldCreationUiState.SelectedGameMode.CREATIVE : this.allowCommands;
160
		}
161
	}
162
163
	public void setSeed(final String seed) {
164
		this.seed = seed;
165
		this.settings = this.settings.withOptions(options -> options.withSeed(WorldOptions.parseSeed(this.getSeed())));
166
		this.onChanged();
167
	}
168
169
	public String getSeed() {
170
		return this.seed;
171
	}
172
173
	public void setGenerateStructures(final boolean generateStructures) {
174
		this.generateStructures = generateStructures;
175
		this.onChanged();
176
	}
177
178
	public boolean isGenerateStructures() {
179
		return this.isDebug() ? false : this.generateStructures;
180
	}
181
182
	public void setBonusChest(final boolean bonusChest) {
183
		this.bonusChest = bonusChest;
184
		this.onChanged();
185
	}
186
187
	public boolean isBonusChest() {
188
		return !this.isDebug() && !this.isHardcore() ? this.bonusChest : false;
189
	}
190
191
	public void setSettings(final WorldCreationContext settings) {
192
		this.settings = settings;
193
		this.updatePresetLists();
194
		this.onChanged();
195
	}
196
197
	public WorldCreationContext getSettings() {
198
		return this.settings;
199
	}
200
201
	public void updateDimensions(final WorldCreationContext.DimensionsUpdater modifier) {
202
		this.settings = this.settings.withDimensions(modifier);
203
		this.onChanged();
204
	}
205
206
	protected boolean tryUpdateDataConfiguration(final WorldDataConfiguration newConfig) {
207
		WorldDataConfiguration oldConfig = this.settings.dataConfiguration();
208
		if (oldConfig.dataPacks().getEnabled().equals(newConfig.dataPacks().getEnabled()) && oldConfig.enabledFeatures().equals(newConfig.enabledFeatures())) {
209
			this.settings = new WorldCreationContext(
210
				this.settings.options(),
211
				this.settings.datapackDimensions(),
212
				this.settings.selectedDimensions(),
213
				this.settings.worldgenRegistries(),
214
				this.settings.dataPackResources(),
215
				newConfig,
216
				this.settings.initialWorldCreationOptions()
217
			);
218
			return true;
219
		} else {
220
			return false;
221
		}
222
	}
223
224
	public boolean isDebug() {
225
		return this.settings.selectedDimensions().isDebug();
226
	}
227
228
	public void setWorldType(final WorldCreationUiState.WorldTypeEntry worldType) {
229
		this.worldType = worldType;
230
		Holder<WorldPreset> preset = worldType.preset();
231
		if (preset != null) {
232
			this.updateDimensions((registryAccess, dimensions) -> preset.value().createWorldDimensions());
233
		}
234
	}
235
236
	public WorldCreationUiState.WorldTypeEntry getWorldType() {
237
		return this.worldType;
238
	}
239
240
	public @Nullable PresetEditor getPresetEditor() {
241
		Holder<WorldPreset> preset = this.getWorldType().preset();
242
		return preset != null ? PresetEditor.EDITORS.get(preset.unwrapKey()) : null;
243
	}
244
245
	public List<WorldCreationUiState.WorldTypeEntry> getNormalPresetList() {
246
		return this.normalPresetList;
247
	}
248
249
	public List<WorldCreationUiState.WorldTypeEntry> getAltPresetList() {
250
		return this.altPresetList;
251
	}
252
253
	private void updatePresetLists() {
254
		Registry<WorldPreset> presetRegistry = this.getSettings().worldgenLoadContext().lookupOrThrow(Registries.WORLD_PRESET);
255
		this.normalPresetList.clear();
256
		this.normalPresetList
257
			.addAll(
258
				getNonEmptyList(presetRegistry, WorldPresetTags.NORMAL)
259
					.orElseGet(() -> presetRegistry.listElements().map(WorldCreationUiState.WorldTypeEntry::new).toList())
260
			);
261
		this.altPresetList.clear();
262
		this.altPresetList.addAll(getNonEmptyList(presetRegistry, WorldPresetTags.EXTENDED).orElse(this.normalPresetList));
263
		Holder<WorldPreset> preset = this.worldType.preset();
264
		if (preset != null) {
265
			WorldCreationUiState.WorldTypeEntry newPreset = findPreset(this.getSettings(), preset.unwrapKey())
266
				.map(WorldCreationUiState.WorldTypeEntry::new)
267
				.orElse(this.normalPresetList.getFirst());
268
			boolean isCustomizablePreset = PresetEditor.EDITORS.get(preset.unwrapKey()) != null;
269
			if (isCustomizablePreset) {
270
				this.worldType = newPreset;
271
			} else {
272
				this.setWorldType(newPreset);
273
			}
274
		}
275
	}
276
277
	private static Optional<Holder<WorldPreset>> findPreset(final WorldCreationContext settings, final Optional<ResourceKey<WorldPreset>> preset) {
278
		return preset.flatMap(k -> settings.worldgenLoadContext().lookupOrThrow(Registries.WORLD_PRESET).get((ResourceKey<WorldPreset>)k));
279
	}
280
281
	private static Optional<List<WorldCreationUiState.WorldTypeEntry>> getNonEmptyList(final Registry<WorldPreset> presetRegistry, final TagKey<WorldPreset> id) {
282
		return presetRegistry.get(id).map(tag -> tag.stream().map(WorldCreationUiState.WorldTypeEntry::new).toList()).filter(l -> !l.isEmpty());
283
	}
284
285
	public void setGameRules(final GameRules gameRules) {
286
		this.gameRules = gameRules;
287
		this.onChanged();
288
	}
289
290
	public GameRules getGameRules() {
291
		return this.gameRules;
292
	}
293
294
	@Environment(EnvType.CLIENT)
295
	public enum SelectedGameMode {
296
		SURVIVAL("survival", GameType.SURVIVAL),
297
		HARDCORE("hardcore", GameType.SURVIVAL),
298
		CREATIVE("creative", GameType.CREATIVE),
299
		DEBUG("spectator", GameType.SPECTATOR);
300
301
		public final GameType gameType;
302
		public final Component displayName;
303
		private final Component info;
304
305
		SelectedGameMode(final String name, final GameType gameType) {
306
			this.gameType = gameType;
307
			this.displayName = Component.translatable("selectWorld.gameMode." + name);
308
			this.info = Component.translatable("selectWorld.gameMode." + name + ".info");
309
		}
310
311
		public Component getInfo() {
312
			return this.info;
313
		}
314
	}
315
316
	@Environment(EnvType.CLIENT)
317
	public record WorldTypeEntry(@Nullable Holder<WorldPreset> preset) {
318
		private static final Component CUSTOM_WORLD_DESCRIPTION = Component.translatable("generator.custom");
319
320
		public Component describePreset() {
321
			return Optional.ofNullable(this.preset)
322
				.flatMap(Holder::unwrapKey)
323
				.map(key -> Component.translatable(key.identifier().toLanguageKey("generator")))
324
				.orElse(CUSTOM_WORLD_DESCRIPTION);
325
		}
326
327
		public boolean isAmplified() {
328
			return Optional.ofNullable(this.preset).flatMap(Holder::unwrapKey).filter(k -> k.equals(WorldPresets.AMPLIFIED)).isPresent();
329
		}
330
	}
331
}
332

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