violation drop after delay to stop stack doubling
[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 pairs(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 -- take stack
232 local sta = holding:take_item(holding:get_count())
233 player:set_wielded_item(holding)
234
235 -- incase of lag, reset stack
236 minetest.after(0.1, function()
237 player:set_wielded_item(holding)
238
239 -- drop stack
240 local obj = minetest.add_item(player:getpos(), sta)
241 obj:setvelocity({x = 0, y = 5, z = 0})
242 end)
243
244 end
245
246 end
247
248 return true
249 end
250
251 return protector.old_is_protected(pos, digger)
252
253 end
254
255 -- Make sure protection block doesn't overlap another protector's area
256
257 function protector.check_overlap(itemstack, placer, pointed_thing)
258
259 if pointed_thing.type ~= "node" then
260 return itemstack
261 end
262
263 if not protector.can_dig(protector.radius * 2, pointed_thing.under,
264 placer:get_player_name(), true, 3)
265 or not protector.can_dig(protector.radius * 2, pointed_thing.above,
266 placer:get_player_name(), true, 3) then
267
268 minetest.chat_send_player(placer:get_player_name(),
269 "Overlaps into above players protected area")
270
271 return
272 end
273
274 return minetest.item_place(itemstack, placer, pointed_thing)
275
276 end
277
278 --= Protection Block
279
280 minetest.register_node("protector:protect", {
281 description = "Protection Block",
282 drawtype = "nodebox",
283 tiles = {
284 "moreblocks_circle_stone_bricks.png",
285 "moreblocks_circle_stone_bricks.png",
286 "moreblocks_circle_stone_bricks.png^protector_logo.png"
287 },
288 sounds = default.node_sound_stone_defaults(),
289 groups = {dig_immediate = 2, unbreakable = 1},
290 is_ground_content = false,
291 paramtype = "light",
292 light_source = 4,
293
294 node_box = {
295 type = "fixed",
296 fixed = {
297 {-0.5 ,-0.5, -0.5, 0.5, 0.5, 0.5},
298 }
299 },
300
301 on_place = protector.check_overlap,
302
303 after_place_node = function(pos, placer)
304
305 local meta = minetest.get_meta(pos)
306
307 meta:set_string("owner", placer:get_player_name() or "")
308 meta:set_string("infotext", "Protection (owned by " .. meta:get_string("owner") .. ")")
309 meta:set_string("members", "")
310 end,
311
312 on_use = function(itemstack, user, pointed_thing)
313
314 if pointed_thing.type ~= "node" then
315 return
316 end
317
318 protector.can_dig(protector.radius, pointed_thing.under, user:get_player_name(), false, 2)
319 end,
320
321 on_rightclick = function(pos, node, clicker, itemstack)
322
323 local meta = minetest.get_meta(pos)
324
325 if protector.can_dig(1, pos,clicker:get_player_name(), true, 1) then
326 minetest.show_formspec(clicker:get_player_name(),
327 "protector:node_" .. minetest.pos_to_string(pos), protector.generate_formspec(meta))
328 end
329 end,
330
331 on_punch = function(pos, node, puncher)
332
333 if not protector.can_dig(1, pos, puncher:get_player_name(), true, 1) then
334 return
335 end
336
337 minetest.add_entity(pos, "protector:display")
338 end,
339
340 can_dig = function(pos, player)
341
342 return protector.can_dig(1, pos, player:get_player_name(), true, 1)
343 end,
344 })
345
346 minetest.register_craft({
347 output = "protector:protect",
348 recipe = {
349 {"default:stone", "default:stone", "default:stone"},
350 {"default:stone", "default:steel_ingot", "default:stone"},
351 {"default:stone", "default:stone", "default:stone"},
352 }
353 })
354
355 --= Protection Logo
356
357 minetest.register_node("protector:protect2", {
358 description = "Protection Logo",
359 tiles = {"protector_logo.png"},
360 wield_image = "protector_logo.png",
361 inventory_image = "protector_logo.png",
362 sounds = default.node_sound_stone_defaults(),
363 groups = {dig_immediate = 2, unbreakable = 1},
364 paramtype = 'light',
365 paramtype2 = "wallmounted",
366 legacy_wallmounted = true,
367 light_source = 4,
368 drawtype = "nodebox",
369 sunlight_propagates = true,
370 walkable = true,
371 node_box = {
372 type = "wallmounted",
373 wall_top = {-0.375, 0.4375, -0.5, 0.375, 0.5, 0.5},
374 wall_bottom = {-0.375, -0.5, -0.5, 0.375, -0.4375, 0.5},
375 wall_side = {-0.5, -0.5, -0.375, -0.4375, 0.5, 0.375},
376 },
377 selection_box = {type = "wallmounted"},
378
379 on_place = protector.check_overlap,
380
381 after_place_node = function(pos, placer)
382
383 local meta = minetest.get_meta(pos)
384
385 meta:set_string("owner", placer:get_player_name() or "")
386 meta:set_string("infotext", "Protection (owned by " .. meta:get_string("owner") .. ")")
387 meta:set_string("members", "")
388 end,
389
390 on_use = function(itemstack, user, pointed_thing)
391
392 if pointed_thing.type ~= "node" then
393 return
394 end
395
396 protector.can_dig(protector.radius, pointed_thing.under, user:get_player_name(), false, 2)
397 end,
398
399 on_rightclick = function(pos, node, clicker, itemstack)
400
401 local meta = minetest.get_meta(pos)
402
403 if protector.can_dig(1, pos, clicker:get_player_name(), true, 1) then
404
405 minetest.show_formspec(clicker:get_player_name(),
406 "protector:node_" .. minetest.pos_to_string(pos), protector.generate_formspec(meta))
407 end
408 end,
409
410 on_punch = function(pos, node, puncher)
411
412 if not protector.can_dig(1, pos, puncher:get_player_name(), true, 1) then
413 return
414 end
415
416 minetest.add_entity(pos, "protector:display")
417 end,
418
419 can_dig = function(pos, player)
420
421 return protector.can_dig(1, pos, player:get_player_name(), true, 1)
422 end,
423 })
424
425 minetest.register_craft({
426 output = "protector:protect2",
427 recipe = {
428 {"default:stone", "default:stone", "default:stone"},
429 {"default:stone", "default:copper_ingot", "default:stone"},
430 {"default:stone", "default:stone", "default:stone"},
431 }
432 })
433
434 -- If name entered or button press
435
436 minetest.register_on_player_receive_fields(function(player, formname, fields)
437
438 if string.sub(formname, 0, string.len("protector:node_")) == "protector:node_" then
439
440 local pos_s = string.sub(formname, string.len("protector:node_") + 1)
441 local pos = minetest.string_to_pos(pos_s)
442 local meta = minetest.get_meta(pos)
443
444 if not protector.can_dig(1, pos, player:get_player_name(), true, 1) then
445 return
446 end
447
448 if fields.protector_add_member then
449
450 for _, i in pairs(fields.protector_add_member:split(" ")) do
451 protector.add_member(meta, i)
452 end
453 end
454
455 for field, value in pairs(fields) do
456
457 if string.sub(field, 0, string.len("protector_del_member_")) == "protector_del_member_" then
458 protector.del_member(meta, string.sub(field,string.len("protector_del_member_") + 1))
459 end
460 end
461
462 if not fields.close_me then
463 minetest.show_formspec(player:get_player_name(), formname, protector.generate_formspec(meta))
464 end
465
466 end
467
468 end)
469
470 -- Display entity shown when protector node is punched
471
472 minetest.register_entity("protector:display", {
473 physical = false,
474 collisionbox = {0, 0, 0, 0, 0, 0},
475 visual = "wielditem",
476 -- wielditem seems to be scaled to 1.5 times original node size
477 visual_size = {x = 1.0 / 1.5, y = 1.0 / 1.5},
478 textures = {"protector:display_node"},
479 timer = 0,
480
481 on_activate = function(self, staticdata)
482
483 -- Xanadu server only
484 if mobs and mobs.entity and mobs.entity == false then
485 self.object:remove()
486 end
487 end,
488
489 on_step = function(self, dtime)
490
491 self.timer = self.timer + dtime
492
493 if self.timer > 5 then
494 self.object:remove()
495 end
496 end,
497 })
498
499 -- Display-zone node, Do NOT place the display as a node,
500 -- it is made to be used as an entity (see above)
501
502 local x = protector.radius
503 minetest.register_node("protector:display_node", {
504 tiles = {"protector_display.png"},
505 use_texture_alpha = true,
506 walkable = false,
507 drawtype = "nodebox",
508 node_box = {
509 type = "fixed",
510 fixed = {
511 -- sides
512 {-(x+.55), -(x+.55), -(x+.55), -(x+.45), (x+.55), (x+.55)},
513 {-(x+.55), -(x+.55), (x+.45), (x+.55), (x+.55), (x+.55)},
514 {(x+.45), -(x+.55), -(x+.55), (x+.55), (x+.55), (x+.55)},
515 {-(x+.55), -(x+.55), -(x+.55), (x+.55), (x+.55), -(x+.45)},
516 -- top
517 {-(x+.55), (x+.45), -(x+.55), (x+.55), (x+.55), (x+.55)},
518 -- bottom
519 {-(x+.55), -(x+.55), -(x+.55), (x+.55), -(x+.45), (x+.55)},
520 -- middle (surround protector)
521 {-.55,-.55,-.55, .55,.55,.55},
522 },
523 },
524 selection_box = {
525 type = "regular",
526 },
527 paramtype = "light",
528 groups = {dig_immediate = 3, not_in_creative_inventory = 1},
529 drop = "",
530 })
531
532 dofile(minetest.get_modpath("protector") .. "/doors_chest.lua")
533 dofile(minetest.get_modpath("protector") .. "/pvp.lua")
534 dofile(minetest.get_modpath("protector") .. "/admin.lua")
535
536 print ("[MOD] Protector Redo loaded")