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