merged PR from adrido for nicer formspecs

This commit is contained in:
Sokomine 2018-07-30 22:00:54 +02:00
commit 58b7496dcb
5 changed files with 232 additions and 37 deletions

195
init.lua

@ -18,9 +18,10 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
--]]
-- Version 2.00
-- Version 2.10
-- Changelog:
-- 30.07.2018 * Merged PR from adrido from 25.02.2016: Added new Locks config Formspec.
-- 30.07.2018 * Removed deprecated minetest.env usage.
-- 30.07.2018 * Front side of chest does not get pipeworks image anymore.
-- Instead it always shows the locks texture as overlay.
@ -44,6 +45,108 @@ minetest.register_privilege("openlocks", { description = "allows to open/use all
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_formspec = [[
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]
]]
function locks.access_granted(playername, formname)
minetest.show_formspec(playername, formname, locks.access_granted_formspec);
end
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]
field[0.4,6.5;4.5,0;password;Password:;%s] 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;
if( minetest.get_modpath("pipeworks") ~= nil ) then
@ -81,7 +184,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
@ -310,6 +415,43 @@ function locks:lock_handle_input( pos, formname, fields, player )
return;
end
local name = player:get_player_name();
local owner = meta:get_string("owner");
--first check for locks_config_button
if fields.locks_config then
-- else the player could set a new pw and gain access anyway...
if( owner and owner ~= "" and owner ~= name ) then
minetest.chat_send_player(name, "Only the owner can change the configuration.");
return;
end
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"),
meta:get_string("password"));
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 == "" )
@ -318,8 +460,6 @@ function locks:lock_handle_input( pos, formname, fields, player )
return;
end
name = player:get_player_name();
if( fields.locks_sent_lock_command == "/help" ) then
if( name == meta:get_string( "owner" )) then
@ -561,6 +701,51 @@ function locks:lock_handle_input( pos, formname, fields, player )
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", {
@ -613,5 +798,3 @@ dofile(minetest.get_modpath("locks").."/shared_locked_chest.lua");
dofile(minetest.get_modpath("locks").."/shared_locked_sign_wall.lua");
dofile(minetest.get_modpath("locks").."/shared_locked_xdoors2.lua");
dofile(minetest.get_modpath("locks").."/shared_locked_furnace.lua");

@ -1,3 +1,4 @@
-- 25.02.16 Added new Locks config Buttons.
-- 09.01.13 Added support for pipeworks.
@ -49,16 +50,24 @@ minetest.register_node("locks:shared_locked_chest", {
-- 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]"..
---- "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;]");
-- "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;]");
locks.uniform_background ..
"list[current_name;main;0,1;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,

@ -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,0.5;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,0.5;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",

@ -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,10 @@ 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]");
-- "field[0.3,3.6;6,0.7;locks_sent_lock_command;Locked sign. Type /help for help:;]"..
-- "background[-0.5,-0.5;9,5;bg_shared_locked_sign.jpg]" );
"button_exit[6.3,3.2;1.7,0.7;locks_sent_input;Proceed]"..
locks.uniform_background );
end,
after_place_node = function(pos, placer)
@ -44,10 +46,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 +61,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,
});

@ -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 = {
@ -79,8 +80,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)
@ -112,8 +113,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)