[ModLib] RecipesLib

Post Reply
Nore
Developer
Posts: 501
Joined: Wed Nov 28, 2012 11:35
GitHub: Ekdohibs

[ModLib] RecipesLib

by Nore » Post

This is a library for registering new recipe types (for example: grinding or alloying in technic), and to get callbacks when registering recipes. However, because of that, it has to be executed before default, so it has to be put in a mod with a name such as aaa_recipeslib and with an empty depends.txt. The registrations should be put in a recipeslib.lua file in the mod folder, as those will be called by recipeslib before any other mod loads.

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.

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
Last edited by Nore on Mon Nov 11, 2013 07:28, edited 1 time in total.

User avatar
rubenwardy
Moderator
Posts: 6425
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: United Kingdom
Contact:

by rubenwardy » Post

bump

This is interesting.
Last edited by rubenwardy on Fri May 10, 2013 19:23, edited 1 time in total.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests