Skip to content

Commit e38b557

Browse files
committed
Use new file to save global shared areas
1 parent 17e89f4 commit e38b557

File tree

3 files changed

+151
-47
lines changed

3 files changed

+151
-47
lines changed

functions.lua

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
1+
function vector.floor(v)
2+
return {
3+
x = math.floor(v.x),
4+
y = math.floor(v.y),
5+
z = math.floor(v.z)
6+
}
7+
end
8+
9+
function table_contains(t, e)
10+
if not t or not e then
11+
return false
12+
end
13+
for i, v in ipairs(t) do
14+
if v == e then
15+
return true
16+
end
17+
end
18+
return false
19+
end
20+
21+
function table_delete(t, e)
22+
if not t or not e then
23+
return false
24+
end
25+
local removed = false
26+
for i, v in ipairs(t) do
27+
if v == e then
28+
table.remove(t, i)
29+
removed = true
30+
end
31+
end
32+
return removed
33+
end
34+
135
simple_protection.can_access = function(pos, player_name)
236
if not player_name or player_name == "" then
337
return false
@@ -15,10 +49,13 @@ simple_protection.can_access = function(pos, player_name)
1549
if player_name == data.owner then
1650
return true
1751
end
18-
if data.shared[player_name] then
52+
if table_contains(simple_protection.share[data.owner], player_name) then
53+
return true
54+
end
55+
if table_contains(data.shared, player_name) then
1956
return true
2057
end
21-
if data.shared["*all"] then
58+
if table_contains(data.shared, "*all") then
2259
return true
2360
end
2461
if minetest.check_player_privs(player_name, {simple_protection=true}) then
@@ -60,7 +97,7 @@ simple_protection.get_center = function(pos1)
6097
return pos
6198
end
6299

63-
simple_protection.load = function()
100+
simple_protection.load_claims = function()
64101
minetest.log("action", "Loading simple protection claims")
65102
local file = io.open(simple_protection.file, "r")
66103
if not file then
@@ -84,20 +121,53 @@ simple_protection.load = function()
84121
io.close(file)
85122
end
86123

124+
simple_protection.load_shareall = function()
125+
minetest.log("action", "Loading shared claims")
126+
local file = io.open(simple_protection.sharefile, "r")
127+
if not file then
128+
return
129+
end
130+
for line in file:lines() do
131+
if line ~= "" then
132+
local data = line:split(" ")
133+
-- owner, shared1, shared2, ..
134+
local _shared = {}
135+
if #data > 1 then
136+
for index = 2, #data do
137+
if data[index] ~= "" then
138+
table.insert(_shared, data[index])
139+
end
140+
end
141+
simple_protection.share[data[1]] = _shared
142+
end
143+
end
144+
end
145+
io.close(file)
146+
end
147+
87148
simple_protection.save = function()
88149
local file = io.open(simple_protection.file, "w")
89150
for pos, data in pairs(simple_protection.claims) do
90151
if data.owner and data.owner ~= "" then
91152
local shared = ""
92-
for player, really in pairs(data.shared) do
93-
if really then
94-
shared = shared.." "..player
95-
end
153+
for i, player in ipairs(data.shared) do
154+
shared = shared.." "..player
96155
end
97156
file:write(pos.." "..data.owner..shared.."\n")
98157
end
99158
end
100159
io.close(file)
160+
file = io.open(simple_protection.sharefile, "w")
161+
for name, players in pairs(simple_protection.share) do
162+
if #players > 0 then
163+
local shared = ""
164+
for i, player in ipairs(players) do
165+
shared = shared.." "..player
166+
end
167+
file:write(name..shared.."\n")
168+
end
169+
end
170+
io.close(file)
101171
end
102172

103173
simple_protection.load_config = function()

init.lua

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,16 @@
55
local world_path = minetest.get_worldpath()
66
simple_protection = {}
77
simple_protection.claims = {}
8+
simple_protection.share = {}
89
simple_protection.mod_path = minetest.get_modpath("simple_protection")
910
simple_protection.conf = world_path.."/s_protect.conf"
1011
simple_protection.file = world_path.."/s_protect.data"
12+
simple_protection.sharefile = world_path.."/s_protect_share.data"
1113

1214
dofile(simple_protection.mod_path.."/functions.lua")
1315
simple_protection.load_config()
1416
dofile(simple_protection.mod_path.."/protection.lua")
1517

