Move register_on_player_receive_fields out of the loop.

Avoids registering one `on_receive_fields` per book color,
which is wasteful.

Also, minor fixes mainly for conciseness' sake.
This commit is contained in:
Diego Martinez 2015-01-23 14:16:40 -03:00
parent 5bae438647
commit 0c7dfa9ba6

View File

@ -1903,6 +1903,8 @@ local bookcolors = {
"brown" "brown"
} }
local BOOK_FORMNAME = "homedecor:book_form"
for c in ipairs(bookcolors) do for c in ipairs(bookcolors) do
local color = bookcolors[c] local color = bookcolors[c]
local color_d = S(bookcolors[c]) local color_d = S(bookcolors[c])
@ -1995,7 +1997,7 @@ minetest.register_node("homedecor:book_"..color, {
"label[1,0.5;"..minetest.formspec_escape(title).."]".. "label[1,0.5;"..minetest.formspec_escape(title).."]"..
"label[0.5,1.5;"..minetest.formspec_escape(text).."]" "label[0.5,1.5;"..minetest.formspec_escape(text).."]"
end end
minetest.show_formspec(user:get_player_name(), "homedecor:book_"..color, formspec) minetest.show_formspec(user:get_player_name(), BOOK_FORMNAME, formspec)
end, end,
}) })
@ -2026,24 +2028,23 @@ minetest.register_node("homedecor:book_open_"..color, {
end, end,
}) })
end
minetest.register_on_player_receive_fields(function(player, form_name, fields) minetest.register_on_player_receive_fields(function(player, form_name, fields)
if form_name ~= "homedecor:book_"..color or not fields.save then if form_name ~= BOOK_FORMNAME or not fields.save then
return return
end end
local stack = player:get_wielded_item() local stack = player:get_wielded_item()
if minetest.get_item_group(stack:get_name(), "book") == 0 then if minetest.get_item_group(stack:get_name(), "book") == 0 then
return return
end end
local data = minetest.deserialize(stack:get_metadata()) local data = minetest.deserialize(stack:get_metadata()) or {}
if not data then data = {} end
data.title, data.text, data.owner = data.title, data.text, data.owner =
fields.title, fields.text, player:get_player_name() fields.title, fields.text, player:get_player_name()
stack:set_metadata(minetest.serialize(data)) stack:set_metadata(minetest.serialize(data))
player:set_wielded_item(stack) player:set_wielded_item(stack)
end) end)
end
minetest.register_node("homedecor:calendar", { minetest.register_node("homedecor:calendar", {
description = "Calendar", description = "Calendar",
drawtype = "signlike", drawtype = "signlike",