Using protector shows location of protection found
[protector.git] / init.lua
1 minetest.register_privilege("delprotect","Ignore player protection")
2
3 protector = {}
4 protector.radius = 5
5
6 protector.get_member_list = function(meta)
7 local s = meta:get_string("members")
8 local list = s:split(" ")
9 return list
10 end
11
12 protector.set_member_list = function(meta, list)
13 meta:set_string("members", table.concat(list, " "))
14 end
15
16 protector.is_member = function (meta, name)
17 local list = protector.get_member_list(meta)
18 for _, n in ipairs(list) do
19 if n == name then
20 return true
21 end
22 end
23 return false
24 end
25
26 protector.add_member = function(meta, name)
27 if protector.is_member(meta, name) then return end
28 local list = protector.get_member_list(meta)
29 table.insert(list,name)
30 protector.set_member_list(meta,list)
31 end
32
33 protector.del_member = function(meta,name)
34 local list = protector.get_member_list(meta)
35 for i, n in ipairs(list) do
36 if n == name then
37 table.remove(list, i)
38 break
39 end
40 end
41 protector.set_member_list(meta,list)
42 end
43
44 -- Protector Interface
45
46 protector.generate_formspec = function(meta)
47
48 local formspec = "size[8,7]"..default.gui_bg..default.gui_bg_img..default.gui_slots
49 .."label[2.5,0;-- Protector interface --]"
50 .."label[0,1;PUNCH node to show protected area or USE for area check]"
51 .."label[0,2;Members: (type player name then press Enter to add)]"
52
53 local members = protector.get_member_list(meta)
54 local npp = 12
55 local i = 0
56
57 for _, member in ipairs(members) do
58 if i < npp then
59 formspec = formspec .. "button["..(i%4*2)..","
60 ..math.floor(i/4+3)..";1.5,.5;protector_member;"..member.."]"
61 formspec = formspec .. "button["..(i%4*2+1.25)..","
62 ..math.floor(i/4+3)..";.75,.5;protector_del_member_"..member..";X]"
63 end
64 i = i +1
65 end
66
67 if i < npp then
68 formspec = formspec
69 .."field["..(i%4*2+1/3)..","..(math.floor(i/4+3)+1/3)..";1.433,.5;protector_add_member;;]"
70 end
71
72 formspec = formspec.."button_exit[2.5,6.2;3,0.5;close_me;Close]"
73
74 return formspec
75 end
76
77 -- ACTUAL PROTECTION SECTION
78
79 -- Infolevel:
80 -- 0 for no info
81 -- 1 for "This area is owned by <owner> !" if you can't dig
82 -- 2 for "This area is owned by <owner>.
83 -- 3 for checking protector overlaps
84
85 protector.can_dig = function(r,pos,digger,onlyowner,infolevel)
86
87 if not digger then
88 return false
89 end
90
91 local whois = digger
92
93 -- Delprotect privileged users can override protections
94
95 if minetest.check_player_privs(whois, {delprotect=true}) and infolevel == 1 then
96 return true
97 end
98
99 if infolevel == 3 then infolevel = 1 end
100
101 -- Find the protector nodes
102
103 local positions = minetest.find_nodes_in_area(
104 {x=pos.x-r, y=pos.y-r, z=pos.z-r},
105 {x=pos.x+r, y=pos.y+r, z=pos.z+r},
106 {"protector:protect", "protector:protect2"})
107
108 for _, pos in ipairs(positions) do
109 local meta = minetest.get_meta(pos)
110 local owner = meta:get_string("owner")
111
112 if owner ~= whois then
113 if onlyowner or not protector.is_member(meta, whois) then
114 if infolevel == 1 then
115 minetest.chat_send_player(whois, "This area is owned by "..owner.." !")
116 elseif infolevel == 2 then
117 minetest.chat_send_player(whois,"This area is owned by "..meta:get_string("owner")..".")
118 if meta:get_string("members") ~= "" then
119 minetest.chat_send_player(whois,"Members: "..meta:get_string("members")..".")
120 end
121 end
122 return false
123 end
124 end
125 end
126
127 if infolevel == 2 then
128 if #positions < 1 then
129 minetest.chat_send_player(whois,"This area is not protected.")
130 else
131 local meta = minetest.get_meta(positions[1])
132 minetest.chat_send_player(whois,"This area is owned by "..meta:get_string("owner")..".")
133 minetest.chat_send_player(whois,"Protection located at: "..minetest.pos_to_string(positions[1]))
134 if meta:get_string("members") ~= "" then
135 minetest.chat_send_player(whois,"Members: "..meta:get_string("members")..".")
136 end
137 end
138 minetest.chat_send_player(whois,"You can build here.")
139 end
140 return true
141 end
142
143 -- Can node be added or removed, if so return node else true (for protected)
144
145 protector.old_is_protected = minetest.is_protected
146 minetest.is_protected = function(pos, digger)
147
148 if protector.can_dig(protector.radius, pos, digger, false, 1) then
149 return protector.old_is_protected(pos, digger)
150 else
151 return true
152 end
153 end
154
155 -- Make sure protection block doesn't overlap another protector's area
156
157 protector.old_node_place = minetest.item_place
158 function minetest.item_place(itemstack, placer, pointed_thing)
159
160 if itemstack:get_name() == "protector:protect" or itemstack:get_name() == "protector:protect2" then
161 local pos = pointed_thing.above
162 local user = placer:get_player_name()
163 if not protector.can_dig(protector.radius * 2, pos, user, true, 3) then
164 minetest.chat_send_player(placer:get_player_name(),"Overlaps into another protected area")
165 return protector.old_node_place(itemstack, placer, pos)
166 end
167 end
168
169 return protector.old_node_place(itemstack, placer, pointed_thing)
170 end
171
172 -- END
173
174 --= Protection Block
175
176 minetest.register_node("protector:protect", {
177 description = "Protection Block",
178 tiles = {"protector_top.png","protector_top.png","protector_side.png"},
179 sounds = default.node_sound_stone_defaults(),
180 groups = {dig_immediate=2},
181 drawtype = "nodebox",
182 node_box = {
183 type="fixed",
184 fixed = { -0.5, -0.5, -0.5, 0.5, 0.5, 0.5 },
185 },
186 selection_box = { type="regular" },
187 paramtype = "light",
188 light_source = 2,
189
190 after_place_node = function(pos, placer)
191 local meta = minetest.get_meta(pos)
192 meta:set_string("owner", placer:get_player_name() or "")
193 meta:set_string("infotext", "Protection (owned by "..meta:get_string("owner")..")")
194 meta:set_string("members", "")
195 end,
196
197 on_use = function(itemstack, user, pointed_thing)
198 if pointed_thing.type ~= "node" then return end
199 protector.can_dig(protector.radius,pointed_thing.under,user:get_player_name(),false,2)
200 end,
201
202 on_rightclick = function(pos, node, clicker, itemstack)
203 local meta = minetest.get_meta(pos)
204 if protector.can_dig(1,pos,clicker:get_player_name(),true,1) then
205 minetest.show_formspec(clicker:get_player_name(),
206 "protector:node_"..minetest.pos_to_string(pos), protector.generate_formspec(meta))
207 end
208 end,
209
210 on_punch = function(pos, node, puncher)
211 if not protector.can_dig(1,pos,puncher:get_player_name(),true,1) then
212 return
213 end
214
215 minetest.add_entity(pos, "protector:display")
216 minetest.get_node_timer(pos):start(10)
217 end,
218
219 on_timer = function(pos)
220 local objs = minetest.get_objects_inside_radius(pos,.5)
221 for _, o in pairs(objs) do
222 if (not o:is_player()) and o:get_luaentity().name == "protector:display" then
223 o:remove()
224 end
225 end
226 end,
227 })
228
229 minetest.register_craft({
230 output = "protector:protect 4",
231 recipe = {
232 {"default:stone","default:stone","default:stone"},
233 {"default:stone","default:steel_ingot","default:stone"},
234 {"default:stone","default:stone","default:stone"},
235 }
236 })
237
238 --= Protection Logo
239
240 minetest.register_node("protector:protect2", {
241 description = "Protection Logo",
242 tiles = {"protector_logo.png"},
243 wield_image = "protector_logo.png",
244 inventory_image = "protector_logo.png",
245 sounds = default.node_sound_stone_defaults(),
246 groups = {dig_immediate=2},
247 paramtype = 'light',
248 paramtype2 = "wallmounted",
249 light_source = 2,
250 drawtype = "nodebox",
251 sunlight_propagates = true,
252 walkable = true,
253 node_box = {
254 type = "wallmounted",
255 wall_top = {-0.375, 0.4375, -0.5, 0.375, 0.5, 0.5},
256 wall_bottom = {-0.375, -0.5, -0.5, 0.375, -0.4375, 0.5},
257 wall_side = {-0.5, -0.5, -0.375, -0.4375, 0.5, 0.375},
258 },
259 selection_box = {type = "wallmounted"},
260
261 after_place_node = function(pos, placer)
262 local meta = minetest.get_meta(pos)
263 meta:set_string("owner", placer:get_player_name() or "")
264 meta:set_string("infotext", "Protection (owned by "..meta:get_string("owner")..")")
265 meta:set_string("members", "")
266 end,
267
268 on_use = function(itemstack, user, pointed_thing)
269 if pointed_thing.type ~= "node" then return end
270 protector.can_dig(protector.radius,pointed_thing.under,user:get_player_name(),false,2)
271 end,
272
273 on_rightclick = function(pos, node, clicker, itemstack)
274 local meta = minetest.get_meta(pos)
275 if protector.can_dig(1,pos,clicker:get_player_name(),true,1) then
276 minetest.show_formspec(clicker:get_player_name(),
277 "protector:node_"..minetest.pos_to_string(pos), protector.generate_formspec(meta))
278 end
279 end,
280
281 on_punch = function(pos, node, puncher)
282 if not protector.can_dig(1,pos,puncher:get_player_name(),true,1) then
283 return
284 end
285
286 minetest.add_entity(pos, "protector:display")
287 minetest.get_node_timer(pos):start(10)
288 end,
289
290 on_timer = function(pos)
291 local objs = minetest.get_objects_inside_radius(pos,.5)
292 for _, o in pairs(objs) do
293 if (not o:is_player()) and o:get_luaentity().name == "protector:display" then
294 o:remove()
295 end
296 end
297 end,
298 })
299
300 minetest.register_craft({
301 output = "protector:protect2 4",
302 recipe = {
303 {"default:stone","default:stone","default:stone"},
304 {"default:stone","default:copper_ingot","default:stone"},
305 {"default:stone","default:stone","default:stone"},
306 }
307 })
308
309 -- If name entered or button press
310 minetest.register_on_player_receive_fields(function(player,formname,fields)
311
312 if string.sub(formname,0,string.len("protector:node_")) == "protector:node_" then
313
314 local pos_s = string.sub(formname,string.len("protector:node_")+1)
315 local pos = minetest.string_to_pos(pos_s)
316 local meta = minetest.get_meta(pos)
317
318 if not protector.can_dig(1,pos,player:get_player_name(),true,1) then
319 return
320 end
321
322 if fields.protector_add_member then
323 for _, i in ipairs(fields.protector_add_member:split(" ")) do
324 protector.add_member(meta,i)
325 end
326 end
327
328 for field, value in pairs(fields) do
329 if string.sub(field,0,string.len("protector_del_member_"))=="protector_del_member_" then
330 protector.del_member(meta, string.sub(field,string.len("protector_del_member_")+1))
331 end
332 end
333
334 if not fields.close_me then
335 minetest.show_formspec(player:get_player_name(), formname, protector.generate_formspec(meta))
336 end
337
338 end
339
340 end)
341
342 minetest.register_entity("protector:display", {
343 physical = false,
344 collisionbox = {0,0,0,0,0,0},
345 visual = "wielditem",
346 visual_size = {x=1.0/1.5,y=1.0/1.5}, -- wielditem seems to be scaled to 1.5 times original node size
347 textures = {"protector:display_node"},
348 on_step = function(self, dtime)
349 local nam = minetest.get_node(self.object:getpos()).name
350 if nam ~= "protector:protect" and nam ~= "protector:protect2" then
351 self.object:remove()
352 return
353 end
354 end,
355 })
356
357 -- Display-zone node, Do NOT place the display as a node, it is made to be used as an entity (see above)
358 local x = protector.radius
359 minetest.register_node("protector:display_node", {
360 tiles = {"protector_display.png"},
361 use_texture_alpha = true,
362 walkable = false,
363 drawtype = "nodebox",
364 node_box = {
365 type = "fixed",
366 fixed = {
367 -- sides
368 {-(x+.55), -(x+.55), -(x+.55), -(x+.45), (x+.55), (x+.55)},
369 {-(x+.55), -(x+.55), (x+.45), (x+.55), (x+.55), (x+.55)},
370 {(x+.45), -(x+.55), -(x+.55), (x+.55), (x+.55), (x+.55)},
371 {-(x+.55), -(x+.55), -(x+.55), (x+.55), (x+.55), -(x+.45)},
372 -- top
373 {-(x+.55), (x+.45), -(x+.55), (x+.55), (x+.55), (x+.55)},
374 -- bottom
375 {-(x+.55), -(x+.55), -(x+.55), (x+.55), -(x+.45), (x+.55)},
376 -- middle (surround protector)
377 {-.55,-.55,-.55, .55,.55,.55},
378 },
379 },
380 selection_box = {
381 type = "regular",
382 },
383 paramtype = "light",
384 groups = {dig_immediate=3,not_in_creative_inventory=1},
385 drop = "",
386 })
387
388 -- Register Protected Doors
389
390 local function on_rightclick(pos, dir, check_name, replace, replace_dir, params)
391 pos.y = pos.y+dir
392 if not minetest.get_node(pos).name == check_name then
393 return
394 end
395 local p2 = minetest.get_node(pos).param2
396 p2 = params[p2+1]
397
398 minetest.swap_node(pos, {name=replace_dir, param2=p2})
399
400 pos.y = pos.y-dir
401 minetest.swap_node(pos, {name=replace, param2=p2})
402
403 local snd_1 = "door_close"
404 local snd_2 = "door_open"
405 if params[1] == 3 then
406 snd_1 = "door_open"
407 snd_2 = "door_close"
408 end
409
410 if minetest.get_meta(pos):get_int("right") ~= 0 then
411 minetest.sound_play(snd_1, {pos = pos, gain = 0.3, max_hear_distance = 10})
412 else
413 minetest.sound_play(snd_2, {pos = pos, gain = 0.3, max_hear_distance = 10})
414 end
415 end
416
417 -- Protected Wooden Door
418
419 local name = "protector:door_wood"
420
421 doors.register_door(name, {
422 description = "Protected Wooden Door",
423 inventory_image = "door_wood.png^protector_logo.png",
424 groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=2,door=1},
425 tiles_bottom = {"door_wood_b.png^protector_logo.png", "door_brown.png"},
426 tiles_top = {"door_wood_a.png", "door_brown.png"},
427 sounds = default.node_sound_wood_defaults(),
428 sunlight = false,
429 })
430
431 minetest.override_item(name.."_b_1", {
432 on_rightclick = function(pos, node, clicker)
433 if not minetest.is_protected(pos, clicker:get_player_name()) then
434 on_rightclick(pos, 1, name.."_t_1", name.."_b_2", name.."_t_2", {1,2,3,0})
435 end
436 end,
437 })
438
439 minetest.override_item(name.."_t_1", {
440 on_rightclick = function(pos, node, clicker)
441 if not minetest.is_protected(pos, clicker:get_player_name()) then
442 on_rightclick(pos, -1, name.."_b_1", name.."_t_2", name.."_b_2", {1,2,3,0})
443 end
444 end,
445 })
446
447 minetest.override_item(name.."_b_2", {
448 on_rightclick = function(pos, node, clicker)
449 if not minetest.is_protected(pos, clicker:get_player_name()) then
450 on_rightclick(pos, 1, name.."_t_2", name.."_b_1", name.."_t_1", {3,0,1,2})
451 end
452 end,
453 })
454
455 minetest.override_item(name.."_t_2", {
456 on_rightclick = function(pos, node, clicker)
457 if not minetest.is_protected(pos, clicker:get_player_name()) then
458 on_rightclick(pos, -1, name.."_b_2", name.."_t_1", name.."_b_1", {3,0,1,2})
459 end
460 end,
461 })
462
463 minetest.register_craft({
464 output = name,
465 recipe = {
466 {"group:wood", "group:wood"},
467 {"group:wood", "default:copper_ingot"},
468 {"group:wood", "group:wood"}
469 }
470 })
471
472 minetest.register_craft({
473 output = name,
474 recipe = {
475 {"doors:door_wood", "default:copper_ingot"}
476 }
477 })
478
479 -- Protected Steel Door
480
481 local name = "protector:door_steel"
482
483 doors.register_door(name, {
484 description = "Protected Steel Door",
485 inventory_image = "door_steel.png^protector_logo.png",
486 groups = {snappy=1,bendy=2,cracky=1,melty=2,level=2,door=1},
487 tiles_bottom = {"door_steel_b.png^protector_logo.png", "door_grey.png"},
488 tiles_top = {"door_steel_a.png", "door_grey.png"},
489 sounds = default.node_sound_wood_defaults(),
490 sunlight = false,
491 })
492
493 minetest.override_item(name.."_b_1", {
494 on_rightclick = function(pos, node, clicker)
495 if not minetest.is_protected(pos, clicker:get_player_name()) then
496 on_rightclick(pos, 1, name.."_t_1", name.."_b_2", name.."_t_2", {1,2,3,0})
497 end
498 end,
499 })
500
501 minetest.override_item(name.."_t_1", {
502 on_rightclick = function(pos, node, clicker)
503 if not minetest.is_protected(pos, clicker:get_player_name()) then
504 on_rightclick(pos, -1, name.."_b_1", name.."_t_2", name.."_b_2", {1,2,3,0})
505 end
506 end,
507 })
508
509 minetest.override_item(name.."_b_2", {
510 on_rightclick = function(pos, node, clicker)
511 if not minetest.is_protected(pos, clicker:get_player_name()) then
512 on_rightclick(pos, 1, name.."_t_2", name.."_b_1", name.."_t_1", {3,0,1,2})
513 end
514 end,
515 })
516
517 minetest.override_item(name.."_t_2", {
518 on_rightclick = function(pos, node, clicker)
519 if not minetest.is_protected(pos, clicker:get_player_name()) then
520 on_rightclick(pos, -1, name.."_b_2", name.."_t_1", name.."_b_1", {3,0,1,2})
521 end
522 end,
523 })
524
525 minetest.register_craft({
526 output = name,
527 recipe = {
528 {"default:steel_ingot", "default:steel_ingot"},
529 {"default:steel_ingot", "default:copper_ingot"},
530 {"default:steel_ingot", "default:steel_ingot"}
531 }
532 })
533
534 minetest.register_craft({
535 output = name,
536 recipe = {
537 {"doors:door_steel", "default:copper_ingot"}
538 }
539 })
540
541 local function get_locked_chest_formspec(pos)
542 local spos = pos.x .. "," .. pos.y .. "," ..pos.z
543 local formspec =
544 "size[8,9]"..
545 default.gui_bg..
546 default.gui_bg_img..
547 default.gui_slots..
548 "list[nodemeta:".. spos .. ";main;0,0.3;8,4;]"..
549 "button[0,4.5;2,0.25;toup;To Chest]"..
550 "field[2.3,4.8;4,0.25;chestname;;]"..
551 "button[6,4.5;2,0.25;todn;To Inventory]"..
552 "list[current_player;main;0,5;8,1;]"..
553 "list[current_player;main;0,6.08;8,3;8]"..
554 default.get_hotbar_bg(0,5)
555 return formspec
556 end
557
558 -- Protected Chest
559
560 minetest.register_node("protector:chest", {
561 description = "Protected Chest",
562 tiles = {"default_chest_top.png", "default_chest_top.png", "default_chest_side.png",
563 "default_chest_side.png", "default_chest_side.png", "default_chest_front.png^protector_logo.png"},
564 paramtype2 = "facedir",
565 groups = {choppy=2,oddly_breakable_by_hand=2},
566 legacy_facedir_simple = true,
567 is_ground_content = false,
568 sounds = default.node_sound_wood_defaults(),
569 on_construct = function(pos)
570 local meta = minetest.get_meta(pos)
571 meta:set_string("infotext", "Protected Chest")
572 local inv = meta:get_inventory()
573 inv:set_size("main", 8*4)
574 end,
575 can_dig = function(pos,player)
576 local meta = minetest.get_meta(pos)
577 local inv = meta:get_inventory()
578 if inv:is_empty("main") then
579 if not minetest.is_protected(pos, player:get_player_name()) then
580 return true
581 end
582 end
583 end,
584 allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
585 return count
586 end,
587 allow_metadata_inventory_put = function(pos, listname, index, stack, player)
588 return stack:get_count()
589 end,
590 allow_metadata_inventory_take = function(pos, listname, index, stack, player)
591 return stack:get_count()
592 end,
593 on_metadata_inventory_put = function(pos, listname, index, stack, player)
594 minetest.log("action", player:get_player_name()..
595 " moves stuff to protected chest at "..minetest.pos_to_string(pos))
596 end,
597 on_metadata_inventory_take = function(pos, listname, index, stack, player)
598 minetest.log("action", player:get_player_name()..
599 " takes stuff from protected chest at "..minetest.pos_to_string(pos))
600 end,
601 on_rightclick = function(pos, node, clicker)
602 local meta = minetest.get_meta(pos)
603 if not minetest.is_protected(pos, clicker:get_player_name()) then
604 minetest.show_formspec(
605 clicker:get_player_name(),
606 "protector:chest_"..minetest.pos_to_string(pos),
607 get_locked_chest_formspec(pos)
608 )
609 end
610 end,
611 })
612
613 -- Protected Chest formspec buttons
614
615 minetest.register_on_player_receive_fields(function(player,formname,fields)
616
617 if string.sub(formname,0,string.len("protector:chest_")) == "protector:chest_" then
618
619 local pos_s = string.sub(formname,string.len("protector:chest_")+1)
620 local pos = minetest.string_to_pos(pos_s)
621 local meta = minetest.get_meta(pos)
622
623 local chest_inv = meta:get_inventory()
624 local player_inv = player:get_inventory()
625
626 if fields.toup then
627
628 -- copy contents of players inventory to chest
629 for i,v in ipairs( player_inv:get_list( "main" ) or {}) do
630 if( chest_inv and chest_inv:room_for_item('main', v)) then
631 local leftover = chest_inv:add_item( 'main', v )
632 player_inv:remove_item( "main", v )
633 if( leftover and not( leftover:is_empty() )) then
634 player_inv:add_item( "main", v )
635 end
636 end
637 end
638
639 elseif fields.todn then
640
641 -- copy contents of chest to players inventory
642 for i,v in ipairs( chest_inv:get_list( 'main' ) or {}) do
643 if( player_inv:room_for_item( "main", v)) then
644 local leftover = player_inv:add_item( "main", v )
645 chest_inv:remove_item( 'main', v )
646 if( leftover and not( leftover:is_empty() )) then
647 chest_inv:add_item( 'main', v )
648 end
649 end
650 end
651
652 elseif fields.chestname then
653
654 -- change chest infotext to display name
655 if fields.chestname ~= "" then
656 meta:set_string("infotext", "Protected Chest ("..fields.chestname..")")
657 else
658 meta:set_string("infotext", "Protected Chest")
659 end
660
661 end
662 end
663
664 end)
665
666 -- Protected Chest recipe
667
668 minetest.register_craft({
669 output = 'protector:chest',
670 recipe = {
671 {'group:wood', 'group:wood', 'group:wood'},
672 {'group:wood', 'default:copper_ingot', 'group:wood'},
673 {'group:wood', 'group:wood', 'group:wood'},
674 }
675 })
676
677 minetest.register_craft({
678 output = 'protector:chest',
679 recipe = {
680 {'default:chest', 'default:copper_ingot', ''},
681 }
682 })