Cloth Config API (Fabric/Forge/NeoForge)

Cloth Config API (Fabric/Forge/NeoForge)

169M Downloads

Setting name provider for enum types in autoconfig

md5 opened this issue · 4 comments

commented

I was looking for a way to hook in my own nameProvider function for an Enum that I'm exposing to Cloth via the bundled autoconfig functionality. It seems that I could do this by registering my own provider using one of the registry.register*Provider methods, but I was hoping to use a GuiTransformer method instead.

It seems that in general the *Entry classes do not have setters and are expecting all the setting to happen in the corresponding *Builder classes, but I did see that IntegerListListEntry exposes setMinimum and setMaximum, presumably to let the GuiTransformer that handles BoundedDiscrete to work.

Would it be reasonable to submit a patch like the following? I believe this would let me easily write a GuiTransformer that can set a nameProvider. Alternatively, maybe @ConfigEntry.Gui.EnumHandler could take a nameProvider as an optional argument? That would allow users to wire in the name provider more directly without adding a GuiTransformer, but there is less precedent that I can see for that since no annotations currently accept a function or functional interface argument.

If you'd like to discuss more, I'm happy to discuss here or hop on Discord if that works better.

diff --git a/common/src/main/java/me/shedaniel/clothconfig2/gui/entries/SelectionListEntry.java b/common/src/main/java/me/shedaniel/clothconfig2/gui/entries/SelectionListEntry.java
index fdec40f..78ad4bc 100644
--- a/common/src/main/java/me/shedaniel/clothconfig2/gui/entries/SelectionListEntry.java
+++ b/common/src/main/java/me/shedaniel/clothconfig2/gui/entries/SelectionListEntry.java
@@ -53,7 +53,7 @@ public class SelectionListEntry<T> extends TooltipListEntry<T> {
     private final Consumer<T> saveConsumer;
     private final Supplier<T> defaultValue;
     private final List<GuiEventListener> widgets;
-    private final Function<T, Component> nameProvider;
+    private Function<T, Component> nameProvider;
     
     @ApiStatus.Internal
     @Deprecated
@@ -96,6 +96,11 @@ public class SelectionListEntry<T> extends TooltipListEntry<T> {
         this.widgets = Lists.newArrayList(buttonWidget, resetButton);
         this.nameProvider = nameProvider == null ? (t -> new TranslatableComponent(t instanceof Translatable ? ((Translatable) t).getKey() : t.toString())) : nameProvider;
     }
+
+    public SelectionListEntry setNameProvider(Function<T, Component> nameProvider) {
+        this.nameProvider = nameProvider;
+        return this;
+    }
     
     @Override
     public void save() {
commented

Any further responses? This is indeed a useful thing but currently it can only be implemented by mods through mixins 🥲.
It will be good of Cloth Config's supporting localizable enums and even localizable enum tooltips natively in the future. They can be implemented quite easily by adding interfaces like EnumLocalizable thus allowing modders to customize the localizations of enum values, or even showing different tooltips with different enum values selected.

Cloth Config does support localising any objects, you can implement Translatable or override toString

I didn't know that! It turns out to be useful. But is there any documentation for all these features? The GitBook document has almost nothing about them. And plus, will Cloth Config support different tooltips for different enum cases in the future? It is useful when I want to explain the enum cases without using a verbose all-in-one tooltip (or text label).

commented

Any further responses? This is indeed a useful thing but currently it can only be implemented by mods through mixins 🥲.

It will be good of Cloth Config's supporting localizable enums and even localizable enum tooltips natively in the future. They can be implemented quite easily by adding interfaces like EnumLocalizable thus allowing modders to customize the localizations of enum values, or even showing different tooltips with different enum values selected.

commented

Any further responses? This is indeed a useful thing but currently it can only be implemented by mods through mixins 🥲.

It will be good of Cloth Config's supporting localizable enums and even localizable enum tooltips natively in the future. They can be implemented quite easily by adding interfaces like EnumLocalizable thus allowing modders to customize the localizations of enum values, or even showing different tooltips with different enum values selected.

Cloth Config does support localising any objects, you can implement Translatable or override toString

commented

Calling setTooltipSupplier() manually in ConfigEntryBuilder can supply the dynamic tooltip. But with auto config, it's hard to achieve.

Implementing interface Translatable is unable to pass args to the translation text. This can be solved by calling setEnumNameProvider() manually in ConfigEntryBuilder, but isn't available in auto config too.