diff options
-rw-r--r-- | .luacheckrc | 9 | ||||
-rw-r--r-- | init.lua | 113 |
2 files changed, 69 insertions, 53 deletions
diff --git a/.luacheckrc b/.luacheckrc new file mode 100644 index 0000000..97738ca --- /dev/null +++ b/.luacheckrc @@ -0,0 +1,9 @@ +max_line_length = 160 + +globals = { + "newplayer", +} + +read_globals = { + "minetest", +} @@ -1,18 +1,10 @@ newplayer = {} -if type(minetest.colorize) == "function" then - newplayer.colorize = minetest.colorize -else - newplayer.colorize = function(color,text) - return text - end -end - -local f = io.open(minetest.get_worldpath()..DIR_DELIM.."newplayer-keywords.txt","r") -if f then - local d = f:read("*all") +local keywordfile = io.open(minetest.get_worldpath().."/newplayer-keywords.txt","r") +if keywordfile then + local d = keywordfile:read("*all") newplayer.keywords = minetest.deserialize(d) - f:close() + keywordfile:close() else newplayer.keywords = {} end @@ -21,29 +13,30 @@ newplayer.assigned_keywords = {} newplayer.hudids = {} -local f = io.open(minetest.get_worldpath()..DIR_DELIM.."newplayer-rules.txt","r") -if f then - local d = f:read("*all") +local rulesfile = io.open(minetest.get_worldpath().."/newplayer-rules.txt","r") +if rulesfile then + local d = rulesfile:read("*all") newplayer.rules = minetest.formspec_escape(d) - f:close() + rulesfile:close() else - newplayer.rules = "Rules file not found!\n\nThe file should be named \"newplayer-rules.txt\" and placed in the following location:\n\n"..minetest.get_worldpath()..DIR_DELIM + local worldpath = minetest.formspec_escape(minetest.get_worldpath()) + newplayer.rules = "Rules file not found!\n\nThe file should be named \"newplayer-rules.txt\" and placed in the following location:\n\n"..worldpath end function newplayer.savekeywords() - local f = io.open(minetest.get_worldpath()..DIR_DELIM.."newplayer-keywords.txt","w") + local f = io.open(minetest.get_worldpath().."/newplayer-keywords.txt","w") local d = minetest.serialize(newplayer.keywords) f:write(d) f:close() end local editformspec1 = "size[13,9]".. - "label[0,-0.1;Editing Server Rules]".. - "textarea[0.25,0.5;12.5,7;rules;;" + "label[0,-0.1;Editing Server Rules]".. + "textarea[0.25,0.5;12.5,7;rules;;" -- the rules get inserted between these two on demand local editformspec2 = "]".. - "button_exit[0.5,8.1;2,1;save;Save]".. - "button_exit[5,8.1;2,1;quit;Cancel]" + "button_exit[0.5,8.1;2,1;save;Save]".. + "button_exit[5,8.1;2,1;quit;Cancel]" function newplayer.showrulesform(name) @@ -56,10 +49,11 @@ function newplayer.showrulesform(name) newplayer.rules_formspec_buffer = "" - while charpos < #newplayer.rules do + while charpos <= #newplayer.rules + 1 do charpos = charpos + 1 linelen = linelen + 1 local c = string.sub(newplayer.rules, charpos, charpos) + if charpos > #newplayer.rules then c = "\n" end if c == " " or c == "\t" or c == "\n" or c == "\r" then lastbreak = charpos end if linelen > 70 or c == "\n" or c == "\r" then newplayer.rules_formspec_buffer = newplayer.rules_formspec_buffer..","..string.sub(newplayer.rules, strstart, lastbreak-1) @@ -70,25 +64,29 @@ function newplayer.showrulesform(name) end end + newplayer.rules_formspec_buffer = string.sub(newplayer.rules_formspec_buffer,2,-1) + if #newplayer.keywords > 0 then newplayer.assigned_keywords[name] = newplayer.keywords[math.random(1,#newplayer.keywords)] - newplayer.rules_subbed = string.gsub(newplayer.rules_formspec_buffer,"@KEYWORD",newplayer.assigned_keywords[name]) + local keyword = minetest.formspec_escape(newplayer.assigned_keywords[name]) + newplayer.rules_subbed = string.gsub(newplayer.rules_formspec_buffer,"@KEYWORD",keyword) else newplayer.rules_subbed = newplayer.rules_formspec_buffer end if #newplayer.keywords > 0 and minetest.check_player_privs(name,{interact=true}) and not minetest.check_player_privs(name,{server=true}) then - newplayer.rules_subbed_interact = string.gsub(newplayer.rules_formspec_buffer,"@KEYWORD",minetest.formspec_escape("[Hidden because you already have interact]")) + local hiddenmsg = minetest.formspec_escape("[Hidden because you already have interact]") + newplayer.rules_subbed_interact = string.gsub(newplayer.rules_formspec_buffer,"@KEYWORD",hiddenmsg) else newplayer.rules_subbed_interact = newplayer.rules_formspec_buffer - end + end local form_interact = "size[13,9]".. - "label[0,-0.1;Server Rules]".. - "textlist[0.25,0.5;12.5,6.25;rules;"..newplayer.rules_subbed_interact.."]" + "label[0,-0.1;Server Rules]".. + "textlist[0.25,0.5;12.5,6.25;rules;"..newplayer.rules_subbed_interact.."]" local form_nointeract = "size[13,9]".. - "label[0,-0.1;Server Rules]".. - "textlist[0.25,0.5;12.5,6.25;rules;"..newplayer.rules_subbed.."]".. - "button[1,8;2,1;yes;I agree]".. - "button[5,8;2,1;no;I do not agree]" + "label[0,-0.1;Server Rules]".. + "textlist[0.25,0.5;12.5,6.25;rules;"..newplayer.rules_subbed.."]".. + "button[1,8;2,1;yes;I agree]".. + "button[5,8;2,1;no;I do not agree]" if #newplayer.keywords > 0 then form_nointeract = form_nointeract.."field[0.5,7.6;8,1;keyword;Enter keyword from rules above:;]" end @@ -131,7 +129,10 @@ minetest.register_on_player_receive_fields(function(player,formname,fields) local name = player:get_player_name() if formname == "newplayer:rules_nointeract" then if fields.yes then - if #newplayer.keywords == 0 or (not newplayer.assigned_keywords[name]) or string.lower(fields.keyword) == string.lower(newplayer.assigned_keywords[name]) then + if #newplayer.keywords == 0 + or (not newplayer.assigned_keywords[name]) + or string.lower(fields.keyword) == string.lower(newplayer.assigned_keywords[name]) + then local privs = minetest.get_player_privs(name) privs.interact = true local extraprivs = minetest.settings:get("newplayer.extra_privs") @@ -151,7 +152,7 @@ minetest.register_on_player_receive_fields(function(player,formname,fields) minetest.chat_send_player(name,"Teleporting to spawn...") player:setpos(spawn) else - minetest.chat_send_player(name,newplayer.colorize("#FF0000","ERROR: ").."The spawn point is not set!") + minetest.chat_send_player(name,minetest.colorize("#FF0000","ERROR: ").."The spawn point is not set!") end local form = "size[5,3]".. "label[1,0;Thank you for agreeing]".. @@ -181,11 +182,11 @@ minetest.register_on_player_receive_fields(function(player,formname,fields) elseif formname == "newplayer:editrules" then if minetest.check_player_privs(name, {server=true}) then if fields.save then - local f = io.open(minetest.get_worldpath()..DIR_DELIM.."newplayer-rules.txt","w") + local f = io.open(minetest.get_worldpath().."/newplayer-rules.txt","w") f:write(fields.rules) f:close() newplayer.rules = minetest.formspec_escape(fields.rules) - minetest.chat_send_player(name,newplayer.colorize("#55FF55","Success: ").."Rules/keyword updated.") + minetest.chat_send_player(name,minetest.colorize("#55FF55","Success: ").."Rules/keyword updated.") end else minetest.chat_send_player(name,"You hacker you... nice try!") @@ -231,9 +232,12 @@ minetest.register_chatcommand("set_no_interact_spawn",{ local player = minetest.get_player_by_name(name) if not player then return end local pos = player:get_pos() - minetest.setting_set("spawnpoint_no_interact",string.format("%s,%s,%s",pos.x,pos.y,pos.z)) - minetest.setting_save() - return true, newplayer.colorize("#55FF55","Success: ").."Spawn point for players without interact set to: "..newplayer.colorize("#00FFFF",minetest.pos_to_string(pos)) + minetest.settings:set("spawnpoint_no_interact",string.format("%s,%s,%s",pos.x,pos.y,pos.z)) + minetest.settings:write() + local message = minetest.colorize("#55FF55","Success: ").. + "Spawn point for players without interact set to: ".. + minetest.colorize("#00FFFF",minetest.pos_to_string(pos)) + return true, message end} ) @@ -245,9 +249,12 @@ minetest.register_chatcommand("set_interact_spawn",{ local player = minetest.get_player_by_name(name) if not player then return end local pos = player:get_pos() - minetest.setting_set("spawnpoint_interact",string.format("%s,%s,%s",pos.x,pos.y,pos.z)) - minetest.setting_save() - return true, newplayer.colorize("#55FF55","Success: ").."Spawn point for players with interact set to: "..newplayer.colorize("#00FFFF",minetest.pos_to_string(pos)) + minetest.settings:set("spawnpoint_interact",string.format("%s,%s,%s",pos.x,pos.y,pos.z)) + minetest.settings:write() + local message = minetest.colorize("#55FF55","Success: ").. + "Spawn point for players with interact set to: ".. + minetest.colorize("#00FFFF",minetest.pos_to_string(pos)) + return true, message end} ) @@ -255,12 +262,12 @@ minetest.register_chatcommand("getkeywords",{ params = "", description = "Gets the list of keywords used to obtain the interact privilege", privs = {server=true}, - func = function(name) - local out = "" + func = function() + local out if #newplayer.keywords > 0 then out = "Currently configured keywords:" for _,kw in pairs(newplayer.keywords) do - out = out.."\n"..newplayer.colorize("#00FFFF",kw) + out = out.."\n"..minetest.colorize("#00FFFF",kw) end else out = "No keywords are currently set." @@ -273,9 +280,9 @@ minetest.register_chatcommand("addkeyword",{ params = "<keyword>", description = "Add a keyword to the list of keywords used to obtain the interact privilege", privs = {server=true}, - func = function(name,param) + func = function(_,param) if (not param) or param == "" then - return true, newplayer.colorize("#FF0000","ERROR: ").."No keyword supplied" + return true, minetest.colorize("#FF0000","ERROR: ").."No keyword supplied" end table.insert(newplayer.keywords,param) newplayer.savekeywords() @@ -287,18 +294,18 @@ minetest.register_chatcommand("delkeyword",{ params = "<keyword>", description = "Remove a keyword from the list of keywords used to obtain the interact privilege", privs = {server=true}, - func = function(name,param) + func = function(_,param) if (not param) or param == "" then - return true, newplayer.colorize("#FF0000","ERROR: ").."No keyword supplied" + return true, minetest.colorize("#FF0000","ERROR: ").."No keyword supplied" end for k,v in pairs(newplayer.keywords) do if v == param then newplayer.keywords[k] = nil newplayer.savekeywords() - return true, "Keyword "..newplayer.colorize("#00FFFF",param).." removed" + return true, "Keyword "..minetest.colorize("#00FFFF",param).." removed" end end - return true, newplayer.colorize("#FF0000","ERROR: ").."Keyword "..newplayer.colorize("#00FFFF",param).." not found" + return true, minetest.colorize("#FF0000","ERROR: ").."Keyword "..minetest.colorize("#00FFFF",param).." not found" end} ) @@ -315,7 +322,7 @@ minetest.register_chatcommand("spawn",{ player:set_pos(pos) return true, "Teleporting to spawn..." else - return true, newplayer.colorize("#FF0000","ERROR: ").."The spawn point is not set!" + return true, minetest.colorize("#FF0000","ERROR: ").."The spawn point is not set!" end else local pos = minetest.setting_get_pos("spawnpoint_no_interact") @@ -323,7 +330,7 @@ minetest.register_chatcommand("spawn",{ player:set_pos(pos) return true, "Teleporting to spawn..." else - return true, newplayer.colorize("#FF0000","ERROR: ").."The spawn point is not set!" + return true, minetest.colorize("#FF0000","ERROR: ").."The spawn point is not set!" end end end} |