16-
function vector.floor(v)
17-
return {
18-
x = math.floor(v.x),
19-
y = math.floor(v.y),
20-
z = math.floor(v.z)
21-
}
22-
end
23-
2418
minetest.register_on_protection_violation(function(pos, player_name)
2519
minetest.chat_send_player(player_name, "Do not try to modify this area!")
2620
--PUNISH HIM!!!!
@@ -80,14 +74,18 @@ simple_protection.command_show = function(name)
8074
end
8175

8276
minetest.chat_send_player(name, "Area status: Owned by "..data.owner)
83-
local shared = ""
84-
for player, really in pairs(data.shared) do
85-
if really then
86-
shared = shared..player..", "
77+
local text = ""
78+
for i, player in ipairs(data.shared) do
79+
text = text..player..", "
80+
end
81+
local shared = simple_protection.share[name]
82+
if shared then
83+
for i, player in ipairs(shared) do
84+
text = text..player.."*, "
8785
end
8886
end
89-
if shared ~= "" then
90-
minetest.chat_send_player(name, "Players with access: "..shared)
87+
if text ~= "" then
88+
minetest.chat_send_player(name, "Players with access: "..text)
9189
end
9290
end
9391

@@ -114,11 +112,17 @@ simple_protection.command_share = function(name, param)
114112
minetest.chat_send_player(name, "You do not own this area.")
115113
return
116114
end
117-
if data.shared[param] then
115+
local shared = simple_protection.share[name]
116+
if shared and shared[param] then
117+
minetest.chat_send_player(name, param.." already has access to all your areas.")
118+
return
119+
end
120+
121+
if table_contains(data.shared, param) then
118122
minetest.chat_send_player(name, param.." already has access to this area.")
119123
return
120124
end
121-
data.shared[param] = true
125+
table.insert(data.shared, param)
122126
simple_protection.save()
123127
minetest.chat_send_player(name, param.." has now access to this area.")
124128
if minetest.get_player_by_name(param) then
@@ -140,11 +144,11 @@ simple_protection.command_unshare = function(name, param)
140144
minetest.chat_send_player(name, "You do not own this area.")
141145
return
142146
end
143-
if not data.shared[param] then
147+
if not table_contains(data.shared, param) then
144148
minetest.chat_send_player(name, "This player has no access to this area.")
145149
return
146150
end
147-
data.shared[param] = nil
151+
table_delete(data.shared, param)
148152
simple_protection.save()
149153
minetest.chat_send_player(name, param.." has no longer access to this area.")
150154
if minetest.get_player_by_name(param) then
@@ -166,12 +170,16 @@ simple_protection.command_shareall = function(name, param)
166170
end
167171
return
168172
end
169-
--loops everywhere
170-
for pos, data in pairs(simple_protection.claims) do
171-
if data.owner == name then
172-
data.shared[param] = true
173-
end
173+
174+
local shared = simple_protection.share[name]
175+
if table_contains(shared, param) then
176+
minetest.chat_send_player(name, param.." already has now access to all your areas.")
177+
return
178+
end
179+
if not shared then
180+
simple_protection.share[name] = {}
174181
end
182+
table.insert(simple_protection.share[name], param)
175183
simple_protection.save()
176184
minetest.chat_send_player(name, param.." has now access to all your areas.")
177185
if minetest.get_player_by_name(param) then
@@ -181,13 +189,25 @@ end
181189

182190
simple_protection.command_unshareall = function(name, param)
183191
if name == param then return end
192+
local removed = false
193+
local shared = simple_protection.share[name]
194+
if table_delete(shared, param) then
195+
removed = true
196+
end
197+
184198
--loops everywhere
185199
for pos, data in pairs(simple_protection.claims) do
186200
if data.owner == name then
187-
data.shared[param] = nil
201+
if table_delete(data.shared, param) then
202+
removed = true
203+
end
188204
end
189205
end
190206
simple_protection.save()
207+
if not removed then
208+
minetest.chat_send_player(name, param.." did not have access to any of your areas.")
209+
return
210+
end
191211
minetest.chat_send_player(name, param.." has no longer access to your areas.")
192212
if minetest.get_player_by_name(param) then
193213
minetest.chat_send_player(param, name.." unshared all areas with you.")

protection.lua

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
minetest.after(1, simple_protection.load)
1+
minetest.after(1, function()
2+
simple_protection.load_claims()
3+
simple_protection.load_shareall()
4+
end)
25

36
simple_protection.old_is_protected = minetest.is_protected
47
minetest.is_protected = function(pos, player_name)
@@ -40,18 +43,28 @@ minetest.register_globalstep(function(dtime)
4043
end
4144
simple_protection.hud_time = 0
4245
-- get players
46+
local shared = simple_protection.share
4347
for _,player in ipairs(minetest.get_connected_players()) do
44-
local player_pos = vector.round(player:getpos())
45-
local data = simple_protection.get_data(player_pos)
48+
local pos = vector.round(player:getpos())
4649
local player_name = player:get_player_name()
47-
if not data then
48-
data = {owner="", shared={}}
50+
51+
local current_owner = ""
52+
local data = simple_protection.get_data(pos)
53+
if data then
54+
current_owner = data.owner
55+
end
56+
57+
local has_access = (current_owner == player_name)
58+
if not has_access and data then
59+
has_access = table_contains(data.shared, player_name)
60+
end
61+
if not has_access then
62+
has_access = table_contains(shared[current_owner], player_name)
4963
end
5064
local changed = true
51-
local has_access = (data.owner == player_name or data.shared[player_name])
5265

5366
if simple_protection.player_huds[player_name] then
54-
if simple_protection.player_huds[player_name].owner == data.owner and
67+
if simple_protection.player_huds[player_name].owner == current_owner and
5568
simple_protection.player_huds[player_name].had_access == has_access then
5669
-- still the same hud
5770
changed = false
@@ -63,7 +76,7 @@ minetest.register_globalstep(function(dtime)
6376
simple_protection.player_huds[player_name] = nil
6477
end
6578

66-
if data.owner ~= "" and changed then
79+
if current_owner ~= "" and changed then
6780
-- green if access
6881
local color = 0xFFFFFF
6982
if has_access then
@@ -75,11 +88,11 @@ minetest.register_globalstep(function(dtime)
7588
name = "area_hud",
7689
number = color,
7790
position = {x=0.15, y=0.97},
78-
text = "Area owner: "..data.owner,
91+
text = "Area owner: "..current_owner,
7992
scale = {x=100,y=25},
8093
alignment = {x=0, y=0},
8194
}),
82-
owner = data.owner,
95+
owner = current_owner,
8396
had_access = has_access
8497
}
8598
end
@@ -95,28 +108,29 @@ minetest.register_craftitem("simple_protection:claim", {
95108
return
96109
end
97110
local player_name = user:get_player_name()
98-
if simple_protection.old_is_protected(pointed_thing.under, player_name) then
111+
local pos = pointed_thing.under
112+
if simple_protection.old_is_protected(pos, player_name) then
99113
minetest.chat_send_player(player_name, "Area is already protected by an other protection mod.")
100114
return
101115
end
102116
if not simple_protection.underground_claim then
103-
local y = simple_protection.get_y_axis(pointed_thing.under.y)
117+
local y = simple_protection.get_y_axis(pos.y)
104118
if y < simple_protection.underground_limit then
105119
minetest.chat_send_player(player_name, "You can not claim areas under "..simple_protection.underground_limit.."m")
106120
return
107121
end
108122
end
109-
local pos = simple_protection.get_location(pointed_thing.under)
110-
local data = simple_protection.claims[pos]
123+
local area_pos = simple_protection.get_location(pos)
124+
local data = simple_protection.claims[area_pos]
111125
if data then
112126
minetest.chat_send_player(player_name, "Area already owned by: "..data.owner)
113127
return
114128
end
115129
itemstack:take_item(1)
116-
simple_protection.claims[pos] = {owner=player_name, shared={}}
130+
simple_protection.claims[area_pos] = {owner=player_name, shared={}}
117131
simple_protection.save()
118132

119-
minetest.add_entity(simple_protection.get_center(user:getpos()), "simple_protection:marker")
133+
minetest.add_entity(simple_protection.get_center(pos), "simple_protection:marker")
120134
minetest.chat_send_player(player_name, "Congratulations! You now own this area.")
121135
return itemstack
122136
end,

0 commit comments

Comments
 (0)