Search in sources :

Example 1 with UnknownNameException

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());
}
Also used : IBiomeBuilder(com.mcmoddev.orespawn.api.os3.IBiomeBuilder) UnknownFieldException(com.mcmoddev.orespawn.api.exceptions.UnknownFieldException) BadValueException(com.mcmoddev.orespawn.api.exceptions.BadValueException) JsonObject(com.google.gson.JsonObject) ISpawnBuilder(com.mcmoddev.orespawn.api.os3.ISpawnBuilder) JsonArray(com.google.gson.JsonArray) UnknownNameException(com.mcmoddev.orespawn.api.exceptions.UnknownNameException) JsonElement(com.google.gson.JsonElement) IDimensionBuilder(com.mcmoddev.orespawn.api.os3.IDimensionBuilder) IFeatureBuilder(com.mcmoddev.orespawn.api.os3.IFeatureBuilder) IBlockBuilder(com.mcmoddev.orespawn.api.os3.IBlockBuilder) IReplacementBuilder(com.mcmoddev.orespawn.api.os3.IReplacementBuilder)

Example 2 with UnknownNameException

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());
    }
}
Also used : MissingVersionException(com.mcmoddev.orespawn.api.exceptions.MissingVersionException) UnknownFieldException(com.mcmoddev.orespawn.api.exceptions.UnknownFieldException) BadValueException(com.mcmoddev.orespawn.api.exceptions.BadValueException) CrashReport(net.minecraft.crash.CrashReport) JsonObject(com.google.gson.JsonObject) NotAProperConfigException(com.mcmoddev.orespawn.api.exceptions.NotAProperConfigException) IOException(java.io.IOException) JsonIOException(com.google.gson.JsonIOException) UnknownNameException(com.mcmoddev.orespawn.api.exceptions.UnknownNameException) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonIOException(com.google.gson.JsonIOException) JsonElement(com.google.gson.JsonElement) BufferedReader(java.io.BufferedReader) OldVersionException(com.mcmoddev.orespawn.api.exceptions.OldVersionException) UnknownVersionException(com.mcmoddev.orespawn.api.exceptions.UnknownVersionException) JsonParser(com.google.gson.JsonParser)

Aggregations

JsonElement (com.google.gson.JsonElement)2 JsonObject (com.google.gson.JsonObject)2 BadValueException (com.mcmoddev.orespawn.api.exceptions.BadValueException)2 UnknownFieldException (com.mcmoddev.orespawn.api.exceptions.UnknownFieldException)2 UnknownNameException (com.mcmoddev.orespawn.api.exceptions.UnknownNameException)2 JsonArray (com.google.gson.JsonArray)1 JsonIOException (com.google.gson.JsonIOException)1 JsonParser (com.google.gson.JsonParser)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 MissingVersionException (com.mcmoddev.orespawn.api.exceptions.MissingVersionException)1 NotAProperConfigException (com.mcmoddev.orespawn.api.exceptions.NotAProperConfigException)1 OldVersionException (com.mcmoddev.orespawn.api.exceptions.OldVersionException)1 UnknownVersionException (com.mcmoddev.orespawn.api.exceptions.UnknownVersionException)1 IBiomeBuilder (com.mcmoddev.orespawn.api.os3.IBiomeBuilder)1 IBlockBuilder (com.mcmoddev.orespawn.api.os3.IBlockBuilder)1 IDimensionBuilder (com.mcmoddev.orespawn.api.os3.IDimensionBuilder)1 IFeatureBuilder (com.mcmoddev.orespawn.api.os3.IFeatureBuilder)1 IReplacementBuilder (com.mcmoddev.orespawn.api.os3.IReplacementBuilder)1 ISpawnBuilder (com.mcmoddev.orespawn.api.os3.ISpawnBuilder)1 BufferedReader (java.io.BufferedReader)1