init.lua 6.9 KB

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