    001/**
    002 * This software is provided under the terms of the Minecraft Forge Public
    003 * License v1.0.
    004 */
    005
    006package net.minecraftforge.common;
    007
    008import java.util.ArrayList;
    009
    010import net.minecraftforge.common.Property.Type;
    011
    012public class Property
    013{
    014    public enum Type
    015    {
    016        STRING,
    017        INTEGER,
    018        BOOLEAN,
    019        DOUBLE;
    020
    021        private static Type[] values = {STRING, INTEGER, BOOLEAN, DOUBLE};
    022
    023        public static Type tryParse(char id)
    024        {
    025            for (int x = 0; x < values.length; x++)
    026            {
    027                if (values[x].getID() == id)
    028                {
    029                    return values[x];
    030                }
    031            }
    032
    033            return STRING;
    034        }
    035
    036        public char getID()
    037        {
    038            return name().charAt(0);
    039        }
    040    }
    041
    042    private String name;
    043    public String value;
    044    public String comment;
    045    public String[] valueList;
    046
    047    private final boolean wasRead;
    048    private final boolean isList;
    049    private final Type type;
    050
    051    public Property()
    052    {
    053        wasRead = false;
    054        type    = null;
    055        isList  = false;
    056    }
    057
    058    public Property(String name, String value, Type type)
    059    {
    060        this(name, value, type, false);
    061    }
    062
    063    Property(String name, String value, Type type, boolean read)
    064    {
    065        setName(name);
    066        this.value = value;
    067        this.type  = type;
    068        wasRead    = read;
    069        isList     = false;
    070    }
    071
    072    public Property(String name, String[] values, Type type)
    073    {
    074        this(name, values, type, false);
    075    }
    076
    077    Property(String name, String[] values, Type type, boolean read)
    078    {
    079        setName(name);
    080        this.type  = type;
    081        valueList  = values;
    082        wasRead    = read;
    083        isList     = true;
    084    }
    085
    086    /**
    087     * Returns the value in this property as an integer,
    088     * if the value is not a valid integer, it will return -1.
    089     *
    090     * @return The value
    091     */
    092    public int getInt()
    093    {
    094        return getInt(-1);
    095    }
    096
    097    /**
    098     * Returns the value in this property as an integer,
    099     * if the value is not a valid integer, it will return the
    100     * provided default.
    101     *
    102     * @param _default The default to provide if the current value is not a valid integer
    103     * @return The value
    104     */
    105    public int getInt(int _default)
    106    {
    107        try
    108        {
    109            return Integer.parseInt(value);
    110        }
    111        catch (NumberFormatException e)
    112        {
    113            return _default;
    114        }
    115    }
    116
    117    /**
    118     * Checks if the current value stored in this property can be converted to an integer.
    119     * @return True if the type of the Property is an Integer
    120     */
    121    public boolean isIntValue()
    122    {
    123        try
    124        {
    125            Integer.parseInt(value);
    126            return true;
    127        }
    128        catch (NumberFormatException e)
    129        {
    130            return false;
    131        }
    132    }
    133
    134    /**
    135     * Returns the value in this property as a boolean,
    136     * if the value is not a valid boolean, it will return the
    137     * provided default.
    138     *
    139     * @param _default The default to provide
    140     * @return The value as a boolean, or the default
    141     */
    142    public boolean getBoolean(boolean _default)
    143    {
    144        if (isBooleanValue())
    145        {
    146            return Boolean.parseBoolean(value);
    147        }
    148        else
    149        {
    150            return _default;
    151        }
    152    }
    153
    154    /**
    155     * Checks if the current value held by this property is a valid boolean value.
    156     * @return True if it is a boolean value
    157     */
    158    public boolean isBooleanValue()
    159    {
    160        return ("true".equals(value.toLowerCase()) || "false".equals(value.toLowerCase()));
    161    }
    162
    163    /**
    164     * Checks if the current value held by this property is a valid double value.
    165     * @return True if the value can be converted to an double
    166     */
    167    public boolean isDoubleValue()
    168    {
    169        try
    170        {
    171            Double.parseDouble(value);
    172            return true;
    173        }
    174        catch (NumberFormatException e)
    175        {
    176            return false;
    177        }
    178    }
    179
    180    /**
    181     * Returns the value in this property as a double,
    182     * if the value is not a valid double, it will return the
    183     * provided default.
    184     *
    185     * @param _default The default to provide if the current value is not a valid double
    186     * @return The value
    187     */
    188    public double getDouble(double _default)
    189    {
    190        try
    191        {
    192            return Double.parseDouble(value);
    193        }
    194        catch (NumberFormatException e)
    195        {
    196            return _default;
    197        }
    198    }
    199
    200    /**
    201     * Returns the integer value of all values that can
    202     * be parsed in the list.
    203     *
    204     * @return Array of length 0 if none of the values could be parsed.
    205     */
    206    public int[] getIntList()
    207    {
    208        ArrayList<Integer> nums = new ArrayList<Integer>();
    209
    210        for (String value : valueList)
    211        {
    212            try
    213            {
    214                nums.add(Integer.parseInt(value));
    215            }
    216            catch (NumberFormatException e){}
    217        }
    218
    219        int[] primitives = new int[nums.size()];
    220
    221        for (int i = 0; i < nums.size(); i++)
    222        {
    223            primitives[i] = nums.get(i);
    224        }
    225
    226        return primitives;
    227    }
    228
    229    /**
    230     * Checks if all of the current values stored in this property can be converted to an integer.
    231     * @return True if the type of the Property is an Integer List
    232     */
    233    public boolean isIntList()
    234    {
    235        for (String value : valueList)
    236        {
    237            try
    238            {
    239                Integer.parseInt(value);
    240            }
    241            catch (NumberFormatException e)
    242            {
    243                return false;
    244            }
    245        }
    246        return true;
    247    }
    248
    249    /**
    250     * Returns the boolean value of all values that can
    251     * be parsed in the list.
    252     *
    253     * @return Array of length 0 if none of the values could be parsed.
    254     */
    255    public boolean[] getBooleanList()
    256    {
    257        ArrayList<Boolean> values = new ArrayList<Boolean>();
    258        for (String value : valueList)
    259        {
    260            try
    261            {
    262                values.add(Boolean.parseBoolean(value));
    263            }
    264            catch (NumberFormatException e){}
    265        }
    266
    267        boolean[] primitives = new boolean[values.size()];
    268
    269        for (int i = 0; i < values.size(); i++)
    270        {
    271            primitives[i] = values.get(i);
    272        }
    273
    274        return primitives;
    275    }
    276
    277    /**
    278     * Checks if all of current values stored in this property can be converted to a boolean.
    279     * @return True if it is a boolean value
    280     */
    281    public boolean isBooleanList()
    282    {
    283        for (String value : valueList)
    284        {
    285            if (!"true".equalsIgnoreCase(value) && !"false".equalsIgnoreCase(value))
    286            {
    287                return false;
    288            }
    289        }
    290
    291        return true;
    292    }
    293
    294    /**
    295     * Returns the double value of all values that can
    296     * be parsed in the list.
    297     *
    298     * @return Array of length 0 if none of the values could be parsed.
    299     */
    300    public double[] getDoubleList()
    301    {
    302        ArrayList<Double> values = new ArrayList<Double>();
    303        for (String value : valueList)
    304        {
    305            try
    306            {
    307                values.add(Double.parseDouble(value));
    308            }
    309            catch (NumberFormatException e) {}
    310        }
    311
    312        double[] primitives = new double[values.size()];
    313
    314        for (int i = 0; i < values.size(); i++)
    315        {
    316            primitives[i] = values.get(i);
    317        }
    318
    319        return primitives;
    320    }
    321
    322    /**
    323     * Checks if all of the current values stored in this property can be converted to a double.
    324     * @return True if the type of the Property is a double List
    325     */
    326    public boolean isDoubleList()
    327    {
    328        for (String value : valueList)
    329        {
    330            try
    331            {
    332                Double.parseDouble(value);
    333            }
    334            catch (NumberFormatException e)
    335            {
    336                return false;
    337            }
    338        }
    339
    340        return true;
    341    }
    342
    343    public String getName()
    344    {
    345        return name;
    346    }
    347
    348    public void setName(String name)
    349    {
    350        this.name = name;
    351    }
    352
    353    /**
    354     * Determines if this config value was just created, or if it was read from the config file.
    355     * This is useful for mods who auto-assign there blocks to determine if the ID returned is
    356     * a configured one, or a automatically generated one.
    357     *
    358     * @return True if this property was loaded from the config file with a value
    359     */
    360    public boolean wasRead()
    361    {
    362        return wasRead;
    363    }
    364
    365    public Type getType()
    366    {
    367        return type;
    368    }
    369
    370    public boolean isList()
    371    {
    372        return isList;
    373    }
    374}