init.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. admin_tnt = admin_tnt or {}
  2. admin_tnt.modpath = minetest.get_modpath("admin_tnt")
  3. admin_tnt.explode_time = 60*60*24*3+60*60*3
  4. admin_tnt.step_time = 30
  5. -- Development protection.
  6. --if not minetest.is_singleplayer() then
  7. -- return
  8. --end
  9. local function time_to_string(time)
  10. local floor = math.floor
  11. local mod = math.mod
  12. local days = floor(time/86400)
  13. local hours = floor(floor(time % 86400)/3600)
  14. local minutes = floor(floor(time % 3600)/60)
  15. local sday = "days"
  16. local shour = "hours"
  17. local smin = "minutes"
  18. if days == 1 then sday = "day" end
  19. if hours == 1 then shour = "hour" end
  20. if minutes == 1 then smin = "minute" end
  21. return days .. " " .. sday .. ", " ..
  22. hours .. " " .. shour .. ", " ..
  23. minutes .. " " .. smin
  24. end
  25. function admin_tnt.on_construct(pos)
  26. end
  27. function admin_tnt.on_timer(pos, elapsed)
  28. local meta = minetest.get_meta(pos)
  29. local time = meta:get_int("explode_in")
  30. time = time - elapsed
  31. if time <= 0 then
  32. time = 0
  33. end
  34. meta:set_int("explode_in", time)
  35. if time <= 0 then
  36. -- Who was this bomb marked for?
  37. local keyname = meta:get_string("keyname") or ""
  38. minetest.remove_node(pos) -- Remove TNT node.
  39. -- Explode. Placed in minetest.after() to avoid callstack issues.
  40. minetest.after(0, function()
  41. tnt.boom(pos, {
  42. radius = 4,
  43. ignore_protection = false,
  44. ignore_on_blast = true, -- Necessary to get rid of protectors.
  45. damage_radius = 6,
  46. disable_drops = true,
  47. name = keyname, -- Only destroy stuff owned by this player, or no-one.
  48. })
  49. end)
  50. end
  51. meta:set_string("infotext", "Admin TNT will explode in " .. time_to_string(time) .. ".")
  52. return true -- Run timer again for same timeout value.
  53. end
  54. function admin_tnt.after_place_node(pos, placer, itemstack, pt)
  55. local pname = placer and placer:get_player_name() or ""
  56. -- Make sure tnt was placed by admin, otherwise remove.
  57. if not gdac.player_is_admin(pname) then
  58. minetest.remove_node(pos)
  59. return
  60. end
  61. local et = math.floor(admin_tnt.explode_time)
  62. -- Start nodetimer.
  63. local timer = minetest.get_node_timer(pos)
  64. timer:start(admin_tnt.step_time)
  65. local meta = minetest.get_meta(pos)
  66. meta:set_string("infotext", "Admin TNT will explode in " .. time_to_string(et) .. ".")
  67. meta:set_int("explode_in", et)
  68. -- Admin TNT becomes keyed to the owner of the area it is placed in.
  69. -- If no owner, then TNT is not keyed to anybody.
  70. local keyname = protector.get_node_owner(pos) or ""
  71. meta:set_string("keyname", keyname)
  72. -- Mark as private!
  73. meta:mark_as_private({"keyname", "explode_in"})
  74. end
  75. -- Does nothing when blasted.
  76. function admin_tnt.on_blast(pos, intensity)
  77. end
  78. function admin_tnt.can_dig(pos, player)
  79. return false
  80. end
  81. if not admin_tnt.registered then
  82. -- There is no craft recipe for this item.
  83. minetest.register_node("admin_tnt:tnt", {
  84. description = "Admin TNT\n\nWarning: will destroy protectors and bedrock!\nIgnores `on_blast' callbacks.\nFor admin use only.\nBlast radius: 4 blocks.\n\nIf placed in a protected area, will only destroy stuff owned by that player.",
  85. tiles = {
  86. "admin_tnt_top.png",
  87. "admin_tnt_bottom.png",
  88. "admin_tnt_side.png",
  89. },
  90. paramtype2 = "facedir",
  91. -- Requires admin pick to remove.
  92. groups = -- comment
  93. {
  94. admin_tnt = 1, immovable = 1, unbreakable = 1,
  95. },
  96. sounds = default.node_sound_wood_defaults(),
  97. drop = "",
  98. diggable = false,
  99. always_protected = true, -- Cannot be removed without protection bypass. Protector mod handles this.
  100. on_construct = function(...)
  101. return admin_tnt.on_construct(...)
  102. end,
  103. after_place_node = function(...)
  104. return admin_tnt.after_place_node(...)
  105. end,
  106. on_timer = function(...)
  107. return admin_tnt.on_timer(...)
  108. end,
  109. on_blast = function(...)
  110. return admin_tnt.on_blast(...)
  111. end,
  112. can_dig = function(...)
  113. return admin_tnt.can_dig(...)
  114. end,
  115. })
  116. local c = "admin_tnt:core"
  117. local f = admin_tnt.modpath .. "/init.lua"
  118. reload.register_file(c, f, false)
  119. admin_tnt.registered = true
  120. end