diff --git a/init.lua b/init.lua index 2a35b02..2f6f548 100644 --- a/init.lua +++ b/init.lua @@ -24,6 +24,7 @@ -- Version 1.20 -- Changelog: +-- 25.02.2016 * Added new Locks config Formspec. -- 08.05.2014 * Changed animation of shared locked furnace (removed pipeworks overlay on front, changed to new animation type) -- 10.01.2013 * Added command to toggle for pipeworks output -- * Added pipeworks support for chests and furnace. @@ -40,6 +41,102 @@ locks = {}; minetest.register_privilege("openlocks", { description = "allows to open/use all locked objects", give_to_singleplayer = false}); minetest.register_privilege("diglocks", { description = "allows to open/use and dig up all locked objects", give_to_singleplayer = false}); +locks.config_button = [[ + image_button[%d,%d;1,1;locks_lock16.png;locks_config;Config +Locks] + tooltip[locks_config;Configure the players or set the password to grant access to other players.] +]] +function locks.get_config_button(x,y) +return locks.config_button:format((x or 0), (y or 0)) +end + +locks.authorize_button = [[ + image_button[%d,%d;1,1;locks_key16.png;locks_authorize;Autho- +rize] + tooltip[locks_authorize;Opens a password prompt to grant you access to this object.] +]] +function locks.get_authorize_button(x,y) +return locks.authorize_button:format((x or 1), (y or 0)) +end + +locks.password_prompt = [[ + size[6,3;] + pwdfield[0.5,1;5.5,0;password;Enter password:] + tooltip[password;Opens a password prompt to grant you access to this object.] + + box[0.1,1.5;5.5,0.05;#FFFFFF] + button[1.5,2;3,1;proceed;Proceed] +]] +function locks.prompt_password(playername, formname) +local fs = locks.password_prompt; + minetest.show_formspec(playername, formname, fs); +end + +function locks.access_denied(playername, formname, ask_for_pw) + local fs = [[ + size[6,3;] + label[0.5,1;Access denied] + + box[0.1,1.5;5.5,0.05;#FFFFFF] + button_exit[4,2;2,1;cancel;Cancel] +]]; + if ask_for_pw == true then + fs = fs.."button[0,2;3.5,1;enter_password;Enter Password]"; + end + minetest.show_formspec(playername, formname, fs); +end + +locks.access_granted = [[ + size[6,3;] + label[0.5,1;Access granted] + + box[0.1,1.5;5.5,0.05;#FFFFFF] + button_exit[1.5,2;3,1;proceed;Proceed] +]] + +locks.config_formspec = [[ + size[12,9;] + + image[0,0;0.7,0.7;locks_lock32.png] label[0.7,0;Locks configuration panel] + box[0.1,0.6;11.5,0.05;#FFFFFF] + + label[0.1,0.7;Owner: %q] + + checkbox[0.1,1;pipeworks;Enable Pipeworks;%s] + tooltip[pipeworks;Tubes from pipeworks may be used to extract items out of/add items to this shared locked object.] + + textarea[0.4,2;6.5,4;allowed_users;Allowed Players:;%s] + label[6.5,2;Insert the Playernames here, +that should have access to this Object. +One Player per line] + tooltip[allowed_users;Insert the Playernames here, +that should have access to this Object. +One Player per line] + + pwdfield[0.4,6.5;4.5,0;password;Password:] button[4.5,6.2;2,0;save_pw;Set PW] + tooltip[password;Every player with this password can access this object] + label[6.5,5.5;Specify a password here. +Every Player with this password can access this object. +Set an empty Password to remove the Password] + + box[0.1,8.5;11.5,0.05;#FFFFFF] + button_exit[4,9;2,0;ok;OK] button_exit[6,9;2,0;cancel;Cancel] +]] +locks.uniform_background = ""; + +if default and default.gui_bg then + locks.uniform_background = locks.uniform_background..default.gui_bg; +end + +if default and default.gui_bg_img then + locks.uniform_background = locks.uniform_background..default.gui_bg_img; +end + +if default and default.gui_slots then + locks.uniform_background = locks.uniform_background..default.gui_slots; +end + +locks.config_formspec = locks.config_formspec..locks.uniform_background locks.pipeworks_enabled = false; @@ -78,7 +175,9 @@ function locks:lock_init( pos, default_formspec ) -- the last player who entered the right password (to save space this is not a list) meta:set_string("pw_user",""); -- this formspec is presented on right-click for every user - meta:set_string("formspec", default_formspec); + meta:set_string("formspec", default_formspec.. + locks.get_authorize_button(6,0).. + locks.get_config_button(7,0)); -- by default, do not send output to pipework tubes meta:set_int( "allow_pipeworks", 0 ); end @@ -306,7 +405,35 @@ function locks:lock_handle_input( pos, formname, fields, player ) print( "Error: [locks] lock_handle_input: unable to get meta data"); return; end - + local name = player:get_player_name(); + + --first check for locks_config_button + if fields.locks_config then + local allow_pipeworks = "false"; + if meta:get_int( 'allow_pipeworks' ) == 1 then + allow_pipeworks = "true"; + end + local data = locks:get_lockdata( pos ); + local fs = locks.config_formspec:format(data.owner, + allow_pipeworks, + data.allowed_users:gsub(",","\n")); + minetest.show_formspec(player:get_player_name(), "locks_config:"..minetest.pos_to_string(pos), fs); + return true; -- we could full handle the input. No need to continue. so we return true + elseif fields.locks_authorize then + local data = locks:get_lockdata( pos ); + + if name == data.owner then + minetest.chat_send_player(name, "You are the owner of this object. Its not required to enter a password.",false) + elseif minetest.string_to_privs(meta:get_string("allowed_users"))[name] then + minetest.chat_send_player(name, "You are already authorized in the whitelist. Its not required to enter a password.",false) + else + + local fs = locks.password_prompt; + minetest.show_formspec(name, "locks_authorize:"..minetest.pos_to_string(pos), fs); + end + return true; + end + -- is this input the lock is supposed to handle? if( ( not( fields.locks_sent_lock_command ) or fields.locks_sent_lock_command == "" ) @@ -315,7 +442,7 @@ function locks:lock_handle_input( pos, formname, fields, player ) return; end - name = player:get_player_name(); + if( fields.locks_sent_lock_command == "/help" ) then @@ -554,7 +681,51 @@ function locks:lock_handle_input( pos, formname, fields, player ) minetest.chat_send_player(name, "Error: Command \""..tostring(help[1]).."\" not understood."); end - +--this is required to handle the locks control panel +minetest.register_on_player_receive_fields(function(player, formname, fields) + local playername = player:get_player_name(); + if formname:find("locks_config:") then -- search if formname contains locks + --minetest.chat_send_player(playername,dump(fields)); + local pos = minetest.string_to_pos(formname:gsub("locks_config:","")) + if fields.ok then + local data = locks:get_lockdata( pos ) + data.allowed_users = fields.allowed_users:gsub("\n",","); + --data.password = fields.password; + locks:set_lockdata( pos, data ) + --print("Player "..player:get_player_name().." submitted fields "..dump(fields)) + elseif fields.save_pw then + local data = locks:get_lockdata( pos ) + data.password = fields.password; + locks:set_lockdata( pos, data ); + elseif fields.pipeworks then + local meta = minetest.get_meta(pos); + if fields.pipeworks == "true" then + meta:set_int( 'allow_pipeworks', 1 ); + else + meta:set_int( 'allow_pipeworks', 0 ); + end + end + return true; --everything handled good :) + + elseif formname:find("locks_authorize:") then + if fields.password and fields.password ~="" then + local pos = minetest.string_to_pos(formname:gsub("locks_authorize:","")) + local meta = minetest.get_meta(pos); + if meta:get_string("password")==fields.password then + locks.access_granted(playername, formname); + meta:set_string("pw_user", playername) + else + locks.access_denied(playername, formname, true); + + end + elseif fields.enter_password then --if the user clicks the "Enter Password" button in the "access denied" formspec + locks.prompt_password(playername, formname); + end + + return true --evrything is great :) + end + return false; +end) -- craftitem; that can be used to craft shared locked objects minetest.register_craftitem("locks:lock", { diff --git a/shared_locked_chest.lua b/shared_locked_chest.lua index 66496b1..e66e37c 100644 --- a/shared_locked_chest.lua +++ b/shared_locked_chest.lua @@ -1,3 +1,5 @@ + +-- 25.02.16 Added new Locks config Buttons. -- 09.01.13 Added support for pipeworks. @@ -47,17 +49,14 @@ minetest.register_node("locks:shared_locked_chest", { local meta = minetest.env:get_meta(pos) -- prepare the lock of the chest locks:lock_init( pos, - "size[8,10]".. --- "field[0.5,0.2;8,1.0;locks_sent_lock_command;Locked chest. Type password, command or /help for help:;]".. --- "button_exit[3,0.8;2,1.0;locks_sent_input;Proceed]".. - "list[current_name;main;0,0;8,4;]".. - "list[current_player;main;0,5;8,4;]".. - "field[0.3,9.6;6,0.7;locks_sent_lock_command;Locked chest. Type /help for help:;]".. - "background[-0.5,-0.65;9,11.2;bg_shared_locked_chest.jpg]".. - "button_exit[6.3,9.2;1.7,0.7;locks_sent_input;Proceed]" ); --- "size[8,9]".. --- "list[current_name;main;0,0;8,4;]".. --- "list[current_player;main;0,5;8,4;]"); + "size[8,10]" .. + locks.uniform_background .. + "list[current_name;main;0,1.3;8,4;]" .. + "list[current_player;main;0,5.85;8,1;]" .. + "list[current_player;main;0,7.08;8,3;8]" .. + "listring[current_name;main]" .. + "listring[current_player;main]" .. + default.get_hotbar_bg(0,5.85) ); local inv = meta:get_inventory() inv:set_size("main", 8*4) end, diff --git a/shared_locked_furnace.lua b/shared_locked_furnace.lua index 1543c7b..1c2a6b0 100644 --- a/shared_locked_furnace.lua +++ b/shared_locked_furnace.lua @@ -3,6 +3,7 @@ -- containing only the furnace and adopted slightly for my locks mod +-- 25.02.16 Added new Locks config Buttons. -- 09.01.13 Added support for pipeworks. @@ -92,10 +93,10 @@ function locks.get_furnace_active_formspec(pos, percent) "list[current_name;src;2,1;1,1;]".. "list[current_name;dst;5,1;2,2;]".. "list[current_player;main;0,5;8,4;]".. - "background[-0.5,-0.65;9,10.35;bg_shared_locked_furnace.jpg]".. - - "field[0.3,4.5;6,0.7;locks_sent_lock_command;Locked furnace. Type /help for help:;]".. - "button_exit[6.3,4;1.7,0.7;locks_sent_input;Proceed]" ; + locks.uniform_background.. + locks.get_authorize_button(6,0).. + locks.get_config_button(7,0) + return formspec end @@ -106,9 +107,9 @@ locks.furnace_inactive_formspec = "list[current_name;src;2,1;1,1;]".. "list[current_name;dst;5,1;2,2;]".. "list[current_player;main;0,5;8,4;]".. - "background[-0.5,-0.65;9,10.35;bg_shared_locked_furnace.jpg]".. - "field[0.3,4.5;6,0.7;locks_sent_lock_command;Locked furnace. Type /help for help:;]".. - "button_exit[6.3,4;1.7,0.7;locks_sent_input;Proceed]" ; + locks.uniform_background.. + locks.get_authorize_button(6,0).. + locks.get_config_button(7,0) minetest.register_node("locks:shared_locked_furnace", { description = "Shared locked furnace", diff --git a/shared_locked_sign_wall.lua b/shared_locked_sign_wall.lua index 2dcf19e..21cd52f 100644 --- a/shared_locked_sign_wall.lua +++ b/shared_locked_sign_wall.lua @@ -1,4 +1,5 @@ +-- 25.02.16 Added new Locks config Buttons. -- allow aborting with ESC in newer Versions of MT again -- a sign @@ -28,9 +29,8 @@ minetest.register_node("locks:shared_locked_sign_wall", { locks:lock_init( pos, "size[8,4]".. "field[0.3,0.6;6,0.7;text;Text:;${text}]".. - "field[0.3,3.6;6,0.7;locks_sent_lock_command;Locked sign. Type /help for help:;]".. "button_exit[6.3,3.2;1.7,0.7;locks_sent_input;Proceed]".. - "background[-0.5,-0.5;9,5;bg_shared_locked_sign.jpg]" ); + locks.uniform_background ); end, after_place_node = function(pos, placer) @@ -44,10 +44,12 @@ minetest.register_node("locks:shared_locked_sign_wall", { on_receive_fields = function(pos, formname, fields, sender) - -- if the user already has the right to use this and did input text - if( fields.text - and ( not(fields.locks_sent_lock_command) - or fields.locks_sent_lock_command=="") + -- if locks can not handle the input + if not locks:lock_handle_input( pos, formname, fields, sender ) then + --then handle compatibility stuff or insert text + if( fields.text + and ( not(fields.locks_sent_lock_command) --compatibility + or fields.locks_sent_lock_command=="") --compatibility and locks:lock_allow_use( pos, sender )) then --print("Sign at "..minetest.pos_to_string(pos).." got "..dump(fields)) @@ -57,11 +59,8 @@ minetest.register_node("locks:shared_locked_sign_wall", { "\" to sign at "..minetest.pos_to_string(pos)); meta:set_string("text", fields.text); meta:set_string("infotext", '"'..fields.text..'"'.." ["..sender:get_player_name().."]"); - - -- a command for the lock? - else - locks:lock_handle_input( pos, formname, fields, sender ); - end + end + end end, }); diff --git a/shared_locked_xdoors2.lua b/shared_locked_xdoors2.lua index 1af36fc..95ba866 100644 --- a/shared_locked_xdoors2.lua +++ b/shared_locked_xdoors2.lua @@ -2,6 +2,7 @@ -- modified by Sokomine to allow locked doors that can only be opened/closed/dig up by the player who placed them -- a little bit modified by addi to allow someone with the priv "opendoors" to open/close/dig all locked doors. -- Sokomine: modified again so that it uses the new locks-mod +-- 25.02.16 Added new Locks config Buttons. local door_bottom = {-0.5, -0.5, -0.5, 0.5, 0.5, -0.4} local door_top = { @@ -77,8 +78,8 @@ for i = 1, 2 do on_construct = function(pos) locks:lock_init( pos, "size[8,2]".. - "field[0.3,0.6;6,0.7;locks_sent_lock_command;Locked door. Type /help for help:;]".. - "button_exit[6.3,1.2;1.7,0.7;locks_sent_input;Proceed]" ); + locks.uniform_background.. + "button_exit[6.3,1.2;1.7,1;locks_sent_input;Proceed]" ); end, on_receive_fields = function(pos, formname, fields, sender) @@ -110,8 +111,8 @@ for i = 1, 2 do on_construct = function(pos) locks:lock_init( pos, "size[8,2]".. - "field[0.3,0.6;6,0.7;locks_sent_lock_command;Locked door. Type /help for help:;]".. - "button_exit[6.3,0.2;1.7,0.7;locks_sent_input;Proceed]" ); + locks.uniform_background.. + "button_exit[6.3,1.2;1.7,1;locks_sent_input;Proceed]" ); end, on_receive_fields = function(pos, formname, fields, sender)