init.lua 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. -- Minetest 0.4 mod: bucket
  2. -- See README.txt for licensing and other information.
  3. minetest.register_alias("bucket", "bucket:bucket_empty")
  4. minetest.register_alias("bucket_water", "bucket:bucket_water")
  5. minetest.register_alias("bucket_lava", "bucket:bucket_lava")
  6. minetest.register_craft({
  7. output = 'bucket:bucket_empty 1',
  8. recipe = {
  9. {'default:steel_ingot', '', 'default:steel_ingot'},
  10. {'', 'default:steel_ingot', ''},
  11. }
  12. })
  13. bucket = {}
  14. bucket.liquids = {}
  15. local function check_protection(pos, name, text)
  16. if minetest.is_protected(pos, name) then
  17. minetest.log("action", (name ~= "" and name or "A mod")
  18. .. " tried to " .. text
  19. .. " at protected position "
  20. .. minetest.pos_to_string(pos)
  21. .. " with a bucket")
  22. minetest.record_protection_violation(pos, name)
  23. return true
  24. end
  25. return false
  26. end
  27. -- Register a new liquid
  28. -- source = name of the source node
  29. -- flowing = name of the flowing node
  30. -- itemname = name of the new bucket item (or nil if liquid is not takeable)
  31. -- inventory_image = texture of the new bucket item (ignored if itemname == nil)
  32. -- name = text description of the bucket item
  33. -- groups = (optional) groups of the bucket item, for example {water_bucket = 1}
  34. -- force_renew = (optional) bool. Force the liquid source to renew if it has a
  35. -- source neighbour, even if defined as 'liquid_renewable = false'.
  36. -- Needed to avoid creating holes in sloping rivers.
  37. -- This function can be called from any mod (that depends on bucket).
  38. function bucket.register_liquid(source, flowing, itemname, inventory_image, name,
  39. groups, force_renew)
  40. bucket.liquids[source] = {
  41. source = source,
  42. flowing = flowing,
  43. itemname = itemname,
  44. force_renew = force_renew,
  45. }
  46. bucket.liquids[flowing] = bucket.liquids[source]
  47. if itemname ~= nil then
  48. minetest.register_craftitem(itemname, {
  49. description = name,
  50. inventory_image = inventory_image,
  51. stack_max = 1,
  52. liquids_pointable = true,
  53. groups = groups,
  54. on_place = function(itemstack, user, pointed_thing)
  55. -- Must be pointing to node
  56. if pointed_thing.type ~= "node" then
  57. return
  58. end
  59. local node = minetest.get_node_or_nil(pointed_thing.under)
  60. local ndef = node and minetest.registered_nodes[node.name]
  61. -- Call on_rightclick if the pointed node defines it
  62. if ndef and ndef.on_rightclick and
  63. not (user and user:is_player() and
  64. user:get_player_control().sneak) then
  65. return ndef.on_rightclick(
  66. pointed_thing.under,
  67. node, user,
  68. itemstack)
  69. end
  70. local lpos
  71. -- Check if pointing to a buildable node
  72. if ndef and ndef.buildable_to then
  73. -- buildable; replace the node
  74. lpos = pointed_thing.under
  75. else
  76. -- not buildable to; place the liquid above
  77. -- check if the node above can be replaced
  78. lpos = pointed_thing.above
  79. node = minetest.get_node_or_nil(lpos)
  80. local above_ndef = node and minetest.registered_nodes[node.name]
  81. if not above_ndef or not above_ndef.buildable_to then
  82. -- do not remove the bucket with the liquid
  83. return itemstack
  84. end
  85. end
  86. if check_protection(lpos, user
  87. and user:get_player_name()
  88. or "", "place "..source) then
  89. return
  90. end
  91. minetest.set_node(lpos, {name = source})
  92. return ItemStack("bucket:bucket_empty")
  93. end
  94. })
  95. end
  96. end
  97. minetest.register_craftitem("bucket:bucket_empty", {
  98. description = "Empty Bucket",
  99. inventory_image = "bucket.png",
  100. stack_max = 99,
  101. liquids_pointable = true,
  102. on_use = function(itemstack, user, pointed_thing)
  103. if pointed_thing.type == "object" then
  104. pointed_thing.ref:punch(user, 1.0, { full_punch_interval=1.0 }, nil)
  105. return user:get_wielded_item()
  106. elseif pointed_thing.type ~= "node" then
  107. -- do nothing if it's neither object nor node
  108. return
  109. end
  110. -- Check if pointing to a liquid source
  111. local node = minetest.get_node(pointed_thing.under)
  112. local liquiddef = bucket.liquids[node.name]
  113. local item_count = user:get_wielded_item():get_count()
  114. if liquiddef ~= nil
  115. and liquiddef.itemname ~= nil
  116. and node.name == liquiddef.source then
  117. if check_protection(pointed_thing.under,
  118. user:get_player_name(),
  119. "take ".. node.name) then
  120. return
  121. end
  122. -- default set to return filled bucket
  123. local giving_back = liquiddef.itemname
  124. -- check if holding more than 1 empty bucket
  125. if item_count > 1 then
  126. -- if space in inventory add filled bucked, otherwise drop as item
  127. local inv = user:get_inventory()
  128. if inv:room_for_item("main", {name=liquiddef.itemname}) then
  129. inv:add_item("main", liquiddef.itemname)
  130. else
  131. local pos = user:getpos()
  132. pos.y = math.floor(pos.y + 0.5)
  133. minetest.add_item(pos, liquiddef.itemname)
  134. end
  135. -- set to return empty buckets minus 1
  136. giving_back = "bucket:bucket_empty "..tostring(item_count-1)
  137. end
  138. -- force_renew requires a source neighbour
  139. local source_neighbor = false
  140. if liquiddef.force_renew then
  141. source_neighbor =
  142. minetest.find_node_near(pointed_thing.under, 1, liquiddef.source)
  143. end
  144. if not (source_neighbor and liquiddef.force_renew) then
  145. minetest.add_node(pointed_thing.under, {name = "air"})
  146. end
  147. return ItemStack(giving_back)
  148. else
  149. -- non-liquid nodes will have their on_punch triggered
  150. local node_def = minetest.registered_nodes[node.name]
  151. if node_def then
  152. node_def.on_punch(pointed_thing.under, node, user, pointed_thing)
  153. end
  154. return user:get_wielded_item()
  155. end
  156. end,
  157. })
  158. bucket.register_liquid(
  159. "default:water_source",
  160. "default:water_flowing",
  161. "bucket:bucket_water",
  162. "bucket_water.png",
  163. "Water Bucket",
  164. {water_bucket = 1}
  165. )
  166. -- River water source is 'liquid_renewable = false' to avoid horizontal spread
  167. -- of water sources in sloping rivers that can cause water to overflow
  168. -- riverbanks and cause floods.
  169. -- River water source is instead made renewable by the 'force renew' option
  170. -- used here.
  171. bucket.register_liquid(
  172. "default:river_water_source",
  173. "default:river_water_flowing",
  174. "bucket:bucket_river_water",
  175. "bucket_river_water.png",
  176. "River Water Bucket",
  177. {water_bucket = 1},
  178. true
  179. )
  180. bucket.register_liquid(
  181. "default:lava_source",
  182. "default:lava_flowing",
  183. "bucket:bucket_lava",
  184. "bucket_lava.png",
  185. "Lava Bucket"
  186. )
  187. minetest.register_craft({
  188. type = "fuel",
  189. recipe = "bucket:bucket_lava",
  190. burntime = 60,
  191. replacements = {{"bucket:bucket_lava", "bucket:bucket_empty"}},
  192. })