Some cleaning in mailbox code

This commit is contained in:
Jean-Patrick Guerrero 2016-02-10 13:14:14 +01:00
parent be813edc39
commit 2ff4a1f042

View File

@ -1,75 +1,6 @@
local mailbox = {}
screwdriver = screwdriver or {}
xdecor.register("mailbox", {
description = "Mailbox",
tiles = {
"xdecor_mailbox_top.png", "xdecor_mailbox_bottom.png",
"xdecor_mailbox_side.png", "xdecor_mailbox_side.png",
"xdecor_mailbox.png", "xdecor_mailbox.png",
},
groups = {cracky=3, oddly_breakable_by_hand=1},
on_rotate = screwdriver.rotate_simple,
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos)
local player_name = placer:get_player_name()
meta:set_string("owner", player_name)
meta:set_string("infotext", player_name.."'s Mailbox")
local inv = meta:get_inventory()
inv:set_size("mailbox", 6*4)
inv:set_size("drop", 1)
end,
on_rightclick = function(pos, _, clicker)
local meta = minetest.get_meta(pos)
local player = clicker:get_player_name()
local owner = meta:get_string("owner")
if player == owner then
minetest.show_formspec(player, "", mailbox.formspec(pos, owner, 1))
else
minetest.show_formspec(player, "", mailbox.formspec(pos, owner, 0))
end
end,
can_dig = function(pos, player)
local meta = minetest.get_meta(pos)
local owner = meta:get_string("owner")
local player_name = player:get_player_name()
local inv = meta:get_inventory()
return inv:is_empty("mailbox") and player and player_name == owner
end,
on_metadata_inventory_put = function(pos, listname, _, stack, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if listname == "drop" and inv:room_for_item("mailbox", stack) then
inv:remove_item("drop", stack)
inv:add_item("mailbox", stack)
for i = 7, 2, -1 do
meta:set_string("giver"..i, meta:get_string("giver"..(i-1)))
meta:set_string("stack"..i, meta:get_string("stack"..(i-1)))
end
meta:set_string("giver1", player:get_player_name())
meta:set_string("stack1", stack:get_name().." "..stack:get_count())
end
end,
allow_metadata_inventory_put = function(pos, listname, _, stack, player)
if listname == "drop" then
local inv = minetest.get_meta(pos):get_inventory()
if inv:room_for_item("mailbox", stack) then
return -1
else
minetest.chat_send_player(player:get_player_name(), "The mailbox is full")
end
end
return 0
end
})
local function img_col(stack)
if not stack then return "" end
if stack.inventory_image ~= "" then
@ -79,7 +10,7 @@ local function img_col(stack)
end
end
function mailbox.formspec(pos, owner, num)
function mailbox:formspec(pos, owner, num)
local spos = pos.x..","..pos.y..","..pos.z
local meta = minetest.get_meta(pos)
local giver, img = "", ""
@ -118,3 +49,82 @@ function mailbox.formspec(pos, owner, num)
end
end
function mailbox.dig(pos, player)
local meta = minetest.get_meta(pos)
local owner = meta:get_string("owner")
local player_name = player:get_player_name()
local inv = meta:get_inventory()
return inv:is_empty("mailbox") and player and player_name == owner
end
function mailbox.after_place_node(pos, placer)
local meta = minetest.get_meta(pos)
local player_name = placer:get_player_name()
meta:set_string("owner", player_name)
meta:set_string("infotext", player_name.."'s Mailbox")
local inv = meta:get_inventory()
inv:set_size("mailbox", 6*4)
inv:set_size("drop", 1)
end
function mailbox.rightclick(pos, _, clicker)
local meta = minetest.get_meta(pos)
local player = clicker:get_player_name()
local owner = meta:get_string("owner")
if player == owner then
minetest.show_formspec(player, "", mailbox:formspec(pos, owner, 1))
else
minetest.show_formspec(player, "", mailbox:formspec(pos, owner, 0))
end
end
function mailbox.put(pos, listname, _, stack, player)
if listname == "drop" then
local inv = minetest.get_meta(pos):get_inventory()
if inv:room_for_item("mailbox", stack) then
return -1
else
minetest.chat_send_player(player:get_player_name(), "[!] The mailbox is full")
end
end
return 0
end
function mailbox.on_put(pos, listname, _, stack, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
if listname == "drop" and inv:room_for_item("mailbox", stack) then
inv:set_list("drop", {})
inv:add_item("mailbox", stack)
for i = 7, 2, -1 do
meta:set_string("giver"..i, meta:get_string("giver"..(i-1)))
meta:set_string("stack"..i, meta:get_string("stack"..(i-1)))
end
meta:set_string("giver1", player:get_player_name())
meta:set_string("stack1", stack:to_string())
end
end
xdecor.register("mailbox", {
description = "Mailbox",
tiles = {
"xdecor_mailbox_top.png", "xdecor_mailbox_bottom.png",
"xdecor_mailbox_side.png", "xdecor_mailbox_side.png",
"xdecor_mailbox.png", "xdecor_mailbox.png",
},
groups = {cracky=3, oddly_breakable_by_hand=1},
on_rotate = screwdriver.rotate_simple,
can_dig = mailbox.dig,
on_rightclick = mailbox.rightclick,
on_metadata_inventory_put = mailbox.on_put,
allow_metadata_inventory_put = mailbox.put,
after_place_node = mailbox.after_place_node
})