<commit_before>package mytown.core.utils.config;

import java.lang.reflect.Field;
import java.util.Map;

import mytown.core.MyTownCore;
import mytown.core.utils.Log;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property;

import com.google.common.collect.ImmutableMap;

public class ConfigProcessor {
	/**
	 * Maps classes to the appropriate Property.Type
	 */
	private static Map<Class<?>, Property.Type> CONFIG_TYPES = ImmutableMap.<Class<?>, Property.Type> builder().put(Integer.class, Property.Type.INTEGER).put(int.class, Property.Type.INTEGER).put(Integer[].class, Property.Type.INTEGER).put(int[].class, Property.Type.INTEGER).put(Double.class, Property.Type.DOUBLE).put(double.class, Property.Type.DOUBLE).put(Double[].class, Property.Type.DOUBLE).put(double[].class, Property.Type.DOUBLE).put(Boolean.class, Property.Type.BOOLEAN).put(boolean.class, Property.Type.BOOLEAN).put(Boolean[].class, Property.Type.BOOLEAN).put(boolean[].class, Property.Type.BOOLEAN).put(String.class, Property.Type.STRING).put(String[].class, Property.Type.STRING).build();

	private static Log log;
	
	/**
	 * @see ConfigProcessor#load(Configuration, Class, Object)
	 * @param config
	 * @param c
	 */
	public static void load(Configuration config, Class<?> c) {
		load(config, c, null);
	}

	/**
	 * load all static fields in c with {@link ConfigProperty} annotation and loads their value from the given config
	 * 
	 * @param config
	 * @param c
	 */
	public static void load(Configuration config, Class<?> c, Object obj) {
		getLog().info("Class: %s", c.getName());
		for (Field f : c.getDeclaredFields()) {
			getLog().info("- Field: %s", f.getName());
			ConfigProperty propAnnot = f.getAnnotation(ConfigProperty.class);
			if (propAnnot == null)
				return;
			String category = propAnnot.category();
			String key = (propAnnot.name().isEmpty() || propAnnot.name() == null) ? f.getName() : propAnnot.name();
			String comment = propAnnot.comment();
			ConfigProcessor.setField(f, obj, config, category, key, comment);
			config.save();
		}
	}
	
	/**
	 * @see ConfigProcessor#save(Configuration, Class, Object)
	 * @param config
	 * @param c
	 */
	public static void save(Configuration config, Class<?> c) {
		save(config, c, null);
	}
	
	/**
	 * Saves all static fields in c with {@link ConfigProcessor} annotation to the config
	 * 
	 * @param config
	 * @param c
	 */
	public static void save(Configuration config, Class<?> c, Object obj) {
		for (Field f : c.getFields()) {
			ConfigProperty propAnnot = f.getAnnotation(ConfigProperty.class);
			if (propAnnot == null)
				return;
			String category = propAnnot.category();
			String key = (propAnnot.name().isEmpty() || propAnnot.name() == null) ? f.getName() : propAnnot.name();
			String comment = propAnnot.comment();
			ConfigProcessor.setConfig(f, obj, config, category, key, comment);
			config.save();
		}
	}

