package com.yolp900.itsjustacharm.config;
import com.yolp900.itsjustacharm.api.IJCConstants;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.fml.client.event.ConfigChangedEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import java.io.File;
public class ModConfig {
public static Configuration configuration;
public static void init(File configFile) {
if (configuration == null) {
configuration = new Configuration(configFile);
}
loadConfiguration();
}
private static void loadConfiguration() {
for (ModConfigs config : ModConfigs.values()) {
if (config.getType() == ModConfigs.configTypes.Boolean) {
config.setValueB(getBoolean(config, config.getBooleanDefault()));
} else if (config.getType() == ModConfigs.configTypes.Float) {
config.setValueF(getFloat(config, config.getFloatDefault(), config.getFloatMin(), config.getFloatMax()));
} else if (config.getType() == ModConfigs.configTypes.Int) {
config.setValueI(getInt(config, config.getIntDefault(), config.getIntMin(), config.getIntMax()));
} else if (config.getType() == ModConfigs.configTypes.String) {
config.setValueS(getString(config, config.getStringDefault(), config.getStringValidValues()));
}
}
if (configuration.hasChanged()) {
configuration.save();
}
}
private static boolean getBoolean(ModConfigs config, boolean defaultValue) {
return configuration.getBoolean(config.getTitle(), config.getCategory().getName(), defaultValue, config.getDesc());
}
private static float getFloat(ModConfigs config, float defaultValue, float minValue, float maxValue) {
return configuration.getFloat(config.getTitle(), config.getCategory().getName(), defaultValue, minValue, maxValue, config.getDesc());
}
private static int getInt(ModConfigs config, int defaultValue, int minValue, int maxValue) {
return configuration.getInt(config.getTitle(), config.getCategory().getName(), defaultValue, minValue, maxValue, config.getDesc());
}
private static String getString(ModConfigs config, String defaultValue, String[] validValues) {
return configuration.getString(config.getTitle(), config.getCategory().getName(), defaultValue, config.getDesc(), validValues);
}
@SubscribeEvent
public void onConfigurationChangedEvent(ConfigChangedEvent.OnConfigChangedEvent event) {
if (event.getModID().equalsIgnoreCase(IJCConstants.MOD_ID)) {
loadConfiguration();
}
}
}