Added the ability to define custom categories for config files.

This commit is contained in:
CovertJaguar 2012-03-23 09:43:48 -07:00
parent 65b3742636
commit 1067a66c06
1 changed files with 79 additions and 57 deletions

View File

@ -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<String, Map<String, Property>> categories = new TreeMap<String, Map<String, Property>>();
public TreeMap<String, Property> blockProperties = new TreeMap<String, Property>();
public TreeMap<String, Property> itemProperties = new TreeMap<String, Property>();
public TreeMap<String, Property> generalProperties = new TreeMap<String, Property>();
@ -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<String, Property>());
}
/**
* 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<String, Property> 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<String, Property> 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<String, Property> 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<String, Property> currentMap = null;
Map<String, Property> 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<String, Map<String, Property>> 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();