	private static void setField(Field f, Object obj, Configuration config, String category, String key, String comment) {
		if (f == null || config == null) {
			ConfigProcessor.getLog().warn("Field or Config was null");
			return;
		}
		Property.Type type = ConfigProcessor.CONFIG_TYPES.get(f.getType());
		if (type == null) {
			ConfigProcessor.getLog().warn("Unknown config type for field type: %s", f.getType().getName());
			return;
		}
		try {
			f.setAccessible(true);
			Object defaultValue = f.get(obj);
			switch (type) {
				case INTEGER:
					if (f.getType().isArray()) {
						f.set(obj, config.get(category, key, (int[]) defaultValue, comment).getIntList());<commit_msg>Made ConfigProcessor add the default value to the comment if its set. Needs testing, but should work.<commit_after>package mytown.core.utils.config;

import java.lang.reflect.Field;
import java.util.Map;

import mytown.core.MyTownCore;
import mytown.core.utils.Log;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property;

import com.google.common.collect.ImmutableMap;

public class ConfigProcessor {
	/**
	 * Maps classes to the appropriate Property.Type
	 */
	private static Map<Class<?>, Property.Type> CONFIG_TYPES = ImmutableMap.<Class<?>, Property.Type> builder().put(Integer.class, Property.Type.INTEGER).put(int.class, Property.Type.INTEGER).put(Integer[].class, Property.Type.INTEGER).put(int[].class, Property.Type.INTEGER).put(Double.class, Property.Type.DOUBLE).put(double.class, Property.Type.DOUBLE).put(Double[].class, Property.Type.DOUBLE).put(double[].class, Property.Type.DOUBLE).put(Boolean.class, Property.Type.BOOLEAN).put(boolean.class, Property.Type.BOOLEAN).put(Boolean[].class, Property.Type.BOOLEAN).put(boolean[].class, Property.Type.BOOLEAN).put(String.class, Property.Type.STRING).put(String[].class, Property.Type.STRING).build();

	private static Log log;
	
	/**
	 * @see ConfigProcessor#load(Configuration, Class, Object)
	 * @param config
	 * @param c
	 */
	public static void load(Configuration config, Class<?> c) {
		load(config, c, null);
	}

	/**
	 * load all static fields in c with {@link ConfigProperty} annotation and loads their value from the given config
	 * 
	 * @param config
	 * @param c
	 */
	public static void load(Configuration config, Class<?> c, Object obj) {
		getLog().info("Class: %s", c.getName());
		for (Field f : c.getDeclaredFields()) {
			getLog().info("- Field: %s", f.getName());
			ConfigProperty propAnnot = f.getAnnotation(ConfigProperty.class);
			if (propAnnot == null)
				return;
			String category = propAnnot.category();
			String key = (propAnnot.name().isEmpty() || propAnnot.name() == null) ? f.getName() : propAnnot.name();
			String comment = propAnnot.comment();
			ConfigProcessor.setField(f, obj, config, category, key, comment);
			config.save();
		}
	}
	
	/**
	 * @see ConfigProcessor#save(Configuration, Class, Object)
	 * @param config
	 * @param c
	 */
	public static void save(Configuration config, Class<?> c) {
		save(config, c, null);
	}
	
	/**
	 * Saves all static fields in c with {@link ConfigProcessor} annotation to the config
	 * 
	 * @param config
	 * @param c
	 */
	public static void save(Configuration config, Class<?> c, Object obj) {
		for (Field f : c.getFields()) {
			ConfigProperty propAnnot = f.getAnnotation(ConfigProperty.class);
			if (propAnnot == null)
				return;
			String category = propAnnot.category();
			String key = (propAnnot.name().isEmpty() || propAnnot.name() == null) ? f.getName() : propAnnot.name();
			String comment = propAnnot.comment();
			ConfigProcessor.setConfig(f, obj, config, category, key, comment);
			config.save();
		}
	}

	private static void setField(Field f, Object obj, Configuration config, String category, String key, String comment) {
		if (f == null || config == null) {
			ConfigProcessor.getLog().warn("Field or Config was null");
			return;
		}
		Property.Type type = ConfigProcessor.CONFIG_TYPES.get(f.getType());
		if (type == null) {
			ConfigProcessor.getLog().warn("Unknown config type for field type: %s", f.getType().getName());
			return;
		}
		try {
			f.setAccessible(true);
			Object defaultValue = f.get(obj);
            if (defaultValue != null && comment != null && !comment.isEmpty()) { // Display the default value in the comment! :D
                comment += "\nDefault: " + defaultValue.toString();
            }
			switch (type) {
				case INTEGER:
					if (f.getType().isArray()) {
						f.set(obj, config.get(category, key, (int[]) defaultValue, comment).getIntList());
{'fraction_non_alphanumeric': 0.15134125183504604, 'fraction_numerical': 0.0, 'mean_word_length': 4.2295882763433355, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2275691', 'n_tokens_mistral': 2467, 'n_tokens_neox': 2280, 'n_words': 574}