fix protection

This commit is contained in:
Evert Prants 2019-01-12 20:16:37 +02:00
parent 17deef2f14
commit 2539ece481
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
2 changed files with 37 additions and 51 deletions

View File

@ -1,4 +1,4 @@
local tr = towny.regions.size
local main_is_protected = minetest.is_protected
-- Calculate a region from the center point and radius
@ -9,8 +9,8 @@ end
-- Test to see if a position is in a region
local function pos_in_region(pos, p1, p2)
return (pos.x >= p1.x and pos.y >= p1.y and pos.z >= p1.z) and
(pos.x <= p2.x and pos.y <= p2.y and pos.z <= p2.z)
return (pos.x <= p1.x and pos.y <= p1.y and pos.z <= p1.z) and
(pos.x >= p2.x and pos.y >= p2.y and pos.z >= p2.z)
end
local function region_equal(v1, v2)
@ -19,15 +19,6 @@ local function region_equal(v1, v2)
(math.floor(v1.z) == math.floor(v2.z))
end
local function in_table(tbl, str)
for _,s in pairs(tbl) do
if s == str then
return true
end
end
return false
end
-- Test to see if there's already a protected node in a region
function towny.regions:already_protected(p1, p2, name)
local found = false
@ -56,10 +47,11 @@ function towny.regions:build_perms(town, name, plotid)
end
-- Not even a town member, can't build here!
if not in_table(towndata.members, name) then return false end
if not towndata.members[name] then return false end
-- Plot build rights
if plotid and towndata.plots[plotid] then
print(plotid, dump(towndata.plots[plotid]))
-- This flag dictates that this member can build in all town plots, no matter if they own it or not
if towndata.members[name]['plot_build'] == true then return true end
@ -70,7 +62,7 @@ function towny.regions:build_perms(town, name, plotid)
-- Plot owner can always build in their plot
if name == plot.owner then return true end
if in_table(plot.members, name) then
if plot.members[name] then
if towndata.flags['plot_member_build'] == false then
return plot.members[name]['plot_build'] == true
else
@ -100,10 +92,6 @@ local function single_range(p)
return p1,p2
end
local function intest(town, name, plotid)
return not towny.regions:build_perms(town, name, plotid)
end
function towny.regions:get_town_at(pos)
local in_town, in_plot, in_claim
for town,regions in pairs(towny.regions.memloaded) do
@ -160,10 +148,9 @@ function towny.regions:town_claim_exists(town,p1)
end
function towny.regions:align_new_claim_block(pos,name)
local r = towny.regions.size
local closest_town,closest_block,distance = towny.regions:get_closest_town(pos,name)
if not closest_town then return nil end
if distance > (r * 2) then return nil end -- Too far
if distance > (tr * 2) then return nil end -- Too far
local new_pos
local p1,p2 = closest_block[1],closest_block[2]
@ -171,23 +158,23 @@ function towny.regions:align_new_claim_block(pos,name)
-- X
if (pos.z <= p1.z and pos.z >= p2.z) and (p1.y >= pos.y and p2.y <= pos.y) then
if pos.x > p1.x then
new_pos = vector.add(p1, {x=r,y=0,z=0})
new_pos = vector.add(p1, {x=tr,y=0,z=0})
else
new_pos = vector.add(p1, {x=-r,y=0,z=0})
new_pos = vector.add(p1, {x=-tr,y=0,z=0})
end
-- Y
elseif (pos.x <= p1.x and pos.x >= p2.x) and (pos.z <= p1.z and pos.z >= p2.z) then
if pos.y > p1.y then
new_pos = vector.add(p1, {x=0,y=r,z=0})
new_pos = vector.add(p1, {x=0,y=tr,z=0})
else
new_pos = vector.add(p1, {x=0,y=-r,z=0})
new_pos = vector.add(p1, {x=0,y=-tr,z=0})
end
-- Z
elseif (pos.x <= p1.x and pos.x >= p2.x) and (p1.y >= pos.y and p2.y <= pos.y) then
if pos.z > p1.z then
new_pos = vector.add(p1, {x=0,y=0,z=r})
new_pos = vector.add(p1, {x=0,y=0,z=tr})
else
new_pos = vector.add(p1, {x=0,y=0,z=-r})
new_pos = vector.add(p1, {x=0,y=0,z=-tr})
end
end
@ -235,7 +222,7 @@ function towny.regions:position_protected_from(pos, name)
local town,plot = towny.regions:get_town_at(pos)
if not town then return false end
return intest(town, name, plot)
return not towny.regions:build_perms(town, name, plot)
end
-- Finally, override is_protected

