Newer
Older
BetterEnhancement / src / main / java / com / watschman / betterenhancement / config / ConfigEntry.java
package com.watschman.betterenhancement.config;

import com.watschman.betterenhancement.reference.Reference;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property;

public class ConfigEntry {
    private Property property;
    private String identifier;
    private String category;
    private String comment;
    private ConfigType configType;
    private Object defaultValue;

    public ConfigEntry(String identifier, Object defaultValue) {
        this(identifier, defaultValue, "");
    }

    public ConfigEntry(String identifier, Object defaultValue, String comment) {
        this(identifier, defaultValue, comment, Configuration.CATEGORY_GENERAL);
    }

    public ConfigEntry(String identifier, Object defaultValue, String comment, String category) {
        this.identifier = identifier;
        this.defaultValue = defaultValue;
        this.comment = comment;
        this.category = category;
        initializeProperty();
    }

    public ConfigEntry setProperty(Property property) {
        this.property = property;
        return this;
    }

    public ConfigEntry setIdentifier(String identifier) {
        this.identifier = identifier;
        return this;
    }

    public ConfigEntry setCategory(String category) {
        this.category = category;
        return this;
    }

    public ConfigEntry setComment(String comment) {
        this.comment = comment;
        return this;
    }

    public ConfigEntry setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue;
        return this;
    }

    public ConfigEntry setConfigType(ConfigType configType) {
        this.configType = configType;
        return this;
    }

    public Property getProperty() {
        return this.property;
    }

    public String getIdentifier() {
        return this.identifier;
    }

    public String getCategory() {
        return this.category;
    }

    public String getComment() {
        return this.comment;
    }

    public Object getDefaultValue() {
        return this.defaultValue;
    }

    public ConfigType getConfigType() {
        return this.configType;
    }

    public Object getCurrentValue() {
        try {
            switch (this.configType) {
                case INT:
                    return this.property.getInt();
                case DOUBLE:
                    return this.property.getDouble();
                case BOOLEAN:
                    return this.property.getBoolean();
                case STRING:
                    return this.property.getString();
                case INT_ARRAY:
                    return this.property.getIntList();
                case DOUBLE_ARRAY:
                    return this.property.getDoubleList();
                case BOOLEAN_ARRAY:
                    return this.property.getBooleanList();
                case STRING_ARRAY:
                    return this.property.getStringList();
                default:
                    return null;
            }
        } catch (NullPointerException e) {

            Reference.MOD_LOGGER.info("NPE: returning default Value...");

            return this.defaultValue;
        }
    }

    private void initializeProperty() {
        if(this.defaultValue instanceof Integer) {
            this.configType = ConfigType.INT;
            this.property = Reference.CONFIG_INSTANCE.configuration.get(this.category, this.identifier, ConfigUtil.getPrimitiveInteger(this.defaultValue), this.comment);
        } else if(this.defaultValue instanceof Double) {
            this.configType = ConfigType.DOUBLE;
            this.property = Reference.CONFIG_INSTANCE.configuration.get(this.category, this.identifier, ConfigUtil.getPrimitiveDouble(this.defaultValue), this.comment);
        } else if(this.defaultValue instanceof Boolean) {
            this.configType = ConfigType.BOOLEAN;
            this.property = Reference.CONFIG_INSTANCE.configuration.get(this.category, this.identifier, ConfigUtil.getPrimitiveBoolean(this.defaultValue), this.comment);
        } else if(this.defaultValue instanceof String) {
            this.configType = ConfigType.STRING;
            this.property = Reference.CONFIG_INSTANCE.configuration.get(this.category, this.identifier, ConfigUtil.getPrimitiveString(this.defaultValue), this.comment);
        } else if(this.defaultValue instanceof Integer[]) {
            this.configType = ConfigType.INT_ARRAY;
            this.property = Reference.CONFIG_INSTANCE.configuration.get(this.category, this.identifier, ConfigUtil.getPrimitiveBooleanArray(this.defaultValue), this.comment);
        } else if(this.defaultValue instanceof Double[]) {
            this.configType = ConfigType.DOUBLE_ARRAY;
            this.property = Reference.CONFIG_INSTANCE.configuration.get(this.category, this.identifier, ConfigUtil.getPrimitiveDoubleArray(this.defaultValue), this.comment);
        } else if(this.defaultValue instanceof Boolean[]) {
            this.configType = ConfigType.BOOLEAN_ARRAY;
            this.property = Reference.CONFIG_INSTANCE.configuration.get(this.category, this.identifier, ConfigUtil.getPrimitiveBooleanArray(this.defaultValue), this.comment);
        } else if(this.defaultValue instanceof String[]) {
            this.configType = ConfigType.STRING_ARRAY;
            this.property = Reference.CONFIG_INSTANCE.configuration.get(this.category, this.identifier, ConfigUtil.getPrimitiveStringArray(this.defaultValue), this.comment);
        } else {
            Reference.MOD_LOGGER.error("Detected unknown PropertyValue... Not initializing it." + this.defaultValue.getClass().getSimpleName());
        }
    }
}