Skip to content
This repository was archived by the owner on Aug 9, 2023. It is now read-only.

Commit 3d77ec5

Browse files
committed
Ability to register custom crafts dynamically
1 parent 83151cb commit 3d77ec5

File tree

2 files changed

+45
-42
lines changed

2 files changed

+45
-42
lines changed

API.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,6 @@ end)
7373

7474
Removes the recipe filter with the given name.
7575

76-
#### `craftguide.set_recipe_filter(name, function(recipe, player))`
77-
78-
Removes all recipe filters and adds a new one.
79-
8076
#### `craftguide.get_recipe_filters()`
8177

8278
Returns a map of recipe filters, indexed by name.

init.lua

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ local fuel_cache = {}
1313
local progressive_mode = core.settings:get_bool("craftguide_progressive_mode")
1414
local sfinv_only = core.settings:get_bool("craftguide_sfinv_only") and rawget(_G, "sfinv")
1515

16+
local log = core.log
1617
local after = core.after
1718
local colorize = core.colorize
1819
local reg_items = core.registered_items
@@ -33,8 +34,8 @@ local on_receive_fields = core.register_on_player_receive_fields
3334
local ESC = core.formspec_escape
3435
local S = core.get_translator("craftguide")
3536

36-
local maxn, sort, concat, copy =
37-
table.maxn, table.sort, table.concat, table.copy
37+
local maxn, sort, concat, copy, insert =
38+
table.maxn, table.sort, table.concat, table.copy, table.insert
3839

3940
local fmt, find, gmatch, match, sub, split, upper, lower =
4041
string.format, string.find, string.gmatch, string.match,
@@ -78,10 +79,6 @@ local function table_replace(t, val, new)
7879
end
7980
end
8081

81-
local function __func()
82-
return debug.getinfo(2, "n").name
83-
end
84-
8582
local function is_str(x)
8683
return type(x) == "string"
8784
end
@@ -98,24 +95,35 @@ local function is_func(x)
9895
return type(x) == "function"
9996
end
10097

101-
local custom_crafts, craft_types = {}, {}
98+
local craft_types = {}
10299

103100
function craftguide.register_craft_type(name, def)
104-
local func = "craftguide." .. __func() .. "(): "
105-
assert(is_str(name), func .. "'name' field missing")
106-
assert(is_str(def.description), func .. "'description' field missing")
107-
assert(is_str(def.icon), func .. "'icon' field missing")
101+
if not is_str(name) or name == "" then
102+
return log("error", "craftguide.register_craft_type(): name missing")
103+
end
104+
105+
if not is_str(def.description) then
106+
def.description = ""
107+
end
108+
109+
if not is_str(def.icon) then
110+
def.icon = ""
111+
end
108112

109113
craft_types[name] = def
110114
end
111115

112116
function craftguide.register_craft(def)
117+
if not is_table(def) or not next(def) then
118+
return log("error", "craftguide.register_craft(): craft definition missing")
119+
end
120+
113121
if def.result then
114122
def.output = def.result -- Backward compatibility
115123
end
116124

117-
if not is_str(def.output) then
118-
def.output = ""
125+
if not is_str(def.output) or def.output == "" then
126+
return log("error", "craftguide.register_craft(): output missing")
119127
end
120128

121129
if not is_table(def.items) then
@@ -151,15 +159,19 @@ function craftguide.register_craft(def)
151159
end
152160
end
153161

154-
custom_crafts[#custom_crafts + 1] = def
162+
local output = match(def.output, "%S*")
163+
recipes_cache[output] = recipes_cache[output] or {}
164+
insert(recipes_cache[output], def)
155165
end
156166

157167
local recipe_filters = {}
158168

159169
function craftguide.add_recipe_filter(name, f)
160-
local func = "craftguide." .. __func() .. "(): "
161-
assert(is_str(name), func .. "filter name missing")
162-
assert(is_func(f), func .. "filter function missing")
170+
if not is_str(name) or name == "" then
171+
return log("error", "craftguide.add_recipe_filter(): name missing")
172+
elseif not is_func(f) then
173+
return log("error", "craftguide.add_recipe_filter(): function missing")
174+
end
163175

164176
recipe_filters[name] = f
165177
end
@@ -168,14 +180,6 @@ function craftguide.remove_recipe_filter(name)
168180
recipe_filters[name] = nil
169181
end
170182

171-
function craftguide.set_recipe_filter(name, f)
172-
local func = "craftguide." .. __func() .. "(): "
173-
assert(is_str(name), func .. "filter name missing")
174-
assert(is_func(f), func .. "filter function missing")
175-
176-
recipe_filters = {[name] = f}
177-
end
178-
179183
function craftguide.get_recipe_filters()
180184
return recipe_filters
181185
end
@@ -191,9 +195,11 @@ end
191195
local search_filters = {}
192196

193197
function craftguide.add_search_filter(name, f)
194-
local func = "craftguide." .. __func() .. "(): "
195-
assert(is_str(name), func .. "filter name missing")
196-
assert(is_func(f), func .. "filter function missing")
198+
if not is_str(name) or name == "" then
199+
return log("error", "craftguide.add_search_filter(): name missing")
200+
elseif not is_func(f) then
201+
return log("error", "craftguide.add_search_filter(): function missing")
202+
end
197203

198204
search_filters[name] = f
199205
end
@@ -299,17 +305,17 @@ end
299305

300306
local function cache_recipes(output)
301307
local recipes = get_all_recipes(output) or {}
308+
local num = #recipes
302309

303-
for i = 1, #custom_crafts do
304-
local custom_craft = custom_crafts[i]
305-
if match(custom_craft.output, "%S*") == output then
306-
recipes[#recipes + 1] = custom_craft
310+
if num > 0 then
311+
if recipes_cache[output] then
312+
for i = 1, num do
313+
insert(recipes_cache[output], 1, recipes[i])
314+
end
315+
else
316+
recipes_cache[output] = recipes
307317
end
308318
end
309-
310-
if #recipes > 0 then
311-
recipes_cache[output] = recipes
312-
end
313319
end
314320

315321
local function cache_usages(item)
@@ -1335,8 +1341,9 @@ register_command("craft", {
13351341
})
13361342

13371343
function craftguide.show(name, item, show_usages)
1338-
local func = "craftguide." .. __func() .. "(): "
1339-
assert(is_str(name), func .. "player name missing")
1344+
if not is_str(name) or name == "" then
1345+
return log("error", "craftguide.show(): player name missing")
1346+
end
13401347

13411348
local data = pdata[name]
13421349
local player = get_player_by_name(name)

0 commit comments

Comments
 (0)