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.

86 lines
2.9 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
function creative_regions:player_enter_region(player, privstring)
local name = player:get_player_name()
local has_creative = minetest.check_player_privs(name, {creative=true})
local privs_grant, privs_revoke = creative_regions:decode_privs_string(privstring)
if not has_creative and privs_grant.creative and not privs_revoke.creative then
minetest.chat_send_player(name, "You entered creative mode")
creative_regions:save_player_inventory(player, "saved_survival")
creative_regions:restore_player_inventory(player, "saved_creative")
if minetest.get_modpath("unified_inventory") then
unified_inventory.set_inventory_formspec(player, unified_inventory.default)
end
elseif has_creative and privs_revoke.creative then
minetest.chat_send_player(name, "You entered survival mode")
creative_regions:save_player_inventory(player, "saved_creative")
creative_regions:restore_player_inventory(player, "saved_survival")
end
local has_privs = minetest.get_player_privs(name)
local privs = has_privs
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