init.lua 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. -- Above 10000 XP, player can use buckets.
  49. -- Note: this will allow high-XP players to place lava (which ignores
  50. -- protection) above ground. If such a player decides to grief somebody,
  51. -- I guess you'll need to form a committee! (You can still use city blocks
  52. -- to protect builds.)
  53. local lxp = (xp.get_xp(name, "digxp") >= 10000)
  54. if not lxp or sheriff.is_cheater(name) then
  55. if pos.y > gl and string.find(text, "place") then
  56. minetest.chat_send_player(name, "# Server: Don't do that above ground!")
  57. easyvend.sound_error(name)
  58. return true
  59. end
  60. end
  61. end
  62. if minetest.is_protected(pos, name) then
  63. minetest.log("action", (name ~= "" and name or "A mod")
  64. .. " tried to " .. text
  65. .. " at protected position "
  66. .. minetest.pos_to_string(pos)
  67. .. " with a bucket")
  68. minetest.record_protection_violation(pos, name)
  69. minetest.chat_send_player(name, "# Server: Nope. Not on someone else's land!")
  70. return true
  71. end
  72. return false
  73. end
  74. local function node_in_group(name, list)
  75. if type(list) == "string" then
  76. return (name == list)
  77. elseif type(list) == "table" then
  78. for k, v in ipairs(list) do
  79. if name == v then
  80. return true
  81. end
  82. end
  83. end
  84. return false
  85. end
  86. -- Register a new liquid
  87. -- source = name of the source node
  88. -- flowing = name of the flowing node
  89. -- itemname = name of the new bucket item (or nil if liquid is not takeable)
  90. -- inventory_image = texture of the new bucket item (ignored if itemname == nil)
  91. -- name = text description of the bucket item
  92. -- groups = (optional) groups of the bucket item, for example {water_bucket = 1}
  93. -- This function can be called from any mod (that depends on bucket).
  94. function bucket.register_liquid(source, flowing, itemname, placename, inventory_image, name, groups)
  95. if type(source) == "string" then
  96. bucket.liquids[source] = {
  97. source = source,
  98. flowing = flowing,
  99. itemname = itemname,
  100. }
  101. else
  102. assert(type(source) == "table")
  103. for k, v in ipairs(source) do
  104. assert(type(v) == "string")
  105. bucket.liquids[v] = {
  106. source = source,
  107. flowing = flowing,
  108. itemname = itemname,
  109. }
  110. end
  111. end
  112. if type(flowing) == "string" then
  113. bucket.liquids[flowing] = {
  114. source = source,
  115. flowing = flowing,
  116. itemname = itemname,
  117. }
  118. else
  119. assert(type(flowing) == "table")
  120. for k, v in ipairs(flowing) do
  121. assert(type(v) == "string")
  122. bucket.liquids[v] = {
  123. source = source,
  124. flowing = flowing,
  125. itemname = itemname,
  126. }
  127. end
  128. end
  129. if itemname ~= nil then
  130. minetest.register_craftitem(itemname, {
  131. description = name,
  132. inventory_image = inventory_image,
  133. stack_max = 1,
  134. liquids_pointable = true,
  135. groups = groups,
  136. on_place = function(itemstack, user, pointed_thing)
  137. if not user or not user:is_player() then
  138. return itemstack
  139. end
  140. -- Must be pointing to node
  141. if pointed_thing.type ~= "node" then
  142. return
  143. end
  144. local node = minetest.get_node_or_nil(pointed_thing.under)
  145. local ndef = node and minetest.reg_ns_nodes[node.name]
  146. -- Call on_rightclick if the pointed node defines it
  147. if ndef and ndef.on_rightclick and
  148. user and not user:get_player_control().sneak then
  149. return ndef.on_rightclick(pointed_thing.under, node, user, itemstack)
  150. end
  151. local lpos
  152. -- Check if pointing to a buildable node
  153. if ndef and ndef.buildable_to then
  154. -- buildable; replace the node
  155. lpos = pointed_thing.under
  156. elseif node_in_group(node.name, flowing) or node_in_group(node.name, source) then
  157. -- flow version of bucket contents, replace the node.
  158. lpos = pointed_thing.under
  159. else
  160. -- not buildable to; place the liquid above
  161. -- check if the node above can be replaced
  162. lpos = pointed_thing.above
  163. node = minetest.get_node_or_nil(lpos)
  164. local above_ndef = node and
  165. minetest.reg_ns_nodes[node.name]
  166. if not above_ndef or not above_ndef.buildable_to then
  167. -- do not remove the bucket with the liquid
  168. return itemstack
  169. end
  170. end
  171. -- Cityblock check.
  172. if city_block:in_city(lpos) then
  173. minetest.chat_send_player(user:get_player_name(), "# Server: Don't do that in town!")
  174. easyvend.sound_error(user:get_player_name())
  175. return itemstack
  176. end
  177. if check_protection(lpos, user:get_player_name(), "place " .. placename) then
  178. return
  179. end
  180. -- this causes a bug with placing water in protection.
  181. --minetest.place_node(lpos, {name = placename})
  182. minetest.set_node(lpos, {name = placename})
  183. minetest.check_for_falling(lpos)
  184. -- Notify dirt.
  185. dirtspread.on_environment(lpos)
  186. droplift.notify(lpos)
  187. return ItemStack("bucket:bucket_empty")
  188. end
  189. })
  190. end
  191. end
  192. minetest.register_craftitem("bucket:bucket_empty", {
  193. description = "Empty Bucket",
  194. inventory_image = "bucket.png",
  195. -- Empty buckets are stackable.
  196. --stack_max = 99,
  197. liquids_pointable = true,
  198. on_use = function(itemstack, user, pointed_thing)
  199. -- Must be pointing to node
  200. if pointed_thing.type ~= "node" then
  201. return
  202. end
  203. -- Check if pointing to a liquid source
  204. local node = minetest.get_node(pointed_thing.under)
  205. local liquiddef = bucket.liquids[node.name]
  206. local item_count = user:get_wielded_item():get_count()
  207. if liquiddef ~= nil and liquiddef.itemname ~= nil and node_in_group(node.name, liquiddef.source) then
  208. if check_protection(pointed_thing.under, user:get_player_name(), "take " .. node.name) then
  209. return
  210. end
  211. -- default set to return filled bucket
  212. local giving_back = liquiddef.itemname
  213. -- check if holding more than 1 empty bucket
  214. if item_count > 1 then
  215. -- if space in inventory add filled bucked, otherwise drop as item
  216. local inv = user:get_inventory()
  217. if inv:room_for_item("main", {name=liquiddef.itemname}) then
  218. inv:add_item("main", liquiddef.itemname)
  219. else
  220. local pos = user:get_pos()
  221. pos.y = math_floor(pos.y + 0.5)
  222. minetest.add_item(pos, liquiddef.itemname)
  223. end
  224. -- set to return empty buckets minus 1
  225. giving_back = "bucket:bucket_empty "..tostring(item_count-1)
  226. end
  227. if node.name == "default:lava_source" then
  228. minetest.add_node(pointed_thing.under, {name="fire:basic_flame"})
  229. local pos = user:get_pos()
  230. minetest.sound_play("default_cool_lava", {pos = pos, max_hear_distance = 16, gain = 0.25}, true)
  231. if not heatdamage.is_immune(user:get_player_name()) then
  232. bucket.harm_player_after(user:get_player_name(), 2)
  233. end
  234. else
  235. minetest.add_node(pointed_thing.under, {name="air"})
  236. end
  237. return ItemStack(giving_back)
  238. end
  239. end,
  240. })
  241. bucket.register_liquid(
  242. {"default:water_source", "cw:water_source"},
  243. {"default:water_flowing", "cw:water_flowing"},
  244. "bucket:bucket_water",
  245. "default:water_source",
  246. "bucket_water.png",
  247. "Water Bucket",
  248. {water_bucket = 1}
  249. )
  250. bucket.register_liquid(
  251. "default:river_water_source",
  252. "default:river_water_flowing",
  253. "bucket:bucket_river_water",
  254. "default:river_water_source",
  255. "bucket_river_water.png",
  256. "Salt Water Bucket",
  257. {water_bucket = 1}
  258. )
  259. bucket.register_liquid(
  260. "default:lava_source",
  261. "default:lava_flowing",
  262. "bucket:bucket_lava",
  263. "default:lava_source",
  264. "bucket_lava.png",
  265. "Lava Bucket"
  266. )
  267. -- Old corium buckets convert to lava. Corium no longer exists.
  268. minetest.register_alias("corium:bucket", "bucket:bucket_lava")
  269. minetest.register_craft({
  270. type = "fuel",
  271. recipe = "bucket:bucket_lava",
  272. burntime = 360,
  273. replacements = {{"bucket:bucket_lava", "bucket:bucket_empty"}},
  274. })