Search in sources :

Example 1 with RecipeItemParser

use of com.codetaylor.mc.athenaeum.parser.recipe.item.RecipeItemParser in project artisan-worktables by codetaylor.

the class ModuleTools method onPreInitializationEvent.

@Override
public void onPreInitializationEvent(FMLPreInitializationEvent event) {
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    File configurationDirectory = event.getModConfigurationDirectory();
    Path configurationPath = Paths.get(configurationDirectory.toString(), ModuleTools.MOD_ID);
    if (!Files.exists(configurationPath)) {
        try {
            Files.createDirectories(configurationPath);
        } catch (IOException e) {
            event.getModLog().error("", e);
        }
    }
    Path generatedPath = Paths.get(configurationPath.toString(), "artisanworktables.module.Tools.Materials.Generated.json");
    Path customPath = Paths.get(configurationPath.toString(), "artisanworktables.module.Tools.Materials.Custom.json");
    // Delete the generated file if it exists.
    if (Files.exists(generatedPath)) {
        try {
            Files.delete(generatedPath);
        } catch (IOException e) {
            event.getModLog().error("", e);
        }
    }
    // Create and write the generated file.
    BufferedWriter writer = null;
    try {
        writer = Files.newBufferedWriter(generatedPath);
        gson.toJson(new DataCustomMaterialListFactory().create(), writer);
        writer.close();
    } catch (IOException e) {
        event.getModLog().error("", e);
    } finally {
        FileHelper.closeSilently(writer);
    }
    // Copy the generated file to the custom file if the custom file doesn't exist.
    if (!Files.exists(customPath)) {
        try {
            Files.copy(generatedPath, customPath);
        } catch (IOException e) {
            event.getModLog().error("", e);
        }
    }
    BufferedReader reader = null;
    try {
        reader = Files.newBufferedReader(customPath);
        DataCustomMaterialList dataCustomMaterialList = gson.fromJson(reader, DataCustomMaterialList.class);
        CustomMaterialListConverter customMaterialListConverter = new CustomMaterialListConverter(new CustomMaterialValidator(), new CustomMaterialConverter(new RecipeItemParser()));
        this.materialList = customMaterialListConverter.convert(dataCustomMaterialList, event.getModLog());
    } catch (IOException e) {
        event.getModLog().error("", e);
    } finally {
        FileHelper.closeSilently(reader);
    }
    super.onPreInitializationEvent(event);
}
Also used : Path(java.nio.file.Path) RecipeItemParser(com.codetaylor.mc.athenaeum.parser.recipe.item.RecipeItemParser) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) BufferedReader(java.io.BufferedReader) File(java.io.File)

Aggregations

RecipeItemParser (com.codetaylor.mc.athenaeum.parser.recipe.item.RecipeItemParser)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1