Added dig/place allowed areas and formspec fields

master
Pierre-Yves Rollo 2018-04-23 22:24:24 +02:00
parent c127e367e5
commit 2318ee08a2
5 changed files with 115 additions and 25 deletions

View File

@ -99,7 +99,48 @@ function areas:canInteract(pos, name)
local owned = false
for _, area in pairs(self:getAreasAtPos(pos)) do
if area.owner == name or area.open then
if area.owner == name or area.open or area.can_dig or area.can_place
then
return true
else
owned = true
end
end
return not owned
end
-- Checks if the area is diggeable or owned by you
function areas:canDig(pos, name)
local player = minetest.get_player_by_name(name)
local meta = minetest.get_meta(pos)
if minetest.check_player_privs(name, self.adminPrivs) then
return true
end
local owned = false
for _, area in pairs(self:getAreasAtPos(pos)) do
if area.owner == name or area.open or area.can_dig == 'true' then
return true
else
owned = true
end
end
return not owned
end
-- Checks if the area is "placeable" or owned by you
function areas:canPlace(pos, name)
local player = minetest.get_player_by_name(name)
local meta = minetest.get_meta(pos)
if minetest.check_player_privs(name, self.adminPrivs) then
return true
end
local owned = false
for _, area in pairs(self:getAreasAtPos(pos)) do
if area.owner == name or area.open or area.can_place == 'true' then
return true
else
owned = true

View File

@ -8,7 +8,7 @@ minetest.log("action", "[" .. modname .. "] version " .. version .. " loaded.")
areas = {}
areas.adminPrivs = {areas=true}
areas.adminPrivs = {areas=true, teacher=true, mapmaker=true}
areas.startTime = os.clock()
areas.modpath = minetest.get_modpath("areas")

View File

@ -16,3 +16,23 @@ minetest.register_on_protection_violation(function(pos, name)
end
end)
-- Place node restriction
minetest.register_on_placenode(function(pos, _, placer, oldnode)
if not placer then return end
local player_name = placer:get_player_name()
if not areas:canPlace(pos, player_name) then
minetest.swap_node(pos, oldnode)
return true
end
end)
-- Dig node restriction
minetest.register_on_dignode(function(pos, oldnode, digger)
if not digger then return end
local player_name = digger:get_player_name()
if not areas:canDig(pos, player_name) then
minetest.swap_node(pos, oldnode)
return true
end
end)

View File

@ -77,24 +77,23 @@ end
--- Add a area.
-- @return The new area's ID.
function areas:add(datas)
local owner = datas.name
local name = datas.area_name
local pos1 = datas.pos1
local pos2 = datas.pos2
local parent = datas.parent
local timer = datas.timer
function areas:add(data)
local pos1 = data.pos1
local pos2 = data.pos2
-- Strange fields renaming inherited from previous code
-- TODO: Tide that up
data.owner = data.name
data.name = data.area_name
data.area_name = nil
local id = findFirstUnusedIndex(self.areas)
self.areas[id] = {
name = name,
pos1 = pos1,
pos2 = pos2,
owner = owner,
parent = parent,
timer = timer,
}
self.areas[id] = { }
-- TODO: deep copy ?
for name, value in pairs(data) do
self.areas[id][name] = value
end
-- Add to AreaStore
if self.store then
@ -193,11 +192,13 @@ end
-- Also checks the size of the area and if the user already
-- has more than max_areas.
function areas:canPlayerAddArea(pos1, pos2, name)
local privs = minetest.get_player_privs(name)
if privs.areas then
if minetest.check_player_privs(name, self.adminPrivs) then
return true
end
local privs = minetest.get_player_privs(name)
-- Check self protection privilege, if it is enabled,
-- and if the area is too big.
if not self.config.self_protection or

View File

