Skip to content

Commit

Permalink
JSON Parser error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Greymerk committed Aug 8, 2014
1 parent f2fcd72 commit d248657
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ buildscript {

apply plugin: 'forge'

version = "1.7.10-1.3.4.3"
version = "1.7.10-1.3.4.4"
group= "ca.rivas.roguelike" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = "roguelike"

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/greymerk/roguelike/Roguelike.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import cpw.mods.fml.common.event.FMLServerStartingEvent;
import cpw.mods.fml.common.registry.GameRegistry;

@Mod(modid="Roguelike", name="Roguelike Dungeons", version="1.3.4.3")
@Mod(modid="Roguelike", name="Roguelike Dungeons", version="1.3.4.4")

public class Roguelike {

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/greymerk/roguelike/catacomb/Catacomb.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public class Catacomb {
public static CatacombSettingsResolver settingsResolver;

static{
initResolver();
}

public static void initResolver(){
settingsResolver = new CatacombSettingsResolver();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public SecretFactory(JsonArray data){
JsonObject room = e.getAsJsonObject();
String type = room.get("type").getAsString();
int num = room.get("num").getAsInt();
this.count += num;
secrets.add(new SecretRoom(Dungeon.valueOf(type), num));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;

public class CatacombSettingsResolver {

Expand Down Expand Up @@ -57,14 +59,22 @@ public CatacombSettingsResolver(){
if(!settingsDir.exists() || !settingsDir.isDirectory()) return;
File[] settingsFiles = settingsDir.listFiles();
Arrays.sort(settingsFiles);

for(int i = 0; i < settingsFiles.length; ++i){
File toParse = settingsFiles[i];
CatacombSettings toAdd = parseFile(toParse);
CatacombSettings toAdd = null;
try{
toAdd = parseFile(toParse);
} catch (Exception e){
System.err.println("Error found in file " + toParse.getName());
System.err.println(e.getCause().getMessage());
return;
}
settings.put(toAdd.getName(), toAdd);
}
}

private CatacombSettings parseFile(File toParse){
private CatacombSettings parseFile(File toParse) throws JsonParseException, JsonSyntaxException{
String content;

try {
Expand All @@ -74,13 +84,27 @@ private CatacombSettings parseFile(File toParse){
}

JsonParser jParser = new JsonParser();
JsonObject root = (JsonObject)jParser.parse(content);
JsonObject root = null;

try {
root = (JsonObject)jParser.parse(content);
} catch (Exception e){
if(e instanceof JsonParseException){
throw (JsonParseException) e;
}

if(e instanceof JsonSyntaxException){
throw (JsonSyntaxException) e;
}
}

return new CatacombSettings(settings, root);
}

public CatacombSettings getByName(String name){
return new CatacombSettings(this.base, this.settings.get(name));
CatacombSettings override = this.settings.get(name);
if(override == null) return null;
return new CatacombSettings(this.base, override);
}

public ICatacombSettings getSettings(World world, Random rand, Coord pos){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private ItemStack book(){
"-greymerk\n";

String page2 =
"Roguelike Dungeons v1.3.4.3\n" +
"Roguelike Dungeons v1.3.4.4\n" +
"Aug 6th 2014\n\n" +
"Credits\n\n" +
"Author: Greymerk\n\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import greymerk.roguelike.catacomb.Catacomb;
import greymerk.roguelike.catacomb.settings.CatacombSettings;
import greymerk.roguelike.catacomb.settings.ICatacombSettings;
import greymerk.roguelike.citadel.Citadel;
import greymerk.roguelike.config.RogueConfig;
Expand Down Expand Up @@ -60,7 +61,13 @@ public void processCommand(ICommandSender sender, String[] args){

if(args.length == 4){
String name = args[3];
Catacomb.generate(world, Catacomb.settingsResolver.getByName(name), x, z);
Catacomb.initResolver();
CatacombSettings setting = Catacomb.settingsResolver.getByName(name);
if(setting == null){
System.err.println(name + " not found.");
return;
}
Catacomb.generate(world, setting, x, z);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/mcmod.info
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"modid" : "Roguelike",
"name" : "Roguelike Dungeons",
"description" : "Adds randomized dungeons to the world",
"version" : "1.3.4.3",
"version" : "1.3.4.4",
"url" : "dungeons.homelinux.org",
"authorList" : ["Greymerk"],
"mcversion" : "1.7.10"
Expand Down

0 comments on commit d248657

Please sign in to comment.