Skip to content

Respect TranslatableOption on enums #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: architectury-1.21.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.minecraft.screen.ScreenTexts;
import net.minecraft.text.Style; import net.minecraft.text.Text;
import net.minecraft.util.Formatting; import net.minecraft.util.Identifier;
import net.minecraft.util.TranslatableOption;
import org.jetbrains.annotations.Nullable;

import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter;
Expand Down Expand Up @@ -170,10 +171,7 @@ else if (info.dataType == boolean.class) {
}, func);
} else if (info.dataType.isEnum()) {
List<?> values = Arrays.asList(field.getType().getEnumConstants());
Function<Object, Text> func = value -> {
String translationKey = modid + ".midnightconfig.enum." + info.dataType.getSimpleName() + "." + info.toTemporaryValue();
return I18n.hasTranslation(translationKey) ? Text.translatable(translationKey) : Text.literal(info.toTemporaryValue());
};
Function<Object, Text> func = value -> getEnumTranslatableText(value, modid, info);
info.function = new AbstractMap.SimpleEntry<ButtonWidget.PressAction, Function<Object, Text>>(button -> {
int index = values.indexOf(info.value) + 1;
info.setValue(values.get(index >= values.size() ? 0 : index));
Expand All @@ -195,6 +193,15 @@ public static Tooltip getTooltip(EntryInfo info, boolean isButton) {
return Tooltip.of(isButton && info.error != null ? info.error : I18n.hasTranslation(key) ? Text.translatable(key) : Text.empty());
}

private static Text getEnumTranslatableText(Object value, String modid, EntryInfo info) {
if (value instanceof TranslatableOption translatableOption) {
return translatableOption.getText();
}

String translationKey = modid + ".midnightconfig.enum." + info.dataType.getSimpleName() + "." + info.toTemporaryValue();
return I18n.hasTranslation(translationKey) ? Text.translatable(translationKey) : Text.literal(info.toTemporaryValue());
}

private static void textField(EntryInfo info, Function<String,Number> f, Pattern pattern, double min, double max, boolean cast) {
boolean isNumber = pattern != null;
info.function = (BiFunction<TextFieldWidget, ButtonWidget, Predicate<String>>) (t, b) -> s -> {
Expand Down Expand Up @@ -359,8 +366,9 @@ public void fillList() {
Entry e = info.entry;
if (info.function instanceof Map.Entry) { // Enums & booleans
var values = (Map.Entry<ButtonWidget.PressAction, Function<Object, Text>>) info.function;
if (info.dataType.isEnum())
values.setValue(value -> Text.translatable(translationPrefix + "enum." + info.dataType.getSimpleName() + "." + info.value.toString()));
if (info.dataType.isEnum()) {
values.setValue(value -> getEnumTranslatableText(value, modid, info));
}
widget = ButtonWidget.builder(values.getValue().apply(info.value), values.getKey()).dimensions(width - 185, 0, 150, 20).tooltip(getTooltip(info, true)).build();
if (info.dataType == boolean.class) info.actionButton = CheckboxWidget.builder(Text.empty(), textRenderer).callback((checkbox, checked) -> values.getKey().onPress((ButtonWidget) widget)).checked((Boolean) info.value).pos(widget.getX(), 1).build();
} else if (e.isSlider())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import com.google.common.collect.Lists;
import eu.midnightdust.lib.config.MidnightConfig;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import net.minecraft.util.TranslatableOption;

import javax.swing.*;
import java.util.ArrayList;
Expand Down Expand Up @@ -31,6 +35,37 @@ public class MidnightConfigExample extends MidnightConfig {
public enum ModPlatform { // Enums allow the user to cycle through predefined options
QUILT, FABRIC, FORGE, NEOFORGE, VANILLA
}
@Entry(category = TEXT) public static GraphicsSteps graphicsSteps = GraphicsSteps.FABULOUS; // Example for an enum option with TranslatableOption
public enum GraphicsSteps implements TranslatableOption {
FAST(0, "options.graphics.fast"),
FANCY(1, "options.graphics.fancy"),
FABULOUS(2, "options.graphics.fabulous");

private final int id;
private final String translationKey;

GraphicsSteps(int id, String translationKey) {
this.id = id;
this.translationKey = translationKey;
}

@Override
public Text getText() {
MutableText mutableText = Text.translatable(this.getTranslationKey());
return this == GraphicsSteps.FABULOUS ? mutableText.formatted(Formatting.ITALIC).formatted(Formatting.AQUA) : mutableText;
}

@Override
public int getId() {
return this.id;
}

@Override
public String getTranslationKey() {
return this.translationKey;
}
}

@Comment(category = TEXT, name = "§nMidnightLib Wiki", centered = true, url = "https://www.midnightdust.eu/wiki/midnightlib/") public static Comment wiki; // Example for a comment with a url

@Entry(category = NUMBERS) public static int fabric = 16777215; // Example for an int option
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"modid.midnightconfig.enum.ModPlatform.QUILT":"Quilt",
"modid.midnightconfig.enum.ModPlatform.NEOFORGE":"NeoForge",
"modid.midnightconfig.enum.ModPlatform.VANILLA":"Vanilla",
"modid.midnightconfig.graphicsSteps":"I am an enum with TranslatableOption!",
"modid.midnightconfig.myFileOrDirectory.fileChooser": "Select an image or directory",
"modid.midnightconfig.myFileOrDirectory.fileFilter": "Supported Images (.png, .jpg, .jpeg)",
"modid.midnightconfig.category.numbers": "Numbers",
Expand Down