diff --git a/src/main/java/de/hysky/skyblocker/config/ConfigUtils.java b/src/main/java/de/hysky/skyblocker/config/ConfigUtils.java index 9a7a41b5ee..8b0f27a72d 100644 --- a/src/main/java/de/hysky/skyblocker/config/ConfigUtils.java +++ b/src/main/java/de/hysky/skyblocker/config/ConfigUtils.java @@ -1,9 +1,7 @@ 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; @@ -11,7 +9,7 @@ import java.util.function.Function; public class ConfigUtils { - public static final Function FORMATTING_TO_STRING = formatting -> StringUtils.capitalize(formatting.getName().replaceAll("_", " ")); + public static final ValueFormatter FORMATTING_FORMATTER = formatting -> Text.literal(StringUtils.capitalize(formatting.getName().replaceAll("_", " "))); public static final ValueFormatter FLOAT_TWO_FORMATTER = value -> Text.literal(String.format("%,.2f", value).replaceAll("[\u00a0\u202F]", " ")); public static BooleanControllerBuilder createBooleanController(Option opt) { @@ -22,4 +20,18 @@ public static BooleanControllerBuilder createBooleanController(Option o public static > EnumControllerBuilder createEnumCyclingListController(Option opt) { return EnumControllerBuilder.create(opt).enumClass((Class) 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: + *
{@code Option.createBuilder().controller(ConfigUtils.getEnumDropdownControllerFactory(MY_CUSTOM_ENUM_TO_TEXT_FUNCTION))}
+ * + * @param formatter The function used to convert enum constants to texts used for display, suggestion, and validation + * @param the enum type + * @return a factory for {@link EnumDropdownControllerBuilder}s + */ + public static > Function, ControllerBuilder> getEnumDropdownControllerFactory(ValueFormatter formatter) { + return opt -> EnumDropdownControllerBuilder.create(opt).formatValue(formatter); + } } diff --git a/src/main/java/de/hysky/skyblocker/config/categories/DungeonsCategory.java b/src/main/java/de/hysky/skyblocker/config/categories/DungeonsCategory.java index 246611cc32..3d30448755 100644 --- a/src/main/java/de/hysky/skyblocker/config/categories/DungeonsCategory.java +++ b/src/main/java/de/hysky/skyblocker/config/categories/DungeonsCategory.java @@ -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; @@ -209,21 +208,21 @@ 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.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.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.createBuilder() .name(Text.translatable("text.autoconfig.skyblocker.option.general.chestValue.incompleteColor")) @@ -231,7 +230,7 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig .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()) diff --git a/src/main/java/de/hysky/skyblocker/config/categories/GeneralCategory.java b/src/main/java/de/hysky/skyblocker/config/categories/GeneralCategory.java index 8d8c6f46fe..2065f6df68 100644 --- a/src/main/java/de/hysky/skyblocker/config/categories/GeneralCategory.java +++ b/src/main/java/de/hysky/skyblocker/config/categories/GeneralCategory.java @@ -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; @@ -474,7 +473,7 @@ 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.createBuilder() .name(Text.translatable("text.autoconfig.skyblocker.option.general.chestValue.incompleteColor")) @@ -482,7 +481,7 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig .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()) diff --git a/src/main/java/de/hysky/skyblocker/config/controllers/EnumDropdownController.java b/src/main/java/de/hysky/skyblocker/config/controllers/EnumDropdownController.java deleted file mode 100644 index 0b9a809d67..0000000000 --- a/src/main/java/de/hysky/skyblocker/config/controllers/EnumDropdownController.java +++ /dev/null @@ -1,93 +0,0 @@ -package de.hysky.skyblocker.config.controllers; - -import dev.isxander.yacl3.api.Option; -import dev.isxander.yacl3.api.utils.Dimension; -import dev.isxander.yacl3.gui.AbstractWidget; -import dev.isxander.yacl3.gui.YACLScreen; -import dev.isxander.yacl3.gui.controllers.dropdown.AbstractDropdownController; -import org.jetbrains.annotations.NotNull; - -import java.util.Arrays; -import java.util.function.Function; -import java.util.stream.Stream; - -public class EnumDropdownController> extends AbstractDropdownController { - /** - * The function used to convert enum constants to strings used for display, suggestion, and validation. Defaults to {@link Enum#toString}. - */ - protected final Function toString; - - protected EnumDropdownController(Option option, Function toString) { - super(option); - this.toString = toString; - } - - @Override - public String getString() { - return toString.apply(option().pendingValue()); - } - - @Override - public void setFromString(String value) { - option().requestSet(getEnumFromString(value)); - } - - /** - * Searches through enum constants for one whose {@link #toString} result equals {@code value} - * - * @return The enum constant associated with the {@code value} or the pending value if none are found - * @implNote The return value of {@link #toString} on each enum constant should be unique in order to ensure accuracy - */ - private E getEnumFromString(String value) { - value = value.toLowerCase(); - for (E constant : option().pendingValue().getDeclaringClass().getEnumConstants()) { - if (toString.apply(constant).toLowerCase().equals(value)) return constant; - } - - return option().pendingValue(); - } - - @Override - public boolean isValueValid(String value) { - value = value.toLowerCase(); - for (E constant : option().pendingValue().getDeclaringClass().getEnumConstants()) { - if (toString.apply(constant).equals(value)) return true; - } - - return false; - } - - @Override - protected String getValidValue(String value, int offset) { - return getValidEnumConstants(value) - .skip(offset) - .findFirst() - .orElseGet(this::getString); - } - - /** - * Filters and sorts through enum constants for those whose {@link #toString} result equals {@code value} - * - * @return a sorted stream containing enum constants associated with the {@code value} - * @implNote The return value of {@link #toString} on each enum constant should be unique in order to ensure accuracy - */ - @NotNull - protected Stream getValidEnumConstants(String value) { - String valueLowerCase = value.toLowerCase(); - return Arrays.stream(option().pendingValue().getDeclaringClass().getEnumConstants()) - .map(this.toString) - .filter(constant -> constant.toLowerCase().contains(valueLowerCase)) - .sorted((s1, s2) -> { - String s1LowerCase = s1.toLowerCase(); - String s2LowerCase = s2.toLowerCase(); - if (s1LowerCase.startsWith(valueLowerCase) && !s2LowerCase.startsWith(valueLowerCase)) return -1; - if (!s1LowerCase.startsWith(valueLowerCase) && s2LowerCase.startsWith(valueLowerCase)) return 1; - return s1.compareTo(s2); - }); - } - - @Override - public AbstractWidget provideWidget(YACLScreen screen, Dimension widgetDimension) { - return new EnumDropdownControllerElement<>(this, screen, widgetDimension); - } -} diff --git a/src/main/java/de/hysky/skyblocker/config/controllers/EnumDropdownControllerBuilder.java b/src/main/java/de/hysky/skyblocker/config/controllers/EnumDropdownControllerBuilder.java deleted file mode 100644 index d451a88c7c..0000000000 --- a/src/main/java/de/hysky/skyblocker/config/controllers/EnumDropdownControllerBuilder.java +++ /dev/null @@ -1,27 +0,0 @@ -package de.hysky.skyblocker.config.controllers; - -import dev.isxander.yacl3.api.Option; -import dev.isxander.yacl3.api.controller.ControllerBuilder; - -import java.util.function.Function; - -public interface EnumDropdownControllerBuilder> extends ControllerBuilder { - EnumDropdownControllerBuilder toString(Function toString); - - static > EnumDropdownControllerBuilder create(Option option) { - return new EnumDropdownControllerBuilderImpl<>(option); - } - - /** - * Creates a factory for {@link EnumDropdownControllerBuilder}s with the given function for converting enum constants to strings. - * Use this if a custom toString function for an enum is needed. - * Use it like this: - *
{@code Option.createBuilder().controller(createEnumDropdownControllerBuilder.getFactory(MY_CUSTOM_ENUM_TO_STRING_FUNCTION))}
- * @param toString The function used to convert enum constants to strings used for display, suggestion, and validation - * @return a factory for {@link EnumDropdownControllerBuilder}s - * @param the enum type - */ - static > Function, ControllerBuilder> getFactory(Function toString) { - return opt -> EnumDropdownControllerBuilder.create(opt).toString(toString); - } -} diff --git a/src/main/java/de/hysky/skyblocker/config/controllers/EnumDropdownControllerBuilderImpl.java b/src/main/java/de/hysky/skyblocker/config/controllers/EnumDropdownControllerBuilderImpl.java deleted file mode 100644 index 8f6dbb2a5c..0000000000 --- a/src/main/java/de/hysky/skyblocker/config/controllers/EnumDropdownControllerBuilderImpl.java +++ /dev/null @@ -1,27 +0,0 @@ -package de.hysky.skyblocker.config.controllers; - -import dev.isxander.yacl3.api.Controller; -import dev.isxander.yacl3.api.Option; -import dev.isxander.yacl3.impl.controller.AbstractControllerBuilderImpl; - -import java.util.function.Function; - -public class EnumDropdownControllerBuilderImpl> extends AbstractControllerBuilderImpl implements EnumDropdownControllerBuilder { - private Function toString = Enum::toString; - - public EnumDropdownControllerBuilderImpl(Option option) { - super(option); - } - - @Override - public EnumDropdownControllerBuilder toString(Function toString) { - this.toString = toString; - return this; - } - - @SuppressWarnings("UnstableApiUsage") - @Override - public Controller build() { - return new EnumDropdownController<>(option, toString); - } -} diff --git a/src/main/java/de/hysky/skyblocker/config/controllers/EnumDropdownControllerElement.java b/src/main/java/de/hysky/skyblocker/config/controllers/EnumDropdownControllerElement.java deleted file mode 100644 index 2a8de609fe..0000000000 --- a/src/main/java/de/hysky/skyblocker/config/controllers/EnumDropdownControllerElement.java +++ /dev/null @@ -1,26 +0,0 @@ -package de.hysky.skyblocker.config.controllers; - -import dev.isxander.yacl3.api.utils.Dimension; -import dev.isxander.yacl3.gui.YACLScreen; -import dev.isxander.yacl3.gui.controllers.dropdown.AbstractDropdownControllerElement; - -import java.util.List; - -public class EnumDropdownControllerElement> extends AbstractDropdownControllerElement { - private final EnumDropdownController controller; - - public EnumDropdownControllerElement(EnumDropdownController control, YACLScreen screen, Dimension dim) { - super(control, screen, dim); - this.controller = control; - } - - @Override - public List computeMatchingValues() { - return controller.getValidEnumConstants(inputField).toList(); - } - - @Override - public String getString(String object) { - return object; - } -}