| 1 | package net.minecraft.client.gui.screens;
|
| 2 |
|
| 3 | import com.google.common.base.Splitter;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import java.util.Collections;
|
| 7 | import java.util.Iterator;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Optional;
|
| 10 | import java.util.Set;
|
| 11 | import java.util.stream.Collectors;
|
| 12 | import net.fabricmc.api.EnvType;
|
| 13 | import net.fabricmc.api.Environment;
|
| 14 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 15 | import net.minecraft.client.gui.components.Button;
|
| 16 | import net.minecraft.client.gui.components.EditBox;
|
| 17 | import net.minecraft.client.gui.components.ObjectSelectionList;
|
| 18 | import net.minecraft.client.gui.screens.worldselection.WorldCreationContext;
|
| 19 | import net.minecraft.client.input.KeyEvent;
|
| 20 | import net.minecraft.core.Holder;
|
| 21 | import net.minecraft.core.HolderGetter;
|
| 22 | import net.minecraft.core.RegistryAccess;
|
| 23 | import net.minecraft.core.Holder.Reference;
|
| 24 | import net.minecraft.core.registries.Registries;
|
| 25 | import net.minecraft.network.chat.CommonComponents;
|
| 26 | import net.minecraft.network.chat.Component;
|
| 27 | import net.minecraft.resources.Identifier;
|
| 28 | import net.minecraft.resources.ResourceKey;
|
| 29 | import net.minecraft.tags.FlatLevelGeneratorPresetTags;
|
| 30 | import net.minecraft.world.flag.FeatureFlagSet;
|
| 31 | import net.minecraft.world.level.biome.Biome;
|
| 32 | import net.minecraft.world.level.biome.Biomes;
|
| 33 | import net.minecraft.world.level.block.Block;
|
| 34 | import net.minecraft.world.level.dimension.DimensionType;
|
| 35 | import net.minecraft.world.level.levelgen.flat.FlatLayerInfo;
|
| 36 | import net.minecraft.world.level.levelgen.flat.FlatLevelGeneratorPreset;
|
| 37 | import net.minecraft.world.level.levelgen.flat.FlatLevelGeneratorSettings;
|
| 38 | import net.minecraft.world.level.levelgen.placement.PlacedFeature;
|
| 39 | import net.minecraft.world.level.levelgen.structure.StructureSet;
|
| 40 | import org.jspecify.annotations.Nullable;
|
| 41 | import org.slf4j.Logger;
|
| 42 |
|
| 43 | @Environment(EnvType.CLIENT)
|
| 44 | public class PresetFlatWorldScreen extends Screen {
|
| 45 | private static final Identifier SLOT_SPRITE = Identifier.withDefaultNamespace("container/slot");
|
| 46 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 47 | private static final int SLOT_BG_SIZE = 18;
|
| 48 | private static final int SLOT_STAT_HEIGHT = 20;
|
| 49 | private static final int SLOT_BG_X = 1;
|
| 50 | private static final int SLOT_BG_Y = 1;
|
| 51 | private static final int SLOT_FG_X = 2;
|
| 52 | private static final int SLOT_FG_Y = 2;
|
| 53 | private static final ResourceKey<Biome> DEFAULT_BIOME = Biomes.PLAINS;
|
| 54 | public static final Component UNKNOWN_PRESET = Component.translatable("flat_world_preset.unknown");
|
| 55 | private final CreateFlatWorldScreen parent;
|
| 56 | private Component shareText;
|
| 57 | private Component listText;
|
| 58 | private PresetFlatWorldScreen.PresetsList list;
|
| 59 | private Button selectButton;
|
| 60 | private EditBox export;
|
| 61 | private FlatLevelGeneratorSettings settings;
|
| 62 |
|
| 63 | public PresetFlatWorldScreen(final CreateFlatWorldScreen parent) {
|
| 64 | super(Component.translatable("createWorld.customize.presets.title"));
|
| 65 | this.parent = parent;
|
| 66 | }
|
| 67 |
|
| 68 | private static @Nullable FlatLayerInfo getLayerInfoFromString(final HolderGetter<Block> blocks, final String input, final int firstFree) {
|
| 69 | List<String> parts = Splitter.on('*').limit(2).splitToList(input);
|
| 70 | int height;
|
| 71 | String blockId;
|
| 72 | if (parts.size() == 2) {
|
| 73 | blockId = parts.get(1);
|
| 74 |
|
| 75 | try {
|
| 76 | height = Math.max(Integer.parseInt(parts.get(0)), 0);
|
| 77 | } catch (NumberFormatException e) {
|
| 78 | LOGGER.error("Error while parsing flat world string", e);
|
| 79 | return null;
|
| 80 | }
|
| 81 | } else {
|
| 82 | blockId = parts.get(0);
|
| 83 | height = 1;
|
| 84 | }
|
| 85 |
|
| 86 | int firstAbove = Math.min(firstFree + height, DimensionType.Y_SIZE);
|
| 87 | int actualHeight = firstAbove - firstFree;
|
| 88 |
|
| 89 | Optional<Reference<Block>> block;
|
| 90 | try {
|
| 91 | block = blocks.get(ResourceKey.create(Registries.BLOCK, Identifier.parse(blockId)));
|
| 92 | } catch (Exception e) {
|
| 93 | LOGGER.error("Error while parsing flat world string", e);
|
| 94 | return null;
|
| 95 | }
|
| 96 |
|
| 97 | if (block.isEmpty()) {
|
| 98 | LOGGER.error("Error while parsing flat world string => Unknown block, {}", blockId);
|
| 99 | return null;
|
| 100 | } else {
|
| 101 | return new FlatLayerInfo(actualHeight, block.get());
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | private static List<FlatLayerInfo> getLayersInfoFromString(final HolderGetter<Block> blocks, final String input) {
|
| 106 | List<FlatLayerInfo> result = Lists.newArrayList();
|
| 107 | String[] depths = input.split(",");
|
| 108 | int firstFree = 0;
|
| 109 |
|
| 110 | for (String depth : depths) {
|
| 111 | FlatLayerInfo layer = getLayerInfoFromString(blocks, depth, firstFree);
|
| 112 | if (layer == null) {
|
| 113 | return Collections.emptyList();
|
| 114 | }
|
| 115 |
|
| 116 | int maxHeight = DimensionType.Y_SIZE - firstFree;
|
| 117 | if (maxHeight > 0) {
|
| 118 | result.add(layer.heightLimited(maxHeight));
|
| 119 | firstFree += layer.getHeight();
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | return result;
|
| 124 | }
|
| 125 |
|
| 126 | public static FlatLevelGeneratorSettings fromString(
|
| 127 | final HolderGetter<Block> blocks,
|
| 128 | final HolderGetter<Biome> biomes,
|
| 129 | final HolderGetter<StructureSet> structureSets,
|
| 130 | final HolderGetter<PlacedFeature> placedFeatures,
|
| 131 | final String definition,
|
| 132 | final FlatLevelGeneratorSettings settings
|
| 133 | ) {
|
| 134 | Iterator<String> parts = Splitter.on(';').split(definition).iterator();
|
| 135 | if (!parts.hasNext()) {
|
| 136 | return FlatLevelGeneratorSettings.getDefault(biomes, structureSets, placedFeatures);
|
| 137 | }
|
| 138 |
|
| 139 | List<FlatLayerInfo> layers = getLayersInfoFromString(blocks, parts.next());
|
| 140 | if (layers.isEmpty()) {
|
| 141 | return FlatLevelGeneratorSettings.getDefault(biomes, structureSets, placedFeatures);
|
| 142 | }
|
| 143 |
|
| 144 | Reference<Biome> defaultBiome = biomes.getOrThrow(DEFAULT_BIOME);
|
| 145 | Holder<Biome> biome = defaultBiome;
|
| 146 | if (parts.hasNext()) {
|
| 147 | String biomeName = parts.next();
|
| 148 | biome = Optional.ofNullable(Identifier.tryParse(biomeName)).map(id -> ResourceKey.create(Registries.BIOME, id)).flatMap(biomes::get).orElseGet(() -> {
|
| 149 | LOGGER.warn("Invalid biome: {}", biomeName);
|
| 150 | return defaultBiome;
|
| 151 | });
|
| 152 | }
|
| 153 |
|
| 154 | return settings.withBiomeAndLayers(layers, settings.structureOverrides(), biome);
|
| 155 | }
|
| 156 |
|
| 157 | private static String save(final FlatLevelGeneratorSettings settings) {
|
| 158 | StringBuilder builder = new StringBuilder();
|
| 159 |
|
| 160 | for (int i = 0; i < settings.getLayersInfo().size(); i++) {
|
| 161 | if (i > 0) {
|
| 162 | builder.append(",");
|
| 163 | }
|
| 164 |
|
| 165 | builder.append(settings.getLayersInfo().get(i));
|
| 166 | }
|
| 167 |
|
| 168 | builder.append(";");
|
| 169 | builder.append(settings.getBiome().unwrapKey().map(ResourceKey::identifier).orElseThrow(() -> new IllegalStateException("Biome not registered")));
|
| 170 | return builder.toString();
|
| 171 | }
|
| 172 |
|
| 173 | @Override
|
| 174 | protected void init() {
|
| 175 | this.shareText = Component.translatable("createWorld.customize.presets.share");
|
| 176 | this.listText = Component.translatable("createWorld.customize.presets.list");
|
| 177 | this.export = new EditBox(this.font, 50, 40, this.width - 100, 20, this.shareText);
|
| 178 | this.export.setMaxLength(1230);
|
| 179 | WorldCreationContext worldCreatingContext = this.parent.parent.getUiState().getSettings();
|
| 180 | RegistryAccess registryAccess = worldCreatingContext.worldgenLoadContext();
|
| 181 | FeatureFlagSet enabledFeatures = worldCreatingContext.dataConfiguration().enabledFeatures();
|
| 182 | HolderGetter<Biome> biomes = registryAccess.lookupOrThrow(Registries.BIOME);
|
| 183 | HolderGetter<StructureSet> structureSets = registryAccess.lookupOrThrow(Registries.STRUCTURE_SET);
|
| 184 | HolderGetter<PlacedFeature> placedFeatures = registryAccess.lookupOrThrow(Registries.PLACED_FEATURE);
|
| 185 | HolderGetter<Block> blocks = registryAccess.lookupOrThrow(Registries.BLOCK).filterFeatures(enabledFeatures);
|
| 186 | this.export.setValue(save(this.parent.settings()));
|
| 187 | this.settings = this.parent.settings();
|
| 188 | this.addWidget(this.export);
|
| 189 | this.list = this.addRenderableWidget(new PresetFlatWorldScreen.PresetsList(registryAccess, enabledFeatures));
|
| 190 | this.selectButton = this.addRenderableWidget(Button.builder(Component.translatable("createWorld.customize.presets.select"), button -> {
|
| 191 | FlatLevelGeneratorSettings generator = fromString(blocks, biomes, structureSets, placedFeatures, this.export.getValue(), this.settings);
|
| 192 | this.parent.setConfig(generator);
|
| 193 | this.minecraft.gui.setScreen(this.parent);
|
| 194 | }).bounds(this.width / 2 - 155, this.height - 28, 150, 20).build());
|
| 195 | this.addRenderableWidget(
|
| 196 | Button.builder(CommonComponents.GUI_CANCEL, button -> this.minecraft.gui.setScreen(this.parent))
|
| 197 | .bounds(this.width / 2 + 5, this.height - 28, 150, 20)
|
| 198 | .build()
|
| 199 | );
|
| 200 | this.updateButtonValidity(this.list.getSelected() != null);
|
| 201 | }
|
| 202 |
|
| 203 | @Override
|
| 204 | public boolean mouseScrolled(final double x, final double y, final double scrollX, final double scrollY) {
|
| 205 | return this.list.mouseScrolled(x, y, scrollX, scrollY);
|
| 206 | }
|
| 207 |
|
| 208 | @Override
|
| 209 | public void resize(final int width, final int height) {
|
| 210 | String oldEdit = this.export.getValue();
|
| 211 | this.init(width, height);
|
| 212 | this.export.setValue(oldEdit);
|
| 213 | }
|
| 214 |
|
| 215 | @Override
|
| 216 | public void onClose() {
|
| 217 | this.minecraft.gui.setScreen(this.parent);
|
| 218 | }
|
| 219 |
|
| 220 | @Override
|
| 221 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 222 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 223 | graphics.centeredText(this.font, this.title, this.width / 2, 8, -1);
|
| 224 | graphics.text(this.font, this.shareText, 51, 30, -6250336);
|
| 225 | graphics.text(this.font, this.listText, 51, 68, -6250336);
|
| 226 | this.export.extractRenderState(graphics, mouseX, mouseY, a);
|
| 227 | }
|
| 228 |
|
| 229 | public void updateButtonValidity(final boolean hasSelected) {
|
| 230 | this.selectButton.active = hasSelected || this.export.getValue().length() > 1;
|
| 231 | }
|
| 232 |
|
| 233 | @Environment(EnvType.CLIENT)
|
| 234 | private class PresetsList extends ObjectSelectionList<net.minecraft.client.gui.screens.PresetFlatWorldScreen.PresetsList.Entry> {
|
| 235 | public PresetsList(final RegistryAccess access, final FeatureFlagSet enabledFeatures) {
|
| 236 | super(PresetFlatWorldScreen.this.minecraft, PresetFlatWorldScreen.this.width, PresetFlatWorldScreen.this.height - 117, 80, 24);
|
| 237 |
|
| 238 | for (Holder<FlatLevelGeneratorPreset> preset : access.lookupOrThrow(Registries.FLAT_LEVEL_GENERATOR_PRESET)
|
| 239 | .getTagOrEmpty(FlatLevelGeneratorPresetTags.VISIBLE)) {
|
| 240 | Set<Block> disabledBlocks = preset.value()
|
| 241 | .settings()
|
| 242 | .getLayersInfo()
|
| 243 | .stream()
|
| 244 | .map(p -> p.getBlockState().getBlock())
|
| 245 | .filter(b -> !b.isEnabled(enabledFeatures))
|
| 246 | .collect(Collectors.toSet());
|
| 247 | if (!disabledBlocks.isEmpty()) {
|
| 248 | PresetFlatWorldScreen.LOGGER
|
| 249 | .info(
|
| 250 | "Discarding flat world preset {} since it contains experimental blocks {}",
|
| 251 | preset.unwrapKey().map(e -> e.identifier().toString()).orElse("<unknown>"),
|
| 252 | disabledBlocks
|
| 253 | );
|
| 254 | } else {
|
| 255 | this.addEntry(new net.minecraft.client.gui.screens.PresetFlatWorldScreen.PresetsList.Entry(this, preset));
|
| 256 | }
|
| 257 | }
|
| 258 | }
|
| 259 |
|
| 260 | public void setSelected(final Entry/* @Nullable */ selected) {
|
| 261 | super.setSelected(selected);
|
| 262 | PresetFlatWorldScreen.this.updateButtonValidity(selected != null);
|
| 263 | }
|
| 264 |
|
| 265 | @Override
|
| 266 | public boolean keyPressed(final KeyEvent event) {
|
| 267 | if (super.keyPressed(event)) {
|
| 268 | return true;
|
| 269 | }
|
| 270 |
|
| 271 | if (event.isSelection() && this.getSelected() != null) {
|
| 272 | this.getSelected().select();
|
| 273 | }
|
| 274 |
|
| 275 | return false;
|
| 276 | }
|
| 277 | }
|
| 278 | }
|
| 279 |
|