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.

167 lines
5.3 KiB

function creative_regions:initial_stuff()
local stuff_string = minetest.settings:get("initial_stuff") or
"default:pick_steel,default:axe_steel,default:shovel_steel," ..
"default:torch 99,default:cobble 99"
local itemtable = {}
local items = stuff_string:split(",")
for _, stack in ipairs(items) do
table.insert(itemtable, ItemStack(stack))
end
return itemtable
end
--[[
function creative_regions:save_player_privs(player)
local name = player:get_player_name()
local pmeta = player:get_meta()
local privs = minetest.get_player_privs(name)
pmeta:set_string("creative_regions.saved_privs", minetest.serialize(privs))
end
function creative_regions:restore_player_privs(player)
local name = player:get_player_name()
local pmeta = player:get_meta()
local privs = minetest.deserialize(pmeta:get_string("creative_regions.saved_privs"))
if privs then
minetest.set_player_privs(name, privs)
pmeta:set_string("creative_regions.saved_privs", nil)
end
end
--]]
function creative_regions:save_player_inventory(player, listname)
local name = player:get_player_name()
local pmeta = player:get_meta()
local inv = player:get_inventory()
local player_items = inv:get_list("main")
inv:set_list(listname, player_items)
end
function creative_regions:restore_player_inventory(player, listname)
local name = player:get_player_name()
local pmeta = player:get_meta()
local inv = player:get_inventory()
local player_items = inv:get_list(listname)
local give_initial = false
if not player_items then
player_items = {}
give_initial = true
end
inv:set_list("main", player_items)
if give_initial then
local initial_items = creative_regions:initial_stuff()
for _, stack in ipairs(initial_items) do
inv:add_item("main", stack)
end
end
end
local DAYNIGHT_AUTO = -1
local DAYNIGHT_UNSET = -2
function creative_regions:save_player_day_night_ratio(player)
local current_day_night_ratio = player:get_day_night_ratio()
if current_day_night_ratio == nil then current_day_night_ratio = DAYNIGHT_AUTO end
local pmeta = player:get_meta()
pmeta:set_int("creative_regions_day_night_ratio", current_day_night_ratio)
end
function creative_regions:restore_player_day_night_ratio(player)
local pmeta = player:get_meta()
local current_day_night_ratio = player:get_day_night_ratio()
local saved_day_night_ratio = pmeta:get_int("creative_region_day_night_ratio")
if saved_day_night_ratio ~= nil and saved_day_night_ratio ~= DAYNIGHT_UNSET then
if saved_day_night_ratio == DAYNIGHT_AUTO then saved_day_night_ratio = nil end
player:override_day_night_ratio(saved_day_night_ratio)
pmeta:set_int("creative_regions_day_night_ratio", DAYNIGHT_UNSET)
end
return current_day_night_ratio
end
function creative_regions:player_enter_region(player, new_privs_table, initialize)
local name = player:get_player_name()
local has_creative = minetest.check_player_privs(name, {creative=true})
local has_arena = minetest.check_player_privs(name, {arena=true})
initialize = initialize or 0
local old_mode = nil
if has_arena then
old_mode = "arena"
elseif has_creative then
old_mode = "creative"
else
old_mode = "survival"
end
local privs_grant, privs_revoke = creative_regions:decode_privs_table(new_privs_table)
local new_mode = nil
if privs_grant.arena and not privs_revoke.arena then
new_mode = "arena"
elseif privs_grant.creative and not privs_revoke.creative then
new_mode = "creative"
else
new_mode = "survival"
end
if minetest.get_modpath("vehicles") then
if privs_revoke.vehicles and not privs_grant.vehicles then
local attached_to = player:get_attach()
if attached_to and attached_to:get_luaentity() then
local entity = attached_to:get_luaentity()
if entity ~= nil and entity.driver then
minetest.chat_send_player(name, "attached to something")
vehicles.object_detach(entity, entity.driver, {x=1, y=0, z=1})
vehicles.explodinate(entity, 5)
entity.object:remove()
end
end
end
end
if new_mode ~= old_mode or initialize == 1 then
if old_mode == "arena" then
-- leaving arena (alive)
creative_regions.drop_everything(player, false)
end
minetest.chat_send_player(name, "You entered "..new_mode.." mode")
creative_regions:save_player_inventory(player, "saved_"..old_mode)
creative_regions:restore_player_inventory(player, "saved_"..new_mode)
if new_mode == "creative" then
if minetest.get_modpath("unified_inventory") then
unified_inventory.set_inventory_formspec(player, unified_inventory.default)
end
if minetest.get_modpath("hunger_ng") then
-- player:get_meta():set_string("hunger_ng:hunger_disabled", 1)
hunger_ng.configure_hunger(name, 'disable')
end
creative_regions:restore_player_day_night_ratio(player)
elseif new_mode == "arena" then
local inv = player:get_inventory()
inv:set_list("main", {})
elseif new_mode == "survival" then
if minetest.get_modpath("hunger_ng") then
hunger_ng.configure_hunger(name, 'enable')
end
creative_regions:save_player_day_night_ratio(player)
player:override_day_night_ratio(nil)
end
end
local has_privs = minetest.get_player_privs(name)
local privs = has_privs
for _, p in pairs(non_sticky_privs) do
privs[p] = nil
end
for newpriv, _ in pairs(privs_grant) do
privs[newpriv] = true
end
for newpriv, _ in pairs(privs_revoke) do
privs[newpriv] = nil
end
minetest.set_player_privs(name, privs)
end