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

@ -1,8 +1,8 @@
-- microexpansion/api.lua
local BASENAME = "microexpansion"
-- [local function] register recipe
local function register_recipe(output, recipe)
-- [function] Register Recipe
function microexpansion.register_recipe(output, recipe)
local function isint(n)
return n==math.floor(n)
end
@ -33,27 +33,52 @@ local function register_recipe(output, recipe)
end
end
-- check type
-- 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
end
-- [function] register item
-- [function] Register Item
function microexpansion.register_item(itemstring, def)
-- usedfor
-- Set usedfor
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
-- register craftitem
minetest.register_craftitem(BASENAME..":"..itemstring, {
description = def.description,
inventory_image = BASENAME.."_"..def.inventory_image..".png",
})
minetest.register_node(BASENAME..":"..itemstring, def)
-- if recipe, register recipe
if def.recipe then
register_recipe(BASENAME..":"..itemstring, def.recipe)
microexpansion.register_recipe(BASENAME..":"..itemstring, def.recipe)
end
end

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

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