Merge pull request #22 from CovertJaguar/patch-5

Added the ability to define custom categories for config files.
This commit is contained in:
LexManos 2012-03-25 22:24:08 -07:00
commit dec539f6f9

View file

@ -16,6 +16,7 @@ import java.io.OutputStreamWriter;
import java.text.DateFormat; import java.text.DateFormat;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
import net.minecraft.src.Block; import net.minecraft.src.Block;
@ -29,12 +30,23 @@ public class Configuration
private boolean configBlocks[] = null; 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; public static final int GENERAL_PROPERTY = 0;
@Deprecated
public static final int BLOCK_PROPERTY = 1; public static final int BLOCK_PROPERTY = 1;
@Deprecated
public static final int ITEM_PROPERTY = 2; public static final int ITEM_PROPERTY = 2;
File file; File file;
public Map<String, Map<String, Property>> categories = new TreeMap<String, Map<String, Property>>();
public TreeMap<String, Property> blockProperties = new TreeMap<String, Property>(); public TreeMap<String, Property> blockProperties = new TreeMap<String, Property>();
public TreeMap<String, Property> itemProperties = new TreeMap<String, Property>(); public TreeMap<String, Property> itemProperties = new TreeMap<String, Property>();
public TreeMap<String, Property> generalProperties = new TreeMap<String, Property>(); public TreeMap<String, Property> generalProperties = new TreeMap<String, Property>();
@ -45,6 +57,23 @@ public class Configuration
public Configuration(File file) public Configuration(File file)
{ {
this.file = file; this.file = file;
categories.put(CATEGORY_GENERAL, generalProperties);
categories.put(CATEGORY_BLOCK, generalProperties);
categories.put(CATEGORY_ITEM, itemProperties);
}
/**
* 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;
}
} }
/** /**
@ -65,9 +94,10 @@ public class Configuration
} }
} }
Map<String, Property> blockProperties = categories.get(CATEGORY_BLOCK);
if (blockProperties.containsKey(key)) 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; configBlocks[Integer.parseInt(property.value)] = true;
return property; return property;
} }
@ -100,9 +130,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 try
{ {
Integer.parseInt(prop.value); Integer.parseInt(prop.value);
@ -115,9 +150,14 @@ public class Configuration
} }
} }
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())) if ("true".equals(prop.value.toLowerCase()) || "false".equals(prop.value.toLowerCase()))
{ {
return prop; return prop;
@ -129,20 +169,19 @@ 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<String, Property> source = null; category = category.toLowerCase();
switch (kind) Map<String, Property> source = categories.get(category);
{
case GENERAL_PROPERTY: if(source == null){
source = generalProperties; source = new TreeMap<String, Property>();
break; categories.put(category, source);
case BLOCK_PROPERTY:
source = blockProperties;
break;
case ITEM_PROPERTY:
source = itemProperties;
break;
} }
if (source.containsKey(key)) if (source.containsKey(key))
@ -185,7 +224,7 @@ public class Configuration
BufferedReader buffer = new BufferedReader(new InputStreamReader(fileinputstream, "8859_1")); BufferedReader buffer = new BufferedReader(new InputStreamReader(fileinputstream, "8859_1"));
String line; String line;
TreeMap<String, Property> currentMap = null; Map<String, Property> currentMap = null;
while (true) while (true)
{ {
@ -224,21 +263,11 @@ public class Configuration
case '{': case '{':
String scopeName = line.substring(nameStart, nameEnd + 1); String scopeName = line.substring(nameStart, nameEnd + 1);
if (scopeName.equals("general")) currentMap = categories.get(scopeName);
if (currentMap == null)
{ {
currentMap = generalProperties; currentMap = new TreeMap<String, Property>();
} categories.put(scopeName, currentMap);
else if (scopeName.equals("block"))
{
currentMap = blockProperties;
}
else if (scopeName.equals("item"))
{
currentMap = itemProperties;
}
else
{
throw new RuntimeException("unknown section " + scopeName);
} }
break; break;
@ -298,29 +327,16 @@ public class Configuration
buffer.write("# Configuration file\r\n"); buffer.write("# Configuration file\r\n");
buffer.write("# Generated on " + DateFormat.getInstance().format(new Date()) + "\r\n"); buffer.write("# Generated on " + DateFormat.getInstance().format(new Date()) + "\r\n");
buffer.write("\r\n"); buffer.write("\r\n");
buffer.write("###########\r\n");
buffer.write("# General #\r\n");
buffer.write("###########\r\n\r\n");
buffer.write("general {\r\n"); for(Map.Entry<String, Map<String, Property>> category : categories.entrySet()){
writeProperties(buffer, generalProperties.values()); buffer.write("####################\r\n");
buffer.write("}\r\n\r\n"); buffer.write("# " + category.getKey() + " \r\n");
buffer.write("####################\r\n\r\n");
buffer.write("#########\r\n"); buffer.write(category.getKey() + " {\r\n");
buffer.write("# Block #\r\n"); writeProperties(buffer, category.getValue().values());
buffer.write("#########\r\n\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.close(); buffer.close();
fos.close(); fos.close();