Improve ME node/item registration functions

Improves item registration and adds node registration. These functions allow easy registration of recipes, changing colour of second line in description, and "autocompletion" for texture names.
This commit is contained in:
octacian 2017-02-24 08:28:36 -08:00
parent a10f2527c0
commit cd99564739
3 changed files with 46 additions and 20 deletions

47
api.lua
View File

@ -1,8 +1,8 @@
-- microexpansion/api.lua -- microexpansion/api.lua
local BASENAME = "microexpansion" local BASENAME = "microexpansion"
-- [local function] register recipe -- [function] Register Recipe
local function register_recipe(output, recipe) function microexpansion.register_recipe(output, recipe)
local function isint(n) local function isint(n)
return n==math.floor(n) return n==math.floor(n)
end end
@ -33,27 +33,52 @@ local function register_recipe(output, recipe)
end end
end end
-- check type -- Check type
if recipe[1] == "single" then single() if recipe[1] == "single" then single()
elseif recipe[1] == "multiple" then multiple() elseif recipe[1] == "multiple" then multiple()
else return microexpansion.log("invalid recipe for definition "..output..". "..dump(recipe[2])) end else return microexpansion.log("invalid recipe for definition "..output..". "..dump(recipe[2])) end
end end
-- [function] register item -- [function] Register Item
function microexpansion.register_item(itemstring, def) function microexpansion.register_item(itemstring, def)
-- usedfor -- Set usedfor
if def.usedfor then if def.usedfor then
def.description = def.description .. "\nfor " .. def.usedfor def.description = def.description .. "\n"..minetest.colorize("grey", def.usedfor)
end
-- Update inventory image
if def.inventory_image then
def.inventory_image = BASENAME.."_"..def.inventory_image..".png"
else
def.inventory_image = BASENAME.."_"..itemstring..".png"
end
-- Register craftitem
minetest.register_craftitem(BASENAME..":"..itemstring, def)
-- if recipe, Register recipe
if def.recipe then
microexpansion.register_recipe(BASENAME..":"..itemstring, def.recipe)
end
end
-- [function] Register Node
function microexpansion.register_node(itemstring, def)
-- Set usedfor
if def.usedfor then
def.description = def.description .. "\n"..minetest.colorize("grey", def.usedfor)
end
-- Update texture
if auto_complete ~= false then
for _,i in ipairs(def.tiles) do
def.tiles[_] = BASENAME.."_"..i..".png"
end
end end
-- register craftitem -- register craftitem
minetest.register_craftitem(BASENAME..":"..itemstring, { minetest.register_node(BASENAME..":"..itemstring, def)
description = def.description,
inventory_image = BASENAME.."_"..def.inventory_image..".png",
})
-- if recipe, register recipe -- if recipe, register recipe
if def.recipe then if def.recipe then
register_recipe(BASENAME..":"..itemstring, def.recipe) microexpansion.register_recipe(BASENAME..":"..itemstring, def.recipe)
end end
end end

View File

@ -26,7 +26,7 @@ function microexpansion.register_cell(itemstring, def)
if def.recipe then if def.recipe then
-- if recipe, register recipe -- if recipe, register recipe
if def.recipe then if def.recipe then
register_recipe(BASENAME..":"..itemstring, def.recipe) microexpansion.register_recipe(BASENAME..":"..itemstring, def.recipe)
end end
end end
end end

View File

@ -50,15 +50,16 @@ local function chest_formspec(pos, start_id, listname, page_max, query)
end end
-- [me chest] Register node -- [me chest] Register node
minetest.register_node("microexpansion:chest", { microexpansion.register_node("chest", {
description = "ME Chest\nCan interact with items in ME storage cells", description = "ME Chest",
usedfor = "Can interact with items in ME storage cells",
tiles = { tiles = {
"microexpansion_chest_top.png", "chest_top",
"microexpansion_chest_top.png", "chest_top",
"microexpansion_chest_side.png", "chest_side",
"microexpansion_chest_side.png", "chest_side",
"microexpansion_chest_side.png", "chest_side",
"microexpansion_chest_front.png", "chest_front",
}, },
is_ground_content = false, is_ground_content = false,
groups = { cracky = 1 }, groups = { cracky = 1 },