Search in sources :

Example 1 with LangKey

use of net.minecraftforge.common.config.Config.LangKey in project MinecraftForge by MinecraftForge.

the class ConfigManager method createConfig.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static void createConfig(String modid, String category, Configuration cfg, Class<?> ftype, Field f, Object instance) {
    Property prop = null;
    String comment = null;
    Comment ca = f.getAnnotation(Comment.class);
    if (ca != null)
        comment = NEW_LINE.join(ca.value());
    String langKey = modid + "." + category + "." + f.getName().toLowerCase(Locale.ENGLISH);
    LangKey la = f.getAnnotation(LangKey.class);
    if (la != null)
        langKey = la.value();
    ITypeAdapter adapter = ADAPTERS.get(ftype);
    if (adapter != null) {
        if (category.isEmpty())
            throw new RuntimeException("Can not specify a primitive field when the category is empty: " + f.getDeclaringClass() + "/" + f.getName());
        prop = adapter.getProp(cfg, category, f, instance, comment);
        set(instance, f, adapter.getValue(prop));
    } else if (ftype.getSuperclass() == Enum.class) {
        if (category.isEmpty())
            throw new RuntimeException("Can not specify a primitive field when the category is empty: " + f.getDeclaringClass() + "/" + f.getName());
        Enum enu = (Enum) get(instance, f);
        prop = cfg.get(category, getName(f), enu.name(), comment);
        prop.setValidationPattern(makePattern((Class<? extends Enum>) ftype));
        set(instance, f, Enum.valueOf((Class<? extends Enum>) ftype, prop.getString()));
    } else if (ftype == Map.class) {
        if (category.isEmpty())
            throw new RuntimeException("Can not specify a primitive field when the category is empty: " + f.getDeclaringClass() + "/" + f.getName());
        String sub = category + "." + getName(f).toLowerCase(Locale.ENGLISH);
        Map<String, Object> m = (Map<String, Object>) get(instance, f);
        ParameterizedType type = (ParameterizedType) f.getGenericType();
        Type mtype = type.getActualTypeArguments()[1];
        cfg.getCategory(sub).setComment(comment);
        for (Entry<String, Object> e : m.entrySet()) {
            ITypeAdapter.Map adpt = MAP_ADAPTERS.get(mtype);
            if (adpt != null) {
                prop = adpt.getProp(cfg, sub, e.getKey(), e.getValue());
            } else if (mtype instanceof Class && ((Class<?>) mtype).getSuperclass() == Enum.class) {
                prop = TypeAdapters.Str.getProp(cfg, sub, e.getKey(), ((Enum) e.getValue()).name());
                prop.setValidationPattern(makePattern((Class<? extends Enum>) mtype));
            } else
                throw new RuntimeException("Unknown type in map! " + f.getDeclaringClass() + "/" + f.getName() + " " + mtype);
            prop.setLanguageKey(langKey + "." + e.getKey().toLowerCase(Locale.ENGLISH));
        }
        prop = null;
    } else if (//Only support classes that are one level below Object.
    ftype.getSuperclass() == Object.class) {
        String sub = (category.isEmpty() ? "" : category + ".") + getName(f).toLowerCase(Locale.ENGLISH);
        cfg.getCategory(sub).setComment(comment);
        Object sinst = get(instance, f);
        for (Field sf : ftype.getDeclaredFields()) {
            if (!Modifier.isPublic(sf.getModifiers()))
                continue;
            createConfig(modid, sub, cfg, sf.getType(), sf, sinst);
        }
    } else
        throw new RuntimeException("Unknown type in config! " + f.getDeclaringClass() + "/" + f.getName() + " " + ftype);
    if (prop != null) {
        prop.setLanguageKey(langKey);
        RangeInt ia = f.getAnnotation(RangeInt.class);
        if (ia != null) {
            prop.setMinValue(ia.min());
            prop.setMaxValue(ia.max());
            if (comment != null)
                prop.setComment(NEW_LINE.join(new String[] { comment, "Min: " + ia.min(), "Max: " + ia.max() }));
            else
                prop.setComment(NEW_LINE.join(new String[] { "Min: " + ia.min(), "Max: " + ia.max() }));
        }
        RangeDouble da = f.getAnnotation(RangeDouble.class);
        if (da != null) {
            prop.setMinValue(da.min());
            prop.setMaxValue(da.max());
            if (comment != null)
                prop.setComment(NEW_LINE.join(new String[] { comment, "Min: " + da.min(), "Max: " + da.max() }));
            else
                prop.setComment(NEW_LINE.join(new String[] { "Min: " + da.min(), "Max: " + da.max() }));
        }
    //TODO List length values
    }
}
Also used : Comment(net.minecraftforge.common.config.Config.Comment) RangeDouble(net.minecraftforge.common.config.Config.RangeDouble) LangKey(net.minecraftforge.common.config.Config.LangKey) ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Map(java.util.Map) RangeInt(net.minecraftforge.common.config.Config.RangeInt)

Aggregations

Field (java.lang.reflect.Field)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 Map (java.util.Map)1 Comment (net.minecraftforge.common.config.Config.Comment)1 LangKey (net.minecraftforge.common.config.Config.LangKey)1 RangeDouble (net.minecraftforge.common.config.Config.RangeDouble)1 RangeInt (net.minecraftforge.common.config.Config.RangeInt)1