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.

142 lines
4.2 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
5 years ago
--[[
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
5 years ago
--]]
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, new_privs_table)
local name = player:get_player_name()
local has_creative = minetest.check_player_privs(name, {creative=true})
5 years ago
local has_arena = minetest.check_player_privs(name, {arena=true})
5 years ago
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)
5 years ago
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
5 years ago
if new_mode ~= old_mode then
if old_mode == "arena" then
-- leaving arena (alive)
creative_regions.drop_everything(player, false)
end
5 years ago
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
5 years ago
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
5 years ago
end
end
local has_privs = minetest.get_player_privs(name)
local privs = has_privs
5 years ago
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