mg76

bucket

May 7th, 2015
347
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Minetest 0.4 mod: bucket
  2. -- See README.txt for licensing and other information.
  3.  
  4.  
  5. --[[
  6. Argos' "spill" priv code has been added to buckets
  7. ~ LazyJ, 2015_03_05
  8. --]]
  9.  
  10. minetest.register_alias("bucket", "bucket:bucket_empty")
  11. minetest.register_alias("bucket_water", "bucket:bucket_water")
  12. minetest.register_alias("bucket_lava", "bucket:bucket_lava")
  13.  
  14. minetest.register_privilege("spill", "Can empty buckets")
  15. -- To deny singleplayer the spill privilege (for local testing):
  16. --minetest.register_privilege("spill", {description = "Can empty buckets",give_to_singleplayer = false})
  17.  
  18. minetest.register_craft({
  19. output = 'bucket:bucket_empty 1',
  20. recipe = {
  21. {'default:steel_ingot', '', 'default:steel_ingot'},
  22. {'', 'default:steel_ingot', ''},
  23. }
  24. })
  25.  
  26. bucket = {}
  27. bucket.liquids = {}
  28.  
  29. local function check_protection(pos, name, text)
  30. local protected = false
  31. if minetest.is_protected(pos, name) then
  32. protected = true
  33. else
  34. -- permafy check (wazuland2)
  35. local meta = minetest.env:get_meta(pos)
  36. local owner
  37. if meta then
  38. owner = meta:get_string("owner")
  39. if owner ~= name and owner ~= "" then
  40. protected = true
  41. if name ~= "" then
  42. minetest.chat_send_player(name, "(" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ") is protected by " .. owner .. ".")
  43. end
  44. end
  45. end
  46. end
  47. if protected then
  48. minetest.log("action", (name ~= "" and name or "A mod")
  49. .. " tried to " .. text
  50. .. " at protected position "
  51. .. minetest.pos_to_string(pos)
  52. .. " with a bucket")
  53. minetest.record_protection_violation(pos, name)
  54. return true
  55. end
  56. return false
  57. end
  58.  
  59. -- Register a new liquid
  60. -- source = name of the source node
  61. -- flowing = name of the flowing node
  62. -- itemname = name of the new bucket item (or nil if liquid is not takeable)
  63. -- inventory_image = texture of the new bucket item (ignored if itemname == nil)
  64. -- This function can be called from any mod (that depends on bucket).
  65. function bucket.register_liquid(source, flowing, itemname, inventory_image, name)
  66. bucket.liquids[source] = {
  67. source = source,
  68. flowing = flowing,
  69. itemname = itemname,
  70. }
  71. bucket.liquids[flowing] = bucket.liquids[source]
  72.  
  73. if itemname ~= nil then
  74. minetest.register_craftitem(itemname, {
  75. description = name,
  76. inventory_image = inventory_image,
  77. stack_max = 1,
  78. liquids_pointable = true,
  79. on_place = function(itemstack, user, pointed_thing)
  80. -- Must be pointing to node
  81. if pointed_thing.type ~= "node" then
  82. return
  83. end
  84.  
  85. local can_spill = minetest.check_player_privs(user.get_player_name(user), {spill=true})
  86.  
  87. local node = minetest.get_node_or_nil(pointed_thing.under)
  88. local ndef
  89. if node then
  90. ndef = minetest.registered_nodes[node.name]
  91. end
  92. -- Call on_rightclick if the pointed node defines it
  93. if ndef and ndef.on_rightclick and
  94. user and not user:get_player_control().sneak then
  95. return ndef.on_rightclick(
  96. pointed_thing.under,
  97. node, user,
  98. itemstack) or itemstack
  99. end
  100.  
  101. local place_liquid = function(pos, node, source, flowing)
  102. if check_protection(pos,
  103. user and user:get_player_name() or "",
  104. "place "..source) then
  105. return
  106. end
  107. minetest.add_node(pos, {name=source})
  108. end
  109.  
  110. -- Check if pointing to a buildable node
  111. local pos = minetest.get_pointed_thing_position(pointed_thing, pointed_thing.above)
  112. local spill_xyz = " at " .. pos.x .. "," .. pos.y .. "," .. pos.z
  113. if can_spill then
  114. if ndef and ndef.buildable_to then
  115. -- buildable; replace the node
  116. minetest.log("action", user.get_player_name(user) .. " spills " .. source .. " replacing " .. ndef.name .. spill_xyz)
  117. place_liquid(pointed_thing.under, node,
  118. source, flowing)
  119. else
  120. -- not buildable to; place the liquid above
  121. -- check if the node above can be replaced
  122. local node = minetest.get_node_or_nil(pointed_thing.above)
  123. if node and minetest.registered_nodes[node.name].buildable_to then
  124. minetest.log("action", user.get_player_name(user) .. " spills " .. source .. " replacing " .. node.name .. spill_xyz)
  125. place_liquid(pointed_thing.above,
  126. node, source,
  127. flowing)
  128. else
  129. -- do not remove the bucket with the liquid
  130. return
  131. end
  132. end
  133. else
  134. -- no spill priv. Allow emptying to origin liquid.
  135. if not ndef or ndef.name ~= source then
  136. minetest.log("action", user.get_player_name(user) .. " is not allowed to spill " .. source .. spill_xyz)
  137. return
  138. else
  139. minetest.log("action", user.get_player_name(user) .. " empties bucket of " .. source .. spill_xyz)
  140. end
  141. end
  142. return {name="bucket:bucket_empty"}
  143. end
  144. })
  145. end
  146. end
  147.  
  148. minetest.register_craftitem("bucket:bucket_empty", {
  149. description = "Empty Bucket",
  150. inventory_image = "bucket.png",
  151. stack_max = 99,
  152. liquids_pointable = true,
  153. on_use = function(itemstack, user, pointed_thing)
  154. -- Must be pointing to node
  155. if pointed_thing.type ~= "node" then
  156. return
  157. end
  158. -- Check if pointing to a liquid source
  159. local node = minetest.get_node(pointed_thing.under)
  160. local liquiddef = bucket.liquids[node.name]
  161. local item_count = user:get_wielded_item():get_count()
  162.  
  163. if liquiddef ~= nil
  164. and liquiddef.itemname ~= nil
  165. and node.name == liquiddef.source then
  166. if check_protection(pointed_thing.under,
  167. user:get_player_name(),
  168. "take ".. node.name) then
  169. return
  170. end
  171.  
  172. -- default set to return filled bucket
  173. local giving_back = liquiddef.itemname
  174.  
  175. -- check if holding more than 1 empty bucket
  176. if item_count > 1 then
  177.  
  178. -- if space in inventory add filled bucked, otherwise drop as item
  179. local inv = user:get_inventory()
  180. if inv:room_for_item("main", {name=liquiddef.itemname}) then
  181. inv:add_item("main", liquiddef.itemname)
  182. else
  183. local pos = user:getpos()
  184. pos.y = math.floor(pos.y + 0.5)
  185. core.add_item(pos, liquiddef.itemname)
  186. end
  187.  
  188. -- set to return empty buckets minus 1
  189. giving_back = "bucket:bucket_empty "..tostring(item_count-1)
  190.  
  191. end
  192.  
  193. minetest.add_node(pointed_thing.under, {name="air"})
  194.  
  195. return ItemStack(giving_back)
  196. end
  197. end,
  198. })
  199.  
  200. bucket.register_liquid(
  201. "default:water_source",
  202. "default:water_flowing",
  203. "bucket:bucket_water",
  204. "bucket_water.png",
  205. "Water Bucket"
  206. )
  207.  
  208. bucket.register_liquid(
  209. "default:lava_source",
  210. "default:lava_flowing",
  211. "bucket:bucket_lava",
  212. "bucket_lava.png",
  213. "Lava Bucket"
  214. )
  215.  
  216. minetest.register_craft({
  217. type = "fuel",
  218. recipe = "bucket:bucket_lava",
  219. burntime = 60,
  220. replacements = {{"bucket:bucket_lava", "bucket:bucket_empty"}},
  221.  
  222. })
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×