bugfix
[protector.git] / init.lua
1 minetest.register_privilege("delprotect","Ignore other players 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 if meta:get_int("page") == nil then meta:set_int("page",0) end
48 local formspec = "size[8,7]"
49 .."label[0,0;-- Protector interface --]"
50 .."label[0,1;Punch node to show protected area]"
51 .."label[0,2;Members: (type nick, press Enter to add)]"
52 members = protector.get_member_list(meta)
53
54 local npp = 12 -- was 15, names per page
55 local s = 0
56 local i = 0
57 for _, member in ipairs(members) do
58 if s < meta:get_int("page")*15 then s = s +1 else
59 if i < npp then
60 formspec = formspec .. "button["..(i%4*2)..","
61 ..math.floor(i/4+3)..";1.5,.5;protector_member;"..member.."]"
62 formspec = formspec .. "button["..(i%4*2+1.25)..","
63 ..math.floor(i/4+3)..";.75,.5;protector_del_member_"..member..";X]"
64 end
65 i = i +1
66 end
67 end
68 local add_i = i
69 if add_i < npp then
70 formspec = formspec
71 .."field["..(add_i%4*2+1/3)..","..(math.floor(add_i/4+3)+1/3)..";1.433,.5;protector_add_member;;]"
72 end
73 formspec = formspec.."button_exit[1,6.2;2,0.5;close_me;<< Back]"
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.env: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.env:get_meta(positions[1])
132 minetest.chat_send_player(whois,"This area is owned by "..meta:get_string("owner")..".")
133 if meta:get_string("members") ~= "" then
134 minetest.chat_send_player(whois,"Members: "..meta:get_string("members")..".")
135 end
136 end
137 minetest.chat_send_player(whois,"You can build here.")
138 end
139 return true
140 end
141
142 -- Can node be added or removed, if so return node else true (for protected)
143
144 protector.old_is_protected = minetest.is_protected
145 minetest.is_protected = function(pos, digger)
146
147 if protector.can_dig(protector.radius, pos, digger, false, 1) then
148 return protector.old_is_protected(pos, digger)
149 else
150 return true
151 end
152 end
153
154 -- Make sure protection block doesn't overlap another protector's area
155
156 protector.old_node_place = minetest.item_place
157 function minetest.item_place(itemstack, placer, pointed_thing)
158
159 if itemstack:get_name() == "protector:protect" or itemstack:get_name() == "protector:protect2" then
160 local pos = pointed_thing.above
161 local user = placer:get_player_name()
162 if not protector.can_dig(protector.radius * 2, pos, user, true, 3) then
163 minetest.chat_send_player(placer:get_player_name(),"Overlaps into another protected area")
164 return protector.old_node_place(itemstack, placer, pos)
165 end
166 end
167
168 return protector.old_node_place(itemstack, placer, pointed_thing)
169 end
170
171 -- END
172
173 --= Protection Block
174
175 minetest.register_node("protector:protect", {
176 description = "Protection Block",
177 tiles = {"protector_top.png","protector_top.png","protector_side.png"},
178 sounds = default.node_sound_stone_defaults(),
179 groups = {dig_immediate=2},
180 drawtype = "nodebox",
181 node_box = {
182 type="fixed",
183 fixed = { -0.5, -0.5, -0.5, 0.5, 0.5, 0.5 },
184 },
185 selection_box = { type="regular" },
186 paramtype = "light",
187
188 after_place_node = function(pos, placer)
189 local meta = minetest.env:get_meta(pos)
190 meta:set_string("owner", placer:get_player_name() or "")
191 meta:set_string("infotext", "Protection (owned by "..
192 meta:get_string("owner")..")")
193 meta:set_string("members", "")
194 end,
195
196 on_use = function(itemstack, user, pointed_thing)
197 if pointed_thing.type ~= "node" then
198 return
199 end
200
201 protector.can_dig(5,pointed_thing.under,user:get_player_name(),false,2)
202 end,
203
204 on_rightclick = function(pos, node, clicker, itemstack)
205 local meta = minetest.env:get_meta(pos)
206 if protector.can_dig(1,pos,clicker:get_player_name(),true,1) then
207 minetest.show_formspec(clicker:get_player_name(),
208 "protector_"..minetest.pos_to_string(pos), protector.generate_formspec(meta)
209 )
210 end
211 end,
212
213 on_punch = function(pos, node, puncher)
214 if not protector.can_dig(1,pos,puncher:get_player_name(),true,1) then
215 return
216 end
217
218 minetest.env:add_entity(pos, "protector:display")
219 minetest.env:get_node_timer(pos):start(10)
220 end,
221
222 on_timer = function(pos)
223 local objs = minetest.env:get_objects_inside_radius(pos,.5)
224 for _, o in pairs(objs) do
225 if (not o:is_player()) and o:get_luaentity().name == "protector:display" then
226 o:remove()
227 end
228 end
229 end,
230 })
231
232 minetest.register_craft({
233 output = "protector:protect 4",
234 recipe = {
235 {"default:stone","default:stone","default:stone"},
236 {"default:stone","default:steel_ingot","default:stone"},
237 {"default:stone","default:stone","default:stone"},
238 }
239 })
240
241 --= Protection Logo
242
243 minetest.register_node("protector:protect2", {
244 description = "Protection Logo",
245 tiles = {"protector_logo.png"},
246 wield_image = "protector_logo.png",
247 inventory_image = "protector_logo.png",
248 sounds = default.node_sound_stone_defaults(),
249 groups = {dig_immediate=2},
250 paramtype = 'light',
251 paramtype2 = "wallmounted",
252 drawtype = "nodebox",
253 sunlight_propagates = true,
254 walkable = true,
255 node_box = {
256 type = "wallmounted",
257 wall_top = {-0.375, 0.4375, -0.5, 0.375, 0.5, 0.5},
258 wall_bottom = {-0.375, -0.5, -0.5, 0.375, -0.4375, 0.5},
259 wall_side = {-0.5, -0.5, -0.375, -0.4375, 0.5, 0.375},
260 },
261 selection_box = {type = "wallmounted"},
262
263 after_place_node = function(pos, placer)
264 local meta = minetest.env:get_meta(pos)
265 meta:set_string("owner", placer:get_player_name() or "")
266 meta:set_string("infotext", "Protection (owned by "..
267 meta:get_string("owner")..")")
268 meta:set_string("members", "")
269 end,
270
271 on_use = function(itemstack, user, pointed_thing)
272 if pointed_thing.type ~= "node" then
273 return
274 end
275
276 protector.can_dig(5,pointed_thing.under,user:get_player_name(),false,2)
277 end,
278
279 on_rightclick = function(pos, node, clicker, itemstack)
280 local meta = minetest.env:get_meta(pos)
281 if protector.can_dig(1,pos,clicker:get_player_name(),true,1) then
282 minetest.show_formspec(clicker:get_player_name(),
283 "protector_"..minetest.pos_to_string(pos), protector.generate_formspec(meta)
284 )
285 end
286 end,
287
288 on_punch = function(pos, node, puncher)
289 if not protector.can_dig(1,pos,puncher:get_player_name(),true,1) then
290 return
291 end
292
293 minetest.env:add_entity(pos, "protector:display")
294 minetest.env:get_node_timer(pos):start(10)
295 end,
296
297 on_timer = function(pos)
298 local objs = minetest.env:get_objects_inside_radius(pos,.5)
299 for _, o in pairs(objs) do
300 if (not o:is_player()) and o:get_luaentity().name == "protector:display" then
301 o:remove()
302 end
303 end
304 end,
305 })
306
307 minetest.register_craft({
308 output = "protector:protect2 4",
309 recipe = {
310 {"default:stone","default:stone","default:stone"},
311 {"default:stone","default:copper_ingot","default:stone"},
312 {"default:stone","default:stone","default:stone"},
313 }
314 })
315
316 -- If name entered into protector formspec
317
318 minetest.register_on_player_receive_fields(function(player,formname,fields)
319 if string.sub(formname,0,string.len("protector_")) == "protector_" then
320 local pos_s = string.sub(formname,string.len("protector_")+1)
321 local pos = minetest.string_to_pos(pos_s)
322 local meta = minetest.env:get_meta(pos)
323
324 if meta:get_int("page") == nil then meta:set_int("page",0) end
325
326 if not protector.can_dig(1,pos,player:get_player_name(),true,1) then
327 return
328 end
329
330 if fields.protector_add_member then
331 for _, i in ipairs(fields.protector_add_member:split(" ")) do
332 protector.add_member(meta,i)
333 end
334 end
335
336 for field, value in pairs(fields) do
337 if string.sub(field,0,string.len("protector_del_member_"))=="protector_del_member_" then
338 protector.del_member(meta, string.sub(field,string.len("protector_del_member_")+1))
339 end
340 end
341
342 if fields.close_me then
343 meta:set_int("page",meta:get_int("page"))
344 else minetest.show_formspec(player:get_player_name(), formname, protector.generate_formspec(meta))
345 end
346 end
347 end)
348
349 minetest.register_entity("protector:display", {
350 physical = false,
351 collisionbox = {0,0,0,0,0,0},
352 visual = "wielditem",
353 visual_size = {x=1.0/1.5,y=1.0/1.5}, -- wielditem seems to be scaled to 1.5 times original node size
354 textures = {"protector:display_node"},
355 on_step = function(self, dtime)
356 local nam = minetest.get_node(self.object:getpos()).name
357 if nam ~= "protector:protect" and nam ~= "protector:protect2" then
358 self.object:remove()
359 return
360 end
361 end,
362 })
363
364 -- Display-zone node, Do NOT place the display as a node, it is made to be used as an entity (see above)
365 local x = protector.radius
366 minetest.register_node("protector:display_node", {
367 tiles = {"protector_display.png"},
368 use_texture_alpha = true,
369 walkable = false,
370 drawtype = "nodebox",
371 node_box = {
372 type = "fixed",
373 fixed = {
374 -- sides
375 {-(x+.55), -(x+.55), -(x+.55), -(x+.45), (x+.55), (x+.55)},
376 {-(x+.55), -(x+.55), (x+.45), (x+.55), (x+.55), (x+.55)},
377 {(x+.45), -(x+.55), -(x+.55), (x+.55), (x+.55), (x+.55)},
378 {-(x+.55), -(x+.55), -(x+.55), (x+.55), (x+.55), -(x+.45)},
379 -- top
380 {-(x+.55), (x+.45), -(x+.55), (x+.55), (x+.55), (x+.55)},
381 -- bottom
382 {-(x+.55), -(x+.55), -(x+.55), (x+.55), -(x+.45), (x+.55)},
383 -- middle (surround protector)
384 {-.55,-.55,-.55, .55,.55,.55},
385 },
386 },
387 selection_box = {
388 type = "regular",
389 },
390 paramtype = "light",
391 groups = {dig_immediate=3,not_in_creative_inventory=1},
392 drop = "",
393 })
394
395 -- Register Protected Doors
396
397 local function on_rightclick(pos, dir, check_name, replace, replace_dir, params)
398 pos.y = pos.y+dir
399 if not minetest.get_node(pos).name == check_name then
400 return
401 end
402 local p2 = minetest.get_node(pos).param2
403 p2 = params[p2+1]
404
405 minetest.swap_node(pos, {name=replace_dir, param2=p2})
406
407 pos.y = pos.y-dir
408 minetest.swap_node(pos, {name=replace, param2=p2})
409
410 local snd_1 = "door_close"
411 local snd_2 = "door_open"
412 if params[1] == 3 then
413 snd_1 = "door_open"
414 snd_2 = "door_close"
415 end
416
417 if minetest.get_meta(pos):get_int("right") ~= 0 then
418 minetest.sound_play(snd_1, {pos = pos, gain = 0.3, max_hear_distance = 10})
419 else
420 minetest.sound_play(snd_2, {pos = pos, gain = 0.3, max_hear_distance = 10})
421 end
422 end
423
424 -- Protected Wooden Door
425
426 local name = "protector:door_wood"
427
428 doors.register_door(name, {
429 description = "Protected Wooden Door",
430 inventory_image = "door_wood.png",
431 groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=2,door=1},
432 tiles_bottom = {"door_wood_b.png^protector_logo.png", "door_brown.png"},
433 tiles_top = {"door_wood_a.png", "door_brown.png"},
434 sounds = default.node_sound_wood_defaults(),
435 sunlight = false,
436 })
437
438 minetest.override_item(name.."_b_1", {
439 on_rightclick = function(pos, node, clicker)
440 if not minetest.is_protected(pos, clicker:get_player_name()) then
441 on_rightclick(pos, 1, name.."_t_1", name.."_b_2", name.."_t_2", {1,2,3,0})
442 end
443 end,
444 })
445
446 minetest.override_item(name.."_t_1", {
447 on_rightclick = function(pos, node, clicker)
448 if not minetest.is_protected(pos, clicker:get_player_name()) then
449 on_rightclick(pos, -1, name.."_b_1", name.."_t_2", name.."_b_2", {1,2,3,0})
450 end
451 end,
452 })
453
454 minetest.override_item(name.."_b_2", {
455 on_rightclick = function(pos, node, clicker)
456 if not minetest.is_protected(pos, clicker:get_player_name()) then
457 on_rightclick(pos, 1, name.."_t_2", name.."_b_1", name.."_t_1", {3,0,1,2})
458 end
459 end,
460 })
461
462 minetest.override_item(name.."_t_2", {
463 on_rightclick = function(pos, node, clicker)
464 if not minetest.is_protected(pos, clicker:get_player_name()) then
465 on_rightclick(pos, -1, name.."_b_2", name.."_t_1", name.."_b_1", {3,0,1,2})
466 end
467 end,
468 })
469
470 minetest.register_craft({
471 output = name,
472 recipe = {
473 {"group:wood", "group:wood"},
474 {"group:wood", "default:copper_ingot"},
475 {"group:wood", "group:wood"}
476 }
477 })
478
479 minetest.register_craft({
480 output = name,
481 recipe = {
482 {"doors:door_wood", "default:copper_ingot"}
483 }
484 })
485
486 -- Protected Steel Door
487
488 local name = "protector:door_steel"
489
490 doors.register_door(name, {
491 description = "Protected Steel Door",
492 inventory_image = "door_steel.png",
493 groups = {snappy=1,bendy=2,cracky=1,melty=2,level=2,door=1},
494 tiles_bottom = {"door_steel_b.png^protector_logo.png", "door_grey.png"},
495 tiles_top = {"door_steel_a.png", "door_grey.png"},
496 sounds = default.node_sound_wood_defaults(),
497 sunlight = false,
498 })
499
500 minetest.override_item(name.."_b_1", {
501 on_rightclick = function(pos, node, clicker)
502 if not minetest.is_protected(pos, clicker:get_player_name()) then
503 on_rightclick(pos, 1, name.."_t_1", name.."_b_2", name.."_t_2", {1,2,3,0})
504 end
505 end,
506 })
507
508 minetest.override_item(name.."_t_1", {
509 on_rightclick = function(pos, node, clicker)
510 if not minetest.is_protected(pos, clicker:get_player_name()) then
511 on_rightclick(pos, -1, name.."_b_1", name.."_t_2", name.."_b_2", {1,2,3,0})
512 end
513 end,
514 })
515
516 minetest.override_item(name.."_b_2", {
517 on_rightclick = function(pos, node, clicker)
518 if not minetest.is_protected(pos, clicker:get_player_name()) then
519 on_rightclick(pos, 1, name.."_t_2", name.."_b_1", name.."_t_1", {3,0,1,2})
520 end
521 end,
522 })
523
524 minetest.override_item(name.."_t_2", {
525 on_rightclick = function(pos, node, clicker)
526 if not minetest.is_protected(pos, clicker:get_player_name()) then
527 on_rightclick(pos, -1, name.."_b_2", name.."_t_1", name.."_b_1", {3,0,1,2})
528 end
529 end,
530 })
531
532 minetest.register_craft({
533 output = name,
534 recipe = {
535 {"default:steel_ingot", "default:steel_ingot"},
536 {"default:steel_ingot", "default:copper_ingot"},
537 {"default:steel_ingot", "default:steel_ingot"}
538 }
539 })
540
541 minetest.register_craft({
542 output = name,
543 recipe = {
544 {"doors:door_steel", "default:copper_ingot"}
545 }
546 })
547
548 -- Protected Chest
549 minetest.register_node("protector:chest", {
550 description = "Protected Chest",
551 tiles = {"default_chest_top.png", "default_chest_top.png", "default_chest_side.png",
552 "default_chest_side.png", "default_chest_side.png", "default_chest_front.png^protector_logo.png"},
553 paramtype2 = "facedir",
554 groups = {choppy=2,oddly_breakable_by_hand=2},
555 legacy_facedir_simple = true,
556 is_ground_content = false,
557 sounds = default.node_sound_wood_defaults(),
558 on_construct = function(pos)
559 local meta = minetest.get_meta(pos)
560 meta:set_string("infotext", "Protected Chest")
561 local inv = meta:get_inventory()
562 inv:set_size("main", 8*4)
563 end,
564 can_dig = function(pos,player)
565 local meta = minetest.get_meta(pos)
566 local inv = meta:get_inventory()
567 if inv:is_empty("main") then
568 if not minetest.is_protected(pos, player:get_player_name()) then
569 return true
570 end
571 end
572 end,
573 allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
574 return count
575 end,
576 allow_metadata_inventory_put = function(pos, listname, index, stack, player)
577 return stack:get_count()
578 end,
579 allow_metadata_inventory_take = function(pos, listname, index, stack, player)
580 return stack:get_count()
581 end,
582 on_metadata_inventory_put = function(pos, listname, index, stack, player)
583 minetest.log("action", player:get_player_name()..
584 " moves stuff to protected chest at "..minetest.pos_to_string(pos))
585 end,
586 on_metadata_inventory_take = function(pos, listname, index, stack, player)
587 minetest.log("action", player:get_player_name()..
588 " takes stuff from protected chest at "..minetest.pos_to_string(pos))
589 end,
590 on_rightclick = function(pos, node, clicker)
591 local meta = minetest.get_meta(pos)
592 if not minetest.is_protected(pos, clicker:get_player_name()) then
593 minetest.show_formspec(
594 clicker:get_player_name(),
595 "default:chest_locked",
596 default.get_locked_chest_formspec(pos)
597 )
598 end
599 end,
600 })
601
602 minetest.register_craft({
603 output = 'protector:chest',
604 recipe = {
605 {'group:wood', 'group:wood', 'group:wood'},
606 {'group:wood', 'default:copper_ingot', 'group:wood'},
607 {'group:wood', 'group:wood', 'group:wood'},
608 }
609 })