@ -1386,6 +1386,10 @@ if minetest.get_modpath("areas") then
local dd_idx = area_datas[name] and area_datas[name].last_dd_idx or 1
local last_selected_area = area_datas[name].last_selected_area or ""
local timer = areas.areas[dd_idx] and areas.areas[dd_idx].timer or ""
local can_dig = area_datas[name].can_dig == "true" and 2 or 1
local can_place = area_datas[name].can_place == "true" and 2 or 1
local kidsbot_mode = area_datas[name].kidsbot_mode == "free" and 2 or 1
local area_idx = 1
local x = 1
@ -1401,7 +1405,7 @@ if minetest.get_modpath("areas") then
end
names = names:sub(1,-2)
return "size[8,4]" .. worldedit.get_formspec_header("worldedit_gui_protect") ..
return "size[8,6.5]" .. worldedit.get_formspec_header("worldedit_gui_protect") ..
string.format("field[0.3,1.4;4,1;worldedit_gui_protect_name;" ..
S("Area name") .. ";%s]", minetest.formspec_escape(area_name)) ..
string.format("field[4.3,1.4;4,1;worldedit_gui_protect_player_name;" ..
@ -1409,11 +1413,22 @@ if minetest.get_modpath("areas") then
"label[0,2.1;" .. S("Areas:") .. "]" ..
"dropdown[0,2.6;4.1;worldedit_gui_protect_areas;" ..
names .. ";" .. area_idx .. "]" ..
"field[4.3,2.82;4,1;worldedit_gui_protect_chrono;"
.. S("Timer (seconds)") .. ";" .. timer .. "]" ..
"button[0,3.5;2.5,1;worldedit_gui_protect_remove;" .. S("Remove area") .. "]" ..
"button[2.66,3.5;2.5,1;worldedit_gui_protect_add_owner;" .. S("Confirm owner") .. "]" ..
"button[5.33,3.5;2.5,1;worldedit_gui_protect_submit;" .. S("Protect Area") .. "]"
"field[4.3,2.82;4,1;worldedit_gui_protect_chrono;" ..
S("Timer (seconds)") .. ";" .. timer .. "]" ..
"label[0,3.5;" .. S("User actions:") .. "]" ..
"dropdown[0,4;4.1;worldedit_gui_protect_can_dig;" ..
S("User can not dig") .. "," .. S("User can dig") ..
";" .. can_dig .. "]" ..
"dropdown[0,4.8;4.1;worldedit_gui_protect_can_place;" ..
S("User can not place") .. "," .. S("User can place") ..
";" .. can_place .. "]" ..
"label[4,3.5;" .. S("Kidsbot mode:") .. "]" ..
"dropdown[4,4;4.1;worldedit_gui_protect_kidsbot_mode;" ..
S("Free") .. "," .. S("Exercice") ..
";" .. kidsbot_mode .. "]" ..
"button[0,6;2.5,1;worldedit_gui_protect_remove;" .. S("Remove area") .. "]" ..
"button[2.66,6;2.5,1;worldedit_gui_protect_add_owner;" .. S("Confirm owner") .. "]" ..
"button[5.33,6;2.5,1;worldedit_gui_protect_submit;" .. S("Protect Area") .. "]"
end,
})
@ -1425,6 +1440,15 @@ if minetest.get_modpath("areas") then
local timer = (fields.worldedit_gui_protect_chrono and
fields.worldedit_gui_protect_chrono:find("^%d+$")) and
tonumber(fields.worldedit_gui_protect_chrono) or ""
local can_dig =
(fields.worldedit_gui_protect_can_dig == S("User can dig")
and 'true' or nil)
local can_place =
(fields.worldedit_gui_protect_can_place == S("User can place")
and 'true' or nil)
local kidsbot_mode =
(fields.worldedit_gui_protect_kidsbot_mode == S("Free")
and 'free' or 'exercice')
if fields.worldedit_gui_protect_areas then
area_datas[name].last_selected_area = fields.worldedit_gui_protect_areas
@ -1435,6 +1459,7 @@ if minetest.get_modpath("areas") then
end
end
local datas = {
name = name,
area_name = area_name,
@ -1442,6 +1467,9 @@ if minetest.get_modpath("areas") then
pos2 = pos2,
parent = nil,
timer = timer,
can_dig = can_dig,
can_place = can_place,
kidsbot_mode = kidsbot_mode,
}
if fields.worldedit_gui_protect_submit then