From 811c943512f1661ffdc4fdb55698ae26cf2097cf Mon Sep 17 00:00:00 2001 From: kakalak-lumberJack Date: Sun, 18 Feb 2018 04:12:06 -0500 Subject: [PATCH] Update init.lua Add remove creative area feature. Use "privs" priv as mod's administrative priv. Other minor feature improvements and cleanup. --- init.lua | 62 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/init.lua b/init.lua index 5ee5f82..ea9dec7 100644 --- a/init.lua +++ b/init.lua @@ -1,7 +1,6 @@ local cr_areas_file = minetest.get_worldpath().."/creative_areas.dat" local cr_areas = {} - --functions function load_file(fname) local file, err = io.open(fname, "r") @@ -20,16 +19,36 @@ function write_file(fname, tbl) else minetest.log("ERROR [creative_areas] "..err) end end - +--Adds creative area to list. function make_cr_area(name, areaID) local id = tonumber(areaID) if areas.areas[id] ~= nil then + if cr_areas ~= {} then + for i = 1, #cr_areas do + if cr_areas[i] == id then + return minetest.chat_send_player(name, "Area " ..id.." is already a creative area.") + end + end + end table.insert(cr_areas, id) write_file(cr_areas_file, cr_areas) - minetest.chat_send_player(name, "Area added to Creative Areas!") + minetest.chat_send_player(name, "Area added to Creative Areas!") else minetest.chat_send_player(name, "Not a valid area ID") end end +--Removes Creative Area +function rm_cr_area(name, areaID) + local id = tonumber(areaID) + for i = 1, #cr_areas do + if cr_areas[i] == id then + table.remove(cr_areas, i) + write_file(cr_areas_file, cr_areas) + return minetest.chat_send_player(name, "Creative area removed!") + end + end + return minetest.chat_send_player(name, "Not a creative area ID") +end + function check_cr_area(player) local pos = player:get_pos() @@ -38,9 +57,13 @@ function check_cr_area(player) if #cr_areas >= 1 then for i = 1, #cr_areas do local areaID = cr_areas[i] + -- Clean up creative areas which are have been deleted from Areas mod + if areas.areas[areaID] == nil then + table.remove(cr_areas, i) + end + -- Compare Areas which player are in with Creative Area. Grant/revoke creative priv accordingly." for _, in_area in pairs(area_at_pos) do - --if in_area["pos1"] ~= nil - if in_area["pos1"] == areas.areas[areaID]["pos1"] + if in_area["pos1"] == areas.areas[areaID]["pos1"] --make sure the areas are not just the same name. and in_area["name"] == areas.areas[areaID]["name"] then status = true end @@ -50,18 +73,7 @@ function check_cr_area(player) return status end -local function on_grant_revoke(grantee, granter, priv) - if priv == "creative" then - local player = mientest.get_player_by_name(grantee) - if player then - sfinv.set_player_inventory_formspec(player, context) - end - end -end - - --Initialize mod -minetest.register_privilege("teacher", "Give access to teacher features.") if cr_areas_file ~= nil then load_file(cr_areas_file) @@ -71,22 +83,30 @@ end minetest.register_chatcommand("creative_area", { description = "Sets area to grant players creative priv while inside it", params = "", - privs = {teacher = true}, + privs = {privs = true}, func = function(name, param) make_cr_area(name, param) end }) +minetest.register_chatcommand("rm_creative_area", { + description = "Revokes area from list of creative areas", + params = "", + privs = {privs = true}, + func = function(name, param) + rm_cr_area(name, param) + end +}) -- Check location and Grant/revoke creative priv local timer = 0 minetest.register_globalstep(function(dtime) timer = timer + dtime - if timer >= math.random(1,3) then + if timer >= math.random(1,3) then for _, player in ipairs(minetest.get_connected_players()) do local pname = player:get_player_name() local privs = minetest.get_player_privs(pname) - --if minetest.get_player_privs(pname).teacher == nil then + if minetest.get_player_privs(pname).privs == nil then --Players with the "privs" priv will not have privileges effected. if check_cr_area(player) == true then if not minetest.check_player_privs(pname, {creative = true}) then privs.creative = true @@ -100,10 +120,10 @@ minetest.register_globalstep(function(dtime) minetest.set_player_privs(pname, privs) local context = {page = sfinv.get_homepage_name(player)} sfinv.set_player_inventory_formspec(player, context) - minetest.chat_send_player(pname, "Leaving creative area.") + minetest.chat_send_player(pname, "You have left creative area.") end end - --end + end end timer = 0 end