From 1067a66c0694b95829ff8a1e175f07fc57f0467a Mon Sep 17 00:00:00 2001 From: CovertJaguar Date: Fri, 23 Mar 2012 09:43:48 -0700 Subject: [PATCH] Added the ability to define custom categories for config files. --- .../minecraft/src/forge/Configuration.java | 136 ++++++++++-------- 1 file changed, 79 insertions(+), 57 deletions(-) diff --git a/forge/forge_common/net/minecraft/src/forge/Configuration.java b/forge/forge_common/net/minecraft/src/forge/Configuration.java index e1350001e..824bc9fe7 100755 --- a/forge/forge_common/net/minecraft/src/forge/Configuration.java +++ b/forge/forge_common/net/minecraft/src/forge/Configuration.java @@ -16,6 +16,7 @@ import java.io.OutputStreamWriter; import java.text.DateFormat; import java.util.Collection; import java.util.Date; +import java.util.Map; import java.util.TreeMap; import net.minecraft.src.Block; @@ -29,12 +30,23 @@ public class Configuration private boolean configBlocks[] = null; + public static final String CATEGORY_GENERAL = "general"; + public static final String CATEGORY_BLOCK = "block"; + public static final String CATEGORY_ITEM = "item"; + + @Deprecated public static final int GENERAL_PROPERTY = 0; + + @Deprecated public static final int BLOCK_PROPERTY = 1; + + @Deprecated public static final int ITEM_PROPERTY = 2; File file; - + + public Map> categories = new TreeMap>(); + public TreeMap blockProperties = new TreeMap(); public TreeMap itemProperties = new TreeMap(); public TreeMap generalProperties = new TreeMap(); @@ -45,6 +57,31 @@ public class Configuration public Configuration(File file) { this.file = file; + categories.put(CATEGORY_GENERAL, generalProperties); + categories.put(CATEGORY_BLOCK, generalProperties); + categories.put(CATEGORY_ITEM, itemProperties); + } + + /** + * Creates a new category, call before calling load(). + * @param category The category name + */ + public final void createCategory(String category){ + categories.put(category.toLowerCase(), new TreeMap()); + } + + /** + * Helper for depreciated functions + */ + private String getCategoryFromIndex(int kind){ + switch(kind){ + case 1: + return CATEGORY_BLOCK; + case 2: + return CATEGORY_ITEM; + default: + return CATEGORY_GENERAL; + } } /** @@ -64,10 +101,11 @@ public class Configuration configBlocks[i] = false; } } - + + Map blockProperties = categories.get(CATEGORY_BLOCK); if (blockProperties.containsKey(key)) { - Property property = getOrCreateIntProperty(key, Configuration.BLOCK_PROPERTY, defaultId); + Property property = getOrCreateIntProperty(key, Configuration.CATEGORY_BLOCK, defaultId); configBlocks[Integer.parseInt(property.value)] = true; return property; } @@ -100,9 +138,14 @@ public class Configuration } } - public Property getOrCreateIntProperty(String key, int kind, int defaultValue) + @Deprecated + public Property getOrCreateIntProperty(String key, int kind, int defaultValue){ + return getOrCreateIntProperty(key, getCategoryFromIndex(kind), defaultValue); + } + + public Property getOrCreateIntProperty(String key, String category, int defaultValue) { - Property prop = getOrCreateProperty(key, kind, Integer.toString(defaultValue)); + Property prop = getOrCreateProperty(key, category, Integer.toString(defaultValue)); try { Integer.parseInt(prop.value); @@ -114,10 +157,15 @@ public class Configuration return prop; } } - - public Property getOrCreateBooleanProperty(String key, int kind, boolean defaultValue) + + @Deprecated + public Property getOrCreateBooleanProperty(String key, int kind, boolean defaultValue){ + return getOrCreateBooleanProperty(key, getCategoryFromIndex(kind), defaultValue); + } + + public Property getOrCreateBooleanProperty(String key, String category, boolean defaultValue) { - Property prop = getOrCreateProperty(key, kind, Boolean.toString(defaultValue)); + Property prop = getOrCreateProperty(key, category, Boolean.toString(defaultValue)); if ("true".equals(prop.value.toLowerCase()) || "false".equals(prop.value.toLowerCase())) { return prop; @@ -129,20 +177,18 @@ public class Configuration } } - public Property getOrCreateProperty(String key, int kind, String defaultValue) + @Deprecated + public Property getOrCreateProperty(String key, int kind, String defaultValue){ + return getOrCreateProperty(key, getCategoryFromIndex(kind), defaultValue); + } + + public Property getOrCreateProperty(String key, String category, String defaultValue) { - TreeMap source = null; - switch (kind) - { - case GENERAL_PROPERTY: - source = generalProperties; - break; - case BLOCK_PROPERTY: - source = blockProperties; - break; - case ITEM_PROPERTY: - source = itemProperties; - break; + category = category.toLowerCase(); + Map source = categories.get(category); + + if(source == null){ + throw new RuntimeException("unknown section " + category); } if (source.containsKey(key)) @@ -185,7 +231,7 @@ public class Configuration BufferedReader buffer = new BufferedReader(new InputStreamReader(fileinputstream, "8859_1")); String line; - TreeMap currentMap = null; + Map currentMap = null; while (true) { @@ -224,19 +270,8 @@ public class Configuration case '{': String scopeName = line.substring(nameStart, nameEnd + 1); - if (scopeName.equals("general")) - { - currentMap = generalProperties; - } - else if (scopeName.equals("block")) - { - currentMap = blockProperties; - } - else if (scopeName.equals("item")) - { - currentMap = itemProperties; - } - else + currentMap = categories.get(scopeName); + if (currentMap == null) { throw new RuntimeException("unknown section " + scopeName); } @@ -298,29 +333,16 @@ public class Configuration buffer.write("# Configuration file\r\n"); buffer.write("# Generated on " + DateFormat.getInstance().format(new Date()) + "\r\n"); buffer.write("\r\n"); - buffer.write("###########\r\n"); - buffer.write("# General #\r\n"); - buffer.write("###########\r\n\r\n"); + + for(Map.Entry> category : categories.entrySet()){ + buffer.write("####################\r\n"); + buffer.write("# " + category.getKey() + " \r\n"); + buffer.write("####################\r\n\r\n"); - buffer.write("general {\r\n"); - writeProperties(buffer, generalProperties.values()); - buffer.write("}\r\n\r\n"); - - buffer.write("#########\r\n"); - buffer.write("# Block #\r\n"); - buffer.write("#########\r\n\r\n"); - - buffer.write("block {\r\n"); - writeProperties(buffer, blockProperties.values()); - buffer.write("}\r\n\r\n"); - - buffer.write("########\r\n"); - buffer.write("# Item #\r\n"); - buffer.write("########\r\n\r\n"); - - buffer.write("item {\r\n"); - writeProperties(buffer, itemProperties.values()); - buffer.write("}\r\n\r\n"); + buffer.write(category.getKey() + " {\r\n"); + writeProperties(buffer, category.getValue().values()); + buffer.write("}\r\n\r\n"); + } buffer.close(); fos.close();