Skip to content

Commit

Permalink
Use custom ExperimentsScreen when modded flags are present
Browse files Browse the repository at this point in the history
  • Loading branch information
XFactHD committed Sep 19, 2024
1 parent 96f3f92 commit 7a41315
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--- a/net/minecraft/client/gui/screens/worldselection/ExperimentsScreen.java
+++ b/net/minecraft/client/gui/screens/worldselection/ExperimentsScreen.java
@@ -50,6 +_,11 @@

@Override
protected void init() {
+ if (net.minecraft.world.flag.FeatureFlags.REGISTRY.hasAnyModdedFlags()) {
+ this.minecraft.setScreen(new net.neoforged.neoforge.client.gui.ScrollableExperimentsScreen(this.parent, this.packRepository, this.output));
+ return;
+ }
+
this.layout.addTitleHeader(TITLE, this.font);
LinearLayout linearlayout = this.layout.addToContents(LinearLayout.vertical());
linearlayout.addChild(new MultiLineTextWidget(INFO, this.font).setMaxWidth(310), p_293611_ -> p_293611_.paddingBottom(15));
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--- a/net/minecraft/world/flag/FeatureFlagRegistry.java
+++ b/net/minecraft/world/flag/FeatureFlagRegistry.java
@@ -75,6 +_,14 @@
@@ -75,6 +_,18 @@
}, p_249796_ -> List.copyOf(this.toNames(p_249796_)));
}

Expand All @@ -11,6 +11,10 @@
+ public Map<ResourceLocation, FeatureFlag> getAllFlags() {
+ return this.names;
+ }
+
+ public boolean hasAnyModdedFlags() {
+ return this.names.values().stream().anyMatch(FeatureFlag::isModded);
+ }
+
public static class Builder {
private final FeatureFlagUniverse universe;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package net.neoforged.neoforge.client.gui;

import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.ContainerObjectSelectionList;
import net.minecraft.client.gui.components.CycleButton;
import net.minecraft.client.gui.components.MultiLineTextWidget;
import net.minecraft.client.gui.components.StringWidget;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.layouts.*;
import net.minecraft.client.gui.narration.NarratableEntry;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.worldselection.ExperimentsScreen;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.server.packs.repository.PackRepository;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;

@ApiStatus.Internal
public class ScrollableExperimentsScreen extends ExperimentsScreen {
private static final int DEFAULT_LIST_HEIGHT = 121;
private static final int ROW_PADDING = 4;
private static final int LIST_PADDING = ROW_PADDING + 40;
private static final int ENTRY_HEIGHT = 42;

@Nullable
private ExperimentSelectionList selectionList;
@Nullable
private LinearLayout listLayout;

public ScrollableExperimentsScreen(Screen parent, PackRepository packRepository, Consumer<PackRepository> output) {
super(parent, packRepository, output);
}

@Override
protected void init() {
this.layout.addTitleHeader(TITLE, this.font);

LinearLayout contentLayout = this.layout.addToContents(LinearLayout.vertical(), LayoutSettings::alignVerticallyTop);
contentLayout.addChild(
new MultiLineTextWidget(INFO, this.font).setMaxWidth(MAIN_CONTENT_WIDTH),
settings -> settings.paddingBottom(15).alignHorizontallyCenter());

this.listLayout = contentLayout.addChild(LinearLayout.vertical());
this.selectionList = new ExperimentSelectionList(this.minecraft);
this.packs.forEach((pack, selected) -> {
selectionList.addEntry(new ExperimentSelectionList.ExperimentEntry(
getHumanReadableTitle(pack),
() -> this.packs.getBoolean(pack),
flag -> this.packs.put(pack, flag.booleanValue()),
pack.getDescription()
));
});
this.listLayout.addChild(selectionList);

LinearLayout footerLayout = this.layout.addToFooter(LinearLayout.horizontal().spacing(8));
footerLayout.addChild(Button.builder(CommonComponents.GUI_DONE, btn -> this.onDone()).build());
footerLayout.addChild(Button.builder(CommonComponents.GUI_CANCEL, btn -> this.onClose()).build());

this.layout.visitWidgets(this::addRenderableWidget);
this.repositionElements();
}

@Override
protected void repositionElements() {
if (this.selectionList != null) {
// Reset list height to empirical default because layouts can't squish elements to fit...
this.selectionList.setHeight(DEFAULT_LIST_HEIGHT);
}
super.repositionElements();
if (this.selectionList != null && this.listLayout != null) {
this.selectionList.setHeight(this.layout.getContentHeight() - this.listLayout.getY());
this.selectionList.setPosition(this.listLayout.getX(), this.listLayout.getY());
this.selectionList.clampScrollAmount();
}
}

private static class ExperimentSelectionList extends ContainerObjectSelectionList<ExperimentSelectionList.ExperimentEntry> {
public ExperimentSelectionList(Minecraft mc) {
super(mc, ExperimentsScreen.MAIN_CONTENT_WIDTH + LIST_PADDING, DEFAULT_LIST_HEIGHT, 0, ENTRY_HEIGHT);
}

@Override
public int getRowWidth() {
return ExperimentsScreen.MAIN_CONTENT_WIDTH + ROW_PADDING;
}

@Override
protected int addEntry(ExperimentEntry entry) {
return super.addEntry(entry);
}

private static class ExperimentEntry extends ContainerObjectSelectionList.Entry<ExperimentEntry> {
private static final int BUTTON_WIDTH = 44;
private static final int TITLE_Y = 6;
private static final int DESCRIPTION_Y = 20;

private final StringWidget titleWidget;
private final MultiLineTextWidget descriptionWidget;
private final CycleButton<Boolean> button;
private final List<AbstractWidget> children;

public ExperimentEntry(Component title, BooleanSupplier selectedSupplier, Consumer<Boolean> selectedSetter, Component description) {
this.titleWidget = new StringWidget(title, Minecraft.getInstance().font).alignLeft();
this.descriptionWidget = new MultiLineTextWidget(description.copy().withStyle(ChatFormatting.GRAY), Minecraft.getInstance().font)
.setMaxRows(2);
this.button = CycleButton.onOffBuilder(selectedSupplier.getAsBoolean())
.displayOnlyValue()
.withCustomNarration(btn -> CommonComponents.joinForNarration(title, btn.createDefaultNarrationMessage(), description))
.create(0, 0, BUTTON_WIDTH, Button.DEFAULT_HEIGHT, Component.empty(), (btn, val) -> selectedSetter.accept(val));
this.children = List.of(titleWidget, descriptionWidget, this.button);
}

@Override
public void render(GuiGraphics graphics, int entryIdx, int top, int left, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float partialTick) {
this.titleWidget.setPosition(left, top + TITLE_Y);
this.descriptionWidget.setPosition(left, top + DESCRIPTION_Y);
this.descriptionWidget.setMaxWidth(entryWidth - this.button.getWidth());
this.button.setPosition(left + entryWidth - this.button.getWidth() - ROW_PADDING, top);

this.titleWidget.render(graphics, mouseX, mouseY, partialTick);
this.descriptionWidget.render(graphics, mouseX, mouseY, partialTick);
this.button.render(graphics, mouseX, mouseY, partialTick);
}

@Override
public List<? extends GuiEventListener> children() {
return this.children;
}

@Override
public List<? extends NarratableEntry> narratables() {
return this.children;
}
}
}
}
7 changes: 7 additions & 0 deletions src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ public net.minecraft.client.gui.components.AbstractWidget renderScrollingString(
public net.minecraft.client.gui.components.AbstractWidget renderScrollingString(Lnet/minecraft/client/gui/GuiGraphics;Lnet/minecraft/client/gui/Font;Lnet/minecraft/network/chat/Component;IIIIII)V
public net.minecraft.client.gui.screens.MenuScreens$ScreenConstructor
public net.minecraft.client.gui.screens.Screen renderables # renderables
protected net.minecraft.client.gui.screens.worldselection.ExperimentsScreen getHumanReadableTitle(Lnet/minecraft/server/packs/repository/Pack;)Lnet/minecraft/network/chat/Component;
protected net.minecraft.client.gui.screens.worldselection.ExperimentsScreen INFO
protected net.minecraft.client.gui.screens.worldselection.ExperimentsScreen layout
protected net.minecraft.client.gui.screens.worldselection.ExperimentsScreen onDone()V
protected net.minecraft.client.gui.screens.worldselection.ExperimentsScreen MAIN_CONTENT_WIDTH
protected net.minecraft.client.gui.screens.worldselection.ExperimentsScreen packs
protected net.minecraft.client.gui.screens.worldselection.ExperimentsScreen TITLE
public net.minecraft.client.model.geom.LayerDefinitions OUTER_ARMOR_DEFORMATION # OUTER_ARMOR_DEFORMATION
public net.minecraft.client.model.geom.LayerDefinitions INNER_ARMOR_DEFORMATION # INNER_ARMOR_DEFORMATION
public net.minecraft.client.multiplayer.ClientPacketListener commands # commands
Expand Down

0 comments on commit 7a41315

Please sign in to comment.