init.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. {'group:metal_ingot', '', 'group:metal_ingot'},
  10. {'', 'group:metal_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. -- This function can be called from any mod (that depends on bucket).
  33. function bucket.register_liquid(source, flowing, itemname, inventory_image, name)
  34. bucket.liquids[source] = {
  35. source = source,
  36. flowing = flowing,
  37. itemname = itemname,
  38. }
  39. bucket.liquids[flowing] = bucket.liquids[source]
  40. if not itemname then return end
  41. minetest.register_craftitem(itemname, {
  42. description = name,
  43. inventory_image = inventory_image,
  44. stack_max = 1,
  45. liquids_pointable = true,
  46. on_place = function(itemstack, user, pointed_thing)
  47. -- Must be pointing to node
  48. if pointed_thing.type ~= "node" then
  49. return
  50. end
  51. local node = minetest.get_node_or_nil(pointed_thing.under)
  52. local ndef
  53. if node then
  54. ndef = minetest.registered_nodes[node.name]
  55. end
  56. -- Call on_rightclick if the pointed node defines it
  57. if ndef and ndef.on_rightclick and
  58. user and not user:get_player_control().sneak then
  59. return ndef.on_rightclick(
  60. pointed_thing.under,
  61. node, user,
  62. itemstack) or itemstack
  63. end
  64. local place_liquid = function(pos, node, source, flowing)
  65. if check_protection(pos,
  66. user and user:get_player_name() or "",
  67. "place "..source) then
  68. return
  69. end
  70. minetest.add_node(pos, {name=source})
  71. end
  72. -- Check if pointing to a buildable node
  73. if ndef and ndef.buildable_to then
  74. -- Buildable; replace the node
  75. place_liquid(pointed_thing.under, node, source, flowing)
  76. else
  77. -- Not buildable to; place the liquid above
  78. -- Check if the node above can be replaced
  79. local node = minetest.get_node_or_nil(pointed_thing.above)
  80. if node and minetest.registered_nodes[node.name].buildable_to then
  81. place_liquid(pointed_thing.above, node, source, flowing)
  82. else
  83. -- Do not remove the bucket with the liquid
  84. return
  85. end
  86. end
  87. return {name="bucket:bucket_empty"}
  88. end
  89. })
  90. end
  91. minetest.register_craftitem("bucket:bucket_empty", {
  92. description = "Empty Bucket",
  93. inventory_image = "bucket.png",
  94. liquids_pointable = true,
  95. on_use = function(itemstack, user, pointed_thing)
  96. -- Must be pointing to node
  97. if pointed_thing.type ~= "node" then
  98. return
  99. end
  100. -- Check if pointing to a liquid source
  101. local node = minetest.get_node(pointed_thing.under)
  102. local liquiddef = bucket.liquids[node.name]
  103. if liquiddef ~= nil and liquiddef.itemname ~= nil and
  104. node.name == liquiddef.source then
  105. if check_protection(pointed_thing.under,
  106. user:get_player_name(),
  107. "take ".. node.name) then
  108. return
  109. end
  110. -- Only one bucket: replace
  111. local count = itemstack:get_count()
  112. if count == 1 then
  113. minetest.add_node(pointed_thing.under, {name="air"})
  114. return ItemStack({name = liquiddef.itemname,
  115. metadata = tostring(node.param2)})
  116. end
  117. -- Staked buckets: add a filled bucket, replace stack
  118. local inv = user:get_inventory()
  119. if inv:room_for_item("main", liquiddef.itemname) then
  120. minetest.add_node(pointed_thing.under, {name="air"})
  121. count = count - 1
  122. itemstack:set_count(count)
  123. bucket_liquid = ItemStack(liquiddef.itemname)
  124. inv:add_item("main", bucket_liquid)
  125. return itemstack
  126. else
  127. minetest.chat_send_player(user:get_player_name(), "Your inventory is full.")
  128. end
  129. end
  130. end,
  131. })
  132. bucket.register_liquid(
  133. "default:water_source",
  134. "default:water_flowing",
  135. "bucket:bucket_water",
  136. "bucket_water.png",
  137. "Water Bucket"
  138. )
  139. bucket.register_liquid(
  140. "default:lava_source",
  141. "default:lava_flowing",
  142. "bucket:bucket_lava",
  143. "bucket_lava.png",
  144. "Lava Bucket"
  145. )
  146. minetest.register_craft({
  147. type = "fuel",
  148. recipe = "bucket:bucket_lava",
  149. burntime = 60,
  150. replacements = {{"bucket:bucket_lava", "bucket:bucket_empty"}},
  151. })