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