package net.minecraft.client.gui.screens; import com.ibm.icu.text.Collator; import java.util.Comparator; import java.util.Locale; import java.util.Objects; import java.util.function.Consumer; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.gui.components.Button; import net.minecraft.client.gui.components.ObjectSelectionList; import net.minecraft.client.gui.components.StringWidget; import net.minecraft.client.gui.layouts.HeaderAndFooterLayout; import net.minecraft.client.gui.layouts.LinearLayout; import net.minecraft.client.gui.screens.CreateBuffetWorldScreen.BiomeList.Entry; import net.minecraft.client.gui.screens.worldselection.WorldCreationContext; import net.minecraft.core.Holder; import net.minecraft.core.Registry; import net.minecraft.core.registries.Registries; import net.minecraft.network.chat.CommonComponents; import net.minecraft.network.chat.Component; import net.minecraft.world.level.biome.Biome; import net.minecraft.world.level.biome.Biomes; import org.jetbrains.annotations.Nullable; @Environment(EnvType.CLIENT) public class CreateBuffetWorldScreen extends Screen { private static final Component BIOME_SELECT_INFO = Component.translatable("createWorld.customize.buffet.biome").withColor(-8355712); private static final int SPACING = 8; private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this); private final Screen parent; private final Consumer> applySettings; final Registry biomes; private CreateBuffetWorldScreen.BiomeList list; Holder biome; private Button doneButton; public CreateBuffetWorldScreen(Screen parent, WorldCreationContext context, Consumer> applySettings) { super(Component.translatable("createWorld.customize.buffet.title")); this.parent = parent; this.applySettings = applySettings; this.biomes = context.worldgenLoadContext().lookupOrThrow(Registries.BIOME); Holder holder = (Holder)this.biomes.get(Biomes.PLAINS).or(() -> this.biomes.listElements().findAny()).orElseThrow(); this.biome = (Holder)context.selectedDimensions().overworld().getBiomeSource().possibleBiomes().stream().findFirst().orElse(holder); } @Override public void onClose() { this.minecraft.setScreen(this.parent); } @Override protected void init() { LinearLayout linearLayout = this.layout.addToHeader(LinearLayout.vertical().spacing(8)); linearLayout.defaultCellSetting().alignHorizontallyCenter(); linearLayout.addChild(new StringWidget(this.getTitle(), this.font)); linearLayout.addChild(new StringWidget(BIOME_SELECT_INFO, this.font)); this.list = this.layout.addToContents(new CreateBuffetWorldScreen.BiomeList()); LinearLayout linearLayout2 = this.layout.addToFooter(LinearLayout.horizontal().spacing(8)); this.doneButton = linearLayout2.addChild(Button.builder(CommonComponents.GUI_DONE, button -> { this.applySettings.accept(this.biome); this.onClose(); }).build()); linearLayout2.addChild(Button.builder(CommonComponents.GUI_CANCEL, button -> this.onClose()).build()); this.list.setSelected((Entry)this.list.children().stream().filter(entry -> Objects.equals(entry.biome, this.biome)).findFirst().orElse(null)); this.layout.visitWidgets(this::addRenderableWidget); this.repositionElements(); } @Override protected void repositionElements() { this.layout.arrangeElements(); this.list.updateSize(this.width, this.layout); } void updateButtonValidity() { this.doneButton.active = this.list.getSelected() != null; } @Environment(EnvType.CLIENT) class BiomeList extends ObjectSelectionList { BiomeList() { super(CreateBuffetWorldScreen.this.minecraft, CreateBuffetWorldScreen.this.width, CreateBuffetWorldScreen.this.height - 77, 40, 16); Collator collator = Collator.getInstance(Locale.getDefault()); CreateBuffetWorldScreen.this.biomes .listElements() .map(reference -> new net.minecraft.client.gui.screens.CreateBuffetWorldScreen.BiomeList.Entry(this, reference)) .sorted(Comparator.comparing(entry -> entry.name.getString(), collator)) .forEach(entry -> this.addEntry(entry)); } public void setSelected(@Nullable net.minecraft.client.gui.screens.CreateBuffetWorldScreen.BiomeList.Entry entry) { super.setSelected(entry); if (entry != null) { CreateBuffetWorldScreen.this.biome = entry.biome; } CreateBuffetWorldScreen.this.updateButtonValidity(); } } }