init.lua 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. -- This function can be called from any mod (that depends on bucket).
  35. function bucket.register_liquid(source, flowing, itemname, inventory_image, name, groups)
  36. bucket.liquids[source] = {
  37. source = source,
  38. flowing = flowing,
  39. itemname = itemname,
  40. }
  41. bucket.liquids[flowing] = bucket.liquids[source]
  42. if itemname ~= nil then
  43. minetest.register_craftitem(itemname, {
  44. description = name,
  45. inventory_image = inventory_image,
  46. stack_max = 1,
  47. liquids_pointable = true,
  48. groups = groups,
  49. on_place = function(itemstack, user, pointed_thing)
  50. -- Must be pointing to node
  51. if pointed_thing.type ~= "node" then
  52. return
  53. end
  54. local node = minetest.get_node_or_nil(pointed_thing.under)
  55. local ndef
  56. if node then
  57. ndef = minetest.registered_nodes[node.name]
  58. end
  59. -- Call on_rightclick if the pointed node defines it
  60. if ndef and ndef.on_rightclick and
  61. user and not user:get_player_control().sneak then
  62. return ndef.on_rightclick(
  63. pointed_thing.under,
  64. node, user,
  65. itemstack) or itemstack
  66. end
  67. local place_liquid = function(pos, node, source, flowing)
  68. if check_protection(pos,
  69. user and user:get_player_name() or "",
  70. "place "..source) then
  71. return
  72. end
  73. minetest.add_node(pos, {name=source})
  74. end
  75. -- Check if pointing to a buildable node
  76. if ndef and ndef.buildable_to then
  77. -- buildable; replace the node
  78. place_liquid(pointed_thing.under, node,
  79. source, flowing)
  80. else
  81. -- not buildable to; place the liquid above
  82. -- check if the node above can be replaced
  83. local node = minetest.get_node_or_nil(pointed_thing.above)
  84. if node and minetest.registered_nodes[node.name].buildable_to then
  85. place_liquid(pointed_thing.above,
  86. node, source,
  87. flowing)
  88. else
  89. -- do not remove the bucket with the liquid
  90. return
  91. end
  92. end
  93. return {name="bucket:bucket_empty"}
  94. end
  95. })
  96. end
  97. end
  98. minetest.register_craftitem("bucket:bucket_empty", {
  99. description = "Empty Bucket",
  100. inventory_image = "bucket.png",
  101. stack_max = 99,
  102. liquids_pointable = true,
  103. on_use = function(itemstack, user, pointed_thing)
  104. -- Must be pointing to node
  105. if pointed_thing.type ~= "node" then
  106. return
  107. end
  108. -- Check if pointing to a liquid source
  109. local node = minetest.get_node(pointed_thing.under)
  110. local liquiddef = bucket.liquids[node.name]
  111. local item_count = user:get_wielded_item():get_count()
  112. if liquiddef ~= nil
  113. and liquiddef.itemname ~= nil
  114. and node.name == liquiddef.source then
  115. if check_protection(pointed_thing.under,
  116. user:get_player_name(),
  117. "take ".. node.name) then
  118. return
  119. end
  120. -- default set to return filled bucket
  121. local giving_back = liquiddef.itemname
  122. -- check if holding more than 1 empty bucket
  123. if item_count > 1 then
  124. -- if space in inventory add filled bucked, otherwise drop as item
  125. local inv = user:get_inventory()
  126. if inv:room_for_item("main", {name=liquiddef.itemname}) then
  127. inv:add_item("main", liquiddef.itemname)
  128. else
  129. local pos = user:getpos()
  130. pos.y = math.floor(pos.y + 0.5)
  131. core.add_item(pos, liquiddef.itemname)
  132. end
  133. -- set to return empty buckets minus 1
  134. giving_back = "bucket:bucket_empty "..tostring(item_count-1)
  135. end
  136. minetest.add_node(pointed_thing.under, {name="air"})
  137. return ItemStack(giving_back)
  138. end
  139. end,
  140. })
  141. bucket.register_liquid(
  142. "default:water_source",
  143. "default:water_flowing",
  144. "bucket:bucket_water",
  145. "bucket_water.png",
  146. "Water Bucket",
  147. {water_bucket = 1}
  148. )
  149. bucket.register_liquid(
  150. "default:river_water_source",
  151. "default:river_water_flowing",
  152. "bucket:bucket_river_water",
  153. "bucket_river_water.png",
  154. "River Water Bucket",
  155. {water_bucket = 1}
  156. )
  157. bucket.register_liquid(
  158. "default:lava_source",
  159. "default:lava_flowing",
  160. "bucket:bucket_lava",
  161. "bucket_lava.png",
  162. "Lava Bucket"
  163. )
  164. minetest.register_craft({
  165. type = "fuel",
  166. recipe = "bucket:bucket_lava",
  167. burntime = 60,
  168. replacements = {{"bucket:bucket_lava", "bucket:bucket_empty"}},
  169. })