package net.minecraft.client.gui.screens; import java.util.List; import java.util.function.Consumer; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.ChatFormatting; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.components.Button; import net.minecraft.client.gui.components.ObjectSelectionList; import net.minecraft.client.gui.layouts.HeaderAndFooterLayout; import net.minecraft.client.gui.layouts.LinearLayout; import net.minecraft.client.gui.screens.CreateFlatWorldScreen.DetailsList.Entry; import net.minecraft.client.gui.screens.worldselection.CreateWorldScreen; import net.minecraft.network.chat.CommonComponents; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.level.levelgen.flat.FlatLayerInfo; import net.minecraft.world.level.levelgen.flat.FlatLevelGeneratorSettings; import org.jetbrains.annotations.Nullable; @Environment(EnvType.CLIENT) public class CreateFlatWorldScreen extends Screen { private static final Component TITLE = Component.translatable("createWorld.customize.flat.title"); static final ResourceLocation SLOT_SPRITE = ResourceLocation.withDefaultNamespace("container/slot"); private static final int SLOT_BG_SIZE = 18; private static final int SLOT_STAT_HEIGHT = 20; private static final int SLOT_BG_X = 1; private static final int SLOT_BG_Y = 1; private static final int SLOT_FG_X = 2; private static final int SLOT_FG_Y = 2; private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this, 33, 64); protected final CreateWorldScreen parent; private final Consumer applySettings; FlatLevelGeneratorSettings generator; @Nullable private CreateFlatWorldScreen.DetailsList list; /** * The remove layer button */ @Nullable private Button deleteLayerButton; public CreateFlatWorldScreen(CreateWorldScreen parent, Consumer applySettings, FlatLevelGeneratorSettings generator) { super(TITLE); this.parent = parent; this.applySettings = applySettings; this.generator = generator; } public FlatLevelGeneratorSettings settings() { return this.generator; } public void setConfig(FlatLevelGeneratorSettings generator) { this.generator = generator; if (this.list != null) { this.list.resetRows(); this.updateButtonValidity(); } } @Override protected void init() { this.layout.addTitleHeader(this.title, this.font); this.list = this.layout.addToContents(new CreateFlatWorldScreen.DetailsList()); LinearLayout linearLayout = this.layout.addToFooter(LinearLayout.vertical().spacing(4)); linearLayout.defaultCellSetting().alignVerticallyMiddle(); LinearLayout linearLayout2 = linearLayout.addChild(LinearLayout.horizontal().spacing(8)); LinearLayout linearLayout3 = linearLayout.addChild(LinearLayout.horizontal().spacing(8)); this.deleteLayerButton = linearLayout2.addChild(Button.builder(Component.translatable("createWorld.customize.flat.removeLayer"), button -> { if (this.hasValidSelection()) { List list = this.generator.getLayersInfo(); int i = this.list.children().indexOf(this.list.getSelected()); int j = list.size() - i - 1; list.remove(j); this.list.setSelected(list.isEmpty() ? null : (Entry)this.list.children().get(Math.min(i, list.size() - 1))); this.generator.updateLayers(); this.list.resetRows(); this.updateButtonValidity(); } }).build()); linearLayout2.addChild(Button.builder(Component.translatable("createWorld.customize.presets"), button -> { this.minecraft.setScreen(new PresetFlatWorldScreen(this)); this.generator.updateLayers(); this.updateButtonValidity(); }).build()); linearLayout3.addChild(Button.builder(CommonComponents.GUI_DONE, button -> { this.applySettings.accept(this.generator); this.onClose(); this.generator.updateLayers(); }).build()); linearLayout3.addChild(Button.builder(CommonComponents.GUI_CANCEL, button -> { this.onClose(); this.generator.updateLayers(); }).build()); this.generator.updateLayers(); this.updateButtonValidity(); this.layout.visitWidgets(this::addRenderableWidget); this.repositionElements(); } @Override protected void repositionElements() { if (this.list != null) { this.list.updateSize(this.width, this.layout); } this.layout.arrangeElements(); } /** * Would update whether the edit and remove buttons are enabled, but is currently disabled and always disables the buttons (which are invisible anyway). */ void updateButtonValidity() { if (this.deleteLayerButton != null) { this.deleteLayerButton.active = this.hasValidSelection(); } } /** * Returns whether there is a valid layer selection */ private boolean hasValidSelection() { return this.list != null && this.list.getSelected() != null; } @Override public void onClose() { this.minecraft.setScreen(this.parent); } @Environment(EnvType.CLIENT) class DetailsList extends ObjectSelectionList { private static final Component LAYER_MATERIAL_TITLE = Component.translatable("createWorld.customize.flat.tile").withStyle(ChatFormatting.UNDERLINE); private static final Component HEIGHT_TITLE = Component.translatable("createWorld.customize.flat.height").withStyle(ChatFormatting.UNDERLINE); public DetailsList() { super(CreateFlatWorldScreen.this.minecraft, CreateFlatWorldScreen.this.width, CreateFlatWorldScreen.this.height - 103, 43, 24, (int)(9.0 * 1.5)); for (int i = 0; i < CreateFlatWorldScreen.this.generator.getLayersInfo().size(); i++) { this.addEntry(new net.minecraft.client.gui.screens.CreateFlatWorldScreen.DetailsList.Entry(this)); } } public void setSelected(@Nullable net.minecraft.client.gui.screens.CreateFlatWorldScreen.DetailsList.Entry entry) { super.setSelected(entry); CreateFlatWorldScreen.this.updateButtonValidity(); } public void resetRows() { int i = this.children().indexOf(this.getSelected()); this.clearEntries(); for (int j = 0; j < CreateFlatWorldScreen.this.generator.getLayersInfo().size(); j++) { this.addEntry(new net.minecraft.client.gui.screens.CreateFlatWorldScreen.DetailsList.Entry(this)); } List list = this.children(); if (i >= 0 && i < list.size()) { this.setSelected((net.minecraft.client.gui.screens.CreateFlatWorldScreen.DetailsList.Entry)list.get(i)); } } @Override protected void renderHeader(GuiGraphics guiGraphics, int x, int y) { guiGraphics.drawString(CreateFlatWorldScreen.this.font, LAYER_MATERIAL_TITLE, x, y, -1); guiGraphics.drawString( CreateFlatWorldScreen.this.font, HEIGHT_TITLE, x + this.getRowWidth() - CreateFlatWorldScreen.this.font.width(HEIGHT_TITLE) - 8, y, -1 ); } } }