Skip to content

Commit

Permalink
refactor: Migrate to MRU ConfigHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
IMB11 committed Aug 15, 2024
1 parent b81bd47 commit 6beaa0c
Showing 1 changed file with 36 additions and 93 deletions.
129 changes: 36 additions & 93 deletions src/main/java/dev/imb11/fog/config/FogConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,22 @@
import com.google.gson.GsonBuilder;
import dev.imb11.fog.client.FogClient;
import dev.imb11.fog.client.FogManager;
import dev.imb11.fog.client.util.math.EnvironmentCalculations;
import dev.imb11.mru.yacl.ConfigHelper;
import dev.imb11.mru.yacl.EntryType;
import dev.isxander.yacl3.api.*;
import dev.isxander.yacl3.api.controller.BooleanControllerBuilder;
import dev.isxander.yacl3.api.controller.FloatSliderControllerBuilder;
import dev.isxander.yacl3.config.v2.api.ConfigClassHandler;
import dev.isxander.yacl3.config.v2.api.SerialEntry;
import dev.isxander.yacl3.config.v2.api.serializer.GsonConfigSerializerBuilder;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.NotNull;
import java.util.Map;
import java.util.TreeMap;

import static dev.imb11.fog.client.FogClient.MOD_ID;

public class FogConfig {
private static final String CONFIG_FILE_NAME = "config";
private static final String CONFIG_FILE_EXTENSION = "json";
private static final String CONFIG_TRANSLATION_KEY = "config";
private static final ConfigClassHandler<FogConfig> HANDLER = ConfigClassHandler
.createBuilder(FogConfig.class)
.id(Identifier.of(MOD_ID, CONFIG_FILE_NAME))
Expand All @@ -31,6 +28,7 @@ public class FogConfig {
.appendGsonBuilder(GsonBuilder::setPrettyPrinting)
.build())
.build();
private static final ConfigHelper HELPER = new ConfigHelper(MOD_ID, "config");
@SerialEntry
public float initialFogStart = 0.1f;
@SerialEntry
Expand All @@ -51,13 +49,6 @@ public class FogConfig {
@SerialEntry
public boolean disableMod = false;

private enum EntryType {
CATEGORY_NAME,
GROUP_NAME,
OPTION_NAME,
OPTION_DESCRIPTION,
}

public static @NotNull FogConfig getInstance() {
return HANDLER.instance();
}
Expand All @@ -79,78 +70,48 @@ public static void save() {
FogManager.INSTANCE = new FogManager();
})
.category(ConfigCategory.createBuilder()
.name(getText(EntryType.CATEGORY_NAME, "fog_calculations"))
.option(LabelOption.create(getText(EntryType.OPTION_NAME, "fog_calculations.warning")))
.option(Option.<Float>createBuilder().name(
getText(EntryType.OPTION_NAME, "initial_fog_start")).description(
initialFogStart -> OptionDescription.createBuilder().text(
getText(EntryType.OPTION_DESCRIPTION, "initial_fog_start")).build()).binding(
defaults.initialFogStart, () -> initialFogStart,
newInitialFogStart -> initialFogStart = newInitialFogStart
).controller(option -> FloatSliderControllerBuilder.create(option).range(0.0F, 1.0F).step(
0.0001F).formatValue(value -> Text.of(String.format("%.2f%%", value * 100.0F)))).build())
.option(Option.<Float>createBuilder().name(
getText(EntryType.OPTION_NAME, "initial_fog_end")).description(
initialFogStart -> OptionDescription.createBuilder().text(
getText(EntryType.OPTION_DESCRIPTION, "initial_fog_end")).build()).binding(
defaults.initialFogEnd, () -> initialFogEnd,
newInitialFogEnd -> initialFogEnd = newInitialFogEnd
).controller(option -> FloatSliderControllerBuilder.create(option).range(0.0F, 1.0F).step(
0.0001F).formatValue(value -> Text.of(String.format("%.2f%%", value * 100.0F)))).build())
.name(HELPER.getText(EntryType.CATEGORY_NAME, "fog_calculations"))
.option(LabelOption.create(HELPER.getText(EntryType.OPTION_NAME, "fog_calculations.warning")))
.option(HELPER.getSlider(
"initial_fog_start", 0f, 1f, 0.05f, defaults.initialFogStart, () -> config.initialFogStart,
val -> config.initialFogStart = val
))
.option(HELPER.getSlider(
"initial_fog_end", 0f, 1f, 0.05f, defaults.initialFogEnd, () -> config.initialFogEnd,
val -> config.initialFogEnd = val
))
.option(HELPER.get(
"disable_raininess_effect", defaults.disableRaininessEffect,
() -> config.disableRaininessEffect, val -> config.disableRaininessEffect = val
))
.option(HELPER.get(
"disable_underground_fog_multiplier", defaults.disableUndergroundFogMultiplier,
() -> config.disableUndergroundFogMultiplier,
val -> config.disableUndergroundFogMultiplier = val
))
.option(HELPER.get(
"disable_biome_fog_colour", defaults.disableBiomeFogColour,
() -> config.disableBiomeFogColour, val -> config.disableBiomeFogColour = val
))
.option(Option.<Boolean>createBuilder().name(
getText(EntryType.OPTION_NAME, "disable_raininess_effect")).description(
HELPER.getText(EntryType.OPTION_NAME, "disable_cloud_whitening")).description(
initialFogStart -> OptionDescription.createBuilder().text(
getText(
EntryType.OPTION_DESCRIPTION,
"disable_raininess_effect"
)).build()).binding(
defaults.disableRaininessEffect, () -> disableRaininessEffect,
newDisableRaininessEffect -> disableRaininessEffect = newDisableRaininessEffect
).controller(BooleanControllerBuilder::create).build())
.option(Option.<Boolean>createBuilder().name(
getText(EntryType.OPTION_NAME, "disable_underground_fog_multiplier")).description(
initialFogStart -> OptionDescription.createBuilder().text(
getText(
EntryType.OPTION_DESCRIPTION,
"disable_underground_fog_multiplier"
)).build()).binding(
defaults.disableRaininessEffect, () -> disableRaininessEffect,
newDisableRaininessEffect -> disableRaininessEffect = newDisableRaininessEffect
).controller(BooleanControllerBuilder::create).build())
.option(Option.<Boolean>createBuilder().name(
getText(EntryType.OPTION_NAME, "disable_biome_fog_colour")).description(
initialFogStart -> OptionDescription.createBuilder().text(
getText(
EntryType.OPTION_DESCRIPTION,
"disable_biome_fog_colour"
)).build()).binding(
defaults.disableBiomeFogColour, () -> disableBiomeFogColour,
newDisableBiomeFogColour -> disableBiomeFogColour = newDisableBiomeFogColour
).controller(BooleanControllerBuilder::create).build())
.option(Option.<Boolean>createBuilder().name(
getText(EntryType.OPTION_NAME, "disable_cloud_whitening")).description(
initialFogStart -> OptionDescription.createBuilder().text(
getText(
HELPER.getText(
EntryType.OPTION_DESCRIPTION,
"disable_cloud_whitening"
)).build()).binding(
defaults.disableCloudWhitening, () -> disableCloudWhitening,
newDisableCloudWhitening -> disableCloudWhitening = newDisableCloudWhitening
).controller(BooleanControllerBuilder::create).available(!FogClient.isModInstalled("sodium")).build())
.option(Option.<Boolean>createBuilder().name(
getText(EntryType.OPTION_NAME, "disable_nether")).description(
initialFogStart -> OptionDescription.createBuilder().text(
getText(
EntryType.OPTION_DESCRIPTION,
"disable_nether"
)).build()).binding(
defaults.disableNether, () -> disableNether,
newDisableNether -> disableNether = newDisableNether
).controller(BooleanControllerBuilder::create).build())
).controller(BooleanControllerBuilder::create).available(
!FogClient.isModInstalled("sodium")).build())
.option(HELPER.get(
"disable_nether", defaults.disableNether, () -> config.disableNether,
val -> config.disableNether = val
))
.option(Option.<Boolean>createBuilder().name(
getText(EntryType.OPTION_NAME, "disable_mod")).description(
HELPER.getText(EntryType.OPTION_NAME, "disable_mod")).description(
initialFogStart -> OptionDescription.createBuilder().text(
getText(
HELPER.getText(
EntryType.OPTION_DESCRIPTION,
"disable_mod"
)).build()).binding(
Expand All @@ -160,22 +121,4 @@ public static void save() {
.build())
));
}

/**
* @param entryType The type of the entry that requires a translation key,
* @param configOptionName The name of the entry that requires a translation key,
* @return The {@link Text} with substituted values in the translation key.
*/
private static @NotNull Text getText(@NotNull FogConfig.EntryType entryType, @NotNull String configOptionName) {
@NotNull String entryText;
switch (entryType) {
case CATEGORY_NAME -> entryText = "category";
case GROUP_NAME -> entryText = "group";
case OPTION_NAME -> entryText = "option";
case OPTION_DESCRIPTION -> entryText = "option.description";
default -> throw new IllegalArgumentException("TextType is invalid.");
}

return Text.translatable(String.format("%s.%s.%s.%s", MOD_ID, CONFIG_TRANSLATION_KEY, entryText, configOptionName));
}
}

0 comments on commit 6beaa0c

Please sign in to comment.