Skip to content

Commit

Permalink
Simplify disableOption
Browse files Browse the repository at this point in the history
  • Loading branch information
FxMorin committed Apr 20, 2024
1 parent 57e0e14 commit adb83db
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 38 deletions.
18 changes: 8 additions & 10 deletions src/api/java/ca/fxco/moreculling/api/config/ConfigAdditions.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,7 +14,7 @@
public class ConfigAdditions {

private static final Map<String, List<ConfigOption<?>>> additionOptions = new LinkedHashMap<>();
private static final Map<String, Pair<String, Function<?, ?>>> disabledOptions = new HashMap<>();
private static final Map<String, OptionOverride> disabledOptions = new HashMap<>();
private static final HashSet<String> separateGroupTabs = new HashSet<>();

/**
Expand All @@ -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 <T> void disableOption(String id, String reason, @Nullable Function<T, T> 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));
}

/**
Expand Down Expand Up @@ -82,10 +80,10 @@ public static Map<String, List<ConfigOption<?>>> getOptions() {
/**
* This is for internal use only
*
* @since 0.23.0
* @since 0.24.0
*/
@ApiStatus.Internal
public static Map<String, Pair<String, Function<?, ?>>> getDisabledOptions() {
public static Map<String, OptionOverride> getDisabledOptions() {
return ConfigAdditions.disabledOptions;
}
}
11 changes: 11 additions & 0 deletions src/api/java/ca/fxco/moreculling/api/config/OptionOverride.java
Original file line number Diff line number Diff line change
@@ -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) {}
4 changes: 4 additions & 0 deletions src/main/java/ca/fxco/moreculling/MoreCulling.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -152,24 +152,14 @@ protected final void setLocked(boolean locked) {
@Override
public final @NotNull A build() {
Objects.requireNonNull(this.value);
Pair<String, Function<?, ?>> 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<T, T> func2 = (Function<T, T>)func1;
this.value = func2.apply(this.value);
if (this.saveConsumer != null) {
this.saveConsumer.accept(this.value);
}
}
this.saveConsumer = null;
}
return this.runBuild();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -270,20 +270,10 @@ public MoreCullingSodiumOptionImpl<S, T> 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<String, Function<?, ?>> 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<T, T> func2 = (Function<T, T>)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,
Expand Down

0 comments on commit adb83db

Please sign in to comment.