| 1 | package net.minecraft.client.gui.screens.worldselection;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import it.unimi.dsi.fastutil.objects.Object2BooleanLinkedOpenHashMap;
|
| 5 | import it.unimi.dsi.fastutil.objects.Object2BooleanMap;
|
| 6 | import java.util.ArrayList;
|
| 7 | import java.util.List;
|
| 8 | import java.util.function.Consumer;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 | import net.minecraft.ChatFormatting;
|
| 12 | import net.minecraft.client.gui.components.Button;
|
| 13 | import net.minecraft.client.gui.components.MultiLineTextWidget;
|
| 14 | import net.minecraft.client.gui.components.ScrollableLayout;
|
| 15 | import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
|
| 16 | import net.minecraft.client.gui.layouts.Layout;
|
| 17 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 18 | import net.minecraft.client.gui.screens.Screen;
|
| 19 | import net.minecraft.locale.Language;
|
| 20 | import net.minecraft.network.chat.CommonComponents;
|
| 21 | import net.minecraft.network.chat.Component;
|
| 22 | import net.minecraft.server.packs.repository.Pack;
|
| 23 | import net.minecraft.server.packs.repository.PackRepository;
|
| 24 | import net.minecraft.server.packs.repository.PackSource;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 |
|
| 27 | @Environment(EnvType.CLIENT)
|
| 28 | public class ExperimentsScreen extends Screen {
|
| 29 | private static final Component TITLE = Component.translatable("selectWorld.experiments");
|
| 30 | private static final Component INFO = Component.translatable("selectWorld.experiments.info").withStyle(ChatFormatting.RED);
|
| 31 | private static final int MAIN_CONTENT_WIDTH = 310;
|
| 32 | private static final int SCROLL_AREA_MIN_HEIGHT = 130;
|
| 33 | private final HeaderAndFooterLayout layout = new HeaderAndFooterLayout(this);
|
| 34 | private final Screen parent;
|
| 35 | private final PackRepository packRepository;
|
| 36 | private final Consumer<PackRepository> output;
|
| 37 | private final Object2BooleanMap<Pack> packs = new Object2BooleanLinkedOpenHashMap<>();
|
| 38 | private @Nullable ScrollableLayout scrollArea;
|
| 39 |
|
| 40 | public ExperimentsScreen(final Screen parent, final PackRepository packRepository, final Consumer<PackRepository> output) {
|
| 41 | super(TITLE);
|
| 42 | this.parent = parent;
|
| 43 | this.packRepository = packRepository;
|
| 44 | this.output = output;
|
| 45 |
|
| 46 | for (Pack pack : packRepository.getAvailablePacks()) {
|
| 47 | if (pack.getPackSource() == PackSource.FEATURE) {
|
| 48 | this.packs.put(pack, packRepository.getSelectedPacks().contains(pack));
|
| 49 | }
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | protected void init() {
|
| 55 | this.layout.addTitleHeader(TITLE, this.font);
|
| 56 | LinearLayout content = this.layout.addToContents(LinearLayout.vertical());
|
| 57 | content.addChild(new MultiLineTextWidget(INFO, this.font).setMaxWidth(310), s -> s.paddingBottom(15));
|
| 58 | SwitchGrid.Builder switchGridBuilder = SwitchGrid.builder(299).withInfoUnderneath(2, true).withRowSpacing(4);
|
| 59 | this.packs
|
| 60 | .forEach(
|
| 61 | (pack, selected) -> switchGridBuilder.addSwitch(
|
| 62 | getHumanReadableTitle(pack), () -> this.packs.getBoolean(pack), newSelected -> this.packs.put(pack, newSelected.booleanValue())
|
| 63 | )
|
| 64 | .withInfo(pack.getDescription())
|
| 65 | );
|
| 66 | Layout switchGridLayout = switchGridBuilder.build().layout();
|
| 67 | this.scrollArea = new ScrollableLayout(this.minecraft, switchGridLayout, 130);
|
| 68 | this.scrollArea.setMinWidth(310);
|
| 69 | content.addChild(this.scrollArea);
|
| 70 | LinearLayout footer = this.layout.addToFooter(LinearLayout.horizontal().spacing(8));
|
| 71 | footer.addChild(Button.builder(CommonComponents.GUI_DONE, button -> this.onDone()).build());
|
| 72 | footer.addChild(Button.builder(CommonComponents.GUI_CANCEL, button -> this.onClose()).build());
|
| 73 | this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
|
| 74 | this.repositionElements();
|
| 75 | }
|
| 76 |
|
| 77 | private static Component getHumanReadableTitle(final Pack pack) {
|
| 78 | String translationKey = "dataPack." + pack.getId() + ".name";
|
| 79 | return Language.getInstance().has(translationKey) ? Component.translatable(translationKey) : pack.getTitle();
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | protected void repositionElements() {
|
| 84 | this.scrollArea.setMaxHeight(130);
|
| 85 | this.layout.arrangeElements();
|
| 86 | int availableExtraHeight = this.height - this.layout.getFooterHeight() - this.scrollArea.getRectangle().bottom();
|
| 87 | this.scrollArea.setMaxHeight(this.scrollArea.getHeight() + availableExtraHeight);
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | public Component getNarrationMessage() {
|
| 92 | return CommonComponents.joinForNarration(super.getNarrationMessage(), INFO);
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | public void onClose() {
|
| 97 | this.minecraft.gui.setScreen(this.parent);
|
| 98 | }
|
| 99 |
|
| 100 | private void onDone() {
|
| 101 | List<Pack> selectedPacks = new ArrayList<>(this.packRepository.getSelectedPacks());
|
| 102 | List<Pack> selectedFeatures = new ArrayList<>();
|
| 103 | this.packs.forEach((pack, selected) -> {
|
| 104 | selectedPacks.remove(pack);
|
| 105 | if (selected) {
|
| 106 | selectedFeatures.add(pack);
|
| 107 | }
|
| 108 | });
|
| 109 | selectedPacks.addAll(Lists.reverse(selectedFeatures));
|
| 110 | this.packRepository.setSelected(selectedPacks.stream().map(Pack::getId).toList());
|
| 111 | this.output.accept(this.packRepository);
|
| 112 | }
|
| 113 | }
|
| 114 |
|