From 06eb9de126bcdac4d5ec1862d1c8299949f52739 Mon Sep 17 00:00:00 2001 From: LexManos Date: Sun, 25 Mar 2012 23:26:01 -0700 Subject: [PATCH] Fixed a couple of bugs in the Configuration categories pull request. Added helper functions to Property for standard int/boolean usage. --- .../minecraft/src/forge/Configuration.java | 24 +++--- .../net/minecraft/src/forge/Property.java | 77 +++++++++++++++++++ 2 files changed, 92 insertions(+), 9 deletions(-) diff --git a/forge/forge_common/net/minecraft/src/forge/Configuration.java b/forge/forge_common/net/minecraft/src/forge/Configuration.java index b8ef22a21..2f888328a 100755 --- a/forge/forge_common/net/minecraft/src/forge/Configuration.java +++ b/forge/forge_common/net/minecraft/src/forge/Configuration.java @@ -58,15 +58,17 @@ public class Configuration { this.file = file; categories.put(CATEGORY_GENERAL, generalProperties); - categories.put(CATEGORY_BLOCK, generalProperties); + categories.put(CATEGORY_BLOCK, blockProperties); categories.put(CATEGORY_ITEM, itemProperties); } /** * Helper for depreciated functions */ - private String getCategoryFromIndex(int kind){ - switch(kind){ + private String getCategoryFromIndex(int kind) + { + switch(kind) + { case 1: return CATEGORY_BLOCK; case 2: @@ -131,7 +133,8 @@ public class Configuration } @Deprecated - public Property getOrCreateIntProperty(String key, int kind, int defaultValue){ + public Property getOrCreateIntProperty(String key, int kind, int defaultValue) + { return getOrCreateIntProperty(key, getCategoryFromIndex(kind), defaultValue); } @@ -151,7 +154,8 @@ public class Configuration } @Deprecated - public Property getOrCreateBooleanProperty(String key, int kind, boolean defaultValue){ + public Property getOrCreateBooleanProperty(String key, int kind, boolean defaultValue) + { return getOrCreateBooleanProperty(key, getCategoryFromIndex(kind), defaultValue); } @@ -170,7 +174,8 @@ public class Configuration } @Deprecated - public Property getOrCreateProperty(String key, int kind, String defaultValue){ + public Property getOrCreateProperty(String key, int kind, String defaultValue) + { return getOrCreateProperty(key, getCategoryFromIndex(kind), defaultValue); } @@ -179,7 +184,8 @@ public class Configuration category = category.toLowerCase(); Map source = categories.get(category); - if(source == null){ + if(source == null) + { source = new TreeMap(); categories.put(category, source); } @@ -296,7 +302,6 @@ public class Configuration } } } - } } catch (IOException e) @@ -328,7 +333,8 @@ public class Configuration buffer.write("# Generated on " + DateFormat.getInstance().format(new Date()) + "\r\n"); buffer.write("\r\n"); - for(Map.Entry> category : categories.entrySet()){ + for(Map.Entry> category : categories.entrySet()) + { buffer.write("####################\r\n"); buffer.write("# " + category.getKey() + " \r\n"); buffer.write("####################\r\n\r\n"); diff --git a/forge/forge_common/net/minecraft/src/forge/Property.java b/forge/forge_common/net/minecraft/src/forge/Property.java index b7501a7bc..471f47b5f 100755 --- a/forge/forge_common/net/minecraft/src/forge/Property.java +++ b/forge/forge_common/net/minecraft/src/forge/Property.java @@ -10,4 +10,81 @@ public class Property public String name; public String value; public String comment; + + /** + * Returns the value in this property as a integer, + * if the value is not a valid integer, it will return -1. + * + * @return The value + */ + public int getInt() + { + return getInt(-1); + } + + /** + * Returns the value in this property as a integer, + * if the value is not a valid integer, it will return the + * provided default. + * + * @param _default The default to provide if the current value is not a valid integer + * @return The value + */ + public int getInt(int _default) + { + try + { + return Integer.parseInt(value); + } + catch (NumberFormatException e) + { + return _default; + } + } + + /** + * Checks if the current value stored in this property can be converted to an integer. + * @return True if the vslue can be converted to an integer + */ + public boolean isIntValue() + { + try + { + Integer.parseInt(value); + return true; + } + catch (NumberFormatException e) + { + return false; + } + } + + /** + * Returns the value in this property as a boolean, + * if the value is not a valid boolean, it will return the + * provided default. + * + * @param _default The default to provide + * @return The value as a boolean, or the default + */ + public boolean getBoolean(boolean _default) + { + if (isBooleanValue()) + { + return Boolean.parseBoolean(value); + } + else + { + return _default; + } + } + + /** + * Checks if the current value held by this property is a valid boolean value. + * @return True if it is a boolean value + */ + public boolean isBooleanValue() + { + return ("true".equals(value.toLowerCase()) || "false".equals(value.toLowerCase())); + } }