init.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. })
  51. end)
  52. end
  53. meta:set_string("infotext", "Admin TNT will explode in " .. time_to_string(time) .. ".")
  54. return true -- Run timer again for same timeout value.
  55. end
  56. function admin_tnt.after_place_node(pos, placer, itemstack, pt)
  57. local pname = placer and placer:get_player_name() or ""
  58. -- Make sure tnt was placed by admin, otherwise remove.
  59. if not gdac.player_is_admin(pname) then
  60. minetest.remove_node(pos)
  61. return
  62. end
  63. local et = math_floor(admin_tnt.explode_time)
  64. -- Start nodetimer.
  65. local timer = minetest.get_node_timer(pos)
  66. timer:start(admin_tnt.step_time)
  67. local meta = minetest.get_meta(pos)
  68. meta:set_string("infotext", "Admin TNT will explode in " .. time_to_string(et) .. ".")
  69. meta:set_int("explode_in", et)
  70. -- Admin TNT becomes keyed to the owner of the area it is placed in.
  71. -- If no owner, then TNT is not keyed to anybody.
  72. local keyname = protector.get_node_owner(pos) or ""
  73. meta:set_string("keyname", keyname)
  74. -- Mark as private!
  75. meta:mark_as_private({"keyname", "explode_in"})
  76. end
  77. -- Does nothing when blasted.
  78. function admin_tnt.on_blast(pos, intensity)
  79. end
  80. function admin_tnt.can_dig(pos, player)
  81. return false
  82. end
  83. if not admin_tnt.registered then
  84. -- There is no craft recipe for this item.
  85. minetest.register_node("admin_tnt:tnt", {
  86. 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.",
  87. tiles = {
  88. "admin_tnt_top.png",
  89. "admin_tnt_bottom.png",
  90. "admin_tnt_side.png",
  91. },
  92. paramtype2 = "facedir",
  93. -- Requires admin pick to remove.
  94. groups = -- comment
  95. {
  96. admin_tnt = 1, immovable = 1, unbreakable = 1,
  97. },
  98. sounds = default.node_sound_wood_defaults(),
  99. drop = "",
  100. diggable = false,
  101. always_protected = true, -- Cannot be removed without protection bypass. Protector mod handles this.
  102. on_construct = function(...)
  103. return admin_tnt.on_construct(...)
  104. end,
  105. after_place_node = function(...)
  106. return admin_tnt.after_place_node(...)
  107. end,
  108. on_timer = function(...)
  109. return admin_tnt.on_timer(...)
  110. end,
  111. on_blast = function(...)
  112. return admin_tnt.on_blast(...)
  113. end,
  114. can_dig = function(...)
  115. return admin_tnt.can_dig(...)
  116. end,
  117. })
  118. local c = "admin_tnt:core"
  119. local f = admin_tnt.modpath .. "/init.lua"
  120. reload.register_file(c, f, false)
  121. admin_tnt.registered = true
  122. end