From 89858c770c1aae0ef48b512f22d7f18fac4bdd95 Mon Sep 17 00:00:00 2001 From: kyeiti Date: Thu, 31 May 2018 19:25:47 +0200 Subject: [PATCH] Made json-import more resilient. Closes #15 --- .../intermod/ModOreRegistrar.java | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/main/java/exterminatorjeff/undergroundbiomes/intermod/ModOreRegistrar.java b/src/main/java/exterminatorjeff/undergroundbiomes/intermod/ModOreRegistrar.java index 0694e7e6..16394e02 100644 --- a/src/main/java/exterminatorjeff/undergroundbiomes/intermod/ModOreRegistrar.java +++ b/src/main/java/exterminatorjeff/undergroundbiomes/intermod/ModOreRegistrar.java @@ -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 ores = new ArrayList<>(); + private HashMap ores = new HashMap<>(); private final Type jsonType = new TypeToken>() { }.getType(); @@ -59,8 +60,8 @@ public void requestOreSetup(RegistryEvent.Register event, ModOre ore) { } public void requestOreSetups(RegistryEvent.Register event) { - for (int i = 0; i < ores.size(); i++) { - requestOreSetup(event, ores.get(i)); + for (ModOre ore : this.ores.values()) { + requestOreSetup(event, ore); } } @@ -121,7 +122,22 @@ private void readFile(File file) { json = FileUtils.readFileToString(file, "UTF8"); ArrayList 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()); } @@ -346,5 +362,9 @@ public ModOre(String ore_name, String overlay) { public ModOre(String ore_name, String overlay, ArrayList oreDirectories) { this(ore_name, UBOre.NO_METADATA, overlay, oreDirectories); } + + public String toKey() { + return internalOreName + ":" + meta; + } } }