Skip to content

Commit efa5f1b

Browse files
committed
send player a message when group was used
1 parent 4977325 commit efa5f1b

File tree

1 file changed

+48
-29
lines changed

1 file changed

+48
-29
lines changed

craft_guide/api_craft_guide.lua

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -219,99 +219,95 @@ craft_guide.update_recipe = function(meta, player, stack, alternate)
219219
--craft_guide.log(player:get_player_name().." requests recipe "..alternate.." for "..stack:get_name())
220220
local craft = crafts[alternate]
221221

222-
-- show me the unknown items
223-
--craft_guide.log(dump(craft))
224-
--minetest.chat_send_player(player:get_player_name(), "recipe for "..stack:get_name()..": "..dump(craft))
225-
226222
local itemstack = ItemStack(craft.output)
227223
inv:set_stack("output", 1, itemstack)
228224

229225
-- cook
230226
if craft.type == "cooking" then
231-
inv:set_stack("cook", 1, craft_guide.get_item_name(craft.recipe))
227+
inv:set_stack("cook", 1, craft_guide.get_item_name(craft.recipe, player))
232228
meta:set_string("formspec",craft_guide.get_craft_guide_formspec(meta))
233229
return
234230
end
235231
-- fuel
236232
if craft.type == "fuel" then
237-
inv:set_stack("fuel", 1, craft_guide.get_item_name(craft.recipe))
233+
inv:set_stack("fuel", 1, craft_guide.get_item_name(craft.recipe, player))
238234
meta:set_string("formspec",craft_guide.get_craft_guide_formspec(meta))
239235
return
240236
end
241237
-- build (shaped or shapeless)
242238
if craft.recipe[1] then
243239
if (type(craft.recipe[1]) == "string") then
244-
inv:set_stack("build", 1, craft_guide.get_item_name(craft.recipe[1]))
240+
inv:set_stack("build", 1, craft_guide.get_item_name(craft.recipe[1], player))
245241
else
246242
if craft.recipe[1][1] then
247-
inv:set_stack("build", 1, craft_guide.get_item_name(craft.recipe[1][1]))
243+
inv:set_stack("build", 1, craft_guide.get_item_name(craft.recipe[1][1], player))
248244
end
249245
if craft.recipe[1][2] then
250-
inv:set_stack("build", 2, craft_guide.get_item_name(craft.recipe[1][2]))
246+
inv:set_stack("build", 2, craft_guide.get_item_name(craft.recipe[1][2], player))
251247
end
252248
if craft.recipe[1][3] then
253-
inv:set_stack("build", 3, craft_guide.get_item_name(craft.recipe[1][3]))
249+
inv:set_stack("build", 3, craft_guide.get_item_name(craft.recipe[1][3], player))
254250
end
255251
end
256252
end
257253
if craft.recipe[2] then
258254
if (type(craft.recipe[2]) == "string") then
259-
inv:set_stack("build", 2, craft_guide.get_item_name(craft.recipe[2]))
255+
inv:set_stack("build", 2, craft_guide.get_item_name(craft.recipe[2], player))
260256
else
261257
if craft.recipe[2][1] then
262-
inv:set_stack("build", 4, craft_guide.get_item_name(craft.recipe[2][1]))
258+
inv:set_stack("build", 4, craft_guide.get_item_name(craft.recipe[2][1], player))
263259
end
264260
if craft.recipe[2][2] then
265-
inv:set_stack("build", 5, craft_guide.get_item_name(craft.recipe[2][2]))
261+
inv:set_stack("build", 5, craft_guide.get_item_name(craft.recipe[2][2], player))
266262
end
267263
if craft.recipe[2][3] then
268-
inv:set_stack("build", 6, craft_guide.get_item_name(craft.recipe[2][3]))
264+
inv:set_stack("build", 6, craft_guide.get_item_name(craft.recipe[2][3], player))
269265
end
270266
end
271267
end
272268
if craft.recipe[3] then
273269
if (type(craft.recipe[3]) == "string") then
274-
inv:set_stack("build", 3, craft_guide.get_item_name(craft.recipe[3]))
270+
inv:set_stack("build", 3, craft_guide.get_item_name(craft.recipe[3], player))
275271
else
276272
if craft.recipe[3][1] then
277-
inv:set_stack("build", 7, craft_guide.get_item_name(craft.recipe[3][1]))
273+
inv:set_stack("build", 7, craft_guide.get_item_name(craft.recipe[3][1], player))
278274
end
279275
if craft.recipe[3][2] then
280-
inv:set_stack("build", 8, craft_guide.get_item_name(craft.recipe[3][2]))
276+
inv:set_stack("build", 8, craft_guide.get_item_name(craft.recipe[3][2], player))
281277
end
282278
if craft.recipe[3][3] then
283-
inv:set_stack("build", 9, craft_guide.get_item_name(craft.recipe[3][3]))
279+
inv:set_stack("build", 9, craft_guide.get_item_name(craft.recipe[3][3], player))
284280
end
285281
end
286282
end
287283
if craft.recipe[4] then
288284
if (type(craft.recipe[4]) == "string") then
289-
inv:set_stack("build", 4, craft_guide.get_item_name(craft.recipe[4]))
285+
inv:set_stack("build", 4, craft_guide.get_item_name(craft.recipe[4], player))
290286
end
291287
end
292288
if craft.recipe[5] then
293289
if (type(craft.recipe[5]) == "string") then
294-
inv:set_stack("build", 5, craft_guide.get_item_name(craft.recipe[5]))
290+
inv:set_stack("build", 5, craft_guide.get_item_name(craft.recipe[5], player))
295291
end
296292
end
297293
if craft.recipe[6] then
298294
if (type(craft.recipe[6]) == "string") then
299-
inv:set_stack("build", 6, craft_guide.get_item_name(craft.recipe[6]))
295+
inv:set_stack("build", 6, craft_guide.get_item_name(craft.recipe[6], player))
300296
end
301297
end
302298
if craft.recipe[7] then
303299
if (type(craft.recipe[7]) == "string") then
304-
inv:set_stack("build", 7, craft_guide.get_item_name(craft.recipe[7]))
300+
inv:set_stack("build", 7, craft_guide.get_item_name(craft.recipe[7], player))
305301
end
306302
end
307303
if craft.recipe[8] then
308304
if (type(craft.recipe[8]) == "string") then
309-
inv:set_stack("build", 8, craft_guide.get_item_name(craft.recipe[8]))
305+
inv:set_stack("build", 8, craft_guide.get_item_name(craft.recipe[8], player))
310306
end
311307
end
312308
if craft.recipe[9] then
313309
if (type(craft.recipe[9]) == "string") then
314-
inv:set_stack("build", 9, craft_guide.get_item_name(craft.recipe[9]))
310+
inv:set_stack("build", 9, craft_guide.get_item_name(craft.recipe[9], player))
315311
end
316312
end
317313
meta:set_string("formspec",craft_guide.get_craft_guide_formspec(meta))
@@ -391,13 +387,21 @@ end
391387

