Minetest mod: Toggle privs in areas
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.7 KiB

-- creative_regions mod by h3ndrik
5 years ago
5 years ago
local S = minetest.get_translator("creative_regions")
creative_regions = {}
5 years ago
areas_interact_privs = minetest.settings:get("creative_regions.interact_privs") or "+creative, +fast, +fly"
5 years ago
areas_guest_privs = minetest.settings:get("creative_regions.guest_privs") or ""
5 years ago
noregion_privs = minetest.settings:get("creative_regions.noregion_privs") or "-creative, +fast, -fly"
default_privs = minetest.settings:get("creative_regions.default_privs") or "+creative, +fast, +fly"
5 years ago
5 years ago
non_sticky_privs = {"arena"} -- always remove areas priv when not explicitly requested by current area
5 years ago
creative_regions.modpath = minetest.get_modpath("creative_regions")
dofile(creative_regions.modpath.."/region.lua")
dofile(creative_regions.modpath.."/player.lua")
dofile(creative_regions.modpath.."/chatcommands.lua")
5 years ago
dofile(creative_regions.modpath.."/items.lua")
dofile(creative_regions.modpath.."/mod_areas.lua")
5 years ago
dofile(creative_regions.modpath.."/arena.lua")
3 years ago
dofile(creative_regions.modpath.."/nopvp.lua")
dofile(creative_regions.modpath.."/vehicles.lua")
5 years ago
function creative_regions:update_player(player, initialize)
5 years ago
local name = player:get_player_name()
local pos = vector.round(player:get_pos())
local pmeta = player:get_meta()
5 years ago
-- pmeta:mark_as_private("creative_regions.active_regions")
local current_regions_size = {}
local player_active_region_str = ""
5 years ago
local areas_owner = false
local areas_guest = false
local areas_open = false
local areas_faction_open = false
local areas_individual_override = false
initialize = initialize or 0
5 years ago
-- skip for players with protection_bypass
5 years ago
if minetest.check_player_privs(name, { protection_bypass=true }) then
return
end
-- find active regions
local current_regions = creative_regions.astore:get_areas_for_pos(pos, true, true)
for astore_id, astore_area in pairs( current_regions ) do
local id = "r"..tostring(astore_id)
local size = creative_regions:get_region_size(astore_area)
table.insert(current_regions_size, { ["id"]=id, ["size"]=size })
5 years ago
end
-- find active areas from areas mod
5 years ago
if minetest.get_modpath("areas") then
local current_areas = areas:getAreasAtPos(pos)
for areas_id, areas_area in pairs( current_areas ) do
local id = "a"..tostring(areas_id)
local size = creative_regions:get_areas_area_size(areas_area)
table.insert(current_regions_size, { ["id"]=id, ["size"]=size })
if areas_area.owner == name then
5 years ago
areas_owner = true
elseif areas_area.open then
areas_open = true
elseif minetest.get_modpath("playerfactions") and areas_area.faction_open then
for _, fname in ipairs(areas_area.faction_open or {}) do
if factions.player_is_in_faction(fname, name) then
areas_faction_open = true
end
end
5 years ago
else
areas_guest = true
5 years ago
--continue
5 years ago
end
end
end
-- calculate privs starting with biggest region and overwriting conscutively
local new_privs_table = {}
creative_regions:add_privs_from_string(new_privs_table, noregion_privs)
table.sort(current_regions_size, function(a,b) return a["size"] > b["size"] end)
for _, region in ipairs(current_regions_size) do
player_active_region_str = player_active_region_str .. "," .. region.id
if creative_regions.region_privs[region.id] then
creative_regions:add_privs_from_string(new_privs_table, creative_regions.region_privs[region.id])
if string.sub(region.id, 1, 1) == "a" then
areas_individual_override = true
end
5 years ago
elseif string.sub(region.id, 1, 1) == "r" then -- region
creative_regions:add_privs_from_string(new_privs_table, default_privs)
5 years ago
elseif string.sub(region.id, 1, 1) == "a" then -- areas mod
if not areas_individual_override then
creative_regions:add_privs_from_string(new_privs_table, areas_guest_privs)
end
end
5 years ago
end
-- overwrite
if areas_owner or areas_open or areas_faction_open then
creative_regions:add_privs_from_string(new_privs_table, areas_interact_privs)
creative_regions:add_privs_from_string(new_privs_table, "-arena")
5 years ago
end
-- did the regions change? update
5 years ago
-- ToDo: Update on privileges changed
local player_last_active_region_str = pmeta:get_string("creative_regions.active_regions") or ""
3 years ago
if player_active_region_str ~= player_last_active_region_str or initialize == 1 then
creative_regions:player_enter_region(player, new_privs_table, initialize)
pmeta:set_string("creative_regions.active_regions", player_active_region_str)
5 years ago
end
-- print(dump(pmeta:to_table()))
end
creative_regions.load_regions()
5 years ago
minetest.register_on_joinplayer(function(player)
creative_regions:update_player(player, 1)
end)
5 years ago
local timer = 0
minetest.register_globalstep(function(dtime)
timer = timer + dtime
if timer >= 1 then
for _, player in pairs(minetest.get_connected_players()) do
creative_regions:update_player(player)
5 years ago
end
end
end)