@@ -13,6 +13,7 @@ local fuel_cache = {}
13
13
local progressive_mode = core .settings :get_bool (" craftguide_progressive_mode" )
14
14
local sfinv_only = core .settings :get_bool (" craftguide_sfinv_only" ) and rawget (_G , " sfinv" )
15
15
16
+ local log = core .log
16
17
local after = core .after
17
18
local colorize = core .colorize
18
19
local reg_items = core .registered_items
@@ -33,8 +34,8 @@ local on_receive_fields = core.register_on_player_receive_fields
33
34
local ESC = core .formspec_escape
34
35
local S = core .get_translator (" craftguide" )
35
36
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
38
39
39
40
local fmt , find , gmatch , match , sub , split , upper , lower =
40
41
string.format , string.find , string.gmatch , string.match ,
@@ -78,10 +79,6 @@ local function table_replace(t, val, new)
78
79
end
79
80
end
80
81
81
- local function __func ()
82
- return debug.getinfo (2 , " n" ).name
83
- end
84
-
85
82
local function is_str (x )
86
83
return type (x ) == " string"
87
84
end
@@ -98,24 +95,35 @@ local function is_func(x)
98
95
return type (x ) == " function"
99
96
end
100
97
101
- local custom_crafts , craft_types = {}, {}
98
+ local craft_types = {}
102
99
103
100
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
108
112
109
113
craft_types [name ] = def
110
114
end
111
115
112
116
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
+
113
121
if def .result then
114
122
def .output = def .result -- Backward compatibility
115
123
end
116
124
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 " )
119
127
end
120
128
121
129
if not is_table (def .items ) then
@@ -151,15 +159,19 @@ function craftguide.register_craft(def)
151
159
end
152
160
end
153
161
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 )
155
165
end
156
166
157
167
local recipe_filters = {}
158
168
159
169
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
163
175
164
176
recipe_filters [name ] = f
165
177
end
@@ -168,14 +180,6 @@ function craftguide.remove_recipe_filter(name)
168
180
recipe_filters [name ] = nil
169
181
end
170
182
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
-
179
183
function craftguide .get_recipe_filters ()
180
184
return recipe_filters
181
185
end
191
195
local search_filters = {}
192
196
193
197
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
197
203
198
204
search_filters [name ] = f
199
205
end
@@ -299,17 +305,17 @@ end
299
305
300
306
local function cache_recipes (output )
301
307
local recipes = get_all_recipes (output ) or {}
308
+ local num = # recipes
302
309
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
307
317
end
308
318
end
309
-
310
- if # recipes > 0 then
311
- recipes_cache [output ] = recipes
312
- end
313
319
end
314
320
315
321
local function cache_usages (item )
@@ -1335,8 +1341,9 @@ register_command("craft", {
1335
1341
})
1336
1342
1337
1343
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
1340
1347
1341
1348
local data = pdata [name ]
1342
1349
local player = get_player_by_name (name )
0 commit comments