392388

393389
-- get an item name, if it's a group then get an item in the group
394-
craft_guide.get_item_name = function(item_name)
390+
craft_guide.get_item_name = function(item_name, player)
395391
if string.find(item_name, "group:") then
396-
if #craft_guide.groups == 0 then
392+
local group_name = item_name
393+
if craft_guide.table_count(craft_guide.groups) == 0 then
397394
craft_guide.load_item_groups()
398395
end
399-
if craft_guide.groups[string.sub(item_name, 7)] ~= nil then
400-
item_name = craft_guide.groups[string.sub(item_name, 7)].item_name
396+
if craft_guide.table_count(craft_guide.groups) > 0 then
397+
local other_items = {}
398+
if craft_guide.groups[string.sub(group_name, 7)] ~= nil then
399+
item_name = craft_guide.groups[string.sub(group_name, 7)].item_name
400+
other_items = craft_guide.groups[string.sub(group_name, 7)].other_items
401+
if #other_items > 0 then
402+
minetest.chat_send_player(player:get_player_name(), "Item "..item_name.." used for "..group_name..". You can also use "..table.concat(other_items, ", ")..".")
403+
end
404+
end
401405
end
402406
end
403407
return item_name
@@ -408,12 +412,27 @@ end
408412
craft_guide.load_item_groups = function()
409413
for name,def in pairs(minetest.registered_items) do
410414
if name ~= nil and def ~= nil and dump(name) ~= "\"\"" and dump(def) ~= "\"\"" then
415+
local i = 1
411416
for group,_ in pairs(def.groups) do
412417
if craft_guide.groups[group] == nil then
413418
craft_guide.groups[group] = {}
419+
craft_guide.groups[group].other_items = {}
414420
craft_guide.groups[group].item_name = name
421+
else
422+
craft_guide.groups[group].other_items[i] = name
423+
i = i+1
415424
end
416425
end
417426
end
418427
end
419-
end
428+
end
429+
430+
431+
-- count items in a table
432+
craft_guide.table_count = function(count_table)
433+
local count = 0;
434+
for k,v in pairs(count_table) do
435+
count = count + 1
436+
end
437+
return count
438+
end

0 commit comments

Comments
 (0)