It allows:
-to register new crafting types, with register_new_craft_type(name,setf,getf)
name if the name of the crafting type, setf is a function that will be called when registering a craft, that should store the recipe somewhere, and getf if called when calling minetest.get_craft_result, and should return something like get_craft_result (a table with the new item, and a table for the replacements)
-to allow or not to register a crafting recipe. For that, use the minetest.register_can_register_craft: it should return true if craft can be registered, or false if you want to cancel craft. For now, it is the only way to remove a crafting definition from default mod.
-to be called when a crafting recipe is registered, by using minetest.register_on_register_craft. This can be useful for mods like crafting guides which would like to access other recipes types than default ones.
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
registered_ctypes_set = {}
registered_ctypes_get = {}
minetest.registered_can_register_craft = {}
minetest.registered_on_register_craft = {}
function minetest.register_can_register_craft(func)
minetest.registered_can_register_craft[
#minetest.registered_can_register_craft+1]=func
end
function minetest.register_on_register_craft(func)
minetest.registered_on_register_craft[
#minetest.registered_on_register_craft+1]=func
end
register_craft = minetest.register_craft
function minetest.register_craft(options)
for _,func in ipairs(minetest.registered_can_register_craft) do
if not func(options) then return end
end
for _,func in ipairs(minetest.registered_on_register_craft) do
func(options)
end
local setf = registered_ctypes_set[options.type]
if setf ~= nil then
setf(options)
else
register_craft(options)
end
end
get_craft_result = minetest.get_craft_result
function minetest.get_craft_result(options)
local getf = registered_rtypes_get[options.type]
if getf ~= nil then
getf(options)
else
get_craft_result(options)
end
end
function register_new_craft_type(name, setf, getf)
if registered_ctypes_set[name] ~= nil then
print("[RecipesLib] Warning: Crafting type "..name.."already"..
"registered, not overwriting")
return
end
registered_ctypes_set[name] = setf
registered_ctypes_get[name] = getf
end
print("[RecipesLib] Loaded core")
local worldpath = minetest.get_worldpath()
local emods_file = io.open(worldpath.."/world.mt","r")
local emods = ""
if emods_file ~= nil then
emods = emods_file:read()
emods_file:close()
end
local function enabled(modname)
return string.find(emods,"load_mod_"..modname.." = false") == nil
end
local modnames = minetest.get_modnames()
for i, modname in ipairs(modnames) do
if enabled(modname) then
local modpath = minetest.get_modpath(modname)
local modfile = modpath.."/recipeslib.lua"
local mfile = io.open(modfile,"r")
if mfile ~= nil then
mfile:close()
dofile(modfile)
print("[RecipesLib] Loaded "..modname)
end
end
end