use of com.mcmoddev.orespawn.api.exceptions.UnknownNameException in project OreSpawn by MinecraftModDevelopmentMods.
the class OreSpawnReader method loadSingleEntry.
private static void loadSingleEntry(Entry<String, JsonElement> entry) throws UnknownFieldException, BadValueException, UnknownNameException {
ISpawnBuilder sb = OreSpawn.API.getSpawnBuilder();
IFeatureBuilder fb = OreSpawn.API.getFeatureBuilder();
sb.setName(entry.getKey());
for (Entry<String, JsonElement> ent : entry.getValue().getAsJsonObject().entrySet()) {
switch(ent.getKey()) {
case Constants.ConfigNames.RETROGEN:
sb.setRetrogen(ent.getValue().getAsBoolean());
break;
case Constants.ConfigNames.ENABLED:
sb.setEnabled(ent.getValue().getAsBoolean());
break;
case Constants.ConfigNames.DIMENSIONS:
IDimensionBuilder db = OreSpawn.API.getDimensionBuilder();
if (ent.getValue().isJsonArray()) {
JsonArray dims = ent.getValue().getAsJsonArray();
if (dims.size() == 0) {
// blank list, accept all overworld
db.setAcceptAllOverworld();
} else {
dims.forEach(item -> {
if (item.isJsonPrimitive() && item.getAsJsonPrimitive().isNumber()) {
db.addWhitelistEntry(item.getAsInt());
}
});
}
} else if (ent.getValue().isJsonObject()) {
loadDimensions(db, ent.getValue().getAsJsonObject());
} else {
throw new BadValueException(Constants.ConfigNames.DIMENSIONS, ent.getValue().toString());
}
sb.setDimensions(db.create());
break;
case Constants.ConfigNames.BIOMES:
if (!ent.getValue().isJsonObject()) {
throw new BadValueException(Constants.ConfigNames.BIOMES, ent.getValue().toString());
}
IBiomeBuilder bb = OreSpawn.API.getBiomeBuilder();
loadBiomes(bb, ent.getValue().getAsJsonObject());
sb.setBiomes(bb.create());
break;
case Constants.ConfigNames.FEATURE:
if (ent.getValue().isJsonPrimitive() && !ent.getValue().getAsJsonPrimitive().isString()) {
throw new BadValueException(Constants.ConfigNames.FEATURE, ent.getValue().toString());
}
String featureName = ent.getValue().getAsString();
if (!OreSpawn.API.featureExists(featureName)) {
throw new UnknownNameException(Constants.ConfigNames.FEATURE, featureName);
}
fb.setFeature(featureName);
break;
case Constants.ConfigNames.REPLACEMENT:
if (!ent.getValue().isJsonArray() && !ent.getValue().getAsJsonPrimitive().isString()) {
throw new BadValueException(Constants.ConfigNames.REPLACEMENT, ent.getValue().toString());
} else if (ent.getValue().getAsJsonPrimitive().isString()) {
if (OreSpawn.API.hasReplacement(ent.getValue().getAsString())) {
sb.setReplacement(OreSpawn.API.getReplacement(ent.getValue().getAsString()));
}
} else {
IReplacementBuilder rb = OreSpawn.API.getReplacementBuilder();
for (JsonElement e : ent.getValue().getAsJsonArray()) {
if (e.isJsonObject()) {
loadBlock(e.getAsJsonObject()).stream().forEach(rb::addEntry);
} else {
OreSpawn.LOGGER.error("Skipping value %s in replacements list as it is not the correct format", e.toString());
}
}
sb.setReplacement(rb.create());
}
break;
case Constants.ConfigNames.BLOCK:
if (ent.getValue().isJsonArray()) {
for (JsonElement elem : ent.getValue().getAsJsonArray()) {
IBlockBuilder block = OreSpawn.API.getBlockBuilder();
if (elem.isJsonObject()) {
JsonObject bl = elem.getAsJsonObject();
if (bl.has(Constants.ConfigNames.STATE)) {
block.setFromNameWithChance(bl.get(Constants.ConfigNames.NAME).getAsString(), bl.get(Constants.ConfigNames.STATE).getAsString(), bl.get(Constants.ConfigNames.CHANCE).getAsInt());
} else if (bl.has(Constants.ConfigNames.METADATA)) {
block.setFromNameWithChance(bl.get(Constants.ConfigNames.NAME).getAsString(), bl.get(Constants.ConfigNames.METADATA).getAsInt(), bl.get(Constants.ConfigNames.CHANCE).getAsInt());
} else {
block.setFromNameWithChance(bl.get(Constants.ConfigNames.NAME).getAsString(), bl.get(Constants.ConfigNames.CHANCE).getAsInt());
}
sb.addBlock(block.create());
} else {
OreSpawn.LOGGER.error("Skipping value %s in blocks list as it is not the correct format", elem.toString());
}
}
} else {
throw new BadValueException(Constants.ConfigNames.BLOCK, ent.getValue().toString());
}
case Constants.ConfigNames.PARAMETERS:
if (ent.getValue().isJsonObject()) {
ent.getValue().getAsJsonObject().entrySet().stream().forEach(e -> fb.setParameter(e.getKey(), e.getValue()));
}
break;
default:
throw new UnknownFieldException(ent.getKey());
}
}
sb.setFeature(fb.create());
OreSpawn.API.addSpawn(sb.create());
}
use of com.mcmoddev.orespawn.api.exceptions.UnknownNameException in project OreSpawn by MinecraftModDevelopmentMods.
the class OreSpawnReader method tryReadFile.
public static void tryReadFile(Path conf, OS3APIImpl os3apiImpl) throws MissingVersionException, NotAProperConfigException, OldVersionException, UnknownVersionException {
JsonParser parser = new JsonParser();
try (BufferedReader data = Files.newBufferedReader(conf)) {
JsonElement json = parser.parse(data);
if (!json.isJsonObject()) {
throw new NotAProperConfigException();
}
JsonObject root = json.getAsJsonObject();
if (!root.has(Constants.ConfigNames.FILE_VERSION)) {
throw new MissingVersionException();
}
float version = root.get(Constants.ConfigNames.FILE_VERSION).getAsFloat();
if (version < 2f) {
throw new OldVersionException();
} else if (version != 2f) {
throw new UnknownVersionException();
}
if (!root.has(Constants.ConfigNames.SPAWNS)) {
throw new NotAProperConfigException();
}
JsonObject spawnData = doHandlePresets(root).get(Constants.ConfigNames.SPAWNS).getAsJsonObject();
spawnData.entrySet().stream().forEach(e -> {
try {
OreSpawn.API.mapEntryToFile(conf, e.getKey());
loadSingleEntry(e);
} catch (UnknownFieldException | BadValueException | UnknownNameException e1) {
CrashReport report = CrashReport.makeCrashReport(e1, "Error parsing an entry " + e.getKey() + " in " + conf.toString());
report.getCategory().addCrashSection(ORE_SPAWN_VERSION, Constants.VERSION);
OreSpawn.LOGGER.info(report.getCompleteReport());
}
});
} catch (IOException e) {
CrashReport report = CrashReport.makeCrashReport(e, "Failed reading config data " + conf.toString());
report.getCategory().addCrashSection(ORE_SPAWN_VERSION, Constants.VERSION);
OreSpawn.LOGGER.info(report.getCompleteReport());
} catch (JsonIOException | JsonSyntaxException e) {
CrashReport report = CrashReport.makeCrashReport(e, "JSON Parsing Error in " + conf.toString());
report.getCategory().addCrashSection(ORE_SPAWN_VERSION, Constants.VERSION);
OreSpawn.LOGGER.info(report.getCompleteReport());
}
}
Aggregations