Config Optimizations

This commit is contained in:
Frank 2022-07-22 01:23:30 +02:00
parent 9cb54346a2
commit a7efcd25ba
15 changed files with 161 additions and 77 deletions

View file

@ -80,9 +80,8 @@ public class BCLib implements ModInitializer {
Configs.save();
WorldsTogether.FORCE_SERVER_TO_BETTERX_PRESET = Configs.SERVER_CONFIG.forceBetterXPreset();
if (isClient()) {
VersionChecker.registerMod(MOD_ID);
}
VersionChecker.registerMod(MOD_ID);
if (false && isDevEnvironment()) {
BCLBiome theYellow = BCLBiomeBuilder

View file

@ -6,10 +6,7 @@ import org.betterx.bclib.config.Configs;
import org.betterx.bclib.config.NamedPathConfig;
import org.betterx.bclib.config.NamedPathConfig.ConfigTokenDescription;
import org.betterx.bclib.config.NamedPathConfig.DependendConfigToken;
import org.betterx.ui.layout.components.Checkbox;
import org.betterx.ui.layout.components.HorizontalStack;
import org.betterx.ui.layout.components.LayoutComponent;
import org.betterx.ui.layout.components.VerticalStack;
import org.betterx.ui.layout.components.*;
import org.betterx.ui.vanilla.LayoutScreenWithIcon;
import net.minecraft.client.gui.screens.Screen;
@ -43,11 +40,33 @@ public class MainScreen extends LayoutScreenWithIcon {
protected <T> void addRow(VerticalStack grid, NamedPathConfig config, ConfigTokenDescription<T> option) {
if (ConfigKeeper.BooleanEntry.class.isAssignableFrom(option.token.type)) {
addCheckbox(grid, config, (ConfigTokenDescription<Boolean>) option);
} else if (ConfigKeeper.FloatEntry.class.isAssignableFrom(option.token.type)) {
addFloat(grid, config, (ConfigTokenDescription<Float>) option);
}
grid.addSpacer(2);
}
protected void addFloat(VerticalStack grid, NamedPathConfig config, ConfigTokenDescription<Float> option) {
if (option.topPadding > 0) {
grid.addSpacer(option.topPadding);
}
HorizontalStack row = grid.addRow();
if (option.leftPadding > 0) {
row.addSpacer(option.leftPadding);
}
Range<Float> cb = row.addRange(
fixed(200), fit(),
getComponent(config, option, "title"),
option.minRange,
option.maxRange,
config.getRaw(option.token)
).onChange(
(caller, state) -> {
config.set(option.token, state);
}
);
}
protected void addCheckbox(VerticalStack grid, NamedPathConfig config, ConfigTokenDescription<Boolean> option) {
if (option.topPadding > 0) {

View file

@ -2,6 +2,7 @@ package org.betterx.bclib.client.gui.screens;
import org.betterx.ui.layout.components.LayoutComponent;
import org.betterx.ui.layout.components.VerticalStack;
import org.betterx.ui.layout.values.Value;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
@ -34,7 +35,7 @@ public class ConfirmRestartScreen extends BCLibLayoutScreen {
protected LayoutComponent<?, ?> initContent() {
VerticalStack grid = new VerticalStack(fill(), fill());
grid.addFiller();
grid.addMultilineText(fill(), fit(), this.description).centerHorizontal();
grid.addMultilineText(Value.relative(0.9), fit(), this.description).centerHorizontal();
grid.addSpacer(10);
grid.addButton(fit(), fit(), CommonComponents.GUI_PROCEED)
.onPress((button) -> listener.proceed())

View file

@ -80,14 +80,13 @@ public class UpdatesScreen extends BCLibLayoutScreen {
footer.addFiller();
footer.addCheckbox(
fit(),
fit(),
fit(), fit(),
Component.translatable("Disable Check"),
!Configs.MAIN_CONFIG.checkVersions()
!Configs.CLIENT_CONFIG.checkVersions()
)
.onChange((cb, state) -> {
Configs.MAIN_CONFIG.setCheckVersions(!state);
Configs.MAIN_CONFIG.saveChanges();
Configs.CLIENT_CONFIG.setCheckVersions(!state);
Configs.CLIENT_CONFIG.saveChanges();
});
footer.addSpacer(4);
footer.addButton(fit(), fit(), CommonComponents.GUI_DONE).onPress((bt -> {

View file

@ -1,9 +1,12 @@
package org.betterx.bclib.client.gui.screens;
import org.betterx.bclib.config.Configs;
import org.betterx.bclib.networking.VersionChecker;
import org.betterx.ui.ColorUtil;
import org.betterx.ui.layout.components.*;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.gui.GuiComponent;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
@ -37,19 +40,35 @@ public class WelcomeScreen extends BCLibLayoutScreen {
}
content.addHorizontalSeparator(48);
content.addCheckbox(fit(), fit(), translatable("bclib.welcome.updater.title"), true)
.onChange((cb, state) -> Configs.MAIN_CONFIG.setCheckVersions(state));
Checkbox check = content.addCheckbox(
fit(),
fit(),
translatable("bclib.welcome.updater.title"),
Configs.CLIENT_CONFIG.checkVersions()
)
.onChange((cb, state) -> {
Configs.CLIENT_CONFIG.setCheckVersions(state);
});
content.addSpacer(2);
content.indent(24)
.addMultilineText(fill(), fit(), translatable("bclib.welcome.updater.description"))
.setColor(ColorUtil.GRAY);
HorizontalStack dscBox = content.indent(24);
dscBox.addMultilineText(fill(), fit(), translatable("bclib.welcome.updater.description"))
.setColor(ColorUtil.GRAY);
dscBox.addSpacer(8);
content.addSpacer(16);
content.addButton(fit(), fit(), CommonComponents.GUI_PROCEED).onPress((bt) -> {
Configs.MAIN_CONFIG.setDidShowWelcomeScreen();
Configs.CLIENT_CONFIG.setDidShowWelcomeScreen();
Configs.CLIENT_CONFIG.setCheckVersions(check.isChecked());
Configs.CLIENT_CONFIG.saveChanges();
VersionChecker.startCheck(true);
onClose();
}).alignRight();
return VerticalScroll.create(fill(), fill(), content);
}
@Override
protected void renderBackground(PoseStack poseStack, int i, int j, float f) {
GuiComponent.fill(poseStack, 0, 0, width, height, 0xBD343444);
}
}

View file

@ -4,12 +4,40 @@ import org.betterx.bclib.BCLib;
import org.betterx.bclib.api.v2.dataexchange.handler.autosync.AutoSync;
public class ClientConfig extends NamedPathConfig {
@ConfigUI(hide = true)
public static final ConfigToken<Boolean> DID_SHOW_WELCOME = ConfigToken.Boolean(
false,
"didShowWelcome",
"version"
);
@ConfigUI(topPadding = 12)
public static final ConfigToken<Boolean> CHECK_VERSIONS = ConfigToken.Boolean(
true,
"check",
"version"
);
@ConfigUI(leftPadding = 8)
public static final ConfigToken<Boolean> SHOW_UPDATE_INFO = ConfigToken.Boolean(
true,
"showUpdateInfo",
"ui"
);
public static final ConfigToken<Boolean> SUPPRESS_EXPERIMENTAL_DIALOG = ConfigToken.Boolean(
false,
"suppressExperimentalDialogOnLoad",
"ui"
);
@ConfigUI(hide = true)
public static final ConfigToken<Boolean> NO_DONOR = ConfigToken.Boolean(
false,
"iAmNotTheDonorType",
"ui"
);
@ConfigUI(topPadding = 12)
public static final ConfigToken<Boolean> ENABLED = ConfigToken.Boolean(true, "enabled", AutoSync.SYNC_CATEGORY);
@ -41,48 +69,38 @@ public class ClientConfig extends NamedPathConfig {
true,
"displayModInfo",
AutoSync.SYNC_CATEGORY,
(config) -> config.get(
ENABLED)
(config) -> config.get(ENABLED)
);
@ConfigUI(topPadding = 12)
@ConfigUI(leftPadding = 8)
public static final ConfigToken<Boolean> DEBUG_HASHES = ConfigToken.Boolean(
false,
"debugHashes",
AutoSync.SYNC_CATEGORY
);
@ConfigUI(leftPadding = 8)
@ConfigUI(topPadding = 12)
public static final ConfigToken<Boolean> CUSTOM_FOG_RENDERING = ConfigToken.Boolean(
true,
"customFogRendering",
"rendering"
);
@ConfigUI(leftPadding = 8)
public static final ConfigToken<Boolean> NETHER_THICK_FOG = ConfigToken.Boolean(
public static final ConfigToken<Boolean> NETHER_THICK_FOG = DependendConfigToken.Boolean(
true,
"netherThickFog",
"rendering"
"rendering",
(config) -> config.get(CUSTOM_FOG_RENDERING)
);
public static final ConfigToken<Float> FOG_DENSITY = ConfigToken.Float(
@ConfigUI(leftPadding = 8, minValue = 0, maxValue = 2)
public static final ConfigToken<Float> FOG_DENSITY = DependendConfigToken.Float(
1.0f,
"FogDensity",
"rendering"
"rendering",
(config) -> config.get(CUSTOM_FOG_RENDERING)
);
public static final ConfigToken<Boolean> SHOW_UPDATE_INFO = ConfigToken.Boolean(
true,
"showUpdateInfo",
"ui"
);
@ConfigUI(leftPadding = 8)
public static final ConfigToken<Boolean> NO_DONOR = ConfigToken.Boolean(
false,
"no_donor",
"version"
);
public ClientConfig() {
super(BCLib.MOD_ID, "client", false);
@ -135,4 +153,21 @@ public class ClientConfig extends NamedPathConfig {
public float fogDensity() {
return get(FOG_DENSITY);
}
public boolean checkVersions() {
return get(ClientConfig.CHECK_VERSIONS);
}
public void setCheckVersions(boolean newValue) {
set(ClientConfig.CHECK_VERSIONS, newValue);
}
public boolean didShowWelcomeScreen() {
return get(ClientConfig.DID_SHOW_WELCOME);
}
public void setDidShowWelcomeScreen() {
set(ClientConfig.DID_SHOW_WELCOME, true);
}
}

View file

@ -198,6 +198,7 @@ public abstract class Config {
protected boolean setBoolean(ConfigKey key, boolean value) {
try {
ConfigKeeper.BooleanEntry entry = keeper.getEntry(key, ConfigKeeper.BooleanEntry.class);
if (entry == null) return false;
entry.setValue(value);
return true;

View file

@ -57,11 +57,17 @@ public final class ConfigKeeper {
}
private static Pair<JsonElement, Pair<String, String>> find(JsonObject json, Pair<String, String> key) {
String kk = key.first + key.second;
for (var entry : json.entrySet()) {
final Pair<String, String> otherKey = ConfigKey.realKey(entry.getKey());
if (otherKey.first.equals(key.first)) return new Pair<>(entry.getValue(), otherKey);
if (kk.equals(entry)) return new Pair<>(entry.getValue(), otherKey);
}
// for (var entry : json.entrySet()) {
// final Pair<String, String> otherKey = ConfigKey.realKey(entry.getKey());
// if (otherKey.first.equals(key.first)) return new Pair<>(entry.getValue(), otherKey);
// }
return null;
}

View file

@ -22,4 +22,14 @@ public @interface ConfigUI {
* When a Widget is generated for this option, it will be indented by this Value
*/
int topPadding() default 0;
/**
* When a Slider is generated, this will be the minimum Value
*/
int minValue() default 0;
/**
* When a Slider is generated, this will be the maximu Value
*/
int maxValue() default 0;
}

View file

@ -19,21 +19,6 @@ public class MainConfig extends NamedPathConfig {
);
@ConfigUI(hide = true)
public static final ConfigToken<Boolean> DID_SHOW_WELCOME = ConfigToken.Boolean(
false,
"did_show_welcome",
"version"
);
public static final ConfigToken<Boolean> CHECK_VERSIONS = DependendConfigToken.Boolean(
true,
"check",
"version",
(config) -> !config.get(DID_SHOW_WELCOME)
);
public MainConfig() {
super(BCLib.MOD_ID, "main", true, true);
}
@ -45,21 +30,4 @@ public class MainConfig extends NamedPathConfig {
public boolean repairBiomes() {
return get(REPAIR_BIOMES);
}
public boolean checkVersions() {
return get(CHECK_VERSIONS);
}
public boolean didShowWelcomeScreen() {
return get(DID_SHOW_WELCOME);
}
public void setDidShowWelcomeScreen() {
set(DID_SHOW_WELCOME, true);
}
public void setCheckVersions(boolean newValue) {
set(CHECK_VERSIONS, newValue);
}
}

View file

@ -17,6 +17,8 @@ public class NamedPathConfig extends PathConfig {
public final Boolean hidden;
public final int leftPadding;
public final int topPadding;
public final int minRange;
public final int maxRange;
@SuppressWarnings("unchecked")
ConfigTokenDescription(Field fl) throws IllegalAccessException {
@ -28,10 +30,14 @@ public class NamedPathConfig extends PathConfig {
this.hidden = ui.hide();
leftPadding = ui.leftPadding();
topPadding = ui.topPadding();
minRange = ui.minValue();
maxRange = ui.maxValue();
} else {
this.hidden = false;
this.leftPadding = 0;
topPadding = 0;
minRange = 0;
maxRange = 0;
}
}
@ -101,6 +107,21 @@ public class NamedPathConfig extends PathConfig {
dependenciesTrue
);
}
public static DependendConfigToken<Float> Float(
float defaultValue,
String entry,
String path,
Predicate<NamedPathConfig> dependenciesTrue
) {
return new DependendConfigToken<Float>(
ConfigKeeper.FloatEntry.class,
defaultValue,
entry,
path,
dependenciesTrue
);
}
}
public static class ConfigToken<T> extends ConfigKey {

View file

@ -57,8 +57,8 @@ public class VersionChecker implements Runnable {
private static Thread versionChecker;
public static void startCheck(boolean isClient) {
if (versionChecker == null) {
if (Configs.MAIN_CONFIG.checkVersions()) {
if (versionChecker == null && isClient) {
if (Configs.CLIENT_CONFIG.checkVersions() && Configs.CLIENT_CONFIG.didShowWelcomeScreen()) {
versionChecker = new Thread(isClient ? new VersionCheckerClient() : new VersionChecker());
versionChecker.start();
}

View file

@ -14,7 +14,7 @@ import net.fabricmc.api.Environment;
public class VersionCheckerClient extends VersionChecker {
public static void presentUpdateScreen(Screen parent) {
if (!Configs.MAIN_CONFIG.didShowWelcomeScreen()) {
if (!Configs.CLIENT_CONFIG.didShowWelcomeScreen()) {
Minecraft.getInstance().setScreen(new WelcomeScreen(parent));
} else if (Configs.CLIENT_CONFIG.showUpdateInfo() && !VersionChecker.isEmpty()) {