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

Commit db626ea

Browse files
committed
Small cleaning
1 parent 3b3da66 commit db626ea

File tree

1 file changed

+38
-46
lines changed

1 file changed

+38
-46
lines changed

init.lua

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,6 @@ function craftguide.register_craft_type(name, def)
155155
craft_types[name] = def
156156
end
157157

158-
local function get_width(recipe)
159-
if is_table(recipe) then
160-
sort(recipe, function(a, b)
161-
return #a > #b
162-
end)
163-
164-
return #recipe[1]
165-
end
166-
167-
return 0
168-
end
169-
170158
local function clean_name(item)
171159
return match(item, "%S+")
172160
end
@@ -198,7 +186,11 @@ function craftguide.register_craft(def)
198186
end
199187

200188
local cp = copy(def.grid)
201-
def.width = get_width(cp)
189+
sort(cp, function(a, b)
190+
return #a > #b
191+
end)
192+
193+
def.width = #cp[1]
202194

203195
for i = 1, #def.grid do
204196
while #def.grid[i] < def.width do
@@ -294,6 +286,7 @@ local function groups_item_in_recipe(item, recipe)
294286
for _, recipe_item in pairs(recipe.items) do
295287
if is_group(recipe_item) then
296288
local groups = extract_groups(recipe_item)
289+
297290
if item_has_groups(item_groups, groups) then
298291
local usage = copy(recipe)
299292
table_replace(usage.items, recipe_item, item)
@@ -338,6 +331,7 @@ local function get_usages(item)
338331
for _, recipes in pairs(recipes_cache) do
339332
for i = 1, #recipes do
340333
local recipe = recipes[i]
334+
341335
if item_in_recipe(item, recipe) then
342336
c = c + 1
343337
usages[c] = recipe
@@ -424,18 +418,7 @@ local function repairable(tool)
424418
end
425419

426420
local function get_tooltip(item, info)
427-
local function get_desc(def, name)
428-
name = name or item
429-
return def and def.description or
430-
(def and match(name, ":.*"):gsub("%W%l", upper):sub(2):gsub("_", " ") or
431-
S("Unknown Item (@1)", name))
432-
end
433-
434-
local tooltip = get_desc(reg_items[item])
435-
436-
local function add(str)
437-
return tooltip .. "\n" .. str
438-
end
421+
local tooltip
439422

440423
if info.groups then
441424
local groupstr, c = {}, 0
@@ -447,6 +430,21 @@ local function get_tooltip(item, info)
447430

448431
groupstr = concat(groupstr, ", ")
449432
tooltip = S("Any item belonging to the group(s): @1", groupstr)
433+
434+
return fmt("tooltip[%s;%s]", item, ESC(tooltip))
435+
end
436+
437+
local function get_desc(def, name)
438+
name = name or item
439+
return def and def.description or
440+
(def and match(name, ":.*"):gsub("%W%l", upper):sub(2):gsub("_", " ") or
441+
S("Unknown Item (@1)", name))
442+
end
443+
444+
tooltip = get_desc(reg_items[item])
445+
446+
local function add(str)
447+
return tooltip .. "\n" .. str
450448
end
451449

452450
if info.cooktime then
@@ -614,7 +612,7 @@ local function get_recipe_fs(data, fs)
614612
for j = 1, #replacements do
615613
local replacement = replacements[j]
616614
if replacement[1] == item then
617-
label = (label ~= "" and "\n" or "") .. label .. "\nR"
615+
label = label .. "\nR"
618616
replace = replacement[2]
619617
end
620618
end
@@ -626,25 +624,20 @@ local function get_recipe_fs(data, fs)
626624

627625
local burntime = fuel_cache[item] and fuel_cache[item].burntime
628626

629-
local more_info
630-
local infos = {
627+
local info = {
631628
groups = groups,
632629
burntime = burntime,
633630
cooktime = cooktime,
634631
replace = replace,
635632
repair = nil,
636633
}
637634

638-
for _, v in pairs(infos) do
635+
for _, v in pairs(info) do
639636
if v then
640-
more_info = true
637+
fs[#fs + 1] = get_tooltip(item, info)
641638
break
642639
end
643640
end
644-
645-
if more_info then
646-
fs[#fs + 1] = get_tooltip(item, infos)
647-
end
648641
end
649642

650643
get_output_fs(fs, {
@@ -836,14 +829,14 @@ end
836829

837830
-- As `core.get_craft_recipe` and `core.get_all_craft_recipes` do not return the replacements,
838831
-- we have to override `core.register_craft` and `core.register_alias` and do some reverse engineering.
839-
-- See engine's issue #4901.
832+
-- See engine's issues #4901 and #8920.
840833

841834
local old_register_alias = core.register_alias
842835
local current_alias = {}
843836

844837
core.register_alias = function(old, new)
845-
current_alias = {old, new}
846838
old_register_alias(old, new)
839+
current_alias = {old, new}
847840
end
848841

849842
local old_register_craft = core.register_craft
@@ -889,7 +882,7 @@ core.register_craft = function(recipe)
889882
recipe.items[#recipe.items + 1] = recipe.recipe[j]
890883
end
891884
else
892-
recipe.width = get_width(recipe.recipe)
885+
recipe.width = #recipe.recipe[1]
893886
local c = 1
894887

895888
for j = 1, #recipe.recipe do
@@ -944,10 +937,9 @@ on_joinplayer(function(player)
944937
init_data(name)
945938
end)
946939

947-
local function _fields(player, fields)
940+
local function fields(player, _f)
948941
local name = player:get_player_name()
949942
local data = pdata[name]
950-
local _f = fields
951943

952944
if _f.clear then
953945
reset_data(data)
@@ -963,10 +955,10 @@ local function _fields(player, fields)
963955
return true
964956

965957
elseif (_f.key_enter_field == "filter" or _f.search) and _f.filter ~= "" then
966-
local fltr = lower(_f.filter)
967-
if data.filter == fltr then return end
958+
local str = lower(_f.filter)
959+
if data.filter == str then return end
968960

969-
data.filter = fltr
961+
data.filter = str
970962
data.pagenum = 1
971963
search(data)
972964

@@ -1039,14 +1031,14 @@ if sfinv_only then
10391031
end
10401032
end,
10411033

1042-
on_player_receive_fields = function(self, player, context, fields)
1043-
_fields(player, fields)
1034+
on_player_receive_fields = function(self, player, context, _f)
1035+
fields(player, _f)
10441036
end,
10451037
})
10461038
else
1047-
on_receive_fields(function(player, formname, fields)
1039+
on_receive_fields(function(player, formname, _f)
10481040
if formname == "craftguide" then
1049-
_fields(player, fields)
1041+
fields(player, _f)
10501042
end
10511043
end)
10521044

0 commit comments

Comments
 (0)