Permalink
Browse files

Add shaped_recipe to Purugin::Recipes

  • Loading branch information...
enebo committed Mar 3, 2013
1 parent 2d5346e commit 37f7073a947b0b5e400ae3ebf845a0531ad0cdf1
Showing with 29 additions and 1 deletion.
  1. +29 −1 src/main/resources/purugin/recipes.rb
@@ -7,7 +7,7 @@ module Recipes
# ingrediants.
#
# === Examples: ===
- # server.add_recipe = shapeless_recipe(item_list(:mossy_cobblestone, 10),
+ # server.add_recipe = shapeless_recipe(item_stack(:mossy_cobblestone, 10),
# [[10, :gold_nugget], [10, :cobblestone]])
#
def shapeless_recipe(result, *ingredients)
@@ -19,5 +19,33 @@ def shapeless_recipe(result, *ingredients)
end
end
end
+
+ ##
+ # Create a shaped recipe where ingredients are required to be put in a particular pattern
+ # to craft an item.
+ #
+ # === Examples: ===
+ #
+ # server.add_recipe shaped_recipe(item_stack(:sand, 10), "xy\nyx\n", x: :wood, y: :gravel)
+ #
+ def shaped_recipe(result, pattern, letter_mappings)
+ raise ArgumentError.new("Pattern must be n by n newline-delimited string of chars") unless pattern
+ raise ArgumentError.new("Must provide char mappings to the pattern") if letter_mappings.size < 1
+ rows = pattern.split(/\n/)
+
+ raise ArgumentError.new("Pattern must be >0 and <3 in n by b size") if rows.find {|row| row.size < 1 || rows.size > 3}
+
+ org.bukkit.inventory.ShapedRecipe.new(result).tap do |recipe|
+ recipe.shape(*rows)
+ letter_mappings.each do |char, ingredient|
+ if ingredient.kind_of? Array
+ ingredient, amount = *ingredient
+ recipe.set_ingredient(char.to_s.ord, ingredient.to_material, amount)
+ else
+ recipe.set_ingredient(char.to_s.ord, ingredient.to_material)
+ end
+ end
+ end
+ end
end
end

0 comments on commit 37f7073

Please sign in to comment.