Recipe parser/loader rework, see http://bdew.net/bacon/ for description of new format

This commit is contained in:
bdew committed 2014-10-06 13:51:38 +03:00
1 parent b3f8fb3c56
commit 5ae0461f10
16 files changed
+533 -384

No files matched your search

+1 -1
View File
@@ -21,7 +21,7 @@ buildscript {
apply plugin: 'scala'
apply plugin: 'forge'
version = "1.4.5"
version = "1.5.0"
if (project.hasProperty('buildnum')) {
project.version = project.version + '.' + project.buildnum
-213
View File
@@ -1,213 +0,0 @@
/*
* Copyright (c) bdew, 2013 - 2014
* https://github.com/bdew/bdlib
*
* This mod is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
*/
package net.bdew.lib.recipes
/**
* Base class for everything that can be resolved to ItemStack or recipe component
* Actual processing is in:
* * [[net.bdew.lib.recipes.RecipeLoader.getRecipeComponent]]
* * [[net.bdew.lib.recipes.RecipeLoader.getConcreteStack]]
* * [[net.bdew.lib.recipes.RecipeLoader.getAllConcreteStacks]]
*
*/
abstract class StackRef
/**
* Ore Dictionary reference
* Syntax: OD:{identifier}
* Parser: [[net.bdew.lib.recipes.RecipeParser.specOD]]
* @param id OD entry name
*/
case class StackOreDict(id: String) extends StackRef
/**
* Generic Referende
* Syntax: S:{modid}:{name}
* Parser: [[net.bdew.lib.recipes.RecipeParser.specGenericStack]]
* @param mod modId of owner mod
* @param name name of the stack
*/
case class StackGeneric(mod: String, name: String) extends StackRef
/**
* Block reference
* Syntax: B:[{modid}:]{name} [@ {meta}]
* Parser: [[net.bdew.lib.recipes.RecipeParser.specBlock]]
* @param mod modId of owner mod, default is "minecraft"
* @param name Registered name
* @param meta Metadata / Damage
*/
case class StackBlock(mod: String, name: String, meta: Int) extends StackRef
/**
* Item reference
* Syntax: I:[{modid}:]{name} [@ {meta}]
* Parser: [[net.bdew.lib.recipes.RecipeParser.specItem]]
* @param mod modId of owner mod, default is "minecraft"
* @param name Registered name
* @param meta Metadata / Damage
*/
case class StackItem(mod: String, name: String, meta: Int) extends StackRef
/**
* Reference to an Item/Block/ItemStack pulic static field
* Syntax: {path}.{to}.{class}[{field}] [@ {meta}]
* Parser: [[net.bdew.lib.recipes.RecipeParser.specReflect]]
* @param cls Class name
* @param field Field name
* @param meta Metadata / Damage (Ignored for ItemStacks)
*/
case class StackReflect(cls: String, field: String, meta: Int) extends StackRef
/**
* Reference to an Item/Block/ItemStack pulic static getter
* Syntax: {path}.{to}.{class}.{method}({string}) [@ {meta}]
* Parser: [[net.bdew.lib.recipes.RecipeParser.specGetter]]
* @param cls Class name
* @param method Method name
* @param name Parameter to getter
* @param meta Metadata / Damage (Ignored for ItemStacks)
*/
case class StackGetter(cls: String, method: String, name: String, meta: Int) extends StackRef
/**
* Macro reference to a previous definition
* Syntax: ${char}
* Parser: [[net.bdew.lib.recipes.RecipeParser.specMacro]]
* @param id The underlying character
*/
case class StackMacro(id: Char) extends StackRef
/**
* Base class for statements
* Actual processing is in [[net.bdew.lib.recipes.RecipeLoader.processStatement]]
*/
abstract class Statement
/**
* Base class for delayed statements (executed after everything is parsed)
* Actual processing is in [[net.bdew.lib.recipes.RecipeLoader.processDelayedStatements]]
*/
abstract class DelayedStatement extends Statement
/**
* Character assignment
* Syntax: {char} = {ref}
* Parser: [[net.bdew.lib.recipes.RecipeParser.charSpec]]
* @param char Character to assign
* @param spec ItemStack Reference
*/
case class StCharAssign(char: Char, spec: StackRef) extends DelayedStatement
/**
* Define class macro
* Syntax: def {id} = {name}
* Parser: [[net.bdew.lib.recipes.RecipeParser.classMacro]]
* @param id Macro identifier
* @param cls Class name
*/
case class StClassMacro(id: String, cls: String) extends DelayedStatement
/**
* Conditional block - checks if mod is loaded
* Syntax: ifMod {string} { {statements} } [else { {statements} }]
* Parser: [[net.bdew.lib.recipes.RecipeParser.ifMod]]
* @param mod mod and version to check [[cpw.mods.fml.common.versioning.VersionParser]]
* @param thn Statements to run if present
* @param els Statements to run otherwise
*/
case class StIfHaveMod(mod: String, thn: List[Statement], els: List[Statement]) extends Statement
/**
* Conditional block - checks if api is loaded
* Syntax: ifAPI {string} { {statements} } [else { {statements} }]
* Parser: [[net.bdew.lib.recipes.RecipeParser.ifApi]]
* @param mod API modid
* @param thn Statements to run if present
* @param els Statements to run otherwise
*/
case class StIfHaveAPI(mod: String, thn: List[Statement], els: List[Statement]) extends Statement
/**
* Conditional block - checks if mod is loaded
* Can contain only delayed statements
* Syntax: ifOreDict {string} { {statements} } [else { {statements} }]
* Parser: [[net.bdew.lib.recipes.RecipeParser.ifOreDict]]
* @param id Ore Dictionary Entry
* @param thn Statements to run if present
* @param els Statements to run otherwise
*/
case class StIfHaveOD(id: String, thn: List[DelayedStatement], els: List[DelayedStatement]) extends DelayedStatement
/**
* Removes previously defined recieps for a specific result
* @param res Recipes that produce this result will be cleared
*/
case class StClearRecipes(res: StackRef) extends Statement
/**
* Base class for staments that represent a crafting recipe
*/
abstract class CraftingStatement extends DelayedStatement {
def result: StackRef
}
/**
* Shaped recipe definition
*
* Syntax:
* XXX
* XXX => {result} [* {count}]
* XXX
*
* Parser: [[net.bdew.lib.recipes.RecipeParser.recipeShaped3x3]]
*
* Syntax:
* XX
* XX => {result} [* {count}]
*
* Parser: [[net.bdew.lib.recipes.RecipeParser.recipeShaped2x2]]
*
* Syntax: XXX XXX XXX => {result} [* {count}]
* Parser: [[net.bdew.lib.recipes.RecipeParser.recipeShaped9]]
*
* @param rec Recipe pattern
* @param result Result reference
* @param cnt Number of output items
*/
case class StRecipeShaped(rec: Seq[String], result: StackRef, cnt: Int) extends CraftingStatement
/**
* Shapeless recipe definition
* Syntax: shapeless: {characters} => {result} [* {count}]
* Parser: [[net.bdew.lib.recipes.RecipeParser.recipeShapeless]]
*
* @param rec Recipe pattehrn
* @param result Result reference
* @param cnt Number of output items
*/
case class StRecipeShapeless(rec: String, result: StackRef, cnt: Int) extends CraftingStatement
/**
* Smelting recipe definition
* Syntax: smelt: {input} => {output}, {XP}
* Parser [[net.bdew.lib.recipes.RecipeParser.recipeSmelting]]
*
* @param in Input Stack
* @param result Output Stack
* @param cnt Output Count
* @param xp XP for smelting
*/
case class StSmeltRecipe(in: StackRef, result: StackRef, cnt: Int, xp: Float) extends CraftingStatement
/**
* Used in statement processing to abort the current statement and show an error without a stack trace
*/
class StatementError(msg: String) extends Exception(msg)
+109 -61
View File
@@ -21,7 +21,7 @@ import net.minecraftforge.oredict.{OreDictionary, ShapelessOreRecipe}
/**
* Main recipe loader class
* The input file is parsed by [[net.bdew.lib.recipes.RecipeParser]]
* Into a list of [[net.bdew.lib.recipes.Statement]] subclasses
* Into a list of [[net.bdew.lib.recipes.ConfigStatement]] subclasses
* That are then executed here
*/
@@ -49,9 +49,9 @@ class RecipeLoader {
def error(msg: String, params: Any*) = throw new StatementError(msg.format(params: _*))
/**
* List of unprocessed delayed statements
* List of unprocessed recipe statements
*/
var delayedStatements = List.empty[DelayedStatement]
var recipeStatements = List.empty[RecipeStatement]
/**
* Looks up a recipe component
@@ -67,11 +67,10 @@ class RecipeLoader {
/**
* Resolve class name if it's a macro
*/
def getRealClassName(s: String): String = {
if (s.startsWith("$")) {
return currClassMacros(s.stripPrefix("$"))
}
return s
def getRealClassName(s: String) = {
if (s.startsWith("$"))
currClassMacros(s.stripPrefix("$"))
else s
}
/**
* Sanitize items from reflection
@@ -207,62 +206,83 @@ class RecipeLoader {
return (comp, needOd)
}
/**
* Helper to remove recipes from lists
* @param list list to check
* @param res crafting result to remove
* @return cleaned list
*/
def clearStatements(list: List[RecipeStatement], res: StackRef): List[RecipeStatement] = {
list flatMap {
case x: CraftingStatement =>
if (res == x.result) {
BdLib.logInfo("Removing recipe %s", this)
None
} else Some(x)
case RsRecipes(inner) =>
Some(RsRecipes(clearStatements(inner, res)))
case RsConditional(cnd, thn, els) =>
Some(RsConditional(cnd, clearStatements(thn, res), clearStatements(els, res)))
case x => Some(x)
}
}
/**
* Checks a condition and returns the result
*/
def resolveCondition(cond: Condition): Boolean = cond match {
case CndHaveMod(mod) =>
Misc.haveModVersion(mod)
case CndHaveAPI(api) =>
ModAPIManager.INSTANCE.hasAPI(api)
case CndHaveOD(od) =>
OreDictionary.getOres(od).size() > 0
case CndNOT(cnd) => !resolveCondition(cnd)
case CndAND(c1, c2) => resolveCondition(c1) && resolveCondition(c2)
case CndOR(c1, c2) => resolveCondition(c1) || resolveCondition(c2)
case x =>
error("Can't process %s - this is a programing bug!", x)
}
/**
* Process a single statement, override this to add more statements
* @param s The statement
*/
def processStatement(s: Statement): Unit = s match {
case StIfHaveMod(mod, thn, els) =>
if (Misc.haveModVersion(mod)) {
BdLib.logInfo("ifMod: %s found", mod)
processStatementsSafe(thn)
def processConfigStatement(s: ConfigStatement): Unit = s match {
case CsConditionalConfig(cnd, thn, els) =>
if (resolveCondition(cnd)) {
BdLib.logInfo("Condition %s - TRUE", cnd)
processConfigStatementsSafe(thn)
} else {
BdLib.logInfo("ifMod: %s not found", mod)
processStatementsSafe(els)
BdLib.logInfo("Condition %s - FALSE", cnd)
processConfigStatementsSafe(els)
}
case StIfHaveAPI(mod, thn, els) =>
if (ModAPIManager.INSTANCE.hasAPI(mod)) {
BdLib.logInfo("ifAPI: %s found", mod)
processStatementsSafe(thn)
} else {
BdLib.logInfo("ifAPI: %s not found", mod)
processStatementsSafe(els)
}
case StClearRecipes(res) =>
case CsClearRecipes(res) =>
BdLib.logInfo("Clearing recipes that produce %s", res)
delayedStatements = delayedStatements.filter({
x =>
if (x.isInstanceOf[CraftingStatement])
if (x.asInstanceOf[CraftingStatement].result == res) {
BdLib.logInfo("Removing recipe %s", x)
false
} else true
else true
})
recipeStatements = clearStatements(recipeStatements, res)
case x: DelayedStatement =>
delayedStatements :+= x
case CsRecipeBlock(lst) =>
recipeStatements :+= RsRecipes(lst)
case x =>
BdLib.logError("Can't process %s - this is a programing bug!", x)
}
/**
* Process a single delayed statement
* Process a single recipe statement
* @param st The statement
*/
def processDelayedStatement(st: DelayedStatement) = st match {
case StCharAssign(c, r) =>
def processRecipeStatement(st: RecipeStatement) = st match {
case RsCharAssign(c, r) =>
currCharMap += (c -> r)
BdLib.logInfo("Added %s = %s", c, r)
case StClassMacro(id, cls) =>
case RsClassMacro(id, cls) =>
currClassMacros += (id -> cls)
BdLib.logInfo("Added def %s = %s", id, cls)
case StRecipeShaped(rec, res, cnt) =>
case RsRecipeShaped(rec, res, cnt) =>
BdLib.logInfo("Adding shaped recipe %s => %s * %d", rec, res, cnt)
val (comp, needOd) = resolveRecipeComponents(rec.mkString(""))
val resStack = getConcreteStack(res, cnt)
@@ -279,7 +299,7 @@ class RecipeLoader {
BdLib.logInfo("Done... result=%s, od=%s", resStack, needOd)
case StRecipeShapeless(rec, res, cnt) =>
case RsRecipeShapeless(rec, res, cnt) =>
BdLib.logInfo("Adding shapeless recipe %s => %s * %d", rec, res, cnt)
val (comp, needOd) = resolveRecipeComponents(rec)
val resStack = getConcreteStack(res, cnt)
@@ -297,7 +317,7 @@ class RecipeLoader {
BdLib.logInfo("Done... result=%s, od=%s", resStack, needOd)
case StSmeltRecipe(in, out, cnt, xp) =>
case RsRecipeSmelting(in, out, cnt, xp) =>
BdLib.logInfo("Adding smelting recipe %s => %s * %d (%f xp)", in, out, cnt, xp)
val outStack = getConcreteStack(out, cnt)
if (outStack.getItemDamage == OreDictionary.WILDCARD_VALUE) {
@@ -309,36 +329,64 @@ class RecipeLoader {
BdLib.logInfo("added %s -> %s", inStack, outStack)
}
case StIfHaveOD(od, thn, els) =>
if (OreDictionary.getOres(od).size() > 0) {
BdLib.logInfo("ifOreDict: %s found".format(od))
processDelayedStatementsSafe(thn)
case RsConditional(cnd, thn, els) =>
if (resolveCondition(cnd)) {
BdLib.logInfo("Condition %s - TRUE", cnd)
processRecipeStatementsInSubcontext(thn)
} else {
BdLib.logInfo("ifOreDict: %s not found".format(od))
processDelayedStatementsSafe(els)
BdLib.logInfo("Condition %s - FALSE", cnd)
processRecipeStatementsInSubcontext(els)
}
case RsRecipes(list) =>
processRecipeStatementsInSubcontext(list)
case RsRegOredict(id, spec, wildcard) =>
BdLib.logInfo("Registering ore dictionary entry: %s -> %s", spec, id)
val stack = getConcreteStack(spec)
if (wildcard) {
BdLib.logInfo("Forcing wildcard damage (was %d)", stack.getItemDamage)
stack.setItemDamage(OreDictionary.WILDCARD_VALUE)
}
BdLib.logInfo("Actual stack: %s", stack)
OreDictionary.registerOre(id, stack)
case x =>
BdLib.logError("Can't process %s - this is a programing bug!", x)
}
/**
* Process main delayed statements list, clear the list afterwards
* Processes recipe statements in a new context
* Any changes to mutable state will not persist when this method returns
*/
def processDelayedStatements() {
BdLib.logInfo("Processing %d delayed statements", delayedStatements.size)
processDelayedStatementsSafe(delayedStatements)
delayedStatements = List.empty
def processRecipeStatementsInSubcontext(list: List[RecipeStatement]) = {
val oldCharMap = currCharMap
val oldClassMacros = currClassMacros
try {
processRecipeStatementsSafe(list)
} finally {
currCharMap = oldCharMap
currClassMacros = oldClassMacros
}
}
/**
* Process a list of delayed statements and catch all exceptions
* Process main recipe statements list, clear the list afterwards
*/
def processRecipeStatements() {
BdLib.logInfo("Processing %d recipe statements", recipeStatements.size)
processRecipeStatementsSafe(recipeStatements)
recipeStatements = List.empty
}
/**
* Process a list of recipe statements and catch all exceptions
* @param list The list to process
*/
def processDelayedStatementsSafe(list: List[DelayedStatement]) {
def processRecipeStatementsSafe(list: List[RecipeStatement]) {
for (s <- list) {
try {
processDelayedStatement(s)
processRecipeStatement(s)
} catch {
case e: StatementError =>
BdLib.logError("Error while processing %s: %s", s, e.getMessage)
@@ -349,13 +397,13 @@ class RecipeLoader {
}
/**
* Process a list of statements and catch all exceptions
* Process a list of config statements and catch all exceptions
* @param r The list to process
*/
def processStatementsSafe(r: List[Statement]): Unit = {
def processConfigStatementsSafe(r: List[ConfigStatement]): Unit = {
for (s <- r) {
try {
processStatement(s)
processConfigStatement(s)
} catch {
case e: StatementError =>
BdLib.logError("Error while processing %s: %s", s, e.getMessage)
@@ -369,7 +417,7 @@ class RecipeLoader {
BdLib.logInfo("Starting parsing")
val r = newParser().doParse(f)
BdLib.logInfo("Processing %d statements", r.size)
processStatementsSafe(r)
processConfigStatementsSafe(r)
BdLib.logInfo("Done")
}
}
+55 -28
View File
@@ -68,68 +68,95 @@ class RecipeParser extends JavaTokenParsers {
// Spec with possible number of items
def specWithCount = spec ~ ("*" ~> int).?
// === Conditions ===
def cndHaveMod = "HaveMod" ~> str ^^ CndHaveMod
def cndHaveAPI = "HaveAPI" ~> str ^^ CndHaveAPI
def cndHaveOD = "HaveOD" ~> str ^^ CndHaveOD
def cndNOT = "!" ~> condition ^^ CndNOT
def cndOR = "(" ~> condition ~ "||" ~ condition <~ ")" ^^ { case c1 ~ or ~ c2 => CndOR(c1, c2) }
def cndAND = "(" ~> condition ~ "&&" ~ condition <~ ")" ^^ { case c1 ~ and ~ c2 => CndAND(c1, c2) }
def condition: Parser[Condition] = cndHaveAPI | cndHaveMod | cndHaveOD | cndNOT | cndOR | cndAND
// === Blocks ===
def recipesSubBlock = "recipes" ~> ("{" ~> recipeStatements <~ "}") ^^ RsRecipes
def recipesTopBlock = "recipes" ~> ("{" ~> recipeStatements <~ "}") ^^ CsRecipeBlock
def conditionRecipes = "if" ~> condition ~ ("{" ~> recipeStatements <~ "}") ~ ("else" ~> "{" ~> recipeStatements <~ "}").? ^^ {
case cond ~ thn ~ els => new RsConditional(cond, thn, els.getOrElse(List.empty))
}
def conditionConfig = "if" ~> condition ~ ("<<" ~> configStatements <~ ">>") ~ ("else" ~> "<<" ~> configStatements <~ ">>").? ^^ {
case cond ~ thn ~ els => new CsConditionalConfig(cond, thn, els.getOrElse(List.empty))
}
def conditionBridge = conditionRecipes ^^ { case cond => CsRecipeBlock(List(cond)) }
// === Statements ===
def charSpec = recipeChar ~ "=" ~ spec ^^ {
case ch ~ eq ~ spec => new StCharAssign(ch, spec)
case ch ~ eq ~ spec => new RsCharAssign(ch, spec)
}
def classMacro = ("def" ~> ident <~ "=") ~ clsPath ^^ {
case id ~ (p ~ cl) => StClassMacro(id, (p :+ cl).mkString("."))
}
def ifMod = ("ifMod" ~> str) ~ ("{" ~> statements <~ "}") ~ ("else" ~> "{" ~> statements <~ "}").? ^^ {
case mod ~ thn ~ els => new StIfHaveMod(mod, thn, els.getOrElse(List.empty[Statement]))
}
def ifAPI = ("ifAPI" ~> str) ~ ("{" ~> statements <~ "}") ~ ("else" ~> "{" ~> statements <~ "}").? ^^ {
case mod ~ thn ~ els => new StIfHaveAPI(mod, thn, els.getOrElse(List.empty[Statement]))
}
def ifOreDict = ("ifOreDict" ~> str) ~ ("{" ~> delayedStatements <~ "}") ~ ("else" ~> "{" ~> delayedStatements <~ "}").? ^^ {
case id ~ thn ~ els => new StIfHaveOD(id, thn, els.getOrElse(List.empty[DelayedStatement]))
case id ~ (p ~ cl) => RsClassMacro(id, (p :+ cl).mkString("."))
}
def recipeShaped3x3 = recipeTriplet ~ recipeTriplet ~ ("=>" ~> specWithCount) ~ recipeTriplet ^^ {
case r1 ~ r2 ~ (res ~ n) ~ r3 => StRecipeShaped(Seq(r1, r2, r3), res, n.getOrElse(1))
case r1 ~ r2 ~ (res ~ n) ~ r3 => RsRecipeShaped(Seq(r1, r2, r3), res, n.getOrElse(1))
}
def recipeShaped2x2 = recipeDuplet ~ recipeDuplet ~ ("=>" ~> specWithCount) ^^ {
case r1 ~ r2 ~ (res ~ n) => StRecipeShaped(Seq(r1, r2), res, n.getOrElse(1))
case r1 ~ r2 ~ (res ~ n) => RsRecipeShaped(Seq(r1, r2), res, n.getOrElse(1))
}
def recipeShaped9 = recipeTriplet ~ recipeTriplet ~ recipeTriplet ~ ("=>" ~> specWithCount) ^^ {
case r1 ~ r2 ~ r3 ~ (res ~ n) => StRecipeShaped(Seq(r1, r2, r3), res, n.getOrElse(1))
case r1 ~ r2 ~ r3 ~ (res ~ n) => RsRecipeShaped(Seq(r1, r2, r3), res, n.getOrElse(1))
}
def recipeShapeless = "shapeless" ~> ":" ~> recipeChar.+ ~ ("=>" ~> specWithCount) ^^ {
case r ~ (res ~ n) => StRecipeShapeless(r.mkString(""), res, n.getOrElse(1))
case r ~ (res ~ n) => RsRecipeShapeless(r.mkString(""), res, n.getOrElse(1))
}
def recipeSmelting = "smelt" ~> ":" ~> spec ~ ("=>" ~> specWithCount) ~ ("," ~> decimalNumber).? ^^ {
case in ~ (out ~ n) ~ xp => StSmeltRecipe(in, out, n.getOrElse(1), xp.getOrElse("0").toFloat)
def recipeSmelting = "smelt" ~> ":" ~> spec ~ ("=>" ~> specWithCount) ~ ("+" ~> decimalNumber <~ "xp").? ^^ {
case in ~ (out ~ n) ~ xp => RsRecipeSmelting(in, out, n.getOrElse(1), xp.getOrElse("0").toFloat)
}
def clearRecipes = "clearRecipes" ~> ":" ~> spec ^^ { case sp => StClearRecipes(sp) }
def regOreDict = "regOreDict" ~> ":" ~> spec ~ "@WILDCARD".? ~ ("=>" ~> str) ^^ {
case spec ~ wildcard ~ id => RsRegOredict(id, spec, wildcard.isDefined)
}
def delayedStatement: Parser[DelayedStatement] = (
def clearRecipes = "clearRecipes" ~> ":" ~> spec ^^ { case sp => CsClearRecipes(sp) }
def recipeStatement: Parser[RecipeStatement] = (
charSpec
| classMacro
| ifOreDict
| recipesSubBlock
| conditionRecipes
| recipesSubBlock
| recipeShaped3x3
| recipeShaped2x2
| recipeShaped9
| recipeShapeless
| recipeSmelting
| regOreDict
)
def statement: Parser[Statement] = delayedStatement | ifMod | ifAPI | clearRecipes
def configStatement: Parser[ConfigStatement] = (
recipesTopBlock
| clearRecipes
| conditionBridge
| conditionConfig
)
def statements = statement.*
def delayedStatements = delayedStatement.*
def configStatements = configStatement.*
def recipeStatements = recipeStatement.*
def doParse(r: Reader): List[Statement] = {
parseAll(statements, r) match {
def doParse(r: Reader): List[ConfigStatement] = {
parseAll(configStatements, r) match {
case Success(res, next) => return res
case NoSuccess(msg, next) => sys.error("Config parsing failed at %s: %s".format(next.pos, msg))
}
@@ -0,0 +1,16 @@
/*
* Copyright (c) bdew, 2013 - 2014
* https://github.com/bdew/bdlib
*
* This mod is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
*/
package net.bdew.lib.recipes
/**
* Used in statement processing to abort the current statement and show an error without a stack trace
*/
class StatementError(msg: String) extends Exception(msg)
-44
View File
@@ -1,44 +0,0 @@
/*
* Copyright (c) bdew, 2013 - 2014
* https://github.com/bdew/bdlib
*
* This mod is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* https://raw.github.com/bdew/bdlib/master/MMPL-1.0.txt
*/
package net.bdew.lib.recipes
import java.lang.reflect.Modifier
import net.minecraft.block.Block
import net.minecraft.item.Item
import scala.reflect.ClassTag
object Vanilla {
// Maps of vanilla blocks and items (without tile. / item. prefixes)
lazy val blocks = mkMap[Block](_.getUnlocalizedName.stripPrefix("tile.")).withDefault(x => throw new StatementError("Block %s not found".format(x)))
lazy val items = mkMap[Item](_.getUnlocalizedName.stripPrefix("item.")).withDefault(x => throw new StatementError("Item %s not found".format(x)))
/**
* Generates the maps
* @tparam T The base class
* @param namer A function that converts objects to names
*/
private def mkMap[T](namer: T => String)(implicit t: ClassTag[T]) = {
val c = t.runtimeClass
// Grab all the fields
c.getFields.filter(x => {
// Keep only static fields that match the base Class
c.isAssignableFrom(x.getType) && Modifier.isStatic(x.getModifiers)
}).map(x => {
// Retrieve the static value
val f = x.get(null).asInstanceOf[T]
// Convert to name -> object tuples
namer(f) -> f
}).toMap // and finally to a map
}
}
@@ -0,0 +1,62 @@
/*
* Copyright (c) bdew, 2013 - 2014
* https://github.com/bdew/bdlib
*
* This mod is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://bdew.net/minecraft-mod-public-license/
*/
package net.bdew.lib.recipes
/**
* Represents a condition, something that can be resolved to a boolean
* Actual processing is in [[RecipeLoader.resolveCondition]]
*
* Naming convention: All subclasses are named CndFoo
*/
abstract class Condition
/**
* True if mod is present
* Syntax: HaveMod {string}
* Parser: [[RecipeParser.cndHaveMod]]
* @param mod mod id
*/
case class CndHaveMod(mod: String) extends Condition
/**
* True if API is present
* Syntax: HaveAPI {string}
* Parser: [[RecipeParser.cndHaveAPI]]
*/
case class CndHaveAPI(api: String) extends Condition
/**
* True if oredict entery is present
* Syntax: HaveOD {string}
* Parser: [[RecipeParser.cndHaveOD]]
*/
case class CndHaveOD(od: String) extends Condition
/**
* Boolean NOT
* Syntax: ! {condition}
* Parser: [[RecipeParser.cndNOT]]
*/
case class CndNOT(cnd: Condition) extends Condition
/**
* Boolean AND
* Syntax: ( {condition} && {condition} )
* Parser: [[RecipeParser.cndAND]]
*/
case class CndAND(c1: Condition, c2: Condition) extends Condition
/**
* Boolean OR
* Syntax: ( {condition} || {condition} )
* Parser: [[RecipeParser.cndOR]]
*/
case class CndOR(c1: Condition, c2: Condition) extends Condition
@@ -0,0 +1,87 @@
/*
* Copyright (c) bdew, 2013 - 2014
* https://github.com/bdew/bdlib
*
* This mod is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://bdew.net/minecraft-mod-public-license/
*/
package net.bdew.lib.recipes
/**
* Represents something that can be resolved to ItemStack or recipe component
* Actual processing is in:
* * [[net.bdew.lib.recipes.RecipeLoader.getRecipeComponent]]
* * [[net.bdew.lib.recipes.RecipeLoader.getConcreteStack]]
* * [[net.bdew.lib.recipes.RecipeLoader.getAllConcreteStacks]]
*
* Naming convention: All subclasses are named StackFoo
*/
abstract class StackRef
/**
* Ore Dictionary reference
* Syntax: OD:{identifier}
* Parser: [[net.bdew.lib.recipes.RecipeParser.specOD]]
* @param id OD entry name
*/
case class StackOreDict(id: String) extends StackRef
/**
* Generic Referende
* Syntax: S:{modid}:{name}
* Parser: [[net.bdew.lib.recipes.RecipeParser.specGenericStack]]
* @param mod modId of owner mod
* @param name name of the stack
*/
case class StackGeneric(mod: String, name: String) extends StackRef
/**
* Block reference
* Syntax: B:[{modid}:]{name} [@ {meta}]
* Parser: [[net.bdew.lib.recipes.RecipeParser.specBlock]]
* @param mod modId of owner mod, default is "minecraft"
* @param name Registered name
* @param meta Metadata / Damage
*/
case class StackBlock(mod: String, name: String, meta: Int) extends StackRef
/**
* Item reference
* Syntax: I:[{modid}:]{name} [@ {meta}]
* Parser: [[net.bdew.lib.recipes.RecipeParser.specItem]]
* @param mod modId of owner mod, default is "minecraft"
* @param name Registered name
* @param meta Metadata / Damage
*/
case class StackItem(mod: String, name: String, meta: Int) extends StackRef
/**
* Reference to an Item/Block/ItemStack pulic static field
* Syntax: {path}.{to}.{class}[{field}] [@ {meta}]
* Parser: [[net.bdew.lib.recipes.RecipeParser.specReflect]]
* @param cls Class name
* @param field Field name
* @param meta Metadata / Damage (Ignored for ItemStacks)
*/
case class StackReflect(cls: String, field: String, meta: Int) extends StackRef
/**
* Reference to an Item/Block/ItemStack pulic static getter
* Syntax: {path}.{to}.{class}.{method}({string}) [@ {meta}]
* Parser: [[net.bdew.lib.recipes.RecipeParser.specGetter]]
* @param cls Class name
* @param method Method name
* @param name Parameter to getter
* @param meta Metadata / Damage (Ignored for ItemStacks)
*/
case class StackGetter(cls: String, method: String, name: String, meta: Int) extends StackRef
/**
* Macro reference to a previous definition
* Syntax: ${char}
* Parser: [[net.bdew.lib.recipes.RecipeParser.specMacro]]
* @param id The underlying character
*/
case class StackMacro(id: Char) extends StackRef
@@ -0,0 +1,43 @@
/*
* Copyright (c) bdew, 2013 - 2014
* https://github.com/bdew/bdlib
*
* This mod is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://bdew.net/minecraft-mod-public-license/
*/
package net.bdew.lib.recipes
/**
* Base class for config statements
* Actual processing is in [[RecipeLoader.processConfigStatement]]
*/
abstract class ConfigStatement
/**
* Block of delayed statements, used to include recipe statements in config scope (which is always top level)
* Syntax: recipes { {statements} }
* Parser: [[RecipeParser.recipesTopBlock]]
* Also used internally by the parser to bridge [[RsConditional]] to config scope in [[RecipeParser.conditionBridge]]
* @param list Statements to run
*/
case class CsRecipeBlock(list: List[RecipeStatement]) extends ConfigStatement
/**
* Conditional config block
* Syntax: if {condition} << {statements} >> [else << {statements} >>]
* Parser: [[RecipeParser.conditionConfig]]
* @param cond Condition
* @param thn Statements to run if true
* @param els Statements to run otherwise
*/
case class CsConditionalConfig(cond: Condition, thn: List[ConfigStatement], els: List[ConfigStatement]) extends ConfigStatement
/**
* Removes previously defined recieps for a specific result
* Syntax: clearRecipes: {spec}
* Parser: [[RecipeParser.clearRecipes]]
* @param res Recipes that produce this result will be cleared
*/
case class CsClearRecipes(res: StackRef) extends ConfigStatement
@@ -0,0 +1,117 @@
/*
* Copyright (c) bdew, 2013 - 2014
* https://github.com/bdew/bdlib
*
* This mod is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://bdew.net/minecraft-mod-public-license/
*/
package net.bdew.lib.recipes
/**
* Base class for recipe statements
* Actual processing is in [[net.bdew.lib.recipes.RecipeLoader.processRecipeStatements]]
*
* Naming convention: All subclasses are named RsFoo
*/
abstract class RecipeStatement
/**
* Character assignment
* Syntax: {char} = {ref}
* Parser: [[net.bdew.lib.recipes.RecipeParser.charSpec]]
* @param char Character to assign
* @param spec ItemStack Reference
*/
case class RsCharAssign(char: Char, spec: StackRef) extends RecipeStatement
/**
* Define class macro
* Syntax: def {id} = {name}
* Parser: [[net.bdew.lib.recipes.RecipeParser.classMacro]]
* @param id Macro identifier
* @param cls Class name
*/
case class RsClassMacro(id: String, cls: String) extends RecipeStatement
/**
* Unconditional block - allows grouping and isolation of statements
* Syntax: recipes { {statements} }
* Parser: [[net.bdew.lib.recipes.RecipeParser.recipesSubBlock]]
* @param list Statements to run
*/
case class RsRecipes(list: List[RecipeStatement]) extends RecipeStatement
/**
* Conditional recipes block
* Syntax: if {condition} recipes { {statements} } [else { {statements} }]
* Parser: [[net.bdew.lib.recipes.RecipeParser.conditionRecipes]]
* @param cond Condition
* @param thn Statements to run if true
* @param els Statements to run otherwise
*/
case class RsConditional(cond: Condition, thn: List[RecipeStatement], els: List[RecipeStatement]) extends RecipeStatement
/**
* Base class for staments that represent a crafting recipe, that has a result
*/
abstract class CraftingStatement extends RecipeStatement {
def result: StackRef
}
/**
* Shaped recipe definition
*
* Syntax:
* XXX
* XXX => {result} [* {count}]
* XXX
*
* Parser: [[net.bdew.lib.recipes.RecipeParser.recipeShaped3x3]]
*
* Syntax:
* XX
* XX => {result} [* {count}]
*
* Parser: [[net.bdew.lib.recipes.RecipeParser.recipeShaped2x2]]
*
* Syntax: XXX XXX XXX => {result} [* {count}]
* Parser: [[net.bdew.lib.recipes.RecipeParser.recipeShaped9]]
*
* @param rec Recipe pattern
* @param result Result reference
* @param cnt Number of output items
*/
case class RsRecipeShaped(rec: Seq[String], result: StackRef, cnt: Int) extends CraftingStatement
/**
* Shapeless recipe definition
* Syntax: shapeless: {characters} => {result} [* {count}]
* Parser: [[net.bdew.lib.recipes.RecipeParser.recipeShapeless]]
*
* @param rec Recipe pattehrn
* @param result Result reference
* @param cnt Number of output items
*/
case class RsRecipeShapeless(rec: String, result: StackRef, cnt: Int) extends CraftingStatement
/**
* Smelting recipe definition
* Syntax: smelt: {input} => {output}, {XP}
* Parser [[net.bdew.lib.recipes.RecipeParser.recipeSmelting]]
*
* @param in Input Stack
* @param result Output Stack
* @param cnt Output Count
* @param xp XP for smelting
*/
case class RsRecipeSmelting(in: StackRef, result: StackRef, cnt: Int, xp: Float) extends CraftingStatement
/**
* Smelting recipe definition
* Syntax: regOreDict: {reference} -> {name}
* Parser [[net.bdew.lib.recipes.RecipeParser.regOreDict]]
*/
case class RsRegOredict(id: String, spec: StackRef, wildcard: Boolean) extends RecipeStatement
@@ -11,19 +11,19 @@ package net.bdew.lib.recipes.gencfg
import net.bdew.lib.gui.Color
abstract class CfgEntry
abstract class ConfigEntry
case class EntryDouble(v: Double) extends CfgEntry
case class EntryDouble(v: Double) extends ConfigEntry
case class EntryStr(v: String) extends CfgEntry
case class EntryStr(v: String) extends ConfigEntry
case class EntryNumList(v: List[Double]) extends CfgEntry
case class EntryNumList(v: List[Double]) extends ConfigEntry
case class ConfigSection(pfx: String = "") extends CfgEntry with Iterable[(String, CfgEntry)] {
var raw = Map.empty[String, CfgEntry].withDefault(x => sys.error("Config value '%s%s' is missing".format(pfx, x)))
case class ConfigSection(pfx: String = "") extends ConfigEntry with Iterable[(String, ConfigEntry)] {
var raw = Map.empty[String, ConfigEntry].withDefault(x => sys.error("Config value '%s%s' is missing".format(pfx, x)))
def iterator: Iterator[(String, CfgEntry)] = raw.iterator
def filterType[T <: CfgEntry](cls: Class[T]): Iterable[(String, T)] = raw.filter(x => cls.isInstance(x._2)).map(x => x.asInstanceOf[(String, T)])
def iterator: Iterator[(String, ConfigEntry)] = raw.iterator
def filterType[T <: ConfigEntry](cls: Class[T]): Iterable[(String, T)] = raw.filter(x => cls.isInstance(x._2)).map(x => x.asInstanceOf[(String, T)])
def keys = raw.keys
@@ -35,7 +35,7 @@ case class ConfigSection(pfx: String = "") extends CfgEntry with Iterable[(Strin
sys.error("Config value '%s%s' is of the wrong type, expected %s, got %s".format(pfx, id, t.getName, v.getClass.getName))
}
def set(id: String, v: CfgEntry) = raw += id -> v
def set(id: String, v: ConfigEntry) = raw += id -> v
def getDouble(id: String) = rawget(id, classOf[EntryDouble]).v
def getString(id: String) = rawget(id, classOf[EntryStr]).v
@@ -10,25 +10,26 @@
package net.bdew.lib.recipes.gencfg
import net.bdew.lib.BdLib
import net.bdew.lib.recipes.{RecipeLoader, Statement}
import net.bdew.lib.recipes.{ConfigStatement, RecipeLoader}
trait GenericConfigLoader extends RecipeLoader {
val cfgStore: ConfigSection
def processConfigStatement(section: ConfigSection, s: CfgStatement): Unit = s match {
def processConfigStatement(section: ConfigSection, s: CfgEntry): Unit = s match {
case CfgVal(id, v) =>
BdLib.logInfo("Config: %s%s = %s", section.pfx, id, v)
section.set(id, v)
case CfgSub(id, st) => processConfigBlock(section.getOrAddSection(id), st)
case CfgSection(id, st) => processConfigBlock(section.getOrAddSection(id), st)
case x =>
BdLib.logError("Can't process %s - this is a programing bug!", x)
}
def processConfigBlock(view: ConfigSection, block: List[CfgStatement]) = for (s <- block) processConfigStatement(view, s)
def processConfigBlock(view: ConfigSection, block: List[CfgEntry]): Unit =
for (s <- block) processConfigStatement(view, s)
override def processStatement(s: Statement) = s match {
case StCfg(id, st) => processConfigBlock(cfgStore.getOrAddSection(id), st)
case _ => super.processStatement(s)
override def processConfigStatement(s: ConfigStatement) = s match {
case CsCfgSection(id, st) => processConfigBlock(cfgStore.getOrAddSection(id), st)
case _ => super.processConfigStatement(s)
}
}
@@ -12,7 +12,7 @@ package net.bdew.lib.recipes.gencfg
import net.bdew.lib.recipes.RecipeParser
trait GenericConfigParser extends RecipeParser {
override def statement = statementCfg | super.statement
override def configStatement = statementCfg | super.configStatement
// Hex variant must come before decimalNumber, otherwise the parser breaks
def signedNumber = (
@@ -21,17 +21,21 @@ trait GenericConfigParser extends RecipeParser {
| ("-" ~> decimalNumber) ^^ { case x => -x.toDouble }
)
def cfgStatementNum = str ~ "=" ~ signedNumber ^^ { case id ~ eq ~ n => CfgVal(id, EntryDouble(n.toDouble)) }
def cfgStatementStr = str ~ "=" ~ str ^^ { case id ~ eq ~ s => CfgVal(id, EntryStr(s)) }
def cfgStatementNumList = str ~ ("=" ~> "{" ~> signedNumber.* <~ "}") ^^ { case id ~ l => CfgVal(id, EntryNumList(l)) }
def cfgStatementSub = cfgBlock ^^ { case (id, st) => CfgSub(id, st) }
def cvNum = signedNumber ^^ EntryDouble
def cvStr = str ^^ EntryStr
def cvNumList = "=" ~> "{" ~> signedNumber.* <~ "}" ^^ EntryNumList
def cfgStatement = cfgStatementNum | cfgStatementStr | cfgStatementSub | cfgStatementNumList
def cfgValue = cvNum | cvStr | cvNumList
def ceSub = cfgBlock ^^ { case (id, st) => CfgSection(id, st) }
def ceAssign = str ~ "=" ~ cfgValue ^^ { case k ~ eq ~ v => CfgVal(k, v) }
def cfgEntry = ceSub | ceAssign
// A bit of type acrobatics because this is recursive (via cfgStatementSub)
// the tuple is translated into the proper class in either cfgStatementSub or cfg
def cfgBlock: Parser[(String, List[CfgStatement])] =
"cfg" ~> str ~ ("{" ~> cfgStatement.* <~ "}") ^^ { case id ~ st => (id, st) }
def cfgBlock: Parser[(String, List[CfgEntry])] =
"cfg" ~> str ~ ("{" ~> cfgEntry.* <~ "}") ^^ { case id ~ st => (id, st) }
def statementCfg = cfgBlock ^^ { case (id, st) => StCfg(id, st) }
def statementCfg = cfgBlock ^^ { case (id, st) => CsCfgSection(id, st) }
}
@@ -9,15 +9,15 @@
package net.bdew.lib.recipes.gencfg
import net.bdew.lib.recipes.Statement
import net.bdew.lib.recipes.ConfigStatement
abstract class CfgStatement
abstract class CfgEntry
case class CfgVal(id: String, value: CfgEntry) extends CfgStatement
case class CfgVal(id: String, value: ConfigEntry) extends CfgEntry
case class CfgSub(id: String, vals: List[CfgStatement]) extends CfgStatement
case class CfgSection(id: String, vals: List[CfgEntry]) extends CfgEntry
case class StCfg(id: String, vals: List[CfgStatement]) extends Statement
case class CsCfgSection(id: String, vals: List[CfgEntry]) extends ConfigStatement
@@ -10,14 +10,14 @@
package net.bdew.lib.recipes.lootlist
import net.bdew.lib.recipes.StackRef
import net.bdew.lib.recipes.gencfg.CfgEntry
import net.bdew.lib.recipes.gencfg.ConfigEntry
/**
* Allos definition of random-weighted loot/drop lists in config sections
* Allows definition of random-weighted loot/drop lists in config sections
* @param list List on (chance percent, stack reference) pairs, resolve to
* real ItemStacks with LootListLoader.resolveLootList
*/
case class EntryLootList(list: List[(Int, StackRef)]) extends CfgEntry
case class EntryLootList(list: List[(Int, StackRef)]) extends ConfigEntry
@@ -9,17 +9,18 @@
package net.bdew.lib.recipes.lootlist
import net.bdew.lib.recipes.gencfg.{CfgVal, GenericConfigParser}
import net.bdew.lib.recipes.gencfg.GenericConfigParser
/**
* Parser mixin for loot lists
* ident: DropsList(
* ident = DropsList(
* {chance}% {stackRef}
* ...
* )
*/
trait LootListParser extends GenericConfigParser {
def dropsEntry = (wholeNumber <~ "%") ~ spec ^^ { case n ~ s => (n.toInt, s) }
def cfgDrops = ident ~ (":" ~> "DropsList" ~> "(" ~> dropsEntry.* <~ ")") ^^ { case id ~ a => CfgVal(id, EntryLootList(a)) }
override def cfgStatement = cfgDrops | super.cfgStatement
def ceDrops = "DropsList" ~> "(" ~> dropsEntry.* <~ ")" ^^ EntryLootList
override def cfgValue = ceDrops | super.cfgValue
}