init.lua 9.2 KB

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