init.lua 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. -- Minetest 0.4 mod: bucket
  2. -- See README.txt for licensing and other information.
  3. -- Localize for performance.
  4. local math_floor = math.floor
  5. minetest.register_alias("bucket", "bucket:bucket_empty")
  6. minetest.register_alias("bucket_water", "bucket:bucket_water")
  7. minetest.register_alias("bucket_lava", "bucket:bucket_lava")
  8. minetest.register_craft({
  9. output = 'bucket:bucket_empty 1',
  10. recipe = {
  11. {'default:steel_ingot', '', 'default:steel_ingot'},
  12. {'', 'default:steel_ingot', ''},
  13. }
  14. })
  15. minetest.register_craft({
  16. output = 'bucket:bucket_empty 1',
  17. recipe = {
  18. {'moreores:tin_ingot', '', 'moreores:tin_ingot'},
  19. {'', 'moreores:tin_ingot', ''},
  20. }
  21. })
  22. bucket = {}
  23. bucket.liquids = {}
  24. -- Used to harm (and possibly kill) a player *after* the bucket on_use code has run.
  25. -- This solves a problem with being able to duplicate buckets when dying while collecting lava.
  26. -- Note the use of `minetest.after`.
  27. bucket.harm_player_after =
  28. function(pname, harm)
  29. minetest.after(0, function()
  30. local player = minetest.get_player_by_name(pname)
  31. if player and player:is_player() then
  32. player:set_hp(player:get_hp() - harm)
  33. if player:get_hp() == 0 then
  34. minetest.chat_send_all("# Server: <" .. rename.gpn(pname) .. "> died while bucketing something hot!")
  35. end
  36. end
  37. end)
  38. end
  39. local function check_protection(pos, name, text)
  40. local success, gl = rc.get_ground_level_at_pos(pos)
  41. if not success then
  42. minetest.chat_send_player(name, "# Server: That position is in the Void!")
  43. return true
  44. end
  45. -- Don't let players make huge liquid griefs. By MustTest.
  46. -- But we allow river water to be placed above ground, because it does not spread.
  47. if not string.find(text, "river_water_source") then
  48. if pos.y > gl and string.find(text, "place") then
  49. minetest.chat_send_player(name, "# Server: Don't do that above ground!")
  50. easyvend.sound_error(name)
  51. return true
  52. end
  53. end
  54. if minetest.is_protected(pos, name) then
  55. minetest.log("action", (name ~= "" and name or "A mod")
  56. .. " tried to " .. text
  57. .. " at protected position "
  58. .. minetest.pos_to_string(pos)
  59. .. " with a bucket")
  60. minetest.record_protection_violation(pos, name)
  61. minetest.chat_send_player(name, "# Server: Nope. Not on someone else's land!")
  62. return true
  63. end
  64. return false
  65. end
  66. local function node_in_group(name, list)
  67. if type(list) == "string" then
  68. return (name == list)
  69. elseif type(list) == "table" then
  70. for k, v in ipairs(list) do
  71. if name == v then
  72. return true
  73. end
  74. end
  75. end
  76. return false
  77. end
  78. -- Register a new liquid
  79. -- source = name of the source node
  80. -- flowing = name of the flowing node
  81. -- itemname = name of the new bucket item (or nil if liquid is not takeable)
  82. -- inventory_image = texture of the new bucket item (ignored if itemname == nil)
  83. -- name = text description of the bucket item
  84. -- groups = (optional) groups of the bucket item, for example {water_bucket = 1}
  85. -- This function can be called from any mod (that depends on bucket).
  86. function bucket.register_liquid(source, flowing, itemname, placename, inventory_image, name, groups)
  87. if type(source) == "string" then
  88. bucket.liquids[source] = {
  89. source = source,
  90. flowing = flowing,
  91. itemname = itemname,
  92. }
  93. else
  94. assert(type(source) == "table")
  95. for k, v in ipairs(source) do
  96. assert(type(v) == "string")
  97. bucket.liquids[v] = {
  98. source = source,
  99. flowing = flowing,
  100. itemname = itemname,
  101. }
  102. end
  103. end
  104. if type(flowing) == "string" then
  105. bucket.liquids[flowing] = {
  106. source = source,
  107. flowing = flowing,
  108. itemname = itemname,
  109. }
  110. else
  111. assert(type(flowing) == "table")
  112. for k, v in ipairs(flowing) do
  113. assert(type(v) == "string")
  114. bucket.liquids[v] = {
  115. source = source,
  116. flowing = flowing,
  117. itemname = itemname,
  118. }
  119. end
  120. end
  121. if itemname ~= nil then
  122. minetest.register_craftitem(itemname, {
  123. description = name,
  124. inventory_image = inventory_image,
  125. stack_max = 1,
  126. liquids_pointable = true,
  127. groups = groups,
  128. on_place = function(itemstack, user, pointed_thing)
  129. if not user or not user:is_player() then
  130. return itemstack
  131. end
  132. -- Must be pointing to node
  133. if pointed_thing.type ~= "node" then
  134. return
  135. end
  136. local node = minetest.get_node_or_nil(pointed_thing.under)
  137. local ndef = node and minetest.reg_ns_nodes[node.name]
  138. -- Call on_rightclick if the pointed node defines it
  139. if ndef and ndef.on_rightclick and
  140. user and not user:get_player_control().sneak then
  141. return ndef.on_rightclick(pointed_thing.under, node, user, itemstack)
  142. end
  143. local lpos
  144. -- Check if pointing to a buildable node
  145. if ndef and ndef.buildable_to then
  146. -- buildable; replace the node
  147. lpos = pointed_thing.under
  148. elseif node_in_group(node.name, flowing) or node_in_group(node.name, source) then
  149. -- flow version of bucket contents, replace the node.
  150. lpos = pointed_thing.under
  151. else
  152. -- not buildable to; place the liquid above
  153. -- check if the node above can be replaced
  154. lpos = pointed_thing.above
  155. node = minetest.get_node_or_nil(lpos)
  156. local above_ndef = node and
  157. minetest.reg_ns_nodes[node.name]
  158. if not above_ndef or not above_ndef.buildable_to then
  159. -- do not remove the bucket with the liquid
  160. return itemstack
  161. end
  162. end
  163. -- Cityblock check.
  164. if city_block:in_city(lpos) then
  165. minetest.chat_send_player(user:get_player_name(), "# Server: Don't do that in town!")
  166. easyvend.sound_error(user:get_player_name())
  167. return itemstack
  168. end
  169. if check_protection(lpos, user:get_player_name(), "place "..placename) then
  170. return
  171. end
  172. -- this causes a bug with placing water in protection.
  173. --minetest.place_node(lpos, {name = placename})
  174. minetest.set_node(lpos, {name = placename})
  175. minetest.check_for_falling(lpos)
  176. -- Notify dirt.
  177. dirtspread.on_environment(lpos)
  178. droplift.notify(lpos)
  179. return ItemStack("bucket:bucket_empty")
  180. end
  181. })
  182. end
  183. end
  184. minetest.register_craftitem("bucket:bucket_empty", {
  185. description = "Empty Bucket",
  186. inventory_image = "bucket.png",
  187. -- Empty buckets are stackable.
  188. --stack_max = 99,
  189. liquids_pointable = true,
  190. on_use = function(itemstack, user, pointed_thing)
  191. -- Must be pointing to node
  192. if pointed_thing.type ~= "node" then
  193. return
  194. end
  195. -- Check if pointing to a liquid source
  196. local node = minetest.get_node(pointed_thing.under)
  197. local liquiddef = bucket.liquids[node.name]
  198. local item_count = user:get_wielded_item():get_count()
  199. if liquiddef ~= nil and liquiddef.itemname ~= nil and node_in_group(node.name, liquiddef.source) then
  200. if check_protection(pointed_thing.under, user:get_player_name(), "take ".. node.name) then
  201. return
  202. end
  203. -- default set to return filled bucket
  204. local giving_back = liquiddef.itemname
  205. -- check if holding more than 1 empty bucket
  206. if item_count > 1 then
  207. -- if space in inventory add filled bucked, otherwise drop as item
  208. local inv = user:get_inventory()
  209. if inv:room_for_item("main", {name=liquiddef.itemname}) then
  210. inv:add_item("main", liquiddef.itemname)
  211. else
  212. local pos = user:getpos()
  213. pos.y = math_floor(pos.y + 0.5)
  214. minetest.add_item(pos, liquiddef.itemname)
  215. end
  216. -- set to return empty buckets minus 1
  217. giving_back = "bucket:bucket_empty "..tostring(item_count-1)
  218. end
  219. if node.name == "default:lava_source" then
  220. minetest.add_node(pointed_thing.under, {name="fire:basic_flame"})
  221. local pos = user:getpos()
  222. minetest.sound_play("default_cool_lava", {pos = pos, max_hear_distance = 16, gain = 0.25})
  223. if not heatdamage.is_immune(user:get_player_name()) then
  224. bucket.harm_player_after(user:get_player_name(), 2)
  225. end
  226. else
  227. minetest.add_node(pointed_thing.under, {name="air"})
  228. end
  229. return ItemStack(giving_back)
  230. end
  231. end,
  232. })
  233. bucket.register_liquid(
  234. {"default:water_source", "cw:water_source"},
  235. {"default:water_flowing", "cw:water_flowing"},
  236. "bucket:bucket_water",
  237. "default:water_source",
  238. "bucket_water.png",
  239. "Water Bucket",
  240. {water_bucket = 1}
  241. )
  242. bucket.register_liquid(
  243. "default:river_water_source",
  244. "default:river_water_flowing",
  245. "bucket:bucket_river_water",
  246. "default:river_water_source",
  247. "bucket_river_water.png",
  248. "Salt Water Bucket",
  249. {water_bucket = 1}
  250. )
  251. bucket.register_liquid(
  252. "default:lava_source",
  253. "default:lava_flowing",
  254. "bucket:bucket_lava",
  255. "default:lava_source",
  256. "bucket_lava.png",
  257. "Lava Bucket"
  258. )
  259. -- Old corium buckets convert to lava. Corium no longer exists.
  260. minetest.register_alias("corium:bucket", "bucket:bucket_lava")
  261. minetest.register_craft({
  262. type = "fuel",
  263. recipe = "bucket:bucket_lava",
  264. burntime = 60,
  265. replacements = {{"bucket:bucket_lava", "bucket:bucket_empty"}},
  266. })