Skip to content

Commit

Permalink
Migrate to upstreamed EnumDropdownController (#447)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinthegreat1 authored Dec 15, 2023
1 parent 6795c4d commit 4b194f8
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 185 deletions.
20 changes: 16 additions & 4 deletions src/main/java/de/hysky/skyblocker/config/ConfigUtils.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package de.hysky.skyblocker.config;

import dev.isxander.yacl3.api.Option;
import dev.isxander.yacl3.api.controller.BooleanControllerBuilder;
import dev.isxander.yacl3.api.controller.EnumControllerBuilder;
import dev.isxander.yacl3.api.controller.ValueFormatter;
import dev.isxander.yacl3.api.controller.*;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.apache.commons.lang3.StringUtils;

import java.util.function.Function;

public class ConfigUtils {
public static final Function<Formatting, String> FORMATTING_TO_STRING = formatting -> StringUtils.capitalize(formatting.getName().replaceAll("_", " "));
public static final ValueFormatter<Formatting> FORMATTING_FORMATTER = formatting -> Text.literal(StringUtils.capitalize(formatting.getName().replaceAll("_", " ")));
public static final ValueFormatter<Float> FLOAT_TWO_FORMATTER = value -> Text.literal(String.format("%,.2f", value).replaceAll("[\u00a0\u202F]", " "));

public static BooleanControllerBuilder createBooleanController(Option<Boolean> opt) {
Expand All @@ -22,4 +20,18 @@ public static BooleanControllerBuilder createBooleanController(Option<Boolean> o
public static <E extends Enum<E>> EnumControllerBuilder<E> createEnumCyclingListController(Option<E> opt) {
return EnumControllerBuilder.create(opt).enumClass((Class<E>) opt.binding().defaultValue().getClass());
}

/**
* Creates a factory for {@link EnumDropdownControllerBuilder}s with the given function for converting enum constants to texts.
* Use this if a custom formatter function for an enum is needed.
* Use it like this:
* <pre>{@code Option.<MyEnum>createBuilder().controller(ConfigUtils.getEnumDropdownControllerFactory(MY_CUSTOM_ENUM_TO_TEXT_FUNCTION))}</pre>
*
* @param formatter The function used to convert enum constants to texts used for display, suggestion, and validation
* @param <E> the enum type
* @return a factory for {@link EnumDropdownControllerBuilder}s
*/
public static <E extends Enum<E>> Function<Option<E>, ControllerBuilder<E>> getEnumDropdownControllerFactory(ValueFormatter<E> formatter) {
return opt -> EnumDropdownControllerBuilder.create(opt).formatValue(formatter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import dev.isxander.yacl3.api.controller.FloatFieldControllerBuilder;
import dev.isxander.yacl3.api.controller.IntegerFieldControllerBuilder;
import dev.isxander.yacl3.api.controller.StringControllerBuilder;
import de.hysky.skyblocker.config.controllers.EnumDropdownControllerBuilder;
import de.hysky.skyblocker.skyblock.dungeon.DungeonMapConfigScreen;
import net.minecraft.client.MinecraftClient;
import net.minecraft.text.Text;
Expand Down Expand Up @@ -209,29 +208,29 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig
.binding(defaults.locations.dungeons.dungeonChestProfit.neutralColor,
() -> config.locations.dungeons.dungeonChestProfit.neutralColor,
newValue -> config.locations.dungeons.dungeonChestProfit.neutralColor = newValue)
.controller(EnumDropdownControllerBuilder.getFactory(ConfigUtils.FORMATTING_TO_STRING))
.controller(ConfigUtils.getEnumDropdownControllerFactory(ConfigUtils.FORMATTING_FORMATTER))
.build())
.option(Option.<Formatting>createBuilder()
.name(Text.translatable("text.autoconfig.skyblocker.option.locations.dungeons.dungeonChestProfit.profitColor"))
.binding(defaults.locations.dungeons.dungeonChestProfit.profitColor,
() -> config.locations.dungeons.dungeonChestProfit.profitColor,
newValue -> config.locations.dungeons.dungeonChestProfit.profitColor = newValue)
.controller(EnumDropdownControllerBuilder.getFactory(ConfigUtils.FORMATTING_TO_STRING))
.controller(ConfigUtils.getEnumDropdownControllerFactory(ConfigUtils.FORMATTING_FORMATTER))
.build())
.option(Option.<Formatting>createBuilder()
.name(Text.translatable("text.autoconfig.skyblocker.option.locations.dungeons.dungeonChestProfit.lossColor"))
.binding(defaults.locations.dungeons.dungeonChestProfit.lossColor,
() -> config.locations.dungeons.dungeonChestProfit.lossColor,
newValue -> config.locations.dungeons.dungeonChestProfit.lossColor = newValue)
.controller(EnumDropdownControllerBuilder.getFactory(ConfigUtils.FORMATTING_TO_STRING))
.controller(ConfigUtils.getEnumDropdownControllerFactory(ConfigUtils.FORMATTING_FORMATTER))
.build())
.option(Option.<Formatting>createBuilder()
.name(Text.translatable("text.autoconfig.skyblocker.option.general.chestValue.incompleteColor"))
.description(OptionDescription.of(Text.translatable("text.autoconfig.skyblocker.option.general.chestValue.incompleteColor.@Tooltip")))
.binding(defaults.locations.dungeons.dungeonChestProfit.incompleteColor,
() -> config.locations.dungeons.dungeonChestProfit.incompleteColor,
newValue -> config.locations.dungeons.dungeonChestProfit.incompleteColor = newValue)
.controller(EnumDropdownControllerBuilder.getFactory(ConfigUtils.FORMATTING_TO_STRING))
.controller(ConfigUtils.getEnumDropdownControllerFactory(ConfigUtils.FORMATTING_FORMATTER))
.build())
.build())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import de.hysky.skyblocker.config.ConfigUtils;
import de.hysky.skyblocker.config.SkyblockerConfig;
import de.hysky.skyblocker.config.controllers.EnumDropdownControllerBuilder;
import de.hysky.skyblocker.skyblock.shortcut.ShortcutsConfigScreen;
import de.hysky.skyblocker.utils.render.title.TitleContainerConfigScreen;
import de.hysky.skyblocker.utils.waypoint.Waypoint;
Expand Down Expand Up @@ -474,15 +473,15 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig
.binding(defaults.general.chestValue.color,
() -> config.general.chestValue.color,
newValue -> config.general.chestValue.color = newValue)
.controller(EnumDropdownControllerBuilder.getFactory(ConfigUtils.FORMATTING_TO_STRING))
.controller(ConfigUtils.getEnumDropdownControllerFactory(ConfigUtils.FORMATTING_FORMATTER))
.build())
.option(Option.<Formatting>createBuilder()
.name(Text.translatable("text.autoconfig.skyblocker.option.general.chestValue.incompleteColor"))
.description(OptionDescription.of(Text.translatable("text.autoconfig.skyblocker.option.general.chestValue.incompleteColor.@Tooltip")))
.binding(defaults.general.chestValue.incompleteColor,
() -> config.general.chestValue.incompleteColor,
newValue -> config.general.chestValue.incompleteColor = newValue)
.controller(EnumDropdownControllerBuilder.getFactory(ConfigUtils.FORMATTING_TO_STRING))
.controller(ConfigUtils.getEnumDropdownControllerFactory(ConfigUtils.FORMATTING_FORMATTER))
.build())
.build())

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 4b194f8

Please sign in to comment.