From ce8269fb03c24e538ae3db79cfde793b0305de4f Mon Sep 17 00:00:00 2001 From: octacian Date: Sun, 26 Feb 2017 11:32:32 -0800 Subject: [PATCH] Improve recipe API --- api.lua | 44 +++++++++++++++++++---------------------- doc/api.md | 38 +++++++++++------------------------ modules/power/gen.lua | 20 +++++++++---------- modules/shared/init.lua | 18 ++++++++--------- 4 files changed, 51 insertions(+), 69 deletions(-) diff --git a/api.lua b/api.lua index 47afef7..cacff92 100644 --- a/api.lua +++ b/api.lua @@ -7,36 +7,32 @@ function microexpansion.register_recipe(output, recipe) return n==math.floor(n) end - local function getAmount() - if isint(recipe[2][1]) then - local q = recipe[2][1] - recipe[2][1] = nil - return q + local function get_amount(_) + if isint(recipe[_][1]) then + return recipe[_][1] else return 1 end end - local function register(amount, recipe) - minetest.register_craft({ - output = output.." "..amount, - recipe = recipe, - }) - end - - local function single() - register(getAmount(), recipe[2]) - end - - local function multiple() - for i, item in ipairs(recipe) do - if i == 0 then return end - register(getAmount(), recipe[i]) + local function get_type(_) + if type(recipe[_][2]) == "string" then + return recipe[_][2] end end - -- Check type - if recipe[1] == "single" then single() - elseif recipe[1] == "multiple" then multiple() - else return microexpansion.log("invalid recipe for definition "..output..". "..dump(recipe[2])) end + local function register(_) + local def = { + type = get_type(_), + output = output.." "..tostring(get_amount(_)), + recipe = recipe[_][3] or recipe[_][2] + } + + microexpansion.log("Recipe: "..dump(def)) + minetest.register_craft(def) + end + + for _, i in ipairs(recipe) do + register(_) + end end -- [local function] Choose description colour diff --git a/doc/api.md b/doc/api.md index 1ee5b20..c51b35f 100644 --- a/doc/api.md +++ b/doc/api.md @@ -4,39 +4,25 @@ The core API is composed of several functions to make registering new items, nod #### `register_recipe(output, def)` __Usage:__ `microexpansion.register_recipe(, )` -Though this may seem rather complex to understand, this is a very useful timesaving function when registering recipes. It allows registering multiple recipes at once in one table. The output must always remain the same as is specified as the first parameter, while the second parameter should be a table structured like one of the tables below. +Though this may seem rather complex to understand, this is a very useful timesaving function when registering recipes. It allows registering multiple recipes at once in one table. The output must always remain the same as is specified as the first parameter, while the second parameter should be a table structured like the table below. -__Single Recipe:__ +__Example:__ ```lua microexpansion.register_recipe("default:steelblock", { - "single", - { 1, - { "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }, - { "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }, - { "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }, + { 1, { + { "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }, + { "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }, + { "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }, + }, + }, + { 1, "shapeless", { + "default:steel_ingot", "default:obsidian_shard", "default:steel_ingot", + }, }, }) ``` -The above registers a single recipe for the item specified. The `1` specifies the output quantity. - -__Multiple Recipes:__ -```lua -microexpansion.register_recipe("default:steelblock", { - "multiple", - { 1, - { "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }, - { "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }, - { "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }, - }, - { 1, - { "default:steel_ingot", "default:steel_ingot" }, - { "default:steel_ingot", "default:steel_ingot" }, - } -}) -``` - -The above registers multiple recipes for the item specified. The `1` specifies the output quantity. +The above registers a two recipees for the item specified. The `1` specifies the output quantity. `shapeless` causes the second recipe to be of the `shapeless` type. After the first one or two definitions (amount, type), the recipe can be specified as normal inside another sub-table. You can have as many recipe sub-tables as you want. #### `register_item(itemstring, def)` __Usage:__ `microexpansion.register_item(, ` diff --git a/modules/power/gen.lua b/modules/power/gen.lua index b3c4c2c..956987e 100644 --- a/modules/power/gen.lua +++ b/modules/power/gen.lua @@ -14,11 +14,11 @@ me.register_node("fuel_fired_generator", { "fuelgen_front", }, recipe = { - "single", - { 1, - { "default:steel_ingot", "default:furnace", "default:steel_ingot" }, - {"default:steel_ingot", "microexpansion:machine_casing", "default:steel_ingot" }, - { "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }, + { 1, { + { "default:steel_ingot", "default:furnace", "default:steel_ingot" }, + {"default:steel_ingot", "microexpansion:machine_casing", "default:steel_ingot" }, + { "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }, + }, } }, groups = { cracky = 1 }, @@ -38,11 +38,11 @@ me.register_node("super_smelter", { "super_smelter_front", }, recipe = { - "single", - { 1, - { "default:furnace", "default:furnace", "default:furnace" }, - { "default:steel_ingot", "microexpansion:machine_casing", "default:steel_ingot" }, - { "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }, + { 1, { + { "default:furnace", "default:furnace", "default:furnace" }, + { "default:steel_ingot", "microexpansion:machine_casing", "default:steel_ingot" }, + { "default:steel_ingot", "default:steel_ingot", "default:steel_ingot" }, + }, }, }, groups = { cracky = 1 }, diff --git a/modules/shared/init.lua b/modules/shared/init.lua index f4ea723..b68bec4 100644 --- a/modules/shared/init.lua +++ b/modules/shared/init.lua @@ -9,9 +9,9 @@ local me = microexpansion me.register_item("steel_infused_obsidian_ingot", { description = "Steel Infused Obsidian Ingot", recipe = { - "single", - { 1, - { "default:steel_ingot", "default:obsidian_shard", "default:steel_ingot" }, + { 1, { + { "default:steel_ingot", "default:obsidian_shard", "default:steel_ingot" }, + }, }, }, }) @@ -20,11 +20,11 @@ me.register_item("steel_infused_obsidian_ingot", { me.register_item("machine_casing", { description = "Machine Casing", recipe = { - "single", - { 1, - {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"}, - {"default:steel_ingot", "default:copper_ingot", "default:steel_ingot"}, - {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"} + { 1, { + {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"}, + {"default:steel_ingot", "default:copper_ingot", "default:steel_ingot"}, + {"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"}, + }, + }, }, -}, })