From 957f1de812f47482ec99ccb05a546f498c0f15ca Mon Sep 17 00:00:00 2001 From: Cheeserolls Date: Sun, 24 May 2015 12:47:08 +0100 Subject: [PATCH] Expand capabilities of ConfigHelper --- .../common/util/config/ConfigHelper.java | 410 ++++++++++++------ 1 file changed, 283 insertions(+), 127 deletions(-) diff --git a/src/main/java/biomesoplenty/common/util/config/ConfigHelper.java b/src/main/java/biomesoplenty/common/util/config/ConfigHelper.java index ca86d8d89..535568243 100644 --- a/src/main/java/biomesoplenty/common/util/config/ConfigHelper.java +++ b/src/main/java/biomesoplenty/common/util/config/ConfigHelper.java @@ -21,6 +21,7 @@ import org.apache.commons.io.FileUtils; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -35,13 +36,9 @@ public class ConfigHelper public static Gson serializer = new GsonBuilder().setPrettyPrinting().create(); public static JsonParser parser = new JsonParser(); public JsonBlockState blockStateParser = new JsonBlockState(); - public JsonObject root; - public ArrayList messages; + public WrappedJsonObject root; + public ArrayList messages = new ArrayList(); - public ConfigHelper() - { - - } public ConfigHelper(String jsonString) { @@ -76,7 +73,7 @@ public class ConfigHelper { if (rootElement.isJsonObject()) { - this.root = rootElement.getAsJsonObject(); + this.root = new WrappedJsonObject(this, rootElement.getAsJsonObject()); } else { BiomesOPlenty.logger.error("Error parsing config: not a JSON object"); } @@ -91,126 +88,7 @@ public class ConfigHelper public void addMessage(String message) { this.messages.add(message); - } - - - - public Boolean getBool(String name, Boolean defaultVal) - { - if (this.root == null || !this.root.has(name)) {return defaultVal;} - try - { - return this.root.get(name).getAsBoolean(); - } catch (Exception e) { - this.addMessage("Error fetching boolean " + name + ": " + e.getMessage()); - return defaultVal; - } - } - - public String getString(String name, String defaultVal) - { - if (this.root == null || !this.root.has(name)) {return defaultVal;} - try - { - return this.root.get(name).getAsString(); - } catch (Exception e) { - this.addMessage("Error fetching string " + name + ": " + e.getMessage()); - return defaultVal; - } - } - - public Integer getInt(String name, Integer defaultVal) - { - if (this.root == null || !this.root.has(name)) {return defaultVal;} - try - { - return this.root.get(name).getAsInt(); - } catch (Exception e) { - this.addMessage("Error fetching integer " + name + ": " + e.getMessage()); - return defaultVal; - } - } - - public Float getFloat(String name, Float defaultVal) - { - if (this.root == null || !this.root.has(name)) {return defaultVal;} - try - { - return this.root.get(name).getAsFloat(); - } catch (Exception e) { - this.addMessage("Error fetching float " + name + ": " + e.getMessage()); - return defaultVal; - } - } - - public IBlockState getBlockState(String name, IBlockState defaultVal) - { - if (this.root == null || !this.root.has(name)) {return defaultVal;} - try { - - JsonObject ele = this.root.get(name).getAsJsonObject(); - - // attempt to load the specified block - if (!ele.has("block")) - { - this.addMessage("Block name missing"); - return defaultVal; - } - JsonElement blockName = ele.get("block"); - if (!blockName.isJsonPrimitive()) - { - this.addMessage("Invalid block name - must be a string"); - return defaultVal; - } - Block block = Block.getBlockFromName(blockName.getAsString()); - if (block == null) - { - this.addMessage("Unrecognised block name " + blockName.getAsString()); - return defaultVal; - } - - IBlockState state = block.getDefaultState(); - - // attempt to add properties - if (ele.has("properties")) { - - JsonElement properties = ele.get("properties"); - if (!properties.isJsonObject()) - { - this.addMessage("Invalid properties list - must be a JSON object"); - return state; - } - - for (Entry entry : properties.getAsJsonObject().entrySet()) - { - IProperty property = BlockStateUtils.getPropertyByName(state, entry.getKey()); - if (property != null) - { - Comparable propertyValue = BlockStateUtils.getPropertyValueByName(state, property, entry.getValue().getAsString()); - if (propertyValue != null) - { - state = state.withProperty(property, propertyValue); - } - else - { - this.addMessage("Invalid value " + entry.getValue().getAsString() + " for property " + entry.getKey()); - } - } - else - { - this.addMessage("Invalid property name: " + entry.getKey()); - } - } - } - - return state; - - } catch (Exception e) { - this.addMessage("Error fetching block state " + name + ": " + e.getMessage()); - return defaultVal; - } - } - + } public static boolean writeFile(File outputFile, Object obj) { @@ -223,4 +101,282 @@ public class ConfigHelper } } + + public WrappedJsonObject getObject(String name) + { + return this.root == null ? null : this.root.getObject(name); + } + public ArrayList getObjectArray(String name) + { + return this.root == null ? new ArrayList() : this.root.getObjectArray(name); + } + public ArrayList getBoolArray(String name) + { + return this.root == null ? new ArrayList() : this.root.getBoolArray(name); + } + public Boolean getBool(String name, Boolean defaultVal) + { + return this.root == null ? defaultVal : this.root.getBool(name, defaultVal); + } + public ArrayList getStringArray(String name) + { + return this.root == null ? new ArrayList() : this.root.getStringArray(name); + } + public String getString(String name, String defaultVal) + { + return this.root == null ? defaultVal : this.root.getString(name, defaultVal); + } + public ArrayList getIntArray(String name) + { + return this.root == null ? new ArrayList() : this.root.getIntArray(name); + } + public Integer getInt(String name, Integer defaultVal) + { + return this.root == null ? defaultVal : this.root.getInt(name, defaultVal); + } + public ArrayList getFloatArray(String name) + { + return this.root == null ? new ArrayList() : this.root.getFloatArray(name); + } + public Float getFloat(String name, Float defaultVal) + { + return this.root == null ? defaultVal : this.root.getFloat(name, defaultVal); + } + public ArrayList getBlockStateArray(String name) + { + return this.root == null ? new ArrayList() : this.root.getBlockStateArray(name); + } + public IBlockState getBlockState(String name, IBlockState defaultVal) + { + return this.root == null ? defaultVal : this.root.getBlockState(name, defaultVal); + } + + + + + + private static enum Types {BOOLEAN, STRING, INTEGER, FLOAT, BLOCKSTATE} + + private class WrappedJsonObject + { + + + protected JsonObject obj; + protected ConfigHelper conf; + WrappedJsonObject(ConfigHelper conf, JsonObject obj) + { + this.obj = obj; + this.conf = conf; + } + + public WrappedJsonObject getObject(String name) + { + if (this.obj == null || !this.obj.has(name)) {return null;} + try + { + JsonObject obj = this.obj.getAsJsonObject(name); + if (obj == null) {return null;} + return new WrappedJsonObject(this.conf, obj); + } catch (Exception e) { + this.conf.addMessage("Error fetching object " + name + ": " + e.getMessage()); + return null; + } + } + + public ArrayList getObjectArray(String name) + { + ArrayList list = new ArrayList(); + if (this.obj != null && this.obj.has(name)) { + try + { + JsonArray arr = this.obj.getAsJsonArray(name); + for (int i = 0; i < arr.size(); i++) + { + try + { + JsonObject obj = arr.get(i).getAsJsonObject(); + if (obj != null) {list.add( new WrappedJsonObject(this.conf, obj) );} + } catch (Exception e) { + this.conf.addMessage("Error fetching object from array " + name + " at index " + i + ": " + e.getMessage()); + } + } + + } catch (Exception e) { + this.conf.addMessage("Error fetching object array " + name + ": " + e.getMessage()); + return null; + } + } + return list; + } + + public ArrayList getBoolArray(String name) {return this.getArray(name, Types.BOOLEAN);} + public Boolean getBool(String name, Boolean defaultVal) {return this.get(name, defaultVal, Types.BOOLEAN);} + public ArrayList getStringArray(String name) {return this.getArray(name, Types.STRING);} + public String getString(String name, String defaultVal) {return this.get(name, defaultVal, Types.STRING);} + public ArrayList getIntArray(String name) {return this.getArray(name, Types.INTEGER);} + public Integer getInt(String name, Integer defaultVal) {return this.get(name, defaultVal, Types.INTEGER);} + public ArrayList getFloatArray(String name) {return this.getArray(name, Types.FLOAT);} + public Float getFloat(String name, Float defaultVal) {return this.get(name, defaultVal, Types.FLOAT);} + public ArrayList getBlockStateArray(String name) {return this.getArray(name, Types.BLOCKSTATE);} + public IBlockState getBlockState(String name, IBlockState defaultVal) {return this.get(name, defaultVal, Types.BLOCKSTATE);} + + + + + private T get(String name, T defaultVal, Types type) + { + if (this.obj == null || !this.obj.has(name)) {return defaultVal;} + T out = this.as(this.obj.get(name), type); + return out == null ? defaultVal : out; + } + + private ArrayList getArray(String name, Types type) + { + ArrayList list = new ArrayList(); + if (this.obj != null && this.obj.has(name)) { + try + { + JsonArray arr = this.obj.getAsJsonArray(name); + for (int i = 0; i < arr.size(); i++) + { + T ele = this.as(arr.get(i), type); + if (ele != null) {list.add(ele);} + } + } catch (Exception e) { + this.conf.addMessage("Error fetching " + type.toString().toLowerCase() + " array: " + e.getMessage()); + } + } + return list; + } + + private T as(JsonElement ele, Types type) + { + switch (type) { + case BOOLEAN: + return (T)this.asBool(ele); + case STRING: + return (T)this.asString(ele); + case INTEGER: + return (T)this.asInt(ele); + case FLOAT: + return (T)this.asFloat(ele); + case BLOCKSTATE: + return (T)this.asBlockState(ele); + default: + return null; + } + } + + public Boolean asBool(JsonElement ele) + { + try + { + return ele.getAsBoolean(); + } catch (Exception e) { + this.conf.addMessage("Error fetching boolean: " + e.getMessage()); + return null; + } + } + + public String asString(JsonElement ele) + { + try + { + return ele.getAsString(); + } catch (Exception e) { + this.conf.addMessage("Error fetching string: " + e.getMessage()); + return null; + } + } + + public Integer asInt(JsonElement ele) + { + try + { + return ele.getAsInt(); + } catch (Exception e) { + this.conf.addMessage("Error fetching integer: " + e.getMessage()); + return null; + } + } + + public Float asFloat(JsonElement ele) + { + try + { + return ele.getAsFloat(); + } catch (Exception e) { + this.conf.addMessage("Error fetching float: " + e.getMessage()); + return null; + } + } + + public IBlockState asBlockState(JsonElement arse) + { + try { + + JsonObject ele = arse.getAsJsonObject(); + + // attempt to load the specified block + if (!ele.has("block")) + { + this.conf.addMessage("Block name missing"); + return null; + } + JsonElement blockName = ele.get("block"); + if (!blockName.isJsonPrimitive()) + { + this.conf.addMessage("Invalid block name - must be a string"); + return null; + } + Block block = Block.getBlockFromName(blockName.getAsString()); + if (block == null) + { + this.conf.addMessage("Unrecognised block name " + blockName.getAsString()); + return null; + } + + IBlockState state = block.getDefaultState(); + + // attempt to add properties + if (ele.has("properties")) { + + JsonElement properties = ele.get("properties"); + if (!properties.isJsonObject()) + { + this.conf.addMessage("Invalid properties list - must be a JSON object"); + return state; + } + + for (Entry entry : properties.getAsJsonObject().entrySet()) + { + IProperty property = BlockStateUtils.getPropertyByName(state, entry.getKey()); + if (property != null) + { + Comparable propertyValue = BlockStateUtils.getPropertyValueByName(state, property, entry.getValue().getAsString()); + if (propertyValue != null) + { + state = state.withProperty(property, propertyValue); + } + else + { + this.conf.addMessage("Invalid value " + entry.getValue().getAsString() + " for property " + entry.getKey()); + } + } + else + { + this.conf.addMessage("Invalid property name: " + entry.getKey()); + } + } + } + + return state; + + } catch (Exception e) { + this.conf.addMessage("Error fetching blockstate: " + e.getMessage()); + return null; + } + } + } + } \ No newline at end of file