Ability to register custom crafts dynamically

This commit is contained in:
Jean-Patrick Guerrero 2019-09-06 14:48:44 +02:00
parent 83151cb394
commit 3d77ec58b4
2 changed files with 45 additions and 42 deletions

4
API.md
View File

@ -73,10 +73,6 @@ end)
Removes the recipe filter with the given name. Removes the recipe filter with the given name.
#### `craftguide.set_recipe_filter(name, function(recipe, player))`
Removes all recipe filters and adds a new one.
#### `craftguide.get_recipe_filters()` #### `craftguide.get_recipe_filters()`
Returns a map of recipe filters, indexed by name. Returns a map of recipe filters, indexed by name.

View File

@ -13,6 +13,7 @@ local fuel_cache = {}
local progressive_mode = core.settings:get_bool("craftguide_progressive_mode") local progressive_mode = core.settings:get_bool("craftguide_progressive_mode")
local sfinv_only = core.settings:get_bool("craftguide_sfinv_only") and rawget(_G, "sfinv") local sfinv_only = core.settings:get_bool("craftguide_sfinv_only") and rawget(_G, "sfinv")
local log = core.log
local after = core.after local after = core.after
local colorize = core.colorize local colorize = core.colorize
local reg_items = core.registered_items local reg_items = core.registered_items
@ -33,8 +34,8 @@ local on_receive_fields = core.register_on_player_receive_fields
local ESC = core.formspec_escape local ESC = core.formspec_escape
local S = core.get_translator("craftguide") local S = core.get_translator("craftguide")
local maxn, sort, concat, copy = local maxn, sort, concat, copy, insert =
table.maxn, table.sort, table.concat, table.copy table.maxn, table.sort, table.concat, table.copy, table.insert
local fmt, find, gmatch, match, sub, split, upper, lower = local fmt, find, gmatch, match, sub, split, upper, lower =
string.format, string.find, string.gmatch, string.match, string.format, string.find, string.gmatch, string.match,
@ -78,10 +79,6 @@ local function table_replace(t, val, new)
end end
end end
local function __func()
return debug.getinfo(2, "n").name
end
local function is_str(x) local function is_str(x)
return type(x) == "string" return type(x) == "string"
end end
@ -98,24 +95,35 @@ local function is_func(x)
return type(x) == "function" return type(x) == "function"
end end
local custom_crafts, craft_types = {}, {} local craft_types = {}
function craftguide.register_craft_type(name, def) function craftguide.register_craft_type(name, def)
local func = "craftguide." .. __func() .. "(): " if not is_str(name) or name == "" then
assert(is_str(name), func .. "'name' field missing") return log("error", "craftguide.register_craft_type(): name missing")
assert(is_str(def.description), func .. "'description' field missing") end
assert(is_str(def.icon), func .. "'icon' field missing")
if not is_str(def.description) then
def.description = ""
end
if not is_str(def.icon) then
def.icon = ""
end
craft_types[name] = def craft_types[name] = def
end end
function craftguide.register_craft(def) function craftguide.register_craft(def)
if not is_table(def) or not next(def) then
return log("error", "craftguide.register_craft(): craft definition missing")
end
if def.result then if def.result then
def.output = def.result -- Backward compatibility def.output = def.result -- Backward compatibility
end end
if not is_str(def.output) then if not is_str(def.output) or def.output == "" then
def.output = "" return log("error", "craftguide.register_craft(): output missing")
end end
if not is_table(def.items) then if not is_table(def.items) then
@ -151,15 +159,19 @@ function craftguide.register_craft(def)
end end
end end
custom_crafts[#custom_crafts + 1] = def local output = match(def.output, "%S*")
recipes_cache[output] = recipes_cache[output] or {}
insert(recipes_cache[output], def)
end end
local recipe_filters = {} local recipe_filters = {}
function craftguide.add_recipe_filter(name, f) function craftguide.add_recipe_filter(name, f)
local func = "craftguide." .. __func() .. "(): " if not is_str(name) or name == "" then
assert(is_str(name), func .. "filter name missing") return log("error", "craftguide.add_recipe_filter(): name missing")
assert(is_func(f), func .. "filter function missing") elseif not is_func(f) then
return log("error", "craftguide.add_recipe_filter(): function missing")
end
recipe_filters[name] = f recipe_filters[name] = f
end end
@ -168,14 +180,6 @@ function craftguide.remove_recipe_filter(name)
recipe_filters[name] = nil recipe_filters[name] = nil
end end
function craftguide.set_recipe_filter(name, f)
local func = "craftguide." .. __func() .. "(): "
assert(is_str(name), func .. "filter name missing")
assert(is_func(f), func .. "filter function missing")
recipe_filters = {[name] = f}
end
function craftguide.get_recipe_filters() function craftguide.get_recipe_filters()
return recipe_filters return recipe_filters
end end
@ -191,9 +195,11 @@ end
local search_filters = {} local search_filters = {}
function craftguide.add_search_filter(name, f) function craftguide.add_search_filter(name, f)
local func = "craftguide." .. __func() .. "(): " if not is_str(name) or name == "" then
assert(is_str(name), func .. "filter name missing") return log("error", "craftguide.add_search_filter(): name missing")
assert(is_func(f), func .. "filter function missing") elseif not is_func(f) then
return log("error", "craftguide.add_search_filter(): function missing")
end
search_filters[name] = f search_filters[name] = f
end end
@ -299,17 +305,17 @@ end
local function cache_recipes(output) local function cache_recipes(output)
local recipes = get_all_recipes(output) or {} local recipes = get_all_recipes(output) or {}
local num = #recipes
for i = 1, #custom_crafts do if num > 0 then
local custom_craft = custom_crafts[i] if recipes_cache[output] then
if match(custom_craft.output, "%S*") == output then for i = 1, num do
recipes[#recipes + 1] = custom_craft insert(recipes_cache[output], 1, recipes[i])
end
else
recipes_cache[output] = recipes
end end
end end
if #recipes > 0 then
recipes_cache[output] = recipes
end
end end
local function cache_usages(item) local function cache_usages(item)
@ -1335,8 +1341,9 @@ register_command("craft", {
}) })
function craftguide.show(name, item, show_usages) function craftguide.show(name, item, show_usages)
local func = "craftguide." .. __func() .. "(): " if not is_str(name) or name == "" then
assert(is_str(name), func .. "player name missing") return log("error", "craftguide.show(): player name missing")
end
local data = pdata[name] local data = pdata[name]
local player = get_player_by_name(name) local player = get_player_by_name(name)