init.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. if not minetest.global_exists("torches") then torches = {} end
  2. torches.modpath = minetest.get_modpath("torches")
  3. function torches.node_supports_torch(name, def)
  4. if name == "protector:protect" or name == "protector:protect3" then
  5. return true
  6. end
  7. local dt = def.drawtype
  8. if dt == "normal" or dt == "glasslike" or dt == "glasslike_framed" or
  9. dt == "glasslike_framed_optional" or dt == "allfaces" or
  10. dt == "allfaces_optional" then
  11. return true
  12. elseif minetest.get_item_group(name, "tree") ~= 0 then
  13. return true
  14. elseif string.find(name, "^maptools:") then
  15. return true
  16. end
  17. return false
  18. end
  19. function torches.put_torch(itemstack, placer, pt, only_wall)
  20. local under = pt.under
  21. local above = pt.above
  22. local node = minetest.get_node(under)
  23. local ndef = minetest.reg_ns_nodes[node.name]
  24. -- Call on_rightclick if target node defines it.
  25. if ndef and ndef.on_rightclick and
  26. ((not placer) or (placer and not placer:get_player_control().sneak)) then
  27. return ndef.on_rightclick(under, node, placer, itemstack, pt) or itemstack
  28. end
  29. -- Check if we can relight an existing torch.
  30. -- If we get an itemstack (and not nil), then we succeeded.
  31. --
  32. -- This code is also used by unlit torches, so we must make sure the itemstack
  33. -- is NOT in the unlit torch group. Otherwise you could replace/relight
  34. -- torches with unlit ones.
  35. --
  36. -- Note: burnt-out torches can be relit/replaced even if protected.
  37. if pt.type == "node" and minetest.get_item_group(itemstack:get_name(), "torch_unlit") == 0 then
  38. local tpos = minetest.find_node_near(pt.under, 0, "group:torch_unlit", true)
  39. if not tpos then
  40. tpos = minetest.find_node_near(pt.above, 0, "group:torch_unlit", true)
  41. end
  42. if tpos then
  43. --minetest.chat_send_all('test')
  44. local fake_pt = {
  45. type = "node",
  46. under = tpos,
  47. above = tpos,
  48. }
  49. local stack = torches.on_use(itemstack, placer, fake_pt)
  50. if stack then return stack end
  51. end
  52. end
  53. local def = minetest.reg_ns_nodes[itemstack:get_name()]
  54. local good = false
  55. if def then
  56. if def._torches_node_ceiling and def._torches_node_floor and def._torches_node_wall then
  57. good = true
  58. end
  59. end
  60. if not good then
  61. return itemstack
  62. end
  63. -- If node under is buildable_to, place into it instead (eg. snow)
  64. local place_to = above
  65. if ndef and ndef.buildable_to then
  66. place_to = under
  67. end
  68. local place_in_non_air = (minetest.get_node(place_to).name ~= "air")
  69. local wdir = minetest.dir_to_wallmounted(vector.subtract(under, above))
  70. if only_wall then
  71. if wdir == 0 or wdir == 1 then
  72. return itemstack
  73. end
  74. end
  75. local fakestack = itemstack
  76. local torch
  77. if wdir == 0 then
  78. torch = def._torches_node_ceiling
  79. elseif wdir == 1 then
  80. torch = def._torches_node_floor
  81. else
  82. torch = def._torches_node_wall
  83. end
  84. fakestack:set_name(torch)
  85. -- Protection check is done by this function.
  86. itemstack = minetest.item_place_node(fakestack, placer, pt, wdir)
  87. itemstack:set_name(def._torches_node_floor)
  88. return itemstack
  89. end
  90. function torches.on_use(itemstack, user, pointed_thing)
  91. if not user or not user:is_player() then return end
  92. if pointed_thing.type ~= "node" then return end
  93. --minetest.chat_send_all('test1')
  94. local torchtype = nil
  95. local ndef = minetest.registered_nodes[minetest.get_node(pointed_thing.under).name]
  96. if not ndef or not ndef.drop then return end
  97. if type(ndef.drop) == "string" then torchtype = ndef.drop end
  98. if not torchtype then return end
  99. --minetest.chat_send_all('test2')
  100. local fakestack = ItemStack("dusts:coal")
  101. fakestack = real_torch.relight(fakestack, user, pointed_thing)
  102. if not fakestack or fakestack:get_count() ~= 0 then return end
  103. -- Add unlit torch to player's inventory OUTSIDE this function's stack frame.
  104. local pname = user:get_player_name()
  105. local pos = pointed_thing.under
  106. minetest.after(0, function()
  107. local user = minetest.get_player_by_name(pname)
  108. if not user then return end
  109. local inv = user:get_inventory()
  110. local leftover = inv:add_item("main", ItemStack(torchtype))
  111. if not leftover:is_empty() then minetest.add_item(pos, leftover) end
  112. end)
  113. --minetest.chat_send_all('test3')
  114. itemstack:take_item()
  115. return itemstack
  116. end
  117. if not torches.run_once then
  118. dofile(torches.modpath .. "/iron_torch.lua")
  119. dofile(torches.modpath .. "/cave_torch.lua")
  120. dofile(torches.modpath .. "/perma_torch.lua")
  121. dofile(torches.modpath .. "/kalite_torch.lua")
  122. dofile(torches.modpath .. "/perma_ktorch.lua")
  123. local c = "torches:core"
  124. local f = torches.modpath .. "/init.lua"
  125. reload.register_file(c, f, false)
  126. torches.run_once = true
  127. end