Advertisement
necauqua

Custom Recipes MinecraftPlugin object

Feb 28th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.04 KB | None | 0 0
  1. object MinecraftPlugin extends IPlugin with ICustomRecipe with IRecipeSubtypes{
  2.  
  3.   val modID: String = "minecraft"
  4.  
  5.   def getMachineRecipes: JCollection[ICustomRecipe] = Seq(this)
  6.  
  7.   def getMachineIDs: JCollection[String] = Seq("wb", "bench", "workbench", "furnace")
  8.  
  9.   def getSubtypes: JCollection[String] = Seq("shaped", "shapeless", null)
  10.  
  11.   def addRecipe(machine: String, subtype: String, recipe: JsonObject) = machine match{
  12.     case "wb" | "bench" | "workbench" => addWorkbenchRecipe(subtype, recipe)
  13.     case "furnace" => addFurnaceRecipe(recipe)
  14.   }
  15.  
  16.   def removeRecipes() = {
  17.     //TODO Save all custom recipes in the list and remove it here
  18.   }
  19.  
  20.   @throws[AddRecipeException]
  21.   def check(json: JsonObject, tag: String, recipeType: String) = {
  22.     if(!json.has(tag))
  23.       throw new AddRecipeException("%s recipe don't have %s tag! JSON: %s", recipeType.capitalize, tag, json.toString)
  24.   }
  25.  
  26.   private def addWorkbenchRecipe(subtype: String, recipe: JsonObject) = subtype match{
  27.     case "shapeless" =>
  28.       check(recipe, "input", "shapeless")
  29.       check(recipe, "output", "shapeless")
  30.       val output: JsonItemStack = RecipeParser.decodeItemStack(recipe.get("output"))
  31.       if(output == null)
  32.         throw new AddRecipeException("Shapeless recipe output tag is invalid! JSON: %s", recipe.get("output").toString)
  33.       if(output.isOredict)
  34.         throw new AddRecipeException("Shapeless output can't be oredict! JSON: %s", recipe.get("output").toString)
  35.       val inputs: Array[Object] = RecipeParser.decodeItemStackArray(recipe.get("input"))
  36.         .map(jis => if(jis.isOredict) jis.getItemName else jis.toItemStack)
  37.       //TODO Add recipes to reload list
  38.       GameRegistry.addRecipe(new ShapelessOreRecipe(output.toItemStack, inputs:_*))
  39.     case "shaped" => //TODO Decode and add a shaped recipes
  40.   }
  41.  
  42.   private def addFurnaceRecipe(recipe: JsonObject) = { //TODO Find a way to remove recipe for reloading
  43.     check(recipe, "input", "furnace")
  44.     check(recipe, "output", "furnace")
  45.     val in: JsonItemStack = RecipeParser.decodeItemStack(recipe.get("input"))
  46.     val out: JsonItemStack = RecipeParser.decodeItemStack(recipe.get("output"))
  47.     if(in == null)
  48.       throw new AddRecipeException("Furnace recipe input tag is invalid! JSON: %s", recipe.get("output").toString)
  49.     if(out == null)
  50.       throw new AddRecipeException("Furnace recipe output tag is invalid! JSON: %s", recipe.get("output").toString)
  51.     if(in.isOredict) //TODO Furnace recipe CAN be oredict, just add recipe for each type of ore
  52.       throw new AddRecipeException("Furnace input can't be oredict! JSON: %s", recipe.get("output").toString)
  53.     if(out.isOredict)
  54.       throw new AddRecipeException("Furnace output can't be oredict! JSON: %s", recipe.get("output").toString)
  55.     val xp: Float =
  56.       if(recipe.has("xp") && recipe.get("xp").isJsonPrimitive && recipe.getAsJsonPrimitive("xp").isNumber)
  57.         recipe.getAsJsonPrimitive("xp").getAsFloat
  58.       else 0.0F
  59.     FurnaceRecipes.smelting().func_151394_a(in.toItemStack, out.toItemStack, xp)
  60.   }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement