2015-08-13 23:34:21 +03:00
local mailbox = { }
2015-11-15 22:35:20 +03:00
screwdriver = screwdriver or { }
2015-08-13 23:34:21 +03:00
2015-07-02 16:26:19 +03:00
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 " ,
} ,
2015-11-16 00:55:12 +03:00
groups = { cracky = 3 , oddly_breakable_by_hand = 1 } ,
2015-11-15 22:35:20 +03:00
on_rotate = screwdriver.rotate_simple ,
2015-08-12 21:49:07 +03:00
after_place_node = function ( pos , placer , _ )
2015-07-02 16:26:19 +03:00
local meta = minetest.get_meta ( pos )
2015-11-25 00:23:43 +03:00
local player_name = placer : get_player_name ( )
2015-07-02 16:26:19 +03:00
2015-11-25 00:23:43 +03:00
meta : set_string ( " owner " , player_name )
meta : set_string ( " infotext " , player_name .. " 's Mailbox " )
2015-07-02 16:26:19 +03:00
local inv = meta : get_inventory ( )
2015-11-25 13:57:53 +03:00
inv : set_size ( " mailbox " , 6 * 4 )
2015-07-02 16:26:19 +03:00
inv : set_size ( " drop " , 1 )
end ,
2015-08-12 21:49:07 +03:00
on_rightclick = function ( pos , _ , clicker , _ )
2015-07-02 16:26:19 +03:00
local meta = minetest.get_meta ( pos )
2015-08-05 11:39:57 +03:00
local player = clicker : get_player_name ( )
local owner = meta : get_string ( " owner " )
2015-07-02 16:26:19 +03:00
2015-11-25 00:23:43 +03:00
if player == owner then
2015-11-25 02:10:33 +03:00
minetest.show_formspec ( player , " " , mailbox.formspec ( pos , owner , 1 ) )
2015-11-25 00:23:43 +03:00
else
2015-11-25 02:10:33 +03:00
minetest.show_formspec ( player , " " , mailbox.formspec ( pos , owner , 0 ) )
2015-07-02 16:26:19 +03:00
end
end ,
2015-08-03 13:08:30 +03:00
can_dig = function ( pos , player )
2015-07-02 16:26:19 +03:00
local meta = minetest.get_meta ( pos )
local owner = meta : get_string ( " owner " )
2015-11-25 00:23:43 +03:00
local player_name = player : get_player_name ( )
2015-07-02 16:26:19 +03:00
local inv = meta : get_inventory ( )
2015-11-25 00:23:43 +03:00
return inv : is_empty ( " mailbox " ) and player and player_name == owner
2015-07-02 16:26:19 +03:00
end ,
2015-11-25 02:10:33 +03:00
on_metadata_inventory_put = function ( pos , listname , _ , stack , player )
2015-08-24 23:50:29 +03:00
local inv = minetest.get_meta ( pos ) : get_inventory ( )
2015-11-25 02:10:33 +03:00
local player_name = player : get_player_name ( )
local meta = minetest.get_meta ( pos )
local stack_name = stack : get_name ( ) .. " " .. stack : get_count ( )
2015-11-25 00:23:43 +03:00
if listname == " drop " and inv : room_for_item ( " mailbox " , stack ) then
2015-07-02 16:26:19 +03:00
inv : remove_item ( " drop " , stack )
2015-11-25 00:23:43 +03:00
inv : add_item ( " mailbox " , stack )
2015-11-25 02:10:33 +03:00
2015-11-25 13:57:53 +03:00
for i = 4 , 2 , - 1 do
2015-11-25 02:10:33 +03:00
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_name )
meta : set_string ( " stack1 " , stack_name )
2015-07-02 16:26:19 +03:00
end
end ,
2015-08-12 21:49:07 +03:00
allow_metadata_inventory_put = function ( pos , listname , _ , stack , _ )
2015-07-02 16:26:19 +03:00
if listname == " drop " then
local meta = minetest.get_meta ( pos )
local inv = meta : get_inventory ( )
2015-11-25 00:23:43 +03:00
if inv : room_for_item ( " mailbox " , stack ) then return - 1 end
2015-07-02 16:26:19 +03:00
end
2015-11-22 16:13:26 +03:00
return 0
2015-08-03 13:08:30 +03:00
end
2015-07-02 16:26:19 +03:00
} )
2015-11-25 02:10:33 +03:00
function mailbox . formspec ( pos , owner , num )
2015-11-25 00:23:43 +03:00
local xbg = default.gui_bg .. default.gui_bg_img .. default.gui_slots
2015-07-02 16:26:19 +03:00
local spos = pos.x .. " , " .. pos.y .. " , " .. pos.z
2015-11-25 02:10:33 +03:00
local meta = minetest.get_meta ( pos )
local giver = " "
2015-07-02 16:26:19 +03:00
2015-11-25 02:10:33 +03:00
if num == 1 then
2015-11-25 13:57:53 +03:00
for i = 1 , 4 do
2015-11-25 02:10:33 +03:00
if meta : get_string ( " giver " .. i ) ~= " " then
2015-11-25 13:57:53 +03:00
giver = giver .. " #FFFF00, " .. meta : get_string ( " giver " .. i ) .. " ,, \t " .. meta : get_string ( " stack " .. i ) .. " , "
2015-11-25 02:10:33 +03:00
end
end
2015-11-25 13:57:53 +03:00
return " size[11,9] " .. xbg .. default.get_hotbar_bg ( 1.5 , 5.25 ) ..
" label[0,0;You have received...]label[6,0;Last donators :]box[6,0.75;4.85,3.9;#555555]tablecolumns[color;text]tableoptions[background=#00000000;highlight=#00000000;border=false]table[6,0.75;4.85,4.4;givers; " .. giver .. " ]list[nodemeta: " .. spos .. " ;mailbox;0,0.75;6,4;]list[current_player;main;1.5,5.25;8,4;] "
2015-11-25 00:23:43 +03:00
else
return " size[8,5] " .. xbg .. default.get_hotbar_bg ( 0 , 1.25 ) ..
" label[0.5,0;Send your goods \n to " .. owner .. " :]list[nodemeta: " .. spos .. " ;drop;3.5,0;1,1;]list[current_player;main;0,1.25;8,4;] "
end
2015-07-02 16:26:19 +03:00
end
2015-11-25 00:23:43 +03:00