Compare commits

...

14 Commits

3 changed files with 89 additions and 103 deletions

192
init.lua
View File

@ -1,12 +1,5 @@
craftguide = {}
local p = 0
local CORE_VERSION = core.get_version().string:match("[^%-]*"):gsub("%.", function(a)
p = p + 1
return p == 3 and a or ""
end)
CORE_VERSION = tonumber(CORE_VERSION)
-- Caches
local pdata = {}
local init_items = {}
@ -44,18 +37,11 @@ local get_player_by_name = core.get_player_by_name
local slz, dslz = core.serialize, core.deserialize
local on_mods_loaded = core.register_on_mods_loaded
local on_leaveplayer = core.register_on_leaveplayer
local get_player_info = core.get_player_information
local on_receive_fields = core.register_on_player_receive_fields
local ESC = core.formspec_escape
local S = CORE_VERSION >= 500 and core.get_translator("craftguide") or
function(...)
local args, i = {...}, 1
return args[1]:gsub("@%d+", function()
i = i + 1
return args[i]
end)
end
local S = core.get_translator("craftguide")
local ES = function(...)
return ESC(S(...))
@ -79,7 +65,7 @@ local IPP = ROWS * LINES
local WH_LIMIT = 8
local XOFFSET = sfinv_only and 3.83 or 11.2
local YOFFSET = sfinv_only and 6 or 1
local YOFFSET = sfinv_only and 4.9 or 1
local PNG = {
bg = "craftguide_bg.png",
@ -590,10 +576,15 @@ local function get_recipes(item, data, player)
end
local no_recipes = not recipes or #recipes == 0
if no_recipes and not usages then
return
elseif sfinv_only and usages and no_recipes then
data.show_usages = true
elseif sfinv_only then
if usages and no_recipes then
data.show_usages = true
elseif recipes and not usages then
data.show_usages = nil
end
end
if not sfinv_only or (sfinv_only and data.show_usages) then
@ -653,6 +644,10 @@ local function is_fav(data)
end
local function get_desc(name)
if sub(name, 1, 1) == "_" then
name = sub(name, 2)
end
local def = reg_items[name]
return def and (match(def.description, "%)([%w%s]*)") or def.description) or
@ -713,7 +708,7 @@ local function get_tooltip(name, info)
return fmt("tooltip[%s;%s]", name, ESC(tooltip))
end
local function get_output_fs(fs, L)
local function get_output_fs(data, fs, L)
local custom_recipe = craft_types[L.recipe.type]
if custom_recipe or L.shapeless or L.recipe.type == "cooking" then
@ -732,9 +727,7 @@ local function get_output_fs(fs, L)
local tooltip = custom_recipe and custom_recipe.description or
L.shapeless and S"Shapeless" or S"Cooking"
if CORE_VERSION >= 500 then
fs[#fs + 1] = fmt(FMT.tooltip, pos_x, pos_y + L.spacing, 0.5, 0.5, ESC(tooltip))
end
fs[#fs + 1] = fmt(FMT.tooltip, pos_x, pos_y + L.spacing, 0.5, 0.5, ESC(tooltip))
end
local arrow_X = L.rightest + (L._btn_size or 1.1)
@ -753,15 +746,17 @@ local function get_output_fs(fs, L)
item = clean_name(item)
local name = match(item, "%S*")
if CORE_VERSION >= 510 then
if data.fs_version >= 3 then
fs[#fs + 1] = fmt(FMT.image,
output_X, YOFFSET + (sfinv_only and 0.7 or 0) + L.spacing,
1.1, 1.1, PNG.selected)
end
fs[#fs + 1] = fmt(FMT.item_image_button,
local _name = sfinv_only and name or fmt("_%s", name)
fs[#fs + 1] = fmt("item_image_button[%f,%f;%f,%f;%s;%s;%s]",
output_X, YOFFSET + (sfinv_only and 0.7 or 0) + L.spacing,
1.1, 1.1, item, name, "")
1.1, 1.1, item, _name, "")
local infos = {
unknown = not reg_items[name] or nil,
@ -771,7 +766,7 @@ local function get_output_fs(fs, L)
}
if next(infos) then
fs[#fs + 1] = get_tooltip(name, infos)
fs[#fs + 1] = get_tooltip(_name, infos)
end
if infos.burntime then
@ -786,7 +781,7 @@ local function get_output_fs(fs, L)
end
end
local function get_grid_fs(fs, rcp, spacing)
local function get_grid_fs(data, fs, rcp, spacing)
local width = rcp.width or 1
local replacements = rcp.replacements
local rarity = rcp.rarity
@ -865,7 +860,7 @@ local function get_grid_fs(fs, rcp, spacing)
end
end
if CORE_VERSION >= 510 and not large_recipe then
if data.fs_version >= 3 and not large_recipe then
fs[#fs + 1] = fmt(FMT.image,
X, Y + (sfinv_only and 0.7 or 0), btn_size, btn_size, PNG.selected)
end
@ -891,7 +886,7 @@ local function get_grid_fs(fs, rcp, spacing)
fs[#fs + 1] = "style_type[item_image_button;border=false]"
end
get_output_fs(fs, {
get_output_fs(data, fs, {
recipe = rcp,
shapeless = shapeless,
rightest = rightest,
@ -903,14 +898,16 @@ local function get_grid_fs(fs, rcp, spacing)
end
local function get_panels(data, fs)
local start_y = CORE_VERSION >= 520 and 0 or 0.33
local start_y = data.fs_version >= 3 and (sfinv_only and 0.33 or 0) or 0.33
local panels = {
{dat = data.usages or {}, height = 3.5},
{dat = data.recipes or {}, height = 3.5},
}
if CORE_VERSION >= 520 and not sfinv_only then
if data.fs_version >= 3 and not sfinv_only then
panels.favs = {dat = {}, height = 2.19}
elseif sfinv_only then
panels = data.show_usages and
{{dat = data.usages}} or {{dat = data.recipes}}
@ -921,13 +918,10 @@ local function get_panels(data, fs)
local spacing = (start_y - 1) * 3.6
if not sfinv_only then
fs[#fs + 1] = CORE_VERSION >= 510 and
fmt("background9[8.1,%f;6.6,%f;%s;false;%d]",
-0.2 + spacing, v.height, PNG.bg_full, 10) or
fmt("background[8.1,%f;6.6,%f;%s;false]",
-0.2 + spacing, v.height, PNG.bg_full)
fs[#fs + 1] = fmt("background9[8.1,%f;6.6,%f;%s;false;%d]",
-0.2 + spacing, v.height, PNG.bg_full, 10)
if k ~= "favs" then
if data.fs_version >= 3 and k == 2 then
local fav = is_fav(data)
local nfavs = #data.favs
@ -954,7 +948,7 @@ local function get_panels(data, fs)
xu = max(-0.3, -((#xu - 3) * 0.05))
xr = max(-0.3, -((#xr - 3) * 0.05))
local is_recipe = k == 2
local is_recipe = sfinv_only and not data.show_usages or k == 2
local lbl
if not sfinv_only and rn == 0 then
@ -975,16 +969,16 @@ local function get_panels(data, fs)
fs[#fs + 1] = fmt(FMT.label,
XOFFSET + (sfinv_only and 2.3 or 1.6) + (is_recipe and xr or xu),
YOFFSET + (sfinv_only and 2.3 or 1.5 + spacing), lbl)
YOFFSET + (sfinv_only and 3.4 or 1.5 + spacing), lbl)
if rn > 1 then
local btn_suffix = is_recipe and "recipe" or "usage"
local prev_name = fmt("prev_%s", btn_suffix)
local next_name = fmt("next_%s", btn_suffix)
local x_arrow = XOFFSET + (sfinv_only and 1.7 or 1)
local y_arrow = YOFFSET + (sfinv_only and 2.2 or 1.4 + spacing)
local y_arrow = YOFFSET + (sfinv_only and 3.3 or 1.4 + spacing)
if CORE_VERSION >= 520 then
if data.fs_version >= 3 then
fs[#fs + 1] = fmt(mul_elem(FMT.arrow, 2),
x_arrow + (is_recipe and xr or xu), y_arrow,
PNG.prev, prev_name, "",
@ -999,10 +993,10 @@ local function get_panels(data, fs)
local rcp = is_recipe and v.dat[data.rnum] or v.dat[data.unum]
if rcp then
get_grid_fs(fs, rcp, spacing)
get_grid_fs(data, fs, rcp, spacing)
end
if k == "favs" and CORE_VERSION >= 520 and not sfinv_only then
if k == "favs" and data.fs_version >= 3 and not sfinv_only then
fs[#fs + 1] = fmt(FMT.label, 8.3, spacing - 0.1, ES"Bookmarks")
for i = 1, #data.favs do
@ -1022,8 +1016,7 @@ local function get_panels(data, fs)
end
end
local function make_formspec(name)
local data = pdata[name]
local function make_fs(data)
local fs = {}
fs[#fs + 1] = fmt([[
@ -1034,11 +1027,8 @@ local function make_formspec(name)
ROWS + (data.query_item and 6.7 or 0) - 1.2, LINES - 0.3)
if not sfinv_only then
fs[#fs + 1] = CORE_VERSION >= 510 and
fmt("background9[-0.15,-0.2;%f,%f;%s;false;%d]",
ROWS - 0.9, LINES + 0.4, PNG.bg_full, 10) or
fmt("background[-0.15,-0.2;%f,%f;%s;false]",
ROWS - 0.9, LINES + 0.4, PNG.bg_full)
fs[#fs + 1] = fmt("background9[-0.15,-0.2;%f,%f;%s;false;%d]",
ROWS - 0.9, LINES + 0.4, PNG.bg_full, 10)
end
fs[#fs + 1] = fmt([[
@ -1047,7 +1037,7 @@ local function make_formspec(name)
]],
sfinv_only and 2.76 or 2.72, ESC(data.filter))
if CORE_VERSION >= 520 then
if data.fs_version >= 3 then
fs[#fs + 1] = fmt([[
style_type[image_button;border=false]
style_type[item_image_button;border=false;bgimg_hovered=%s;bgimg_pressed=%s]
@ -1077,9 +1067,9 @@ local function make_formspec(name)
fs[#fs + 1] = fmt(mul_elem(FMT.arrow, 2),
sfinv_only and 5.45 or (ROWS * 6.83) / 11, -0.05,
PNG.prev, "prev_page", PNG.prev,
PNG.prev, "prev_page", PNG.prev_hover,
sfinv_only and 7.2 or (ROWS * 8.75) / 11, -0.05,
PNG.next, "next_page", PNG.next)
PNG.next, "next_page", PNG.next_hover)
end
data.pagemax = max(1, ceil(#data.items / IPP))
@ -1109,7 +1099,7 @@ local function make_formspec(name)
local X = i % ROWS
local Y = (i % IPP - X) / ROWS + 1
if CORE_VERSION >= 510 and data.query_item == item then
if data.fs_version >= 3 and data.query_item == item then
fs[#fs + 1] = fmt(FMT.image,
X - (X * (sfinv_only and 0.12 or 0.14)) - 0.05,
Y - (Y * 0.1) - 0.1,
@ -1130,10 +1120,11 @@ local function make_formspec(name)
end
local show_fs = function(player, name)
local data = pdata[name]
if sfinv_only then
sfinv.set_player_inventory_formspec(player)
else
show_formspec(name, "craftguide", make_formspec(name))
show_formspec(name, "craftguide", make_fs(data))
end
end
@ -1523,15 +1514,13 @@ local function get_init_items()
end
local function init_data(name)
local items = CORE_VERSION >= 500 and init_items or
(#init_items == 0 and get_init_items() or init_items)
pdata[name] = {
filter = "",
pagenum = 1,
items = items,
items_raw = items,
favs = {},
filter = "",
pagenum = 1,
items = init_items,
items_raw = init_items,
favs = {},
fs_version = get_player_info(name).formspec_version,
}
end
@ -1547,9 +1536,7 @@ local function reset_data(data)
data.items = data.items_raw
end
if CORE_VERSION >= 500 then
on_mods_loaded(get_init_items)
end
on_mods_loaded(get_init_items)
on_joinplayer(function(player)
local name = player:get_player_name()
@ -1559,22 +1546,25 @@ end)
local function fields(player, _f)
local name = player:get_player_name()
local data = pdata[name]
--print(dump(_f))
if _f.clear then
reset_data(data)
return true, show_fs(player, name)
elseif _f.prev_recipe or _f.next_recipe then
local num = data.rnum + (_f.prev_recipe and -1 or 1)
data.rnum = data.recipes[num] and num or (_f.prev_recipe and #data.recipes or 1)
return true, show_fs(player, name)
elseif _f.prev_usage or _f.next_usage then
local num = data.unum + (_f.prev_usage and -1 or 1)
data.unum = data.usages[num] and num or (_f.prev_usage and #data.usages or 1)
return true, show_fs(player, name)
elseif (_f.key_enter_field == "filter" or _f.search) and _f.filter ~= "" then
elseif _f.key_enter_field == "filter" or _f.search then
if _f.filter == "" then
reset_data(data)
return true, show_fs(player, name)
end
local str = lower(_f.filter)
if data.filter == str then return end
@ -1582,8 +1572,6 @@ local function fields(player, _f)
data.pagenum = 1
search(data)
return true, show_fs(player, name)
elseif _f.prev_page or _f.next_page then
if data.pagemax == 1 then return end
data.pagenum = data.pagenum - (_f.prev_page and 1 or -1)
@ -1594,8 +1582,6 @@ local function fields(player, _f)
data.pagenum = data.pagemax
end
return true, show_fs(player, name)
elseif _f.fav then
local fav, i = is_fav(data)
local total = #data.favs
@ -1605,8 +1591,6 @@ local function fields(player, _f)
elseif fav then
remove(data.favs, i)
end
return true, show_fs(player, name)
else
local item
for field in pairs(_f) do
@ -1620,6 +1604,8 @@ local function fields(player, _f)
return
elseif sub(item, -4) == "_inv" then
item = sub(item, 1, -5)
elseif sub(item, 1, 1) == "_" then
item = sub(item, 2)
end
item = reg_aliases[item] or item
@ -1630,28 +1616,37 @@ local function fields(player, _f)
else
data.show_usages = not data.show_usages
end
elseif item == data.query_item then
return
end
local recipes, usages = get_recipes(item, data, player)
if not recipes and not usages then return end
if not recipes and not usages then return end
if data.show_usages and not usages then return end
data.query_item = item
data.recipes = recipes
data.usages = usages
data.rnum = 1
data.unum = 1
return true, show_fs(player, name)
end
return true, show_fs(player, name)
end
if sfinv_only then
sfinv.register_page("craftguide:craftguide", {
title = S"Craft Guide",
is_in_nav = function(self, player, context)
local name = player:get_player_name()
return get_player_info(name).formspec_version >= 2
end,
get = function(self, player, context)
local name = player:get_player_name()
local formspec = make_formspec(name)
local data = pdata[name]
local formspec = make_fs(data)
return sfinv.make_formspec(player, context, formspec)
end,
@ -1679,14 +1674,19 @@ else
local function on_use(user)
local name = user:get_player_name()
local data = pdata[name]
if data.fs_version == 1 then
return msg(name, "Your Minetest client is outdated. " ..
"Get the latest version on minetest.net to use the Crafting Guide.")
end
if next(recipe_filters) then
local data = pdata[name]
data.items_raw = get_filtered_items(user)
search(data)
end
show_formspec(name, "craftguide", make_formspec(name))
show_formspec(name, "craftguide", make_fs(data))
end
core.register_craftitem("craftguide:book", {
@ -1955,14 +1955,9 @@ if progressive_mode then
local name = player:get_player_name()
local data = pdata[name]
if CORE_VERSION >= 500 then
local meta = player:get_meta()
data.inv_items = dslz(meta:get_string("inv_items")) or {}
data.known_recipes = dslz(meta:get_string("known_recipes")) or 0
else
data.inv_items = dslz(player:get_attribute("inv_items")) or {}
data.known_recipes = dslz(player:get_attribute("known_recipes")) or 0
end
local meta = player:get_meta()
data.inv_items = dslz(meta:get_string("inv_items")) or {}
data.known_recipes = dslz(meta:get_string("known_recipes")) or 0
data.hud = {
bg = player:hud_add{
@ -1985,7 +1980,7 @@ if progressive_mode then
hud_elem_type = "text",
position = {x = 0.84, y = 1.04},
alignment = {x = 1, y = 1},
number = 0xfff,
number = 0xffffff,
text = "",
},
}
@ -1997,22 +1992,13 @@ if progressive_mode then
}
local function save_meta(player)
local meta
local meta = player:get_meta()
local name = player:get_player_name()
local data = pdata[name]
if CORE_VERSION >= 500 then
meta = player:get_meta()
end
for i = 1, #to_save do
local meta_name = to_save[i]
if CORE_VERSION >= 500 then
meta:set_string(meta_name, slz(data[meta_name]))
else
player:set_attribute(meta_name, slz(data[meta_name]))
end
meta:set_string(meta_name, slz(data[meta_name]))
end
end

Binary file not shown.

Before

Width:  |  Height:  |  Size: 916 B

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 3.4 KiB