mirror of
https://github.com/minetest-mods/craftguide.git
synced 2025-06-28 22:26:28 +02:00
Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
d1ee125a1a | |||
b980dccaee | |||
4c02a5e5f5 | |||
f8b7a72635 | |||
1034fd7c0e | |||
4bca02f0db | |||
4b97b08aa7 | |||
7745ccd863 | |||
65b51ad3e5 | |||
897fc3a21d | |||
e1eedb69bb | |||
f188580b8c | |||
783a84d3c6 |
@ -20,9 +20,8 @@ Use the command `/craft` to show the recipe(s) of the pointed node.
|
||||
#### Registering a custom crafting type ####
|
||||
```Lua
|
||||
craftguide.register_craft_type("digging", {
|
||||
description = S("Digging"),
|
||||
icon = "default_tool_steelpick.png",
|
||||
width = 1,
|
||||
description = "Digging",
|
||||
icon = "default_tool_steelpick.png",
|
||||
})
|
||||
```
|
||||
|
||||
@ -30,6 +29,7 @@ craftguide.register_craft_type("digging", {
|
||||
```Lua
|
||||
craftguide.register_craft({
|
||||
type = "digging",
|
||||
width = 1,
|
||||
output = "default:cobble 2",
|
||||
items = {"default:stone"},
|
||||
})
|
||||
|
455
init.lua
455
init.lua
@ -21,9 +21,10 @@ craftguide.intllib = S
|
||||
|
||||
-- Lua 5.3 removed `table.maxn`, use this alternative in case of breakage:
|
||||
-- https://github.com/kilbith/xdecor/blob/master/handlers/helpers.lua#L1
|
||||
local remove, maxn, sort = table.remove, table.maxn, table.sort
|
||||
local remove, maxn, sort, concat = table.remove, table.maxn, table.sort, table.concat
|
||||
local vector_add, vector_mul = vector.add, vector.multiply
|
||||
local min, max, floor, ceil = math.min, math.max, math.floor, math.ceil
|
||||
local fmt = string.format
|
||||
|
||||
local DEFAULT_SIZE = 10
|
||||
local MIN_LIMIT, MAX_LIMIT = 10, 12
|
||||
@ -43,11 +44,23 @@ local group_stereotypes = {
|
||||
}
|
||||
|
||||
local function extract_groups(str)
|
||||
if str:sub(1,6) ~= "group:" then return end
|
||||
if str:sub(1,6) ~= "group:" then
|
||||
return
|
||||
end
|
||||
|
||||
return str:sub(7):split(",")
|
||||
end
|
||||
|
||||
local function __func()
|
||||
return debug.getinfo(2, "n").name
|
||||
end
|
||||
|
||||
function craftguide.register_craft_type(name, def)
|
||||
local func = "craftguide." .. __func() .. "(): "
|
||||
assert(name, func .. "'name' field missing")
|
||||
assert(def.description, func .. "'description' field missing")
|
||||
assert(def.icon, func .. "'icon' field missing")
|
||||
|
||||
if not craftguide.craft_types[name] then
|
||||
craftguide.craft_types[name] = def
|
||||
end
|
||||
@ -55,22 +68,34 @@ end
|
||||
|
||||
craftguide.register_craft_type("digging", {
|
||||
description = S("Digging"),
|
||||
icon = "default_tool_steelpick.png",
|
||||
width = 1,
|
||||
icon = "default_tool_steelpick.png",
|
||||
})
|
||||
|
||||
function craftguide.register_craft(def)
|
||||
local func = "craftguide." .. __func() .. "(): "
|
||||
assert(def.type, func .. "'type' field missing")
|
||||
assert(def.width, func .. "'width' field missing")
|
||||
assert(def.output, func .. "'output' field missing")
|
||||
assert(def.items, func .. "'items' field missing")
|
||||
|
||||
craftguide.custom_crafts[#craftguide.custom_crafts + 1] = def
|
||||
end
|
||||
|
||||
craftguide.register_craft({
|
||||
type = "digging",
|
||||
width = 1,
|
||||
output = "default:cobble",
|
||||
items = {"default:stone"},
|
||||
})
|
||||
|
||||
local function colorize(str)
|
||||
return mt.colorize("#FFFF00", str)
|
||||
local color_codes = {
|
||||
red = "#FF0000",
|
||||
yellow = "#FFFF00",
|
||||
}
|
||||
|
||||
local function colorize(str, color)
|
||||
color = color or "yellow"
|
||||
return mt.colorize(color_codes[color], str)
|
||||
end
|
||||
|
||||
local function get_fueltime(item)
|
||||
@ -80,10 +105,10 @@ end
|
||||
local function reset_datas(data)
|
||||
data.show_usage = nil
|
||||
data.filter = ""
|
||||
data.item = nil
|
||||
data.input = nil
|
||||
data.pagenum = 1
|
||||
data.rnum = 1
|
||||
data.fuel = nil
|
||||
data.items = progressive_mode and data.init_filter_items or datas.init_items
|
||||
end
|
||||
|
||||
local function in_table(T)
|
||||
@ -166,16 +191,24 @@ local function get_tooltip(item, recipe_type, cooktime, groups)
|
||||
return has_extras and tooltip .. "]" or ""
|
||||
end
|
||||
|
||||
local function _get_recipe(iX, iY, xoffset, recipe_num, recipes, show_usage)
|
||||
local formspec, recipes_total = "", #recipes
|
||||
if recipes_total > 1 then
|
||||
formspec = formspec ..
|
||||
"button[" .. (iX - (sfinv_only and 2.2 or 2.6)) .. "," ..
|
||||
(iY + (sfinv_only and 3.9 or 3.3)) .. ";2.2,1;alternate;" ..
|
||||
(show_usage and S("Usage") or S("Recipe")) .. " " ..
|
||||
S("@1 of @2", recipe_num, recipes_total) .. "]"
|
||||
local function get_recipe_fs(iX, iY, xoffset, recipe_num, recipes, show_usage)
|
||||
if not recipes[1] then
|
||||
return ""
|
||||
end
|
||||
|
||||
local fs = {}
|
||||
|
||||
fs[#fs + 1] = fmt("button[%f,%f;%f,%f;%s;%s %u %s %u]",
|
||||
iX - (sfinv_only and 2.2 or 2.6),
|
||||
iY + (sfinv_only and 3.9 or 3.3),
|
||||
2.2,
|
||||
1,
|
||||
"alternate",
|
||||
show_usage and S("Usage") or S("Recipe"),
|
||||
recipe_num,
|
||||
S("of"),
|
||||
#recipes)
|
||||
|
||||
local recipe_type = recipes[recipe_num].type
|
||||
local items = recipes[recipe_num].items
|
||||
local width = recipes[recipe_num].width
|
||||
@ -190,18 +223,20 @@ local function _get_recipe(iX, iY, xoffset, recipe_num, recipes, show_usage)
|
||||
local rows = ceil(maxn(items) / width)
|
||||
local rightest, s_btn_size = 0
|
||||
|
||||
if recipe_type ~= "fuel" and (width > GRID_LIMIT or rows > GRID_LIMIT) then
|
||||
formspec = formspec ..
|
||||
"label[" .. ((iX / 2) - 2) .. "," .. (iY + 2.2) .. ";" ..
|
||||
S("Recipe is too big to be displayed (@1x@2)", width, rows) .. "]"
|
||||
return formspec
|
||||
if recipe_type ~= "cooking" and (width > GRID_LIMIT or rows > GRID_LIMIT) then
|
||||
fs[#fs + 1] = fmt("label[%f,%f;%s]",
|
||||
(iX / 2) - 2,
|
||||
iY + 2.2,
|
||||
S("Recipe is too big to be displayed (@1x@2)", width, rows))
|
||||
|
||||
return concat(fs)
|
||||
else
|
||||
for i, v in pairs(items) do
|
||||
local X = ceil((i - 1) % width + xoffset - width) -
|
||||
(sfinv_only and 0 or 0.2)
|
||||
local Y = ceil(i / width + (iY + 2) - min(2, rows))
|
||||
|
||||
if recipe_type ~= "fuel" and (width > 3 or rows > 3) then
|
||||
if recipe_type ~= "cooking" and (width > 3 or rows > 3) then
|
||||
BUTTON_SIZE = width > 3 and 3 / width or 3 / rows
|
||||
s_btn_size = BUTTON_SIZE
|
||||
X = BUTTON_SIZE * (i % width) + xoffset - 2.65
|
||||
@ -215,66 +250,96 @@ local function _get_recipe(iX, iY, xoffset, recipe_num, recipes, show_usage)
|
||||
local groups = extract_groups(v)
|
||||
local label = groups and "\nG" or ""
|
||||
local item_r = group_to_item(v)
|
||||
local tltip = get_tooltip(item_r, recipe_type, cooktime, groups)
|
||||
local tltp = get_tooltip(item_r, recipe_type, cooktime, groups)
|
||||
|
||||
formspec = formspec ..
|
||||
"item_image_button[" .. X .. "," ..
|
||||
(Y + (sfinv_only and 0.7 or 0.2)) .. ";" ..
|
||||
BUTTON_SIZE .. "," .. BUTTON_SIZE .. ";" .. item_r ..
|
||||
";" .. item_r:match("%S*") .. ";" .. label .. "]" .. tltip
|
||||
fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s;%s]",
|
||||
X,
|
||||
Y + (sfinv_only and 0.7 or 0.2),
|
||||
BUTTON_SIZE,
|
||||
BUTTON_SIZE,
|
||||
item_r,
|
||||
item_r:match("%S*"),
|
||||
label)
|
||||
|
||||
fs[#fs + 1] = tltp
|
||||
end
|
||||
|
||||
BUTTON_SIZE = 1.1
|
||||
end
|
||||
|
||||
local custom_recipe = craftguide.craft_types[recipe_type]
|
||||
if recipe_type == "cooking" or (recipe_type == "normal" and width == 0) or
|
||||
custom_recipe then
|
||||
local icon = recipe_type == "cooking" and "furnace" or "shapeless"
|
||||
local coords = (rightest + 1.2) .. "," ..
|
||||
(iY + (sfinv_only and 2.2 or 1.7)) ..
|
||||
";0.5,0.5;"
|
||||
if recipe_type == "cooking" or custom_recipe or
|
||||
(recipe_type == "normal" and width == 0) then
|
||||
|
||||
formspec = formspec ..
|
||||
"image[" .. coords ..
|
||||
(custom_recipe and custom_recipe.icon or
|
||||
"craftguide_" .. icon .. ".png^[resize:16x16") .. "]" ..
|
||||
"tooltip[" .. coords ..
|
||||
(custom_recipe and custom_recipe.description or
|
||||
recipe_type:gsub("^%l", string.upper)) .. "]"
|
||||
local icon = recipe_type == "cooking" and "furnace" or "shapeless"
|
||||
|
||||
fs[#fs + 1] = fmt("image[%f,%f;%f,%f;%s]",
|
||||
rightest + 1.2,
|
||||
iY + (sfinv_only and 2.2 or 1.7),
|
||||
0.5,
|
||||
0.5,
|
||||
custom_recipe and custom_recipe.icon or
|
||||
"craftguide_" .. icon .. ".png^[resize:16x16")
|
||||
|
||||
fs[#fs + 1] = fmt("tooltip[%f,%f;%f,%f;%s]",
|
||||
rightest + 1.2,
|
||||
iY + (sfinv_only and 2.2 or 1.7),
|
||||
0.5,
|
||||
0.5,
|
||||
custom_recipe and custom_recipe.description or
|
||||
recipe_type:gsub("^%l", string.upper))
|
||||
end
|
||||
|
||||
local output = recipes[recipe_num].output
|
||||
local output = recipes[recipe_num].output
|
||||
local output_s = output:match("%S+")
|
||||
local output_is_fuel = get_fueltime(output) > 0
|
||||
|
||||
local arrow_X = rightest + (s_btn_size or BUTTON_SIZE)
|
||||
local output_X = arrow_X + 0.9
|
||||
|
||||
formspec = formspec ..
|
||||
"image[" .. arrow_X .. "," ..
|
||||
(iY + (sfinv_only and 2.85 or 2.35)) ..
|
||||
";0.9,0.7;craftguide_arrow.png]" ..
|
||||
fs[#fs + 1] = fmt("image[%f,%f;%f,%f;%s]",
|
||||
arrow_X,
|
||||
iY + (sfinv_only and 2.85 or 2.35),
|
||||
0.9,
|
||||
0.7,
|
||||
"craftguide_arrow.png")
|
||||
|
||||
"item_image_button[" .. output_X .. "," ..
|
||||
(iY + (sfinv_only and 2.7 or 2.2)) .. ";" ..
|
||||
BUTTON_SIZE .. "," .. BUTTON_SIZE .. ";" ..
|
||||
output .. ";" .. output_s .. ";]" ..
|
||||
|
||||
get_tooltip(output_s)
|
||||
|
||||
if output_is_fuel then
|
||||
formspec = formspec ..
|
||||
"image[" .. (output_X + 1) .. "," ..
|
||||
(iY + (sfinv_only and 2.83 or 2.33)) ..
|
||||
";0.6,0.4;craftguide_arrow.png]" ..
|
||||
|
||||
"image[" .. (output_X + 1.6) .. "," ..
|
||||
(iY + (sfinv_only and 2.68 or 2.18)) ..
|
||||
";0.6,0.6;craftguide_fire.png]"
|
||||
if output == "BURN" then
|
||||
fs[#fs + 1] = fmt("image[%f,%f;%f,%f;%s]",
|
||||
output_X,
|
||||
iY + (sfinv_only and 2.68 or 2.18),
|
||||
1.1,
|
||||
1.1,
|
||||
"craftguide_fire.png")
|
||||
else
|
||||
fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s;]",
|
||||
output_X,
|
||||
iY + (sfinv_only and 2.7 or 2.2),
|
||||
BUTTON_SIZE,
|
||||
BUTTON_SIZE,
|
||||
output,
|
||||
output_s)
|
||||
end
|
||||
|
||||
return formspec
|
||||
fs[#fs + 1] = get_tooltip(output_s)
|
||||
|
||||
local output_is_fuel = get_fueltime(output) > 0
|
||||
if output_is_fuel then
|
||||
fs[#fs + 1] = fmt("image[%f,%f;%f,%f;%s]",
|
||||
output_X + 1,
|
||||
iY + (sfinv_only and 2.83 or 2.33),
|
||||
0.6,
|
||||
0.4,
|
||||
"craftguide_arrow.png")
|
||||
|
||||
fs[#fs + 1] = fmt("image[%f,%f;%f,%f;%s]",
|
||||
output_X + 1.6,
|
||||
iY + (sfinv_only and 2.68 or 2.18),
|
||||
0.6,
|
||||
0.6,
|
||||
"craftguide_fire.png")
|
||||
end
|
||||
|
||||
return concat(fs)
|
||||
end
|
||||
|
||||
local function get_formspec(player_name)
|
||||
@ -288,94 +353,82 @@ local function get_formspec(player_name)
|
||||
|
||||
data.pagemax = max(1, ceil(#data.items / ipp))
|
||||
|
||||
local formspec = ""
|
||||
local fs = {}
|
||||
if not sfinv_only then
|
||||
formspec = formspec ..
|
||||
"size[" .. (data.iX - 0.35) .. "," .. (iY + 4) .. ";]" ..
|
||||
"background[1,1;1,1;craftguide_bg.png;true]" ..
|
||||
"tooltip[size_inc;" .. S("Increase window size") .. "]" ..
|
||||
"tooltip[size_dec;" .. S("Decrease window size") .. "]" ..
|
||||
"image_button[" .. (data.iX * 0.47) ..
|
||||
",0.12;0.8,0.8;craftguide_zoomin_icon.png;size_inc;]" ..
|
||||
"image_button[" .. ((data.iX * 0.47) + 0.6) ..
|
||||
fs[#fs + 1] = "size[" .. (data.iX - 0.35) .. "," .. (iY + 4) .. ";]"
|
||||
fs[#fs + 1] = "tooltip[size_inc;" .. S("Increase window size") .. "]"
|
||||
fs[#fs + 1] = "tooltip[size_dec;" .. S("Decrease window size") .. "]"
|
||||
fs[#fs + 1] = "image_button[" .. (data.iX * 0.47) ..
|
||||
",0.12;0.8,0.8;craftguide_zoomin_icon.png;size_inc;]"
|
||||
fs[#fs + 1] = "image_button[" .. ((data.iX * 0.47) + 0.6) ..
|
||||
",0.12;0.8,0.8;craftguide_zoomout_icon.png;size_dec;]"
|
||||
end
|
||||
|
||||
formspec = formspec .. [[
|
||||
image_button[2.4,0.12;0.8,0.8;craftguide_search_icon.png;search;]
|
||||
image_button[3.05,0.12;0.8,0.8;craftguide_clear_icon.png;clear;]
|
||||
field_close_on_enter[filter;false]
|
||||
]] ..
|
||||
"tooltip[search;" .. S("Search") .. "]" ..
|
||||
"tooltip[clear;" .. S("Reset") .. "]" ..
|
||||
"tooltip[prev;" .. S("Previous page") .. "]" ..
|
||||
"tooltip[next;" .. S("Next page") .. "]" ..
|
||||
"image_button[" .. (data.iX - (sfinv_only and 2.6 or 3.1)) ..
|
||||
",0.12;0.8,0.8;craftguide_prev_icon.png;prev;]" ..
|
||||
"label[" .. (data.iX - (sfinv_only and 1.7 or 2.2)) .. ",0.22;" ..
|
||||
colorize(data.pagenum) .. " / " .. data.pagemax .. "]" ..
|
||||
"image_button[" .. (data.iX - (sfinv_only and 0.7 or 1.2) -
|
||||
(data.iX >= 11 and 0.08 or 0)) ..
|
||||
",0.12;0.8,0.8;craftguide_next_icon.png;next;]" ..
|
||||
"field[0.3,0.32;2.5,1;filter;;" .. mt.formspec_escape(data.filter) .. "]"
|
||||
fs[#fs + 1] = [[
|
||||
background[1,1;1,1;craftguide_bg.png;true]
|
||||
image_button[2.4,0.12;0.8,0.8;craftguide_search_icon.png;search;]
|
||||
image_button[3.05,0.12;0.8,0.8;craftguide_clear_icon.png;clear;]
|
||||
field_close_on_enter[filter;false]
|
||||
]]
|
||||
|
||||
fs[#fs + 1] = "tooltip[search;" .. S("Search") .. "]"
|
||||
fs[#fs + 1] = "tooltip[clear;" .. S("Reset") .. "]"
|
||||
fs[#fs + 1] = "tooltip[prev;" .. S("Previous page") .. "]"
|
||||
fs[#fs + 1] = "tooltip[next;" .. S("Next page") .. "]"
|
||||
fs[#fs + 1] = "image_button[" .. (data.iX - (sfinv_only and 2.6 or 3.1)) ..
|
||||
",0.12;0.8,0.8;craftguide_prev_icon.png;prev;]"
|
||||
fs[#fs + 1] = "label[" .. (data.iX - (sfinv_only and 1.7 or 2.2)) ..
|
||||
",0.22;" .. colorize(data.pagenum) .. " / " .. data.pagemax .. "]"
|
||||
fs[#fs + 1] = "image_button[" .. (data.iX - (sfinv_only and 0.7 or 1.2) -
|
||||
(data.iX >= 11 and 0.08 or 0)) ..
|
||||
",0.12;0.8,0.8;craftguide_next_icon.png;next;]"
|
||||
fs[#fs + 1] = "field[0.3,0.32;2.5,1;filter;;" .. mt.formspec_escape(data.filter) .. "]"
|
||||
|
||||
local xoffset = data.iX / 2.15
|
||||
|
||||
if not next(data.items) then
|
||||
formspec = formspec ..
|
||||
"label[" .. ((data.iX / 2) - 1) .. ",2;" .. S("No item to show") .. "]"
|
||||
fs[#fs + 1] = fmt("label[%f,%f;%s]",
|
||||
(data.iX / 2) - 1,
|
||||
2,
|
||||
S("No item to show"))
|
||||
end
|
||||
|
||||
local first_item = (data.pagenum - 1) * ipp
|
||||
for i = first_item, first_item + ipp - 1 do
|
||||
local name = data.items[i + 1]
|
||||
if not name then break end
|
||||
if not name then
|
||||
break
|
||||
end
|
||||
|
||||
local X = i % data.iX
|
||||
local Y = (i % ipp - X) / data.iX + 1
|
||||
|
||||
formspec = formspec ..
|
||||
"item_image_button[" ..
|
||||
(X - (sfinv_only and 0 or (X * 0.05))) .. "," ..
|
||||
Y .. ";" .. BUTTON_SIZE .. "," .. BUTTON_SIZE .. ";" ..
|
||||
name .. ";" .. name .. "_inv;]"
|
||||
fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s;]",
|
||||
X - (sfinv_only and 0 or (X * 0.05)),
|
||||
Y,
|
||||
BUTTON_SIZE,
|
||||
BUTTON_SIZE,
|
||||
name,
|
||||
name)
|
||||
end
|
||||
|
||||
if data.item and reg_items[data.item] then
|
||||
if not data.recipes_item or (data.fuel and not get_recipe(data.item).items) then
|
||||
local X = floor(xoffset) - (sfinv_only and 0 or 0.2)
|
||||
formspec = formspec ..
|
||||
"item_image_button[" .. X .. "," ..
|
||||
(iY + (sfinv_only and 2.7 or 2.2)) ..
|
||||
";" .. BUTTON_SIZE .. "," .. BUTTON_SIZE ..
|
||||
";" .. data.item .. ";" .. data.item .. ";]" ..
|
||||
|
||||
"image[" .. (X + 1.1) .. "," ..
|
||||
(iY + (sfinv_only and 2.85 or 2.35)) ..
|
||||
";0.9,0.7;craftguide_arrow.png]" ..
|
||||
|
||||
get_tooltip(data.item) ..
|
||||
|
||||
"image[" .. (X + 2.1) .. "," ..
|
||||
(iY + (sfinv_only and 2.68 or 2.18)) ..
|
||||
";1.1,1.1;craftguide_fire.png]"
|
||||
else
|
||||
local show_usage = data.show_usage
|
||||
formspec = formspec ..
|
||||
_get_recipe(data.iX,
|
||||
iY,
|
||||
xoffset,
|
||||
data.rnum,
|
||||
(show_usage and data.usages or data.recipes_item),
|
||||
show_usage)
|
||||
end
|
||||
if data.input and reg_items[data.input] then
|
||||
local usage = data.show_usage
|
||||
fs[#fs + 1] = get_recipe_fs(data.iX,
|
||||
iY,
|
||||
xoffset,
|
||||
data.rnum,
|
||||
(usage and data.usages or data.recipes_item),
|
||||
usage)
|
||||
end
|
||||
|
||||
data.formspec = formspec
|
||||
fs = concat(fs)
|
||||
data.formspec = fs
|
||||
|
||||
if sfinv_only then
|
||||
return formspec
|
||||
return fs
|
||||
else
|
||||
show_formspec(player_name, "craftguide", formspec)
|
||||
show_formspec(player_name, "craftguide", fs)
|
||||
end
|
||||
end
|
||||
|
||||
@ -404,6 +457,7 @@ local function recipe_in_inv(inv, item_name, recipes_f)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if not group_in_inv and not item_in_inv(inv, item) then
|
||||
show_item_recipes[i] = false
|
||||
end
|
||||
@ -476,8 +530,8 @@ local function add_custom_recipes(item, recipes)
|
||||
local craft = craftguide.custom_crafts[j]
|
||||
if craft.output:match("%S*") == item then
|
||||
recipes[#recipes + 1] = {
|
||||
width = craftguide.craft_types[craft.type].width,
|
||||
type = craft.type,
|
||||
width = craft.width,
|
||||
items = craft.items,
|
||||
output = craft.output,
|
||||
}
|
||||
@ -567,24 +621,30 @@ local function get_fields(player, ...)
|
||||
formname, fields = args[1], args[2]
|
||||
end
|
||||
|
||||
if not sfinv_only and formname ~= "craftguide" then return end
|
||||
if not sfinv_only and formname ~= "craftguide" then
|
||||
return
|
||||
end
|
||||
|
||||
local player_name = player:get_player_name()
|
||||
local data = datas[player_name]
|
||||
|
||||
if fields.clear then
|
||||
reset_datas(data)
|
||||
data.items = progressive_mode and data.init_filter_items or datas.init_items
|
||||
show_fs(player, player_name)
|
||||
|
||||
elseif fields.alternate then
|
||||
local num
|
||||
if data.show_usage then
|
||||
num = data.usages[data.rnum + 1]
|
||||
else
|
||||
num = data.recipes_item[data.rnum + 1]
|
||||
if #(data.show_usage and data.usages or data.recipes_item) == 1 then
|
||||
return
|
||||
end
|
||||
|
||||
data.rnum = num and data.rnum + 1 or 1
|
||||
local next_i
|
||||
if data.show_usage then
|
||||
next_i = data.usages[data.rnum + 1]
|
||||
else
|
||||
next_i = data.recipes_item[data.rnum + 1]
|
||||
end
|
||||
|
||||
data.rnum = next_i and data.rnum + 1 or 1
|
||||
show_fs(player, player_name)
|
||||
|
||||
elseif (fields.key_enter_field == "filter" or fields.search) and
|
||||
@ -596,7 +656,6 @@ local function get_fields(player, ...)
|
||||
|
||||
elseif fields.prev or fields.next then
|
||||
data.pagenum = data.pagenum - (fields.prev and 1 or -1)
|
||||
|
||||
if data.pagenum > data.pagemax then
|
||||
data.pagenum = 1
|
||||
elseif data.pagenum == 0 then
|
||||
@ -613,41 +672,60 @@ local function get_fields(player, ...)
|
||||
|
||||
else for item in pairs(fields) do
|
||||
if item:find(":") then
|
||||
if item:sub(-4) == "_inv" then
|
||||
item = item:sub(1,-5)
|
||||
elseif item:find("%s") then
|
||||
if item:find("%s") then
|
||||
item = item:match("%S*")
|
||||
end
|
||||
|
||||
local is_fuel = get_fueltime(item) > 0
|
||||
local recipes = get_recipes(item) or {}
|
||||
recipes = add_custom_recipes(item, recipes)
|
||||
if not next(recipes) and not is_fuel then return end
|
||||
|
||||
if not data.show_usage and item == data.item and not progressive_mode then
|
||||
data.usages = get_item_usages(item)
|
||||
if next(data.usages) then
|
||||
data.show_usage = true
|
||||
data.rnum = 1
|
||||
end
|
||||
|
||||
show_fs(player, player_name)
|
||||
else
|
||||
if progressive_mode then
|
||||
local inv = player:get_inventory()
|
||||
local has_item
|
||||
recipes, has_item = recipe_in_inv(inv, item, recipes)
|
||||
if not has_item then return end
|
||||
end
|
||||
|
||||
data.item = item
|
||||
data.recipes_item = recipes
|
||||
data.rnum = 1
|
||||
data.show_usage = nil
|
||||
data.fuel = is_fuel
|
||||
|
||||
show_fs(player, player_name)
|
||||
local no_recipes = not next(recipes)
|
||||
if no_recipes and not is_fuel then
|
||||
return
|
||||
end
|
||||
|
||||
if item ~= data.input then
|
||||
data.show_usage = nil
|
||||
else
|
||||
data.show_usage = not data.show_usage
|
||||
end
|
||||
|
||||
if not progressive_mode and is_fuel and no_recipes then
|
||||
data.show_usage = true
|
||||
end
|
||||
|
||||
if data.show_usage then
|
||||
data.usages = get_item_usages(item)
|
||||
|
||||
if is_fuel then
|
||||
data.usages[#data.usages + 1] = {
|
||||
width = 1,
|
||||
type = "normal",
|
||||
items = {item},
|
||||
output = "BURN",
|
||||
}
|
||||
end
|
||||
|
||||
if not next(data.usages) then
|
||||
data.show_usage = nil
|
||||
end
|
||||
|
||||
elseif progressive_mode then
|
||||
local inv = player:get_inventory()
|
||||
local has_item
|
||||
recipes, has_item = recipe_in_inv(inv, item, recipes)
|
||||
|
||||
if not has_item then
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
data.input = item
|
||||
data.recipes_item = recipes
|
||||
data.rnum = 1
|
||||
|
||||
show_fs(player, player_name)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -656,6 +734,7 @@ end
|
||||
if sfinv_only then
|
||||
sfinv.register_page("craftguide:craftguide", {
|
||||
title = "Craft Guide",
|
||||
|
||||
get = function(self, player, context)
|
||||
local player_name = player:get_player_name()
|
||||
return sfinv.make_formspec(
|
||||
@ -664,6 +743,7 @@ if sfinv_only then
|
||||
get_formspec(player_name)
|
||||
)
|
||||
end,
|
||||
|
||||
on_enter = function(self, player, context)
|
||||
local player_name = player:get_player_name()
|
||||
local data = datas[player_name]
|
||||
@ -672,6 +752,7 @@ if sfinv_only then
|
||||
init_datas(player, player_name)
|
||||
end
|
||||
end,
|
||||
|
||||
on_player_receive_fields = function(self, player, context, fields)
|
||||
get_fields(player, fields)
|
||||
end,
|
||||
@ -718,10 +799,12 @@ else
|
||||
wall_bottom = {-0.4375, -0.5, -0.3125, 0.4375, -0.4375, 0.3125},
|
||||
wall_side = {-0.5, -0.3125, -0.4375, -0.4375, 0.3125, 0.4375}
|
||||
},
|
||||
|
||||
on_construct = function(pos)
|
||||
local meta = mt.get_meta(pos)
|
||||
meta:set_string("infotext", S("Crafting Guide Sign"))
|
||||
end,
|
||||
|
||||
on_rightclick = function(pos, node, user, itemstack)
|
||||
on_use(itemstack, user)
|
||||
end
|
||||
@ -755,10 +838,10 @@ else
|
||||
sfinv_buttons.register_button("craftguide", {
|
||||
title = S("Crafting Guide"),
|
||||
tooltip = S("Shows a list of available crafting recipes, cooking recipes and fuels"),
|
||||
image = "craftguide_book.png",
|
||||
action = function(player)
|
||||
on_use(nil, player)
|
||||
end,
|
||||
image = "craftguide_book.png",
|
||||
})
|
||||
end
|
||||
end
|
||||
@ -784,30 +867,44 @@ if not progressive_mode then
|
||||
end
|
||||
|
||||
if not node_name then
|
||||
return false, colorize("[craftguide] ") .. S("No node pointed")
|
||||
return false, colorize("[craftguide] ", "red") ..
|
||||
S("No node pointed")
|
||||
elseif not datas[name] then
|
||||
init_datas(player, name)
|
||||
end
|
||||
|
||||
local data = datas[name]
|
||||
local data = datas[name]
|
||||
reset_datas(data)
|
||||
|
||||
local is_fuel = get_fueltime(node_name) > 0
|
||||
local recipes = get_recipes(node_name) or {}
|
||||
recipes = add_custom_recipes(node_name, recipes)
|
||||
local no_recipes = not next(recipes)
|
||||
|
||||
if not next(recipes) and not is_fuel then
|
||||
return false, colorize("[craftguide] ") ..
|
||||
if no_recipes and not is_fuel then
|
||||
return false, colorize("[craftguide] ", "red") ..
|
||||
S("No recipe for this node:") .. " " ..
|
||||
colorize(node_name)
|
||||
end
|
||||
|
||||
data.show_usage = nil
|
||||
data.filter = ""
|
||||
data.item = node_name
|
||||
data.pagenum = 1
|
||||
data.rnum = 1
|
||||
if is_fuel and no_recipes then
|
||||
data.usages = get_item_usages(node_name)
|
||||
if is_fuel then
|
||||
data.usages[#data.usages + 1] = {
|
||||
width = 1,
|
||||
type = "normal",
|
||||
items = {node_name},
|
||||
output = "BURN",
|
||||
}
|
||||
end
|
||||
|
||||
if next(data.usages) then
|
||||
data.show_usage = true
|
||||
end
|
||||
end
|
||||
|
||||
data.input = node_name
|
||||
data.recipes_item = recipes
|
||||
data.items = datas.init_items
|
||||
data.fuel = is_fuel
|
||||
|
||||
return true, show_fs(player, name)
|
||||
end,
|
||||
@ -835,4 +932,4 @@ for x = 1, 6 do
|
||||
recipe = cr[x]
|
||||
})
|
||||
end
|
||||
]]
|
||||
]]
|
||||
|
Reference in New Issue
Block a user