Skip to content

Commit

Permalink
Made json-import more resilient. Closes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
kyeiti committed May 31, 2018
1 parent 16479de commit 89858c7
Showing 1 changed file with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class ModOreRegistrar implements UBModOreRegistrar {

private static final UBLogger LOGGER = new UBLogger(OresRegistry.class, Level.INFO);
private final File directory;
private ArrayList<ModOre> ores = new ArrayList<>();
private HashMap<String, ModOre> ores = new HashMap<>();
private final Type jsonType = new TypeToken<ArrayList<ModOre>>() {
}.getType();

Expand All @@ -59,8 +60,8 @@ public void requestOreSetup(RegistryEvent.Register<Block> event, ModOre ore) {
}

public void requestOreSetups(RegistryEvent.Register<Block> event) {
for (int i = 0; i < ores.size(); i++) {
requestOreSetup(event, ores.get(i));
for (ModOre ore : this.ores.values()) {
requestOreSetup(event, ore);
}
}

Expand Down Expand Up @@ -121,7 +122,22 @@ private void readFile(File file) {
json = FileUtils.readFileToString(file, "UTF8");
ArrayList<ModOre> ores = gson.fromJson(json, jsonType);
if (ores != null) {
this.ores.addAll(ores);
for(int i = 0; i < ores.size(); i++) {
ModOre ore = ores.get(i);
if(ore.oreDirectories == null) {
ore.oreDirectories = new ArrayList<>();
}
if(this.ores.containsKey(ore.toKey())) {
String message = "Ore " + ore.toKey() + " has already been defined elsewhere!\nFound while checking: " + file.getAbsolutePath();
if(API.SETTINGS.crashOnProblems()) {
LOGGER.fatal(message);
}
else {
LOGGER.warn(message);
}
}
this.ores.put(ore.toKey(), ore);
}
} else {
LOGGER.warn("No ores found in " + file.getPath());
}
Expand Down Expand Up @@ -346,5 +362,9 @@ public ModOre(String ore_name, String overlay) {
public ModOre(String ore_name, String overlay, ArrayList<String> oreDirectories) {
this(ore_name, UBOre.NO_METADATA, overlay, oreDirectories);
}

public String toKey() {
return internalOreName + ":" + meta;
}
}
}

0 comments on commit 89858c7

Please sign in to comment.