init.lua 4.0 KB

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