| 1 | package net.minecraft.client.gui.screens;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.function.Consumer;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.ChatFormatting;
|
| 8 | import net.minecraft.client.gui.components.Button;
|
| 9 | import net.minecraft.client.gui.components.ObjectSelectionList;
|
| 10 | import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
|
| 11 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 12 | import net.minecraft.client.gui.screens.CreateFlatWorldScreen.DetailsList.HeaderEntry;
|
| 13 | import net.minecraft.client.gui.screens.CreateFlatWorldScreen.DetailsList.LayerEntry;
|
| 14 | import net.minecraft.client.gui.screens.worldselection.CreateWorldScreen;
|
| 15 | import net.minecraft.network.chat.CommonComponents;
|
| 16 | import net.minecraft.network.chat.Component;
|
| 17 | import net.minecraft.resources.Identifier;
|
| 18 | import net.minecraft.world.level.levelgen.flat.FlatLayerInfo;
|
| 19 | import net.minecraft.world.level.levelgen.flat.FlatLevelGeneratorSettings;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 |
|
| 22 | @Environment(EnvType.CLIENT)
|
| 23 | public class CreateFlatWorldScreen extends Screen {
|
| 24 | private static final Component TITLE = Component.translatable("createWorld.customize.flat.title");
|
| 25 | private static final Identifier SLOT_SPRITE = Identifier.withDefaultNamespace("container/slot");
|
| 26 | private static final int SLOT_BG_SIZE = 18;
|
| 27 | private static final int SLOT_STAT_HEIGHT = 20;
|
| 28 | private static final int SLOT_BG_X = 1;
|
| 29 | private static final int SLOT_BG_Y = 1;
|
| 30 | private static final int SLOT_FG_X = 2;
|
| 31 | private static final int SLOT_FG_Y = 2;
|
| 32 | private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this, 33, 64);
|
| 33 | protected final CreateWorldScreen parent;
|
| 34 | private final Consumer<FlatLevelGeneratorSettings> applySettings;
|
| 35 | private FlatLevelGeneratorSettings generator;
|
| 36 | private CreateFlatWorldScreen.@Nullable DetailsList list;
|
| 37 | private @Nullable Button deleteLayerButton;
|
| 38 |
|
| 39 | public CreateFlatWorldScreen(
|
| 40 | final CreateWorldScreen parent, final Consumer<FlatLevelGeneratorSettings> applySettings, final FlatLevelGeneratorSettings generator
|
| 41 | ) {
|
| 42 | super(TITLE);
|
| 43 | this.parent = parent;
|
| 44 | this.applySettings = applySettings;
|
| 45 | this.generator = generator;
|
| 46 | }
|
| 47 |
|
| 48 | public FlatLevelGeneratorSettings settings() {
|
| 49 | return this.generator;
|
| 50 | }
|
| 51 |
|
| 52 | public void setConfig(final FlatLevelGeneratorSettings generator) {
|
| 53 | this.generator = generator;
|
| 54 | if (this.list != null) {
|
| 55 | this.list.resetRows();
|
| 56 | this.updateButtonValidity();
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | protected void init() {
|
| 62 | this.layout.addTitleHeader(this.title, this.font);
|
| 63 | this.list = this.layout.addToContents(new CreateFlatWorldScreen.DetailsList());
|
| 64 | LinearLayout footer = this.layout.addToFooter(LinearLayout.vertical().spacing(4));
|
| 65 | footer.defaultCellSetting().alignVerticallyMiddle();
|
| 66 | LinearLayout topFooterButtons = footer.addChild(LinearLayout.horizontal().spacing(8));
|
| 67 | LinearLayout bottomFooterButtons = footer.addChild(LinearLayout.horizontal().spacing(8));
|
| 68 | this.deleteLayerButton = topFooterButtons.addChild(Button.builder(Component.translatable("createWorld.customize.flat.removeLayer"), button -> {
|
| 69 | if (this.list != null && this.list.getSelected() instanceof LayerEntry selectedLayerEntry) {
|
| 70 | this.list.deleteLayer(selectedLayerEntry);
|
| 71 | }
|
| 72 | }).build());
|
| 73 | topFooterButtons.addChild(Button.builder(Component.translatable("createWorld.customize.presets"), button -> {
|
| 74 | this.minecraft.gui.setScreen(new PresetFlatWorldScreen(this));
|
| 75 | this.generator.updateLayers();
|
| 76 | this.updateButtonValidity();
|
| 77 | }).build());
|
| 78 | bottomFooterButtons.addChild(Button.builder(CommonComponents.GUI_DONE, button -> {
|
| 79 | this.applySettings.accept(this.generator);
|
| 80 | this.onClose();
|
| 81 | this.generator.updateLayers();
|
| 82 | }).build());
|
| 83 | bottomFooterButtons.addChild(Button.builder(CommonComponents.GUI_CANCEL, button -> {
|
| 84 | this.onClose();
|
| 85 | this.generator.updateLayers();
|
| 86 | }).build());
|
| 87 | this.generator.updateLayers();
|
| 88 | this.updateButtonValidity();
|
| 89 | this.layout.visitWidgets(this::addRenderableWidget);
|
| 90 | this.repositionElements();
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | protected void repositionElements() {
|
| 95 | if (this.list != null) {
|
| 96 | this.list.updateSize(this.width, this.layout);
|
| 97 | }
|
| 98 |
|
| 99 | this.layout.arrangeElements();
|
| 100 | }
|
| 101 |
|
| 102 | private void updateButtonValidity() {
|
| 103 | if (this.deleteLayerButton != null) {
|
| 104 | this.deleteLayerButton.active = this.hasValidSelection();
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | private boolean hasValidSelection() {
|
| 109 | return this.list != null && this.list.getSelected() instanceof LayerEntry;
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | public void onClose() {
|
| 114 | this.minecraft.gui.setScreen(this.parent);
|
| 115 | }
|
| 116 |
|
| 117 | @Environment(EnvType.CLIENT)
|
| 118 | private class DetailsList extends ObjectSelectionList<net.minecraft.client.gui.screens.CreateFlatWorldScreen.DetailsList.Entry> {
|
| 119 | private static final Component LAYER_MATERIAL_TITLE = Component.translatable("createWorld.customize.flat.tile").withStyle(ChatFormatting.UNDERLINE);
|
| 120 | private static final Component HEIGHT_TITLE = Component.translatable("createWorld.customize.flat.height").withStyle(ChatFormatting.UNDERLINE);
|
| 121 |
|
| 122 | public DetailsList() {
|
| 123 | super(CreateFlatWorldScreen.this.minecraft, CreateFlatWorldScreen.this.width, CreateFlatWorldScreen.this.height - 103, 43, 24);
|
| 124 | this.populateList();
|
| 125 | }
|
| 126 |
|
| 127 | private void populateList() {
|
| 128 | this.addEntry(new HeaderEntry(CreateFlatWorldScreen.this.font), (int)(9.0 * 1.5));
|
| 129 | List<FlatLayerInfo> layersInfo = CreateFlatWorldScreen.this.generator.getLayersInfo().reversed();
|
| 130 |
|
| 131 | for (int i = 0; i < layersInfo.size(); i++) {
|
| 132 | this.addEntry(new LayerEntry(this, layersInfo.get(i), i));
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | public void setSelected(final @Nullable Entry selected) {
|
| 137 | super.setSelected(selected);
|
| 138 | CreateFlatWorldScreen.this.updateButtonValidity();
|
| 139 | }
|
| 140 |
|
| 141 | public void resetRows() {
|
| 142 | int index = this.children().indexOf(this.getSelected());
|
| 143 | this.clearEntries();
|
| 144 | this.populateList();
|
| 145 | List<net.minecraft.client.gui.screens.CreateFlatWorldScreen.DetailsList.Entry> children = this.children();
|
| 146 | if (index >= 0 && index < children.size()) {
|
| 147 | this.setSelected(children.get(index));
|
| 148 | }
|
| 149 | }
|
| 150 |
|
| 151 | private void deleteLayer(final LayerEntry selectedLayerEntry) {
|
| 152 | List<FlatLayerInfo> layersInfo = CreateFlatWorldScreen.this.generator.getLayersInfo();
|
| 153 | int deletedLayerIndex = this.children().indexOf(selectedLayerEntry);
|
| 154 | this.removeEntry(selectedLayerEntry);
|
| 155 | layersInfo.remove(selectedLayerEntry.layerInfo);
|
| 156 | this.setSelected(layersInfo.isEmpty() ? null : this.children().get(Math.min(deletedLayerIndex, layersInfo.size())));
|
| 157 | CreateFlatWorldScreen.this.generator.updateLayers();
|
| 158 | this.resetRows();
|
| 159 | CreateFlatWorldScreen.this.updateButtonValidity();
|
| 160 | }
|
| 161 | }
|
| 162 | }
|
| 163 |
|