View File

@ -1,13 +1,4 @@
local tr = towny.regions.size
local function in_table(tbl, str)
for _,s in pairs(tbl) do
if s == str then
return true
end
end
return false
end
local function err_msg(player, msg)
minetest.chat_send_player(player, minetest.colorize("#ff1111", msg))
@ -18,7 +9,7 @@ function towny:get_player_town(name)
for town,data in pairs(towny.towns) do
if data.mayor == name then
return town
elseif in_table(data.members, name) then
elseif data.members[name] then
return town
end
end
@ -27,7 +18,7 @@ end
function towny:get_town_by_name(name)
for town,data in pairs(towny.towns) do
if data.name.lower() == name.lower() then
if data.name:lower() == name:lower() then
return town
end
end
@ -162,12 +153,11 @@ function towny:abridge_town(pos,player)
return err_msg(player, "You are not in any town you can modify.")
end
local success,message = towny.regions:remove_claim(c,t)
local success,message = towny.regions:remove_claim(c[1],t)
if not success then
return err_msg(player, "Failed to abandon claim block: " .. message)
end
table.insert(towny.regions.memloaded[t].blocks, p1)
data.flags["claim_blocks"] = data.flags["claim_blocks"] + 1
minetest.chat_send_player(player, "Successfully abandoned this claim block!")
towny:mark_dirty(t, true)
@ -310,9 +300,9 @@ function towny:create_plot(pos,player)
return err_msg(player, "You do not have permission to create plots in this town.")
end
local pid = minetest.sha1(minetest.hash_node_position(c))
local pid = minetest.sha1(minetest.hash_node_position(c[1]))
local success,message = towny.regions:set_plot(c,t,pid)
local success,message = towny.regions:set_plot(c[1],t,pid)
if not success then
minetest.chat_send_player(player, "Failed to create a plot here: " .. message)
return false
@ -326,10 +316,25 @@ function towny:create_plot(pos,player)
towny:mark_dirty(t, true)
minetest.chat_send_player(player, "Successfully created a plot!")
towny.regions:visualize_radius(vector.subtract(c, {x=tr/2,y=tr/2,z=tr/2}))
towny.regions:visualize_radius(vector.subtract(c[1], {x=tr/2,y=tr/2,z=tr/2}))
return true
end
local function flag_typeify(value)
if type(value) == "string" then
if value == "true" then
value = true
elseif value == "false" then
value = false
elseif tonumber(value) ~= nil then
value = tonumber(value)
elseif minetest.string_to_pos(value) ~= nil then
value = minetest.string_to_pos(value)
end
end
return value
end
function towny:set_plot_flags(pos,player,flag,value)
local towny_admin = minetest.check_player_privs(player, { towny_admin = true })
if not pos then
@ -357,11 +362,8 @@ function towny:set_plot_flags(pos,player,flag,value)
end
minetest.chat_send_player(player, "Successfully set the plot flag '" .. flag .."' to '" .. value .. "'!")
if type(value) == "string" and minetest.string_to_pos(value) then
value = minetest.string_to_pos(value)
end
towny:mark_dirty(t, false)
plot_data.flags[flag] = value
plot_data.flags[flag] = flag_typeify(value)
end
function towny:set_town_flags(pos,player,flag,value)
@ -390,11 +392,8 @@ function towny:set_town_flags(pos,player,flag,value)
end
minetest.chat_send_player(player, "Successfully set the town flag '" .. flag .."' to '" .. value .. "'!")
if type(value) == "string" and minetest.string_to_pos(value) then
value = minetest.string_to_pos(value)
end
towny:mark_dirty(t, false)
data.flags[flag] = value
data.flags[flag] = flag_typeify(value)
end
function towny:get_claims_total(town)