From adb83db00ba39ec4682471324e9d06db00b7b03a Mon Sep 17 00:00:00 2001 From: fxmorin <28154542+fxmorin@users.noreply.github.com> Date: Sat, 20 Apr 2024 05:10:06 -0400 Subject: [PATCH] Simplify `disableOption` --- .../api/config/ConfigAdditions.java | 18 ++++++++---------- .../moreculling/api/config/OptionOverride.java | 11 +++++++++++ .../java/ca/fxco/moreculling/MoreCulling.java | 4 ++++ .../config/cloth/AbstractDynamicBuilder.java | 18 ++++-------------- .../sodium/MoreCullingSodiumOptionImpl.java | 18 ++++-------------- 5 files changed, 31 insertions(+), 38 deletions(-) create mode 100644 src/api/java/ca/fxco/moreculling/api/config/OptionOverride.java diff --git a/src/api/java/ca/fxco/moreculling/api/config/ConfigAdditions.java b/src/api/java/ca/fxco/moreculling/api/config/ConfigAdditions.java index b9201ff0..04276edb 100755 --- a/src/api/java/ca/fxco/moreculling/api/config/ConfigAdditions.java +++ b/src/api/java/ca/fxco/moreculling/api/config/ConfigAdditions.java @@ -1,11 +1,9 @@ package ca.fxco.moreculling.api.config; -import com.mojang.datafixers.util.Pair; import org.jetbrains.annotations.ApiStatus; -import org.jetbrains.annotations.Nullable; import java.util.*; -import java.util.function.Function; +import java.util.function.BooleanSupplier; /** * Add your config options to this class to add them to the MoreCulling config @@ -16,7 +14,7 @@ public class ConfigAdditions { private static final Map>> additionOptions = new LinkedHashMap<>(); - private static final Map>> disabledOptions = new HashMap<>(); + private static final Map disabledOptions = new HashMap<>(); private static final HashSet separateGroupTabs = new HashSet<>(); /** @@ -33,12 +31,12 @@ public static void addOption(String group, ConfigOption option) { * @param id The option to be disabled. This will attempt to match against the option name, if the option * uses a translation key, it will attempt to match the translation key. * @param reason The reason why this option was disabled. - * @param setOption A function that can be used to set the options value. + * @param canChange A supplier that returns if the option can be changed. * - * @since 0.23.0 + * @since 0.24.0 */ - public static void disableOption(String id, String reason, @Nullable Function setOption) { - ConfigAdditions.disabledOptions.put(id, Pair.of(reason, setOption)); + public static void disableOption(String id, String reason, BooleanSupplier canChange) { + ConfigAdditions.disabledOptions.put(id, new OptionOverride(reason, canChange)); } /** @@ -82,10 +80,10 @@ public static Map>> getOptions() { /** * This is for internal use only * - * @since 0.23.0 + * @since 0.24.0 */ @ApiStatus.Internal - public static Map>> getDisabledOptions() { + public static Map getDisabledOptions() { return ConfigAdditions.disabledOptions; } } diff --git a/src/api/java/ca/fxco/moreculling/api/config/OptionOverride.java b/src/api/java/ca/fxco/moreculling/api/config/OptionOverride.java new file mode 100644 index 00000000..b7539331 --- /dev/null +++ b/src/api/java/ca/fxco/moreculling/api/config/OptionOverride.java @@ -0,0 +1,11 @@ +package ca.fxco.moreculling.api.config; + +import java.util.function.BooleanSupplier; + +/** + * This controls how the options should be overridden. + * + * @since 0.24.0 + * @author FX + */ +public record OptionOverride(String reason, BooleanSupplier canChange) {} diff --git a/src/main/java/ca/fxco/moreculling/MoreCulling.java b/src/main/java/ca/fxco/moreculling/MoreCulling.java index ec98d92a..535795eb 100755 --- a/src/main/java/ca/fxco/moreculling/MoreCulling.java +++ b/src/main/java/ca/fxco/moreculling/MoreCulling.java @@ -42,4 +42,8 @@ public MoreCullingConfig deserialize() { ConfigUpdater.updateConfig(CONFIG); MoreCulling.CONFIG.modCompatibility.defaultReturnValue(MoreCulling.CONFIG.useOnModdedBlocksByDefault); } + + public void saveConfig() { + AutoConfig.getConfigHolder(MoreCullingConfig.class).save(); + } } diff --git a/src/main/java/ca/fxco/moreculling/config/cloth/AbstractDynamicBuilder.java b/src/main/java/ca/fxco/moreculling/config/cloth/AbstractDynamicBuilder.java index cbd09445..288162aa 100755 --- a/src/main/java/ca/fxco/moreculling/config/cloth/AbstractDynamicBuilder.java +++ b/src/main/java/ca/fxco/moreculling/config/cloth/AbstractDynamicBuilder.java @@ -1,7 +1,7 @@ package ca.fxco.moreculling.config.cloth; import ca.fxco.moreculling.api.config.ConfigAdditions; -import com.mojang.datafixers.util.Pair; +import ca.fxco.moreculling.api.config.OptionOverride; import me.shedaniel.clothconfig2.api.AbstractConfigListEntry; import me.shedaniel.clothconfig2.impl.builders.FieldBuilder; import net.minecraft.text.Text; @@ -152,24 +152,14 @@ protected final void setLocked(boolean locked) { @Override public final @NotNull A build() { Objects.requireNonNull(this.value); - Pair> pair = ConfigAdditions.getDisabledOptions().get(this.translationKey); - if (pair != null) { - this.setTooltip(Text.literal(pair.getFirst())); + OptionOverride optionOverride = ConfigAdditions.getDisabledOptions().get(this.translationKey); + if (optionOverride != null && !optionOverride.canChange().getAsBoolean()) { + this.setTooltip(Text.literal(optionOverride.reason())); if (this.defaultValue != null && this.value != null && this.defaultValue.get() != this.value) { this.value = this.defaultValue.get(); } this.locked = true; - this.changeConsumer = null; this.requireRestart(false); - Function func1 = pair.getSecond(); - if (func1 != null) { - Function func2 = (Function)func1; - this.value = func2.apply(this.value); - if (this.saveConsumer != null) { - this.saveConsumer.accept(this.value); - } - } - this.saveConsumer = null; } return this.runBuild(); } diff --git a/src/main/java/ca/fxco/moreculling/config/sodium/MoreCullingSodiumOptionImpl.java b/src/main/java/ca/fxco/moreculling/config/sodium/MoreCullingSodiumOptionImpl.java index 4632d297..63ddff58 100755 --- a/src/main/java/ca/fxco/moreculling/config/sodium/MoreCullingSodiumOptionImpl.java +++ b/src/main/java/ca/fxco/moreculling/config/sodium/MoreCullingSodiumOptionImpl.java @@ -1,7 +1,7 @@ package ca.fxco.moreculling.config.sodium; import ca.fxco.moreculling.api.config.ConfigAdditions; -import com.mojang.datafixers.util.Pair; +import ca.fxco.moreculling.api.config.OptionOverride; import me.jellysquid.mods.sodium.client.gui.options.Option; import me.jellysquid.mods.sodium.client.gui.options.OptionFlag; import me.jellysquid.mods.sodium.client.gui.options.OptionImpact; @@ -270,20 +270,10 @@ public MoreCullingSodiumOptionImpl build() { Validate.notNull(this.tooltip, "Tooltip must be specified"); Validate.notNull(this.binding, "Option binding must be specified"); Validate.notNull(this.control, "Control must be specified"); - Pair> pair = ConfigAdditions.getDisabledOptions().get(this.nameTranslationKey); - if (pair != null) { + OptionOverride optionOverride = ConfigAdditions.getDisabledOptions().get(this.nameTranslationKey); + if (optionOverride != null && !optionOverride.canChange().getAsBoolean()) { this.locked = true; - this.enabled = false; - this.tooltip = Text.literal(pair.getFirst()); - this.onChanged = null; - Function func1 = pair.getSecond(); - if (func1 != null) { - Function func2 = (Function)func1; - this.binding.setValue( - this.storage.getData(), - func2.apply(this.binding.getValue(this.storage.getData())) - ); - } + this.tooltip = Text.literal(optionOverride.reason()); } return new MoreCullingSodiumOptionImpl<>( this.storage, Text.translatable(this.nameTranslationKey), this.tooltip, this.binding, this.control,