From 01e3943e7f3ffc1dcf7e002d47ba6a16c5c2f12b Mon Sep 17 00:00:00 2001 From: cheapie Date: Tue, 17 May 2016 21:55:58 -0500 Subject: Add keyword, editor, and @KEYWORD substitution --- init.lua | 203 ++++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 157 insertions(+), 46 deletions(-) diff --git a/init.lua b/init.lua index 5b4cad2..84e8b2e 100644 --- a/init.lua +++ b/init.lua @@ -1,5 +1,8 @@ newplayer = {} +newplayer.keyword = minetest.setting_get("interact_keyword") +if newplayer.keyword == "" then newplayer.keyword = nil end + newplayer.hudids = {} local f = io.open(minetest.get_modpath("newplayer")..DIR_DELIM.."rules.txt","r") @@ -12,17 +15,31 @@ else end function newplayer.showrulesform(name) - local form_interact = "size[8,9]".. + if newplayer.keyword then + newplayer.rules_subbed = string.gsub(newplayer.rules,"@KEYWORD",newplayer.keyword) + else + newplayer.rules_subbed = newplayer.rules + end + local form_interact = "size[8,10]".. "label[0,0;Server Rules]".. - "textarea[0.25,1;8,7;rules;;"..newplayer.rules.."]".. - "button_exit[3,8;2,1;quit;OK]" - local form_nointeract = "size[8,9]".. + "textarea[0.25,1;8,7;rules;;"..newplayer.rules.."]" + local form_nointeract = "size[8,10]".. "label[0,0;Server Rules]".. - "textarea[0.25,1;8,7;rules;;"..newplayer.rules.."]".. - "button[1,8;2,1;yes;I agree]".. - "button[5,8;2,1;no;I do not agree]" + "textarea[0.25,1;8,7;rules;;"..newplayer.rules_subbed.."]".. + "button[1,9;2,1;yes;I agree]".. + "button[5,9;2,1;no;I do not agree]" + if newplayer.keyword then + form_nointeract = form_nointeract.."field[0.25,8;8,1;keyword;Enter keyword from rules above\:;]" + end local hasinteract = minetest.check_player_privs(name,{interact=true}) if hasinteract then + if minetest.check_player_privs(name,{server=true}) then + form_interact = form_interact.."button_exit[1,9;2,1;quit;OK]" + form_interact = form_interact.."button[5,9;2,1;edit;Edit]" + form_interact = form_interact.."field[0.25,8;8,1;keyword;Keyword\:;"..(newplayer.keyword or "").."]" + else + form_interact = form_interact.."button_exit[3,9;2,1;quit;OK]" + end minetest.show_formspec(name,"newplayer:rules_interact",form_interact) else minetest.show_formspec(name,"newplayer:rules_nointeract",form_nointeract) @@ -47,47 +64,92 @@ minetest.register_on_joinplayer(function(player) alignment = {x=0,y=0}, offset = {x=0,y=0} }) - minetest.after(3,newplayer.showrulesform,name) + minetest.after(0,newplayer.showrulesform,name) end) minetest.register_on_player_receive_fields(function(player,formname,fields) - if formname ~= "newplayer:rules_nointeract" then - return false - end local name = player:get_player_name() - if fields.quit then - newplayer.showrulesform(name) - elseif fields.yes then - local privs = minetest.get_player_privs(name) - privs.interact = true - minetest.set_player_privs(name,privs) - if newplayer.hudids[name] then - minetest.get_player_by_name(name):hud_remove(newplayer.hudids[name]) - newplayer.hudids[name] = nil + if formname == "newplayer:rules_nointeract" then + if fields.quit then + newplayer.showrulesform(name) + elseif fields.yes then + if not newplayer.keyword or string.lower(fields.keyword) == string.lower(newplayer.keyword) then + local privs = minetest.get_player_privs(name) + privs.interact = true + minetest.set_player_privs(name,privs) + if newplayer.hudids[name] then + minetest.get_player_by_name(name):hud_remove(newplayer.hudids[name]) + minetest.get_player_by_name(name):hud_remove(newplayer.hudids[name]-1) + newplayer.hudids[name] = nil + end + local spawn = minetest.setting_get_pos("spawnpoint_interact") + if spawn then + minetest.chat_send_player(name,"Teleporting to spawn...") + player:setpos(spawn) + else + minetest.chat_send_player(name,"ERROR: The spawn point is not set!") + end + local form = "size[5,3]".. + "label[1,0;Thank you for agreeing]".. + "label[1,0.5;to the rules!]".. + "label[1,1;You are now free to play normally.]".. + "label[1,1.5;You can also use /spawn to return here.]".. + "button_exit[1.5,2;2,1;quit;OK]" + minetest.show_formspec(name,"newplayer:agreethanks",form) + else + local form = "size[5,3]".. + "label[1,0;Incorrect keyword!]".. + "button[1.5,2;2,1;quit;Try Again]" + minetest.show_formspec(name,"newplayer:tryagain",form) + end + elseif fields.no then + local form = "size[5,3]".. + "label[1,0;You may remain on the server\,]".. + "label[1,0.5;but you may not dig or build]".. + "label[1,1;until you agree to the rules.]".. + "button_exit[1.5,2;2,1;quit;OK]" + minetest.show_formspec(name,"newplayer:disagreewarning",form) end - local spawn = minetest.setting_get_pos("spawnpoint_interact") - if spawn then - minetest.chat_send_player(name,"Teleporting to spawn...") - player:setpos(spawn) + return true + elseif formname == "newplayer:tryagain" then + newplayer.showrulesform(name) + return true + elseif formname == "newplayer:editrules" then + if minetest.check_player_privs(name, {server=true}) then + if fields.save then + local f = io.open(minetest.get_modpath("newplayer")..DIR_DELIM.."rules.txt","w") + f:write(fields.rules) + f:close() + newplayer.rules = fields.rules + if fields.keyword ~= "" then + newplayer.keyword = fields.keyword + minetest.setting_set("interact_keyword",fields.keyword) + minetest.setting_save() + else + newplayer.keyword = nil + minetest.setting_set("interact_keyword","") + minetest.setting_save() + end + minetest.chat_send_player(name,"Rules/keyword updated successfully.") + end else - minetest.chat_send_player(name,"ERROR: The spawn point is not set!") + minetest.chat_send_player(name,"You hacker you... nice try!") end - local form = "size[5,3]".. - "label[1,0;Thank you for agreeing]".. - "label[1,0.5;to the rules!]".. - "label[1,1;You are now free to play normally.]".. - "label[1,1.5;You can also use /spawn to return here.]".. - "button_exit[1.5,2;2,1;quit;OK]" - minetest.show_formspec(name,"newplayer:agreethanks",form) - elseif fields.no then - local form = "size[5,3]".. - "label[1,0;You may remain on the server\,]".. - "label[1,0.5;but you may not dig or build]".. - "label[1,1;until you agree to the rules.]".. - "button_exit[1.5,2;2,1;quit;OK]" - minetest.show_formspec(name,"newplayer:disagreewarning",form) + elseif formname == "newplayer:rules_interact" then + if fields.edit and minetest.check_player_privs(name,{server=true}) then + local form = "size[8,10]".. + "label[0,0;Editing Server Rules]".. + "textarea[0.25,1;8,7;rules;;"..newplayer.rules.."]".. + "button_exit[1,9;2,1;save;Save]".. + "button_exit[5,9;2,1;quit;Cancel]".. + "field[0.25,8;8,1;keyword;Keyword\:;"..(newplayer.keyword or "").."]" + minetest.show_formspec(name,"newplayer:editrules",form) + end + elseif formname == "newplayer:agreethanks" or formname == "newplayer:disagreewarning" then + return true + else + return false end - return true end) minetest.register_chatcommand("rules",{ @@ -97,6 +159,22 @@ minetest.register_chatcommand("rules",{ } ) +minetest.register_chatcommand("editrules",{ + params = "", + description = "Edit the rules", + privs = {server=true}, + func = function(name) + local form = "size[8,10]".. + "label[0,0;Editing Server Rules]".. + "textarea[0.25,1;8,7;rules;;"..newplayer.rules.."]".. + "button_exit[1,9;2,1;save;Save]".. + "button_exit[5,9;2,1;quit;Cancel]".. + "field[0.25,8;8,1;keyword;Keyword\:;"..(newplayer.keyword or "").."]" + minetest.show_formspec(name,"newplayer:editrules",form) + return true + end} +) + minetest.register_chatcommand("set_no_interact_spawn",{ params = "", description = "Set the spawn point for players without interact to your current position", @@ -105,7 +183,7 @@ minetest.register_chatcommand("set_no_interact_spawn",{ local pos = minetest.get_player_by_name(name):getpos() minetest.setting_set("spawnpoint_no_interact",string.format("%s,%s,%s",pos.x,pos.y,pos.z)) minetest.setting_save() - minetest.chat_send_player(name,"Spawn point for players without interact set to: "..minetest.pos_to_string(pos)) + return true, "Spawn point for players without interact set to: "..minetest.pos_to_string(pos) end} ) @@ -117,7 +195,40 @@ minetest.register_chatcommand("set_interact_spawn",{ local pos = minetest.get_player_by_name(name):getpos() minetest.setting_set("spawnpoint_interact",string.format("%s,%s,%s",pos.x,pos.y,pos.z)) minetest.setting_save() - minetest.chat_send_player(name,"Spawn point for players with interact set to: "..minetest.pos_to_string(pos)) + return true, "Spawn point for players with interact set to: "..minetest.pos_to_string(pos) + end} +) + +minetest.register_chatcommand("set_keyword",{ + params = "[keyword]", + description = "Set the keyword used to get interact", + privs = {server=true}, + func = function(name,param) + if param and param ~= "" then + newplayer.keyword = param + minetest.setting_set("interact_keyword",param) + minetest.setting_save() + return true, "Keyword set to: "..param + else + newplayer.keyword = nil + minetest.setting_set("interact_keyword","") + minetest.setting_save() + return true, "Keyword cleared." + end + end} +) + +minetest.register_chatcommand("get_keyword",{ + params = "", + description = "Shows the keyword used to get interact", + privs = {server=true}, + func = function(name) + local kw = newplayer.keyword + if kw then + return true, "Keyword is: "..kw + else + return true, "No keyword is currently set." + end end} ) @@ -130,18 +241,18 @@ minetest.register_chatcommand("spawn",{ if hasinteract then local pos = minetest.setting_get_pos("spawnpoint_interact") if pos then - minetest.chat_send_player(name,"Teleporting to spawn...") player:setpos(pos) + return true, "Teleporting to spawn..." else - minetest.chat_send_player(name,"ERROR: The spawn point is not set!") + return true, "ERROR: The spawn point is not set!" end else local pos = minetest.setting_get_pos("spawnpoint_no_interact") if pos then - minetest.chat_send_player(name,"Teleporting to spawn...") player:setpos(pos) + return true, "Teleporting to spawn..." else - minetest.chat_send_player(name,"ERROR: The spawn point is not set!") + return true, "ERROR: The spawn point is not set!" end end end} -- cgit v1.2.3