clay_pot.lua 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. ----------------------------------------------------------
  2. -- Clay Pot (i.e. a clay bucket)
  3. -------------------------------------------------
  4. --Functions
  5. --protection function
  6. local function check_protection(pos, name, text)
  7. if minetest.is_protected(pos, name) then
  8. minetest.log("action", (name ~= "" and name or "A mod")
  9. .. " tried to " .. text
  10. .. " at protected position "
  11. .. minetest.pos_to_string(pos)
  12. .. " with a bucket")
  13. minetest.record_protection_violation(pos, name)
  14. return true
  15. end
  16. return false
  17. end
  18. --------------
  19. -- Can't just use the bucket api because that gives the player back the default steel bucket...
  20. --otherwise this is just the same as from buckets
  21. local earthbuild = {}
  22. earthbuild.liquids = {}
  23. function register_liquid(source, flowing, itemname, inventory_image, name,
  24. groups, force_renew)
  25. earthbuild.liquids[source] = {
  26. source = source,
  27. flowing = flowing,
  28. itemname = itemname,
  29. force_renew = force_renew,
  30. }
  31. earthbuild.liquids[flowing] = earthbuild.liquids[source]
  32. if itemname ~= nil then
  33. minetest.register_craftitem(itemname, {
  34. description = name,
  35. inventory_image = inventory_image,
  36. stack_max = 1,
  37. liquids_pointable = true,
  38. groups = groups,
  39. on_place = function(itemstack, user, pointed_thing)
  40. if minetest.check_player_privs(user.get_player_name(user), {spill=true}) then
  41. -- Must be pointing to node
  42. if pointed_thing.type ~= "node" then
  43. return
  44. end
  45. local node = minetest.get_node_or_nil(pointed_thing.under)
  46. local ndef = node and minetest.registered_nodes[node.name]
  47. -- Call on_rightclick if the pointed node defines it
  48. if ndef and ndef.on_rightclick and
  49. user and not user:get_player_control().sneak then
  50. return ndef.on_rightclick(
  51. pointed_thing.under,
  52. node, user,
  53. itemstack)
  54. end
  55. local lpos
  56. -- Check if pointing to a buildable node
  57. if ndef and ndef.buildable_to then
  58. -- buildable; replace the node
  59. lpos = pointed_thing.under
  60. else
  61. -- not buildable to; place the liquid above
  62. -- check if the node above can be replaced
  63. lpos = pointed_thing.above
  64. node = minetest.get_node_or_nil(lpos)
  65. local above_ndef = node and minetest.registered_nodes[node.name]
  66. if not above_ndef or not above_ndef.buildable_to then
  67. -- do not remove the bucket with the liquid
  68. return itemstack
  69. end
  70. end
  71. if check_protection(lpos, user
  72. and user:get_player_name()
  73. or "", "place "..source) then
  74. return
  75. end
  76. minetest.set_node(lpos, {name = source})
  77. minetest.log('action', user:get_player_name()..' placed '..source..' at '..minetest.pos_to_string(lpos))
  78. return ItemStack("earthbuild:clay_pot")
  79. end
  80. end
  81. })
  82. end
  83. end
  84. ----------------------------------------------------------
  85. --Register unfired pot
  86. minetest.register_craftitem("earthbuild:unfired_clay_pot", {
  87. description = "Unfired Clay Pot",
  88. inventory_image = "earthbuild_unfired_clay_pot.png",
  89. stack_max = 99,
  90. })
  91. --Register useable pot
  92. minetest.register_craftitem("earthbuild:clay_pot", {
  93. description = "Clay Pot",
  94. inventory_image = "earthbuild_clay_pot.png",
  95. stack_max = 99,
  96. liquids_pointable = true,
  97. on_use = function(itemstack, user, pointed_thing)
  98. if pointed_thing.type == "object" then
  99. pointed_thing.ref:punch(user, 1.0, { full_punch_interval=1.0 }, nil)
  100. return user:get_wielded_item()
  101. elseif pointed_thing.type ~= "node" then
  102. -- do nothing if it's neither object nor node
  103. return
  104. end
  105. -- Check if pointing to a liquid source
  106. local node = minetest.get_node(pointed_thing.under)
  107. local liquiddef = earthbuild.liquids[node.name]
  108. local item_count = user:get_wielded_item():get_count()
  109. if liquiddef ~= nil
  110. and liquiddef.itemname ~= nil
  111. and node.name == liquiddef.source then
  112. if check_protection(pointed_thing.under,
  113. user:get_player_name(),
  114. "take ".. node.name) then
  115. return
  116. end
  117. -- default set to return filled bucket
  118. local giving_back = liquiddef.itemname
  119. -- check if holding more than 1 empty bucket
  120. if item_count > 1 then
  121. -- if space in inventory add filled bucked, otherwise drop as item
  122. local inv = user:get_inventory()
  123. if inv:room_for_item("main", {name=liquiddef.itemname}) then
  124. inv:add_item("main", liquiddef.itemname)
  125. else
  126. local pos = user:getpos()
  127. pos.y = math.floor(pos.y + 0.5)
  128. minetest.add_item(pos, liquiddef.itemname)
  129. end
  130. -- set to return empty buckets minus 1
  131. giving_back = "earthbuild:clay_pot "..tostring(item_count-1)
  132. end
  133. -- force_renew requires a source neighbour
  134. local source_neighbor = false
  135. if liquiddef.force_renew then
  136. source_neighbor =
  137. minetest.find_node_near(pointed_thing.under, 1, liquiddef.source)
  138. end
  139. if not (source_neighbor and liquiddef.force_renew) then
  140. minetest.add_node(pointed_thing.under, {name = "air"})
  141. end
  142. return ItemStack(giving_back)
  143. else
  144. -- non-liquid nodes will have their on_punch triggered
  145. local node_def = minetest.registered_nodes[node.name]
  146. if node_def then
  147. node_def.on_punch(pointed_thing.under, node, user, pointed_thing)
  148. end
  149. return user:get_wielded_item()
  150. end
  151. end,
  152. })
  153. -------------------------------------------------
  154. --Register the liquids the pot can be used for
  155. register_liquid(
  156. "default:water_source",
  157. "default:water_flowing",
  158. "earthbuild:clay_pot_water",
  159. "earthbuild_clay_pot_water.png",
  160. "Water Clay Pot",
  161. {water_bucket = 1}
  162. )
  163. register_liquid(
  164. "default:river_water_source",
  165. "default:river_water_flowing",
  166. "earthbuild:clay_pot_river_water",
  167. "earthbuild_clay_pot_river_water.png",
  168. "River Water Clay Pot",
  169. {water_bucket = 1},
  170. true
  171. )
  172. register_liquid(
  173. "default:lava_source",
  174. "default:lava_flowing",
  175. "earthbuild:clay_pot_lava",
  176. "earthbuild_clay_pot_lava.png",
  177. "Clay Pot Lava"
  178. )
  179. --Register lava as a fuel source
  180. minetest.register_craft({
  181. type = "fuel",
  182. recipe = "earthbuild:clay_pot_lava",
  183. burntime = 60,
  184. replacements = {{"earthbuild:clay_pot_lava", "earthbuild:clay_pot"}},
  185. })
  186. ---------------------------------------
  187. --Recipes to make the pot
  188. --Craft unfired pot
  189. minetest.register_craft({
  190. output = 'earthbuild:unfired_clay_pot 1',
  191. recipe = {
  192. {'default:clay_lump', '', 'default:clay_lump'},
  193. {'', 'default:clay_lump', ''},
  194. }
  195. })
  196. --Craft unfired pot back into clay
  197. minetest.register_craft({
  198. output = 'default:clay_lump 3',
  199. recipe = {{'earthbuild:unfired_clay_pot'}}
  200. })
  201. --Cook unfired pot to give the useable bucket
  202. minetest.register_craft({
  203. type = "cooking",
  204. output = "earthbuild:clay_pot",
  205. recipe = "earthbuild:unfired_clay_pot",
  206. cooktime = 3,
  207. })