mirror of
https://github.com/minetest-mods/craftguide.git
synced 2024-11-26 04:23:44 +01:00
Code clean-up
This commit is contained in:
parent
ee3984da77
commit
86a96c31ad
@ -4,4 +4,5 @@ allow_defined_top = true
|
||||
read_globals = {
|
||||
"minetest",
|
||||
"default",
|
||||
"sfinv_buttons",
|
||||
}
|
||||
|
77
init.lua
77
init.lua
@ -63,6 +63,7 @@ function craftguide:get_tooltip(item, recipe_type, cooktime, groups)
|
||||
else
|
||||
return tooltip .. "Unknown Item (" .. item .. ")]"
|
||||
end
|
||||
|
||||
if groups then
|
||||
local groupstr = "Any item belonging to the "
|
||||
for i=1, #groups do
|
||||
@ -71,13 +72,13 @@ function craftguide:get_tooltip(item, recipe_type, cooktime, groups)
|
||||
end
|
||||
tooltip = tooltip .. groupstr .. " group(s)"
|
||||
end
|
||||
|
||||
if recipe_type == "cooking" then
|
||||
tooltip = tooltip..item_desc.."\nCooking time: "..
|
||||
colorize(cooktime)
|
||||
tooltip = tooltip .. item_desc .. "\nCooking time: " .. colorize(cooktime)
|
||||
end
|
||||
|
||||
if fueltime > 0 then
|
||||
tooltip = tooltip..item_desc.."\nBurning time: "..
|
||||
colorize(fueltime)
|
||||
tooltip = tooltip .. item_desc .. "\nBurning time: " .. colorize(fueltime)
|
||||
end
|
||||
|
||||
return has_extras and tooltip .. "]" or ""
|
||||
@ -91,19 +92,16 @@ function craftguide:get_recipe(iY, xoffset, tooltip, item, recipe_num, recipes)
|
||||
"label[0," .. (iY + 2) .. ".5;Recipe " ..
|
||||
recipe_num .. " of " .. recipes_total .. "]"
|
||||
end
|
||||
|
||||
local recipe_type = recipes[recipe_num].type
|
||||
local items = recipes[recipe_num].items
|
||||
local width = recipes[recipe_num].width
|
||||
|
||||
if recipe_type == "cooking" then
|
||||
if recipe_type == "cooking" or (recipe_type == "normal" and width == 0) then
|
||||
local icon = recipe_type == "cooking" and "furnace" or "shapeless"
|
||||
formspec = formspec ..
|
||||
"image[" .. (xoffset - 0.8) .. "," .. (iY + 1) ..
|
||||
".5;0.5,0.5;craftguide_furnace.png]"
|
||||
-- Shapeless recipe
|
||||
elseif recipe_type == "normal" and width == 0 then
|
||||
formspec = formspec..
|
||||
"image["..(xoffset-0.8)..","..(iY+1)..
|
||||
".5;0.5,0.5;craftguide_shapeless.png]"
|
||||
".5;0.5,0.5;craftguide_" .. icon .. ".png]"
|
||||
end
|
||||
|
||||
if width == 0 then width = min(3, #items) end
|
||||
@ -119,21 +117,18 @@ function craftguide:get_recipe(iY, xoffset, tooltip, item, recipe_num, recipes)
|
||||
else
|
||||
for i, v in pairs(items) do
|
||||
local X = (i - 1) % width + xoffset
|
||||
local Y = ceil(i / width + iY+2 - min(2, rows))
|
||||
local Y = ceil(i / width + (iY + 2) - min(2, rows))
|
||||
|
||||
if recipe_type == "normal" and
|
||||
width > 3 or rows > 3 then
|
||||
if recipe_type == "normal" and width > 3 or rows > 3 then
|
||||
btn_size = width > 3 and 3 / width or 3 / rows
|
||||
X = btn_size * (i % width) + xoffset
|
||||
Y = btn_size * floor((i-1) / width) + iY+3 -
|
||||
min(2, rows)
|
||||
Y = btn_size * floor((i - 1) / width) + (iY + 3) - min(2, rows)
|
||||
end
|
||||
|
||||
local groups = extract_groups(v)
|
||||
local label = groups and "\nG" or ""
|
||||
local item_r = self:group_to_item(v)
|
||||
local tltip = self:get_tooltip(
|
||||
item_r, recipe_type, width, groups)
|
||||
local tltip = self:get_tooltip(item_r, recipe_type, width, groups)
|
||||
|
||||
formspec = formspec ..
|
||||
"item_image_button[" .. X .. "," .. Y .. ";" ..
|
||||
@ -183,8 +178,7 @@ function craftguide:get_formspec(player_name, is_fuel)
|
||||
|
||||
if not next(data.items) then
|
||||
formspec = formspec ..
|
||||
"label["..(xoffset - (even_num and 1.5 or 1))..
|
||||
",2;No item to show]"
|
||||
"label[" .. (xoffset - (even_num and 1.5 or 1)) .. ",2;No item to show]"
|
||||
end
|
||||
|
||||
local first_item = (data.pagenum - 1) * ipp
|
||||
@ -201,8 +195,7 @@ function craftguide:get_formspec(player_name, is_fuel)
|
||||
|
||||
if data.item and reg_items[data.item] then
|
||||
local tooltip = self:get_tooltip(data.item)
|
||||
if not data.recipes_item or (is_fuel and not
|
||||
get_recipe(data.item).items) then
|
||||
if not data.recipes_item or (is_fuel and not get_recipe(data.item).items) then
|
||||
formspec = formspec ..
|
||||
"image[" .. (xoffset - 1) .. "," .. (iY + 2) ..
|
||||
".12;0.9,0.7;craftguide_arrow.png]" ..
|
||||
@ -211,8 +204,8 @@ function craftguide:get_formspec(player_name, is_fuel)
|
||||
tooltip .. "image[" .. (xoffset - 2) .. "," ..
|
||||
(iY + 1.98) .. ";1,1;craftguide_fire.png]"
|
||||
else
|
||||
formspec = formspec..self:get_recipe(
|
||||
iY, xoffset, tooltip, data.item,
|
||||
formspec = formspec ..
|
||||
self:get_recipe(iY, xoffset, tooltip, data.item,
|
||||
data.recipe_num, data.recipes_item)
|
||||
end
|
||||
end
|
||||
@ -223,7 +216,9 @@ end
|
||||
|
||||
local function player_has_item(T)
|
||||
for i = 1, #T do
|
||||
if T[i] then return true end
|
||||
if T[i] then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -235,6 +230,7 @@ local function group_to_items(group)
|
||||
items_with_group[counter] = name
|
||||
end
|
||||
end
|
||||
|
||||
return items_with_group
|
||||
end
|
||||
|
||||
@ -263,6 +259,7 @@ function craftguide:recipe_in_inv(inv, item_name, recipes_f)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for i = #show_item_recipes, 1, -1 do
|
||||
if not show_item_recipes[i] then
|
||||
remove(recipes, i)
|
||||
@ -291,8 +288,7 @@ end
|
||||
|
||||
function craftguide:get_filter_items(data, player)
|
||||
local filter = data.filter
|
||||
local items_list = progressive_mode and data.init_filter_items or
|
||||
datas.init_items
|
||||
local items_list = progressive_mode and data.init_filter_items or datas.init_items
|
||||
local inv = player:get_inventory()
|
||||
local filtered_list, counter = {}, 0
|
||||
|
||||
@ -301,8 +297,7 @@ function craftguide:get_filter_items(data, player)
|
||||
local item_desc = reg_items[item].description:lower()
|
||||
|
||||
if filter ~= "" then
|
||||
if item:find(filter, 1, true) or
|
||||
item_desc:find(filter, 1, true) then
|
||||
if item:find(filter, 1, true) or item_desc:find(filter, 1, true) then
|
||||
counter = counter + 1
|
||||
filtered_list[counter] = item
|
||||
end
|
||||
@ -327,21 +322,22 @@ mt.register_on_player_receive_fields(function(player, formname, fields)
|
||||
local data = datas[player_name]
|
||||
|
||||
if fields.clear then
|
||||
data.filter, data.item, data.pagenum, data.recipe_num =
|
||||
"", nil, 1, 1
|
||||
data.items = progressive_mode and data.init_filter_items or
|
||||
datas.init_items
|
||||
data.filter, data.item, data.pagenum, data.recipe_num = "", nil, 1, 1
|
||||
data.items = progressive_mode and data.init_filter_items or datas.init_items
|
||||
craftguide:get_formspec(player_name)
|
||||
|
||||
elseif fields.alternate then
|
||||
local recipe = data.recipes_item[data.recipe_num + 1]
|
||||
data.recipe_num = recipe and data.recipe_num + 1 or 1
|
||||
craftguide:get_formspec(player_name)
|
||||
|
||||
elseif (fields.key_enter_field == "filter" or fields.search) and
|
||||
fields.filter ~= "" then
|
||||
data.filter = fields.filter:lower()
|
||||
data.pagenum = 1
|
||||
craftguide:get_filter_items(data, player)
|
||||
craftguide:get_formspec(player_name)
|
||||
|
||||
elseif fields.prev or fields.next then
|
||||
data.pagenum = data.pagenum - (fields.prev and 1 or -1)
|
||||
if data.pagenum > data.pagemax then
|
||||
@ -350,11 +346,13 @@ mt.register_on_player_receive_fields(function(player, formname, fields)
|
||||
data.pagenum = data.pagemax
|
||||
end
|
||||
craftguide:get_formspec(player_name)
|
||||
|
||||
elseif (fields.size_inc and data.iX < 12) or
|
||||
(fields.size_dec and data.iX > 8) then
|
||||
data.pagenum = 1
|
||||
data.iX = data.iX - (fields.size_dec and 1 or -1)
|
||||
craftguide:get_formspec(player_name)
|
||||
|
||||
else for item in pairs(fields) do
|
||||
if item:find(":") then
|
||||
if item:sub(-4) == "_inv" then
|
||||
@ -366,22 +364,18 @@ mt.register_on_player_receive_fields(function(player, formname, fields)
|
||||
if not recipes and not is_fuel then return end
|
||||
|
||||
if item == data.item then
|
||||
-- Cycle through alternatives when clicking same item again
|
||||
if data.recipes_item and #data.recipes_item >= 2 then
|
||||
local recipe = data.recipes_item[data.recipe_num + 1]
|
||||
data.recipe_num = recipe and data.recipe_num + 1 or 1
|
||||
craftguide:get_formspec(player_name)
|
||||
end
|
||||
else
|
||||
|
||||
if progressive_mode then
|
||||
local inv = player:get_inventory()
|
||||
local _, has_item =
|
||||
craftguide:recipe_in_inv(inv, item)
|
||||
local _, has_item = craftguide:recipe_in_inv(inv, item)
|
||||
|
||||
if not has_item then return end
|
||||
recipes = craftguide:recipe_in_inv(
|
||||
inv, item, recipes)
|
||||
recipes = craftguide:recipe_in_inv(inv, item, recipes)
|
||||
end
|
||||
|
||||
data.item = item
|
||||
@ -405,8 +399,7 @@ function craftguide:on_use(itemstack, user)
|
||||
if progressive_mode or not data then
|
||||
datas[player_name] = {filter = "", pagenum = 1, iX = 9}
|
||||
if progressive_mode then
|
||||
craftguide:get_filter_items(
|
||||
datas[player_name], user)
|
||||
craftguide:get_filter_items(datas[player_name], user)
|
||||
end
|
||||
craftguide:get_formspec(player_name)
|
||||
else
|
||||
@ -474,7 +467,7 @@ mt.register_craft({
|
||||
burntime = 10
|
||||
})
|
||||
|
||||
if mt.get_modpath("sfinv_buttons") then
|
||||
if rawget(_G, "sfinv_buttons") then
|
||||
sfinv_buttons.register_button("craftguide", {
|
||||
title = "Crafting guide",
|
||||
tooltip = "Shows a list of available crafting recipes, cooking recipes and fuels",
|
||||
|
Loading…
Reference in New Issue
Block a user