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.

122 lines
4.0 KiB

if minetest.get_modpath("areas") then
5 years ago
function creative_regions.protector_set_area_privs(pos, placer, itemstack, pointed_thing, privstring, description)
if not pos or not placer then
return
end
local meta = minetest.get_meta(pos)
local name = placer:get_player_name();
local player_owns_id = nil
local current_areas = areas:getAreasAtPos(pos)
for areas_id, areas_area in pairs( current_areas ) do
if areas_area.owner == name then
player_owns_id = "a"..tostring(areas_id)
break
end
end
if player_owns_id then
if creative_regions.region_privs[player_owns_id] then
minetest.chat_send_player(name, "[creative_regions] Failed. Privileges for area already set.")
minetest.after(0.2, function(pos)
minetest.remove_node(pos)
end, pos)
return
end
5 years ago
meta:set_string("infotext", string.format(description.." for area %s owned by %s", player_owns_id, name))
meta:set_string("area_id", player_owns_id)
5 years ago
meta:set_string("privstring", privstring)
meta:set_string("owner", name)
creative_regions.set_region_privs(tostring(player_owns_id), privstring)
5 years ago
minetest.chat_send_player(name, "[creative_regions] "..description.." set for region " .. player_owns_id)
creative_regions.save_regions()
else
5 years ago
minetest.chat_send_player(name, "[creative_regions] You need to be inside an area to set a "..description)
minetest.after(0.2, function(pos)
minetest.remove_node(pos)
end, pos)
return
end
end
function creative_regions.protector_can_dig(pos, player)
if not pos or not player then
return true
end
local name = player:get_player_name()
local meta = minetest.get_meta(pos)
local id = meta:get_string("area_id")
5 years ago
local privstring = meta:get_string("privstring")
local owner = meta:get_string("owner")
local player_owns_id = nil
5 years ago
-- allow for players with areas privilege
if minetest.check_player_privs(name, { areas=true }) then
return true
end
local current_areas = areas:getAreasAtPos(pos)
for areas_id, areas_area in pairs( current_areas ) do
if areas_area.owner == name then
player_owns_id = "a"..tostring(areas_id)
break
end
end
if player_owns_id then
return true
elseif owner == name then
return true
else
5 years ago
minetest.chat_send_player(name, "[creative_regions] Cannot remove protector block for area "..id.." owned by "..owner)
return false
end
end
5 years ago
function creative_regions.protector_remove_area_privs(pos, oldnode, oldmetadata, digger, description)
if not oldmetadata or not oldmetadata.fields then
return
end
local owner = oldmetadata.fields.owner
local stored_id = oldmetadata.fields.area_id
5 years ago
local privstring = oldmetadata.fields.privstring
local name = digger:get_player_name()
local current_areas = areas:getAreasAtPos(pos)
for areas_id, areas_area in pairs( current_areas ) do
local id = "a"..tostring(areas_id)
if stored_id == id and privstring == creative_regions.region_privs[tostring(id)] then
creative_regions.set_region_privs(id, nil)
5 years ago
minetest.chat_send_player(name, "[creative_regions] "..description.." removed for area " .. id)
creative_regions.save_regions()
else
minetest.chat_send_player(name, "[creative_regions] Failed to remove privileges!")
end
end
end
minetest.register_node("creative_regions:protector_nofly", {
5 years ago
description = "No-Fly-Zone protector",
tiles = {"creative_regions_protector_nofly.png"},
5 years ago
tiles = {"default_cobble.png^default_clay_brick.png"},
is_ground_content = true,
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=1},
light_source = 1,
5 years ago
after_place_node = function(pos, placer, itemstack, pointed_thing)
return creative_regions.protector_set_area_privs(pos, placer, itemstack, pointed_thing, "-fast, -fly", "No-Fly-Zone Protector")
end,
can_dig = creative_regions.protector_can_dig,
5 years ago
after_dig_node = function(pos, oldnode, oldmetadata, digger)
return creative_regions.protector_remove_area_privs(pos, oldnode, oldmetadata, digger, "No-Fly-Zone protector")
end
})
--minetest.register_alias("noflyzone", "creative_regions:noflyprotector")
end