Skip to content

Commit

Permalink
fix: Add translation keys to NeoForge config options
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Oct 24, 2024
1 parent 56a5898 commit 3ca93a6
Showing 1 changed file with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import net.neoforged.fml.config.ModConfig;
import net.neoforged.fml.event.config.ModConfigEvent;
import net.neoforged.fml.loading.FMLPaths;
import net.neoforged.neoforge.client.gui.ConfigurationScreen;
import net.neoforged.neoforge.client.gui.IConfigScreenFactory;
import net.neoforged.neoforge.common.ModConfigSpec;
import net.neoforged.neoforge.server.ServerLifecycleHooks;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -48,6 +50,8 @@ private <T extends BalmConfigData> IConfigSpec createConfigSpec(Class<T> clazz)
private void buildConfigSpec(String parentPath, ModConfigSpec.Builder builder, Class<?> clazz) throws IllegalAccessException {
List<Field> fields = ConfigReflection.getAllFields(clazz);
Object defaults = createConfigDataInstance(clazz);
final var modContainer = ModLoadingContext.get().getActiveContainer();
final var modId = modContainer.getModId();
for (Field field : fields) {
Class<?> type = field.getType();
Object defaultValue = field.get(defaults);
Expand All @@ -58,6 +62,8 @@ private void buildConfigSpec(String parentPath, ModConfigSpec.Builder builder, C
builder.comment(comment.value());
}

builder.translation("config." + modId + "." + path);

if (String.class.isAssignableFrom(type)) {
final var property = builder.define(path, (String) defaultValue);
configProperties.put(clazz, path, property);
Expand Down Expand Up @@ -243,17 +249,19 @@ private <T> void writeConfigValues(String parentPath, Class<?> clazz, T instance
@Override
public <T extends BalmConfigData> T initializeBackingConfig(Class<T> clazz) {
IConfigSpec configSpec = createConfigSpec(clazz);
ModLoadingContext.get().getActiveContainer().registerConfig(ModConfig.Type.COMMON, configSpec);
final var modContainer = ModLoadingContext.get().getActiveContainer();
modContainer.registerConfig(ModConfig.Type.COMMON, configSpec);
modContainer.registerExtensionPoint(IConfigScreenFactory.class, ConfigurationScreen::new);

ModLoadingContext.get().getActiveContainer().getEventBus().addListener((ModConfigEvent.Loading event) -> {
modContainer.getEventBus().addListener((ModConfigEvent.Loading event) -> {
configs.put(clazz, event.getConfig());
T newConfigData = readConfigValues(clazz, event.getConfig());
configData.put(clazz, newConfigData);

setActiveConfig(clazz, newConfigData);
});

ModLoadingContext.get().getActiveContainer().getEventBus().addListener((ModConfigEvent.Reloading event) -> {
modContainer.getEventBus().addListener((ModConfigEvent.Reloading event) -> {
configs.put(clazz, event.getConfig());
T newConfigData = readConfigValues(clazz, event.getConfig());
configData.put(clazz, newConfigData);
Expand Down

0 comments on commit 3ca93a6

Please sign in to comment.