Update init.lua

Add remove creative area feature. Use "privs" priv as mod's administrative priv. Other minor feature improvements and cleanup.
This commit is contained in:
kakalak-lumberJack 2018-02-18 04:12:06 -05:00 committed by GitHub
parent 01c4432148
commit 811c943512
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,6 @@
local cr_areas_file = minetest.get_worldpath().."/creative_areas.dat" local cr_areas_file = minetest.get_worldpath().."/creative_areas.dat"
local cr_areas = {} local cr_areas = {}
--functions --functions
function load_file(fname) function load_file(fname)
local file, err = io.open(fname, "r") local file, err = io.open(fname, "r")
@ -20,16 +19,36 @@ function write_file(fname, tbl)
else minetest.log("ERROR [creative_areas] "..err) else minetest.log("ERROR [creative_areas] "..err)
end end
end end
--Adds creative area to list.
function make_cr_area(name, areaID) function make_cr_area(name, areaID)
local id = tonumber(areaID) local id = tonumber(areaID)
if areas.areas[id] ~= nil then 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) table.insert(cr_areas, id)
write_file(cr_areas_file, cr_areas) 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") else minetest.chat_send_player(name, "Not a valid area ID")
end end
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) function check_cr_area(player)
local pos = player:get_pos() local pos = player:get_pos()
@ -38,9 +57,13 @@ function check_cr_area(player)
if #cr_areas >= 1 then if #cr_areas >= 1 then
for i = 1, #cr_areas do for i = 1, #cr_areas do
local areaID = cr_areas[i] 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 for _, in_area in pairs(area_at_pos) do
--if in_area["pos1"] ~= nil if in_area["pos1"] == areas.areas[areaID]["pos1"] --make sure the areas are not just the same name.
if in_area["pos1"] == areas.areas[areaID]["pos1"]
and in_area["name"] == areas.areas[areaID]["name"] then and in_area["name"] == areas.areas[areaID]["name"] then
status = true status = true
end end
@ -50,18 +73,7 @@ function check_cr_area(player)
return status return status
end 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 --Initialize mod
minetest.register_privilege("teacher", "Give access to teacher features.")
if cr_areas_file ~= nil then if cr_areas_file ~= nil then
load_file(cr_areas_file) load_file(cr_areas_file)
@ -71,22 +83,30 @@ end
minetest.register_chatcommand("creative_area", { minetest.register_chatcommand("creative_area", {
description = "Sets area to grant players creative priv while inside it", description = "Sets area to grant players creative priv while inside it",
params = "<AreaID>", params = "<AreaID>",
privs = {teacher = true}, privs = {privs = true},
func = function(name, param) func = function(name, param)
make_cr_area(name, param) make_cr_area(name, param)
end end
}) })
minetest.register_chatcommand("rm_creative_area", {
description = "Revokes area from list of creative areas",
params = "<AreaID>",
privs = {privs = true},
func = function(name, param)
rm_cr_area(name, param)
end
})
-- Check location and Grant/revoke creative priv -- Check location and Grant/revoke creative priv
local timer = 0 local timer = 0
minetest.register_globalstep(function(dtime) minetest.register_globalstep(function(dtime)
timer = timer + 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 for _, player in ipairs(minetest.get_connected_players()) do
local pname = player:get_player_name() local pname = player:get_player_name()
local privs = minetest.get_player_privs(pname) 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 check_cr_area(player) == true then
if not minetest.check_player_privs(pname, {creative = true}) then if not minetest.check_player_privs(pname, {creative = true}) then
privs.creative = true privs.creative = true
@ -100,10 +120,10 @@ minetest.register_globalstep(function(dtime)
minetest.set_player_privs(pname, privs) minetest.set_player_privs(pname, privs)
local context = {page = sfinv.get_homepage_name(player)} local context = {page = sfinv.get_homepage_name(player)}
sfinv.set_player_inventory_formspec(player, context) 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 end
end end
timer = 0 timer = 0
end end