| 1 | package net.minecraft.client.gui.screens;
|
| 2 |
|
| 3 | import com.ibm.icu.text.Collator;
|
| 4 | import java.util.Comparator;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Locale;
|
| 7 | import java.util.Objects;
|
| 8 | import java.util.function.Consumer;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 | import net.minecraft.client.gui.components.Button;
|
| 12 | import net.minecraft.client.gui.components.EditBox;
|
| 13 | import net.minecraft.client.gui.components.ObjectSelectionList;
|
| 14 | import net.minecraft.client.gui.components.StringWidget;
|
| 15 | import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
|
| 16 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 17 | import net.minecraft.client.gui.screens.worldselection.WorldCreationContext;
|
| 18 | import net.minecraft.core.Holder;
|
| 19 | import net.minecraft.core.Registry;
|
| 20 | import net.minecraft.core.registries.Registries;
|
| 21 | import net.minecraft.network.chat.CommonComponents;
|
| 22 | import net.minecraft.network.chat.Component;
|
| 23 | import net.minecraft.world.level.biome.Biome;
|
| 24 | import net.minecraft.world.level.biome.Biomes;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 |
|
| 27 | @Environment(EnvType.CLIENT)
|
| 28 | public class CreateBuffetWorldScreen extends Screen {
|
| 29 | private static final Component SEARCH_HINT = Component.translatable("createWorld.customize.buffet.search").withStyle(EditBox.SEARCH_HINT_STYLE);
|
| 30 | private static final int SPACING = 3;
|
| 31 | private static final int SEARCH_BOX_HEIGHT = 15;
|
| 32 | private final HeaderAndFooterLayout layout;
|
| 33 | private final Screen parent;
|
| 34 | private final Consumer<Holder<Biome>> applySettings;
|
| 35 | private final Registry<Biome> biomes;
|
| 36 | private CreateBuffetWorldScreen.BiomeList list;
|
| 37 | private Holder<Biome> biome;
|
| 38 | private Button doneButton;
|
| 39 |
|
| 40 | public CreateBuffetWorldScreen(final Screen parent, final WorldCreationContext settings, final Consumer<Holder<Biome>> applySettings) {
|
| 41 | super(Component.translatable("createWorld.customize.buffet.title"));
|
| 42 | this.parent = parent;
|
| 43 | this.applySettings = applySettings;
|
| 44 | this.layout = new HeaderAndFooterLayout(this, 13 + 9 + 3 + 15, 33);
|
| 45 | this.biomes = settings.worldgenLoadContext().lookupOrThrow(Registries.BIOME);
|
| 46 | Holder<Biome> defaultBiome = this.biomes.get(Biomes.PLAINS).or(() -> this.biomes.listElements().findAny()).orElseThrow();
|
| 47 | this.biome = settings.selectedDimensions().overworld().getBiomeSource().possibleBiomes().stream().findFirst().orElse(defaultBiome);
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public void onClose() {
|
| 52 | this.minecraft.gui.setScreen(this.parent);
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | protected void init() {
|
| 57 | LinearLayout header = this.layout.addToHeader(LinearLayout.vertical().spacing(3));
|
| 58 | header.defaultCellSetting().alignHorizontallyCenter();
|
| 59 | header.addChild(new StringWidget(this.getTitle(), this.font));
|
| 60 | EditBox search = header.addChild(new EditBox(this.font, 200, 15, Component.empty()));
|
| 61 | CreateBuffetWorldScreen.BiomeList biomeList = new CreateBuffetWorldScreen.BiomeList();
|
| 62 | search.setHint(SEARCH_HINT);
|
| 63 | search.setResponder(biomeList::filterEntries);
|
| 64 | this.list = this.layout.addToContents(biomeList);
|
| 65 | LinearLayout footer = this.layout.addToFooter(LinearLayout.horizontal().spacing(8));
|
| 66 | this.doneButton = footer.addChild(Button.builder(CommonComponents.GUI_DONE, button -> {
|
| 67 | this.applySettings.accept(this.biome);
|
| 68 | this.onClose();
|
| 69 | }).build());
|
| 70 | footer.addChild(Button.builder(CommonComponents.GUI_CANCEL, button -> this.onClose()).build());
|
| 71 | this.list.setSelected(this.list.children().stream().filter(e -> Objects.equals(e.biome, this.biome)).findFirst().orElse(null));
|
| 72 | this.layout.visitWidgets(this::addRenderableWidget);
|
| 73 | this.repositionElements();
|
| 74 | }
|
| 75 |
|
| 76 | @Override
|
| 77 | protected void repositionElements() {
|
| 78 | this.layout.arrangeElements();
|
| 79 | this.list.updateSize(this.width, this.layout);
|
| 80 | }
|
| 81 |
|
| 82 | private void updateButtonValidity() {
|
| 83 | this.doneButton.active = this.list.getSelected() != null;
|
| 84 | }
|
| 85 |
|
| 86 | @Environment(EnvType.CLIENT)
|
| 87 | private class BiomeList extends ObjectSelectionList<net.minecraft.client.gui.screens.CreateBuffetWorldScreen.BiomeList.Entry> {
|
| 88 | private BiomeList() {
|
| 89 | super(
|
| 90 | CreateBuffetWorldScreen.this.minecraft,
|
| 91 | CreateBuffetWorldScreen.this.width,
|
| 92 | CreateBuffetWorldScreen.this.layout.getContentHeight(),
|
| 93 | CreateBuffetWorldScreen.this.layout.getHeaderHeight(),
|
| 94 | 15
|
| 95 | );
|
| 96 | this.filterEntries("");
|
| 97 | }
|
| 98 |
|
| 99 | private void filterEntries(final String filter) {
|
| 100 | Collator localeCollator = Collator.getInstance(Locale.getDefault());
|
| 101 | String lowercaseFilter = filter.toLowerCase(Locale.ROOT);
|
| 102 | List<net.minecraft.client.gui.screens.CreateBuffetWorldScreen.BiomeList.Entry> list = CreateBuffetWorldScreen.this.biomes
|
| 103 | .listElements()
|
| 104 | .map(x$0 -> new net.minecraft.client.gui.screens.CreateBuffetWorldScreen.BiomeList.Entry(this, x$0))
|
| 105 | .sorted(Comparator.comparing(e -> e.name.getString(), localeCollator))
|
| 106 | .filter(entry -> filter.isEmpty() || entry.name.getString().toLowerCase(Locale.ROOT).contains(lowercaseFilter))
|
| 107 | .toList();
|
| 108 | this.replaceEntries(list);
|
| 109 | this.refreshScrollAmount();
|
| 110 | }
|
| 111 |
|
| 112 | public void setSelected(final Entry/* @Nullable */ selected) {
|
| 113 | super.setSelected(selected);
|
| 114 | if (selected != null) {
|
| 115 | CreateBuffetWorldScreen.this.biome = selected.biome;
|
| 116 | }
|
| 117 |
|
| 118 | CreateBuffetWorldScreen.this.updateButtonValidity();
|
| 119 | }
|
| 120 | }
|
| 121 | }
|
| 122 |
|