Fix crash for invalid award index selection

This commit is contained in:
SmallJoker 2018-07-15 14:17:06 +02:00 committed by rubenwardy
parent 8c0bb00b1a
commit 32150bdd66

97
gui.lua

@ -75,60 +75,58 @@ function awards.get_formspec(name, to, sid)
formspec = formspec .. "button_exit[4.2,2.3;3,1;close;"..minetest.formspec_escape(S("OK")).."]" formspec = formspec .. "button_exit[4.2,2.3;3,1;close;"..minetest.formspec_escape(S("OK")).."]"
return formspec return formspec
end end
sid = awards_list[sid] and sid or 1
-- Sidebar -- Sidebar
if sid then local sitem = awards_list[sid]
local item = awards_list[sid+0] local sdef = sitem.def
local def = item.def if sdef and sdef.secret and not sitem.unlocked then
formspec = formspec .. "label[1,2.75;"..
minetest.formspec_escape(S("(Secret Award)")).."]"..
"image[1,0;3,3;awards_unknown.png]"
if sdef and sdef.description then
formspec = formspec .. "textarea[0.25,3.25;4.8,1.7;;"..
minetest.formspec_escape(
S("Unlock this award to find out what it is."))..";]"
end
else
local title = sitem.name
if sdef and sdef.title then
title = sdef.title
end
local status = "%s"
if sitem.unlocked then
status = S("%s (unlocked)")
end
if def and def.secret and not item.unlocked then formspec = formspec .. "textarea[0.5,2.7;4.8,1.45;;" ..
formspec = formspec .. "label[1,2.75;".. string.format(status, minetest.formspec_escape(title)) ..
minetest.formspec_escape(S("(Secret Award)")).."]".. ";]"
"image[1,0;3,3;awards_unknown.png]"
if def and def.description then
formspec = formspec .. "textarea[0.25,3.25;4.8,1.7;;"..
minetest.formspec_escape(
S("Unlock this award to find out what it is."))..";]"
end
else
local title = item.name
if def and def.title then
title = def.title
end
local status = "%s"
if item.unlocked then
status = S("%s (unlocked)")
end
formspec = formspec .. "textarea[0.5,2.7;4.8,1.45;;" .. if sdef and sdef.icon then
string.format(status, minetest.formspec_escape(title)) .. formspec = formspec .. "image[1,0;3,3;" .. sdef.icon .. "]"
";]" end
local barwidth = 4.6
if def and def.icon then local perc = nil
formspec = formspec .. "image[1,0;3,3;" .. def.icon .. "]" local label = nil
if sdef.getProgress and data then
local res = sdef:getProgress(data)
perc = res.perc
label = res.label
end
if perc then
if perc > 1 then
perc = 1
end end
local barwidth = 4.6 formspec = formspec .. "background[0,4.80;" .. barwidth ..",0.25;awards_progress_gray.png;false]"
local perc = nil formspec = formspec .. "background[0,4.80;" .. (barwidth * perc) ..",0.25;awards_progress_green.png;false]"
local label = nil if label then
if def.getProgress and data then formspec = formspec .. "label[1.75,4.63;" .. minetest.formspec_escape(label) .. "]"
local res = def:getProgress(data)
perc = res.perc
label = res.label
end
if perc then
if perc > 1 then
perc = 1
end
formspec = formspec .. "background[0,4.80;" .. barwidth ..",0.25;awards_progress_gray.png;false]"
formspec = formspec .. "background[0,4.80;" .. (barwidth * perc) ..",0.25;awards_progress_green.png;false]"
if label then
formspec = formspec .. "label[1.75,4.63;" .. minetest.formspec_escape(label) .. "]"
end
end
if def and def.description then
formspec = formspec .. "textarea[0.25,3.75;4.8,1.7;;"..minetest.formspec_escape(def.description)..";]"
end end
end end
if sdef and sdef.description then
formspec = formspec .. "textarea[0.25,3.75;4.8,1.7;;"..minetest.formspec_escape(sdef.description)..";]"
end
end end
-- Create list box -- Create list box
@ -199,9 +197,6 @@ function awards.show_to(name, to, sid, text)
end end
end end
else else
if sid == nil or sid < 1 then
sid = 1
end
local deco = "" local deco = ""
if minetest.global_exists("default") then if minetest.global_exists("default") then
deco = default.gui_bg .. default.gui_bg_img deco = default.gui_bg .. default.gui_bg_img
@ -226,7 +221,7 @@ if minetest.get_modpath("sfinv") then
get = function(self, player, context) get = function(self, player, context)
local name = player:get_player_name() local name = player:get_player_name()
return sfinv.make_formspec(player, context, return sfinv.make_formspec(player, context,
awards.get_formspec(name, name, context.awards_idx or 1), awards.get_formspec(name, name, context.awards_idx),
false, "size[11,5]") false, "size[11,5]")
end, end,
on_player_receive_fields = function(self, player, context, fields) on_player_receive_fields = function(self, player, context, fields)