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
1 changed files with 46 additions and 51 deletions

37
gui.lua
View File

@ -75,28 +75,27 @@ 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
if def and def.secret and not item.unlocked then
formspec = formspec .. "label[1,2.75;".. formspec = formspec .. "label[1,2.75;"..
minetest.formspec_escape(S("(Secret Award)")).."]".. minetest.formspec_escape(S("(Secret Award)")).."]"..
"image[1,0;3,3;awards_unknown.png]" "image[1,0;3,3;awards_unknown.png]"
if def and def.description then if sdef and sdef.description then
formspec = formspec .. "textarea[0.25,3.25;4.8,1.7;;".. formspec = formspec .. "textarea[0.25,3.25;4.8,1.7;;"..
minetest.formspec_escape( minetest.formspec_escape(
S("Unlock this award to find out what it is."))..";]" S("Unlock this award to find out what it is."))..";]"
end end
else else
local title = item.name local title = sitem.name
if def and def.title then if sdef and sdef.title then
title = def.title title = sdef.title
end end
local status = "%s" local status = "%s"
if item.unlocked then if sitem.unlocked then
status = S("%s (unlocked)") status = S("%s (unlocked)")
end end
@ -104,14 +103,14 @@ function awards.get_formspec(name, to, sid)
string.format(status, minetest.formspec_escape(title)) .. string.format(status, minetest.formspec_escape(title)) ..
";]" ";]"
if def and def.icon then if sdef and sdef.icon then
formspec = formspec .. "image[1,0;3,3;" .. def.icon .. "]" formspec = formspec .. "image[1,0;3,3;" .. sdef.icon .. "]"
end end
local barwidth = 4.6 local barwidth = 4.6
local perc = nil local perc = nil
local label = nil local label = nil
if def.getProgress and data then if sdef.getProgress and data then
local res = def:getProgress(data) local res = sdef:getProgress(data)
perc = res.perc perc = res.perc
label = res.label label = res.label
end end
@ -125,9 +124,8 @@ function awards.get_formspec(name, to, sid)
formspec = formspec .. "label[1.75,4.63;" .. minetest.formspec_escape(label) .. "]" formspec = formspec .. "label[1.75,4.63;" .. minetest.formspec_escape(label) .. "]"
end end
end end
if def and def.description then if sdef and sdef.description then
formspec = formspec .. "textarea[0.25,3.75;4.8,1.7;;"..minetest.formspec_escape(def.description)..";]" formspec = formspec .. "textarea[0.25,3.75;4.8,1.7;;"..minetest.formspec_escape(sdef.description)..";]"
end
end end
end end
@ -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)