init.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. user and not user:get_player_control().sneak then
  64. return ndef.on_rightclick(
  65. pointed_thing.under,
  66. node, user,
  67. itemstack)
  68. end
  69. local lpos
  70. -- Check if pointing to a buildable node
  71. if ndef and ndef.buildable_to then
  72. -- buildable; replace the node
  73. lpos = pointed_thing.under
  74. else
  75. -- not buildable to; place the liquid above
  76. -- check if the node above can be replaced
  77. lpos = pointed_thing.above
  78. node = minetest.get_node_or_nil(lpos)
  79. local above_ndef = node and minetest.registered_nodes[node.name]
  80. if not above_ndef or not above_ndef.buildable_to then
  81. -- do not remove the bucket with the liquid
  82. return itemstack
  83. end
  84. end
  85. if check_protection(lpos, user
  86. and user:get_player_name()
  87. or "", "place "..source) then
  88. return
  89. end
  90. minetest.set_node(lpos, {name = source})
  91. return ItemStack("bucket:bucket_empty")
  92. end
  93. })
  94. end
  95. end
  96. minetest.register_craftitem("bucket:bucket_empty", {
  97. description = "Empty Bucket",
  98. inventory_image = "bucket.png",
  99. stack_max = 99,
  100. liquids_pointable = true,
  101. on_use = function(itemstack, user, pointed_thing)
  102. if pointed_thing.type == "object" then
  103. pointed_thing.ref:punch(user, 1.0, { full_punch_interval=1.0 }, nil)
  104. return user:get_wielded_item()
  105. elseif pointed_thing.type ~= "node" then
  106. -- do nothing if it's neither object nor node
  107. return
  108. end
  109. -- Check if pointing to a liquid source
  110. local node = minetest.get_node(pointed_thing.under)
  111. local liquiddef = bucket.liquids[node.name]
  112. local item_count = user:get_wielded_item():get_count()
  113. if liquiddef ~= nil
  114. and liquiddef.itemname ~= nil
  115. and node.name == liquiddef.source then
  116. if check_protection(pointed_thing.under,
  117. user:get_player_name(),
  118. "take ".. node.name) then
  119. return
  120. end
  121. -- default set to return filled bucket
  122. local giving_back = liquiddef.itemname
  123. -- check if holding more than 1 empty bucket
  124. if item_count > 1 then
  125. -- if space in inventory add filled bucked, otherwise drop as item
  126. local inv = user:get_inventory()
  127. if inv:room_for_item("main", {name=liquiddef.itemname}) then
  128. inv:add_item("main", liquiddef.itemname)
  129. else
  130. local pos = user:getpos()
  131. pos.y = math.floor(pos.y + 0.5)
  132. minetest.add_item(pos, liquiddef.itemname)
  133. end
  134. -- set to return empty buckets minus 1
  135. giving_back = "bucket:bucket_empty "..tostring(item_count-1)
  136. end
  137. -- force_renew requires a source neighbour
  138. local source_neighbor = false
  139. if liquiddef.force_renew then
  140. source_neighbor =
  141. minetest.find_node_near(pointed_thing.under, 1, liquiddef.source)
  142. end
  143. if not (source_neighbor and liquiddef.force_renew) then
  144. minetest.add_node(pointed_thing.under, {name = "air"})
  145. end
  146. return ItemStack(giving_back)
  147. else
  148. -- non-liquid nodes will have their on_punch triggered
  149. local node_def = minetest.registered_nodes[node.name]
  150. if node_def then
  151. node_def.on_punch(pointed_thing.under, node, user, pointed_thing)
  152. end
  153. return user:get_wielded_item()
  154. end
  155. end,
  156. })
  157. bucket.register_liquid(
  158. "default:water_source",
  159. "default:water_flowing",
  160. "bucket:bucket_water",
  161. "bucket_water.png",
  162. "Water Bucket",
  163. {water_bucket = 1}
  164. )
  165. bucket.register_liquid(
  166. "default:river_water_source",
  167. "default:river_water_flowing",
  168. "bucket:bucket_river_water",
  169. "bucket_river_water.png",
  170. "River Water Bucket",
  171. {water_bucket = 1},
  172. true
  173. )
  174. bucket.register_liquid(
  175. "default:lava_source",
  176. "default:lava_flowing",
  177. "bucket:bucket_lava",
  178. "bucket_lava.png",
  179. "Lava Bucket"
  180. )
  181. minetest.register_craft({
  182. type = "fuel",
  183. recipe = "bucket:bucket_lava",
  184. burntime = 60,
  185. replacements = {{"bucket:bucket_lava", "bucket:bucket_empty"}},
  186. })