Tweaked code to work better for violation drops
[protector.git] / init.lua
1 minetest.register_privilege("delprotect","Ignore player protection")
2
3 protector = {}
4 protector.mod = "redo"
5 protector.radius = (tonumber(minetest.setting_get("protector_radius")) or 5)
6 protector.drop = minetest.setting_getbool("protector_drop") or false
7 protector.hurt = (tonumber(minetest.setting_get("protector_hurt")) or 0)
8
9 protector.get_member_list = function(meta)
10
11 return meta:get_string("members"):split(" ")
12 end
13
14 protector.set_member_list = function(meta, list)
15
16 meta:set_string("members", table.concat(list, " "))
17 end
18
19 protector.is_member = function (meta, name)
20
21 for _, n in ipairs(protector.get_member_list(meta)) do
22
23 if n == name then
24 return true
25 end
26 end
27
28 return false
29 end
30
31 protector.add_member = function(meta, name)
32
33 if protector.is_member(meta, name) then
34 return
35 end
36
37 local list = protector.get_member_list(meta)
38
39 table.insert(list, name)
40
41 protector.set_member_list(meta, list)
42 end
43
44 protector.del_member = function(meta, name)
45
46 local list = protector.get_member_list(meta)
47
48 for i, n in pairs(list) do
49
50 if n == name then
51 table.remove(list, i)
52 break
53 end
54 end
55
56 protector.set_member_list(meta, list)
57 end
58
59 -- Protector Interface
60
61 protector.generate_formspec = function(meta)
62
63 local formspec = "size[8,7]"
64 ..default.gui_bg..default.gui_bg_img..default.gui_slots
65 .."label[2.5,0;-- Protector interface --]"
66 .."label[0,1;PUNCH node to show protected area or USE for area check]"
67 .."label[0,2;Members: (type player name then press Enter to add)]"
68 .. "button_exit[2.5,6.2;3,0.5;close_me;Close]"
69
70 local members = protector.get_member_list(meta)
71 local npp = 12 -- max users added onto protector list
72 local i = 0
73
74 for _, member in pairs(members) do
75
76 if i < npp then
77
78 -- show username
79 formspec = formspec .. "button[" .. (i % 4 * 2)
80 .. "," .. math.floor(i / 4 + 3)
81 .. ";1.5,.5;protector_member;" .. member .. "]"
82
83 -- username remove button
84 .. "button[" .. (i % 4 * 2 + 1.25) .. ","
85 .. math.floor(i / 4 + 3)
86 .. ";.75,.5;protector_del_member_" .. member .. ";X]"
87 end
88
89 i = i + 1
90 end
91
92 if i < npp then
93
94 -- user name entry field
95 formspec = formspec .. "field[" .. (i % 4 * 2 + 1 / 3) .. ","
96 .. (math.floor(i / 4 + 3) + 1 / 3)
97 .. ";1.433,.5;protector_add_member;;]"
98
99 -- username add button
100 .."button[" .. (i % 4 * 2 + 1.25) .. ","
101 .. math.floor(i / 4 + 3) .. ";.75,.5;protector_submit;+]"
102
103 end
104
105 return formspec
106 end
107
108 -- Infolevel:
109 -- 0 for no info
110 -- 1 for "This area is owned by <owner> !" if you can't dig
111 -- 2 for "This area is owned by <owner>.
112 -- 3 for checking protector overlaps
113
114 protector.can_dig = function(r, pos, digger, onlyowner, infolevel)
115
116 if not digger
117 or not pos then
118 return false
119 end
120
121 -- Delprotect privileged users can override protections
122
123 if minetest.check_player_privs(digger, {delprotect = true})
124 and infolevel == 1 then
125 return true
126 end
127
128 if infolevel == 3 then infolevel = 1 end
129
130 -- Find the protector nodes
131
132 local positions = minetest.find_nodes_in_area(
133 {x = pos.x - r, y = pos.y - r, z = pos.z - r},
134 {x = pos.x + r, y = pos.y + r, z = pos.z + r},
135 {"protector:protect", "protector:protect2"})
136
137 local meta, owner, members
138
139 for _, pos in pairs(positions) do
140
141 meta = minetest.get_meta(pos)
142 owner = meta:get_string("owner")
143 members = meta:get_string("members")
144
145 if owner ~= digger then
146
147 if onlyowner
148 or not protector.is_member(meta, digger) then
149
150 if infolevel == 1 then
151
152 minetest.chat_send_player(digger,
153 "This area is owned by " .. owner .. " !")
154
155 elseif infolevel == 2 then
156
157 minetest.chat_send_player(digger,
158 "This area is owned by " .. owner .. ".")
159
160 minetest.chat_send_player(digger,
161 "Protection located at: " .. minetest.pos_to_string(pos))
162
163 if members ~= "" then
164
165 minetest.chat_send_player(digger,
166 "Members: " .. members .. ".")
167 end
168 end
169
170 return false
171 end
172 end
173
174 if infolevel == 2 then
175
176 minetest.chat_send_player(digger,
177 "This area is owned by " .. owner .. ".")
178
179 minetest.chat_send_player(digger,
180 "Protection located at: " .. minetest.pos_to_string(pos))
181
182 if members ~= "" then
183
184 minetest.chat_send_player(digger,
185 "Members: " .. members .. ".")
186 end
187
188 return false
189 end
190
191 end
192
193 if infolevel == 2 then
194
195 if #positions < 1 then
196
197 minetest.chat_send_player(digger,
198 "This area is not protected.")
199 end
200
201 minetest.chat_send_player(digger, "You can build here.")
202 end
203
204 return true
205 end
206
207 -- Can node be added or removed, if so return node else true (for protected)
208
209 protector.old_is_protected = minetest.is_protected
210
211 function minetest.is_protected(pos, digger)
212
213 if not protector.can_dig(protector.radius, pos, digger, false, 1) then
214
215 local player = minetest.get_player_by_name(digger)
216
217 -- hurt player if protection violated
218 if protector.hurt > 0
219 and player then
220 player:set_hp(player:get_hp() - protector.hurt)
221 end
222
223 -- drop tool/item if protection violated
224 if protector.drop == true
225 and player then
226
227 local holding = player:get_wielded_item()
228
229 if holding:to_string() ~= "" then
230
231 local sta = holding:take_item(holding:get_count())
232 local obj = minetest.add_item(player:getpos(), sta)
233
234 if obj then
235 obj:setvelocity({x = 0, y = 5, z = 0})
236 player:set_wielded_item(holding)
237 end
238
239 end
240
241 end
242
243 return true
244 end
245
246 return protector.old_is_protected(pos, digger)
247
248 end
249
250 -- Make sure protection block doesn't overlap another protector's area
251
252 function protector.check_overlap(itemstack, placer, pointed_thing)
253
254 if pointed_thing.type ~= "node" then
255 return itemstack
256 end
257
258 if not protector.can_dig(protector.radius * 2, pointed_thing.under,
259 placer:get_player_name(), true, 3)
260 or not protector.can_dig(protector.radius * 2, pointed_thing.above,
261 placer:get_player_name(), true, 3) then
262
263 minetest.chat_send_player(placer:get_player_name(),
264 "Overlaps into above players protected area")
265
266 return
267 end
268
269 return minetest.item_place(itemstack, placer, pointed_thing)
270
271 end
272
273 --= Protection Block
274
275 minetest.register_node("protector:protect", {
276 description = "Protection Block",
277 drawtype = "nodebox",
278 tiles = {
279 "moreblocks_circle_stone_bricks.png",
280 "moreblocks_circle_stone_bricks.png",
281 "moreblocks_circle_stone_bricks.png^protector_logo.png"
282 },
283 sounds = default.node_sound_stone_defaults(),
284 groups = {dig_immediate = 2, unbreakable = 1},
285 is_ground_content = false,
286 paramtype = "light",
287 light_source = 4,
288
289 node_box = {
290 type = "fixed",
291 fixed = {
292 {-0.5 ,-0.5, -0.5, 0.5, 0.5, 0.5},
293 }
294 },
295
296 on_place = protector.check_overlap,
297
298 after_place_node = function(pos, placer)
299
300 local meta = minetest.get_meta(pos)
301
302 meta:set_string("owner", placer:get_player_name() or "")
303 meta:set_string("infotext", "Protection (owned by " .. meta:get_string("owner") .. ")")
304 meta:set_string("members", "")
305 end,
306
307 on_use = function(itemstack, user, pointed_thing)
308
309 if pointed_thing.type ~= "node" then
310 return
311 end
312
313 protector.can_dig(protector.radius, pointed_thing.under, user:get_player_name(), false, 2)
314 end,
315
316 on_rightclick = function(pos, node, clicker, itemstack)
317
318 local meta = minetest.get_meta(pos)
319
320 if protector.can_dig(1, pos,clicker:get_player_name(), true, 1) then
321 minetest.show_formspec(clicker:get_player_name(),
322 "protector:node_" .. minetest.pos_to_string(pos), protector.generate_formspec(meta))
323 end
324 end,
325
326 on_punch = function(pos, node, puncher)
327
328 if not protector.can_dig(1, pos, puncher:get_player_name(), true, 1) then
329 return
330 end
331
332 minetest.add_entity(pos, "protector:display")
333 end,
334
335 can_dig = function(pos, player)
336
337 return protector.can_dig(1, pos, player:get_player_name(), true, 1)
338 end,
339 })
340
341 minetest.register_craft({
342 output = "protector:protect",
343 recipe = {
344 {"default:stone", "default:stone", "default:stone"},
345 {"default:stone", "default:steel_ingot", "default:stone"},
346 {"default:stone", "default:stone", "default:stone"},
347 }
348 })
349
350 --= Protection Logo
351
352 minetest.register_node("protector:protect2", {
353 description = "Protection Logo",
354 tiles = {"protector_logo.png"},
355 wield_image = "protector_logo.png",
356 inventory_image = "protector_logo.png",
357 sounds = default.node_sound_stone_defaults(),
358 groups = {dig_immediate = 2, unbreakable = 1},
359 paramtype = 'light',
360 paramtype2 = "wallmounted",
361 legacy_wallmounted = true,
362 light_source = 4,
363 drawtype = "nodebox",
364 sunlight_propagates = true,
365 walkable = true,
366 node_box = {
367 type = "wallmounted",
368 wall_top = {-0.375, 0.4375, -0.5, 0.375, 0.5, 0.5},
369 wall_bottom = {-0.375, -0.5, -0.5, 0.375, -0.4375, 0.5},
370 wall_side = {-0.5, -0.5, -0.375, -0.4375, 0.5, 0.375},
371 },
372 selection_box = {type = "wallmounted"},
373
374 on_place = protector.check_overlap,
375
376 after_place_node = function(pos, placer)
377
378 local meta = minetest.get_meta(pos)
379
380 meta:set_string("owner", placer:get_player_name() or "")
381 meta:set_string("infotext", "Protection (owned by " .. meta:get_string("owner") .. ")")
382 meta:set_string("members", "")
383 end,
384
385 on_use = function(itemstack, user, pointed_thing)
386
387 if pointed_thing.type ~= "node" then
388 return
389 end
390
391 protector.can_dig(protector.radius, pointed_thing.under, user:get_player_name(), false, 2)
392 end,
393
394 on_rightclick = function(pos, node, clicker, itemstack)
395
396 local meta = minetest.get_meta(pos)
397
398 if protector.can_dig(1, pos, clicker:get_player_name(), true, 1) then
399
400 minetest.show_formspec(clicker:get_player_name(),
401 "protector:node_" .. minetest.pos_to_string(pos), protector.generate_formspec(meta))
402 end
403 end,
404
405 on_punch = function(pos, node, puncher)
406
407 if not protector.can_dig(1, pos, puncher:get_player_name(), true, 1) then
408 return
409 end
410
411 minetest.add_entity(pos, "protector:display")
412 end,
413
414 can_dig = function(pos, player)
415
416 return protector.can_dig(1, pos, player:get_player_name(), true, 1)
417 end,
418 })
419
420 minetest.register_craft({
421 output = "protector:protect2",
422 recipe = {
423 {"default:stone", "default:stone", "default:stone"},
424 {"default:stone", "default:copper_ingot", "default:stone"},
425 {"default:stone", "default:stone", "default:stone"},
426 }
427 })
428
429 -- If name entered or button press
430
431 minetest.register_on_player_receive_fields(function(player, formname, fields)
432
433 if string.sub(formname, 0, string.len("protector:node_")) == "protector:node_" then
434
435 local pos_s = string.sub(formname, string.len("protector:node_") + 1)
436 local pos = minetest.string_to_pos(pos_s)
437 local meta = minetest.get_meta(pos)
438
439 if not protector.can_dig(1, pos, player:get_player_name(), true, 1) then
440 return
441 end
442
443 if fields.protector_add_member then
444
445 for _, i in ipairs(fields.protector_add_member:split(" ")) do
446 protector.add_member(meta, i)
447 end
448 end
449
450 for field, value in pairs(fields) do
451
452 if string.sub(field, 0, string.len("protector_del_member_")) == "protector_del_member_" then
453 protector.del_member(meta, string.sub(field,string.len("protector_del_member_") + 1))
454 end
455 end
456
457 if not fields.close_me then
458 minetest.show_formspec(player:get_player_name(), formname, protector.generate_formspec(meta))
459 end
460
461 end
462
463 end)
464
465 -- Display entity shown when protector node is punched
466
467 minetest.register_entity("protector:display", {
468 physical = false,
469 collisionbox = {0, 0, 0, 0, 0, 0},
470 visual = "wielditem",
471 -- wielditem seems to be scaled to 1.5 times original node size
472 visual_size = {x = 1.0 / 1.5, y = 1.0 / 1.5},
473 textures = {"protector:display_node"},
474 timer = 0,
475
476 on_activate = function(self, staticdata)
477
478 -- Xanadu server only
479 if mobs and mobs.entity and mobs.entity == false then
480 self.object:remove()
481 end
482 end,
483
484 on_step = function(self, dtime)
485
486 self.timer = self.timer + dtime
487
488 if self.timer > 5 then
489 self.object:remove()
490 end
491 end,
492 })
493
494 -- Display-zone node, Do NOT place the display as a node,
495 -- it is made to be used as an entity (see above)
496
497 local x = protector.radius
498 minetest.register_node("protector:display_node", {
499 tiles = {"protector_display.png"},
500 use_texture_alpha = true,
501 walkable = false,
502 drawtype = "nodebox",
503 node_box = {
504 type = "fixed",
505 fixed = {
506 -- sides
507 {-(x+.55), -(x+.55), -(x+.55), -(x+.45), (x+.55), (x+.55)},
508 {-(x+.55), -(x+.55), (x+.45), (x+.55), (x+.55), (x+.55)},
509 {(x+.45), -(x+.55), -(x+.55), (x+.55), (x+.55), (x+.55)},
510 {-(x+.55), -(x+.55), -(x+.55), (x+.55), (x+.55), -(x+.45)},
511 -- top
512 {-(x+.55), (x+.45), -(x+.55), (x+.55), (x+.55), (x+.55)},
513 -- bottom
514 {-(x+.55), -(x+.55), -(x+.55), (x+.55), -(x+.45), (x+.55)},
515 -- middle (surround protector)
516 {-.55,-.55,-.55, .55,.55,.55},
517 },
518 },
519 selection_box = {
520 type = "regular",
521 },
522 paramtype = "light",
523 groups = {dig_immediate = 3, not_in_creative_inventory = 1},
524 drop = "",
525 })
526
527 dofile(minetest.get_modpath("protector") .. "/doors_chest.lua")
528 dofile(minetest.get_modpath("protector") .. "/pvp.lua")
529 dofile(minetest.get_modpath("protector") .. "/admin.lua")
530
531 print ("[MOD] Protector Redo loaded")