From c37d360f0849cd1a5a9752cea954e39b41ba4bfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Wed, 25 Dec 2013 01:59:42 +0100 Subject: [PATCH] custom includer to make .recipes extension work --- assets/opencomputers/recipes/user.recipes | 8 +++---- li/cil/oc/Recipes.scala | 29 ++++++++++++++++++++--- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/assets/opencomputers/recipes/user.recipes b/assets/opencomputers/recipes/user.recipes index d966eb1eb..79b5d4581 100644 --- a/assets/opencomputers/recipes/user.recipes +++ b/assets/opencomputers/recipes/user.recipes @@ -3,10 +3,10 @@ # priority (i.e. included recipes simply replace the current definition for all # already known recipes). -include "default.recipes" -#include "buildcraft.recipes" -#include "gregtech.recipes" -#include "your_custom.recipes" +include file("default.recipes") +#include file("buildcraft.recipes") +#include file("gregtech.recipes") +#include file("your_custom.recipes") # You can also specify custom recipes in this file directly. Have a look at the # default.recipes file to get an idea how recipes have to be structured. You'll diff --git a/li/cil/oc/Recipes.scala b/li/cil/oc/Recipes.scala index 2fbcafdf7..7e19ab86d 100644 --- a/li/cil/oc/Recipes.scala +++ b/li/cil/oc/Recipes.scala @@ -1,15 +1,16 @@ package li.cil.oc -import com.typesafe.config.{Config, ConfigFactory} +import com.typesafe.config._ import cpw.mods.fml.common.Loader import cpw.mods.fml.common.registry.GameRegistry -import java.io.File +import java.io.{FileReader, File} import java.util.logging.Level import net.minecraft.block.Block import net.minecraft.item.crafting.FurnaceRecipes import net.minecraft.item.{ItemStack, Item} import net.minecraftforge.oredict.{OreDictionary, ShapelessOreRecipe, ShapedOreRecipe} import org.apache.commons.io.FileUtils +import scala.Some import scala.collection.convert.wrapAsScala._ import scala.collection.mutable.ArrayBuffer @@ -24,7 +25,26 @@ object Recipes { if (!userRecipes.exists()) { FileUtils.copyURLToFile(getClass.getResource("/assets/opencomputers/recipes/user.recipes"), userRecipes) } - val recipes = ConfigFactory.parseFile(userRecipes) + val config = ConfigParseOptions.defaults. + setSyntax(ConfigSyntax.CONF). + setIncluder(new ConfigIncluder with ConfigIncluderFile { + var fallback: ConfigIncluder = _ + + def withFallback(fallback: ConfigIncluder) = { + this.fallback = fallback + this + } + + def include(context: ConfigIncludeContext, what: String) = fallback.include(context, what) + + def includeFile(context: ConfigIncludeContext, what: File) = { + val in = if (what.isAbsolute) new FileReader(what) else new FileReader(new File(userRecipes.getParentFile, what.getPath)) + val result = ConfigFactory.parseReader(in) + in.close() + result.root() + } + }) + val recipes = ConfigFactory.parseFile(userRecipes, config) // Try to keep this in the same order as the fields in the Items class // to make it easier to match them and check if anything is missing. @@ -114,6 +134,9 @@ object Recipes { case other => throw new RecipeException("Invalid recipe type '" + other + "'.") } } + else { + OpenComputers.log.info("No recipe for '" + name + "', you will not be able to craft this item.") + } } catch { case e: RecipeException => OpenComputers.log.warning("Failed adding recipe for '" + name + "', you will not be able to craft this item! The error was: " + e.getMessage)