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