This commit is contained in:
Rhys⁣⁣⁣⁣⁣⁣⁣ de Haan 2023-07-18 15:35:06 +00:00 committed by GitHub
commit 1941630f08
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 27 deletions

View file

@ -22,6 +22,8 @@ public abstract class ConfigBase {
protected List<ConfigBase> children;
public void registerAll(final ForgeConfigSpec.Builder builder) {
if(allValues == null) // avoid null pointer exception if config class is empty
allValues = new ArrayList<>();
for (CValue<?, ?> cValue : allValues)
cValue.register(builder);
}

View file

@ -3,11 +3,14 @@ package com.simibubi.create.foundation.config.ui;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.function.UnaryOperator;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.simibubi.create.foundation.utility.Pair;
import org.lwjgl.glfw.GLFW;
import com.mojang.blaze3d.vertex.PoseStack;
@ -34,30 +37,43 @@ public class BaseConfigScreen extends ConfigScreen {
private static final Map<String, UnaryOperator<BaseConfigScreen>> DEFAULTS = new HashMap<>();
static {
DEFAULTS.put(Create.ID, (base) -> base
.withTitles("Client Settings", "World Generation Settings", "Gameplay Settings")
setDefaultActionFor(Create.ID, base -> base
.withLabels("Client Settings", "World Generation Settings", "Gameplay Settings")
.withSpecs(AllConfigs.client().specification, AllConfigs.common().specification, AllConfigs.server().specification)
);
// also set titles for jei and computercraft
setDefaultActionFor("jei", base -> base.withTitle("Just Enough Items"));
setDefaultActionFor("computercraft", base -> base.withTitle("ComputerCraft", false));
}
/**
* If you are a Create Addon dev and want to change the config labels,
* add a default action here.
*
* If you are a Create Addon dev and want to change the config labels or title, add a default action here.
* <p>
* Make sure you call either {@link #withSpecs(ForgeConfigSpec, ForgeConfigSpec, ForgeConfigSpec)}
* or {@link #searchForSpecsInModContainer()}
*
* @param modID the modID of your addon/mod
*/
public static void setDefaultActionFor(String modID, UnaryOperator<BaseConfigScreen> transform) {
if (modID.equals(Create.ID))
if (DEFAULTS.containsKey(modID)) {
Create.LOGGER.error("Somebody tried to set default action for mod {}, but it was already set!", modID);
return;
// or throw an exception
}
DEFAULTS.put(modID, transform);
}
public static Optional<String> getCustomTitle(String modID) {
return DEFAULTS.entrySet()
.stream()
.filter(entry -> entry.getKey().equals(modID))
.map(entry -> entry.getValue().apply(new BaseConfigScreen(null, modID)).displayTitle.getFirst())
.filter(s -> !s.equals(modID))
.findFirst();
}
public static BaseConfigScreen forCreate(Screen parent) {
return new BaseConfigScreen(parent);
return new BaseConfigScreen(parent, Create.ID);
}
BoxWidget clientConfigWidget;
@ -70,25 +86,22 @@ public class BaseConfigScreen extends ConfigScreen {
ForgeConfigSpec clientSpec;
ForgeConfigSpec commonSpec;
ForgeConfigSpec serverSpec;
String clientTitle = "Client Config";
String commonTitle = "Common Config";
String serverTitle = "Server Config";
String clientLabel = "Client Config";
String commonLabel = "Common Config";
String serverLabel = "Server Config";
String modID;
Pair<String, Boolean> displayTitle;
protected boolean returnOnClose;
public BaseConfigScreen(Screen parent, @Nonnull String modID) {
super(parent);
this.modID = modID;
this.displayTitle = Pair.of(modID, true);
if (DEFAULTS.containsKey(modID))
DEFAULTS.get(modID).apply(this);
else {
this.searchForSpecsInModContainer();
}
}
private BaseConfigScreen(Screen parent) {
this(parent, Create.ID);
else this.searchForSpecsInModContainer();
}
/**
@ -128,15 +141,25 @@ public class BaseConfigScreen extends ConfigScreen {
return this;
}
public BaseConfigScreen withTitles(@Nullable String client, @Nullable String common, @Nullable String server) {
public BaseConfigScreen withTitle(String title, boolean uppercase) {
if(title != null && !title.isEmpty())
displayTitle = Pair.of(title, uppercase);
return this;
}
public BaseConfigScreen withTitle(String title) {
return withTitle(title, true);
}
public BaseConfigScreen withLabels(@Nullable String client, @Nullable String common, @Nullable String server) {
if (client != null)
clientTitle = client;
clientLabel = client;
if (common != null)
commonTitle = common;
commonLabel = common;
if (server != null)
serverTitle = server;
serverLabel = server;
return this;
}
@ -146,7 +169,7 @@ public class BaseConfigScreen extends ConfigScreen {
super.init();
returnOnClose = true;
TextStencilElement clientText = new TextStencilElement(font, Components.literal(clientTitle)).centered(true, true);
TextStencilElement clientText = new TextStencilElement(font, Components.literal(clientLabel)).centered(true, true);
addRenderableWidget(clientConfigWidget = new BoxWidget(width / 2 - 100, height / 2 - 15 - 30, 200, 16).showingElement(clientText));
if (clientSpec != null) {
@ -158,7 +181,7 @@ public class BaseConfigScreen extends ConfigScreen {
clientText.withElementRenderer(DISABLED_RENDERER);
}
TextStencilElement commonText = new TextStencilElement(font, Components.literal(commonTitle)).centered(true, true);
TextStencilElement commonText = new TextStencilElement(font, Components.literal(commonLabel)).centered(true, true);
addRenderableWidget(commonConfigWidget = new BoxWidget(width / 2 - 100, height / 2 - 15, 200, 16).showingElement(commonText));
if (commonSpec != null) {
@ -170,7 +193,7 @@ public class BaseConfigScreen extends ConfigScreen {
commonText.withElementRenderer(DISABLED_RENDERER);
}
TextStencilElement serverText = new TextStencilElement(font, Components.literal(serverTitle)).centered(true, true);
TextStencilElement serverText = new TextStencilElement(font, Components.literal(serverLabel)).centered(true, true);
addRenderableWidget(serverConfigWidget = new BoxWidget(width / 2 - 100, height / 2 - 15 + 30, 200, 16).showingElement(serverText));
if (serverSpec == null) {
@ -191,7 +214,11 @@ public class BaseConfigScreen extends ConfigScreen {
serverText.withElementRenderer(BoxWidget.gradientFactory.apply(serverConfigWidget));
}
TextStencilElement titleText = new TextStencilElement(font, modID.toUpperCase(Locale.ROOT))
String titleToDisplay = displayTitle.getFirst();
if(displayTitle.getSecond())
titleToDisplay = titleToDisplay.toUpperCase(Locale.ROOT);
TextStencilElement titleText = new TextStencilElement(font, titleToDisplay)
.centered(true, true)
.withElementRenderer((ms, w, h, alpha) -> {
UIRenderHelper.angledGradient(ms, 0, 0, h / 2, h, w / 2, Theme.p(Theme.Key.CONFIG_TITLE_A));

View file

@ -99,7 +99,7 @@ public class ConfigModListScreen extends ConfigScreen {
protected String id;
public ModEntry(String id, Screen parent) {
super(toHumanReadable(id));
super(BaseConfigScreen.getCustomTitle(id).orElse(toHumanReadable(id)));
this.id = id;
button = new BoxWidget(0, 0, 35, 16)
@ -112,7 +112,7 @@ public class ConfigModListScreen extends ConfigScreen {
button.active = false;
button.updateColorsFromState();
button.modifyElement(e -> ((DelegatedStencilElement) e).withElementRenderer(BaseConfigScreen.DISABLED_RENDERER));
labelTooltip.add(Components.literal(toHumanReadable(id)));
labelTooltip.add(Components.literal(BaseConfigScreen.getCustomTitle(id).orElse(toHumanReadable(id))));
labelTooltip.addAll(TooltipHelper.cutStringTextComponent("This Mod does not have any configs registered or is not using Forge's config system", Palette.ALL_GRAY));
}