init.lua 7.3 KB

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