Search in sources :

Example 1 with IntegrationRecipeBasic

use of buildcraft.lib.recipe.IntegrationRecipeBasic in project BuildCraft by BuildCraft.

the class BCSiliconRecipes method registerRecipes.

@SubscribeEvent
public static void registerRecipes(RegistryEvent.Register<IRecipe> event) {
    Loader.instance().getActiveModList().forEach((mod) -> {
        JsonContext ctx = new JsonContext(mod.getModId());
        CraftingHelper.findFiles(mod, "assets/" + mod.getModId() + "/assembly_recipes", null, (root, file) -> {
            String path = root.relativize(file).toString();
            if (!FilenameUtils.getExtension(file.toString()).equals("json"))
                return true;
            String name = FilenameUtils.removeExtension(path).replaceAll("\\\\", "/");
            ResourceLocation key = new ResourceLocation(mod.getModId(), name);
            BufferedReader reader = null;
            try {
                reader = Files.newBufferedReader(file);
                JsonObject json = JsonUtils.fromJson(GSON, reader, JsonObject.class);
                if (json == null || json.isJsonNull())
                    throw new JsonSyntaxException("Json is null (empty file?)");
                ItemStack output = CraftingHelper.getItemStack(json.getAsJsonObject("result"), ctx);
                long powercost = json.get("MJ").getAsLong() * MjAPI.MJ;
                ArrayList<IngredientStack> ingredients = new ArrayList<>();
                json.getAsJsonArray("components").forEach(element -> {
                    JsonObject object = element.getAsJsonObject();
                    ingredients.add(new IngredientStack(CraftingHelper.getIngredient(object.get("ingredient"), ctx), JsonUtils.getInt(object, "amount", 1)));
                });
                AssemblyRecipeRegistry.REGISTRY.put(key, new AssemblyRecipeBasic(key, powercost, ImmutableSet.copyOf(ingredients), output));
            } catch (IOException e) {
                BCLog.logger.error("Couldn't read recipe {} from {}", key, file, e);
                return false;
            } finally {
                IOUtils.closeQuietly(reader);
            }
            return true;
        });
        CraftingHelper.findFiles(mod, "assets/" + mod.getModId() + "/integration_recipes", null, (root, file) -> {
            String path = root.relativize(file).toString();
            if (!FilenameUtils.getExtension(file.toString()).equals("json"))
                return true;
            String name = FilenameUtils.removeExtension(path).replaceAll("\\\\", "/");
            ResourceLocation key = new ResourceLocation(mod.getModId(), name);
            BufferedReader reader = null;
            try {
                reader = Files.newBufferedReader(file);
                JsonObject json = JsonUtils.fromJson(GSON, reader, JsonObject.class);
                if (json == null || json.isJsonNull())
                    throw new JsonSyntaxException("Json is null (empty file?)");
                ItemStack output = CraftingHelper.getItemStack(json.getAsJsonObject("result"), ctx);
                IngredientStack centerStack = IngredientStack.of(CraftingHelper.getIngredient(json.getAsJsonObject("centerStack"), ctx));
                long powercost = json.get("MJ").getAsLong() * MjAPI.MJ;
                ArrayList<IngredientStack> ingredients = new ArrayList<>();
                json.getAsJsonArray("components").forEach(element -> {
                    JsonObject object = element.getAsJsonObject();
                    ingredients.add(new IngredientStack(CraftingHelper.getIngredient(object.get("ingredient"), ctx), JsonUtils.getInt(object, "amount", 1)));
                });
                IntegrationRecipeRegistry.INSTANCE.addRecipe(new IntegrationRecipeBasic(key, powercost, centerStack, ingredients, output));
            } catch (IOException e) {
                BCLog.logger.error("Couldn't read recipe {} from {}", key, file, e);
                return false;
            } finally {
                IOUtils.closeQuietly(reader);
            }
            return true;
        });
    });
}
Also used : ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) JsonContext(net.minecraftforge.common.crafting.JsonContext) AssemblyRecipeBasic(buildcraft.api.recipes.AssemblyRecipeBasic) IntegrationRecipeBasic(buildcraft.lib.recipe.IntegrationRecipeBasic) JsonSyntaxException(com.google.gson.JsonSyntaxException) ResourceLocation(net.minecraft.util.ResourceLocation) BufferedReader(java.io.BufferedReader) ItemStack(net.minecraft.item.ItemStack) IngredientStack(buildcraft.api.recipes.IngredientStack) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Aggregations

AssemblyRecipeBasic (buildcraft.api.recipes.AssemblyRecipeBasic)1 IngredientStack (buildcraft.api.recipes.IngredientStack)1 IntegrationRecipeBasic (buildcraft.lib.recipe.IntegrationRecipeBasic)1 JsonObject (com.google.gson.JsonObject)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ItemStack (net.minecraft.item.ItemStack)1 ResourceLocation (net.minecraft.util.ResourceLocation)1 JsonContext (net.minecraftforge.common.crafting.JsonContext)1 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)1