init.lua 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. if not minetest.global_exists("cans") then cans = {} end
  2. -- Localize for performance.
  3. local math_floor = math.floor
  4. local function set_can_wear(itemstack, level, max_level)
  5. local temp
  6. if level == 0 then
  7. temp = 0
  8. else
  9. temp = 65536 - math_floor(level / max_level * 65535)
  10. if temp > 65535 then temp = 65535 end
  11. if temp < 1 then temp = 1 end
  12. end
  13. itemstack:set_wear(temp)
  14. end
  15. local function get_can_level(itemstack)
  16. if itemstack:get_metadata() == "" then
  17. return 0
  18. else
  19. return tonumber(itemstack:get_metadata())
  20. end
  21. end
  22. cans.get_can_level = get_can_level
  23. local function set_can_level(itemstack, charge)
  24. itemstack:set_metadata(tostring(charge))
  25. end
  26. function cans.set_can_level(itemstack, level)
  27. if level < 0 then
  28. level = 0
  29. end
  30. local idef = minetest.registered_items[itemstack:get_name()]
  31. local max_level = idef._can_max_liquid_level
  32. set_can_level(itemstack, level)
  33. set_can_wear(itemstack, level, max_level)
  34. end
  35. local function node_in_group(name, list)
  36. if type(list) == "string" then
  37. return (name == list)
  38. elseif type(list) == "table" then
  39. for k, v in ipairs(list) do
  40. if name == v then
  41. return true
  42. end
  43. end
  44. end
  45. return false
  46. end
  47. function cans.register_can(d)
  48. local data = table.copy(d)
  49. minetest.register_tool(data.can_name, {
  50. description = data.can_description,
  51. inventory_image = data.can_inventory_image,
  52. stack_max = 1,
  53. liquids_pointable = true,
  54. wear_represents = "liquid_amount",
  55. groups = {not_repaired_by_anvil = 1, disable_repair = 1},
  56. _can_max_liquid_level = data.can_capacity,
  57. on_use = function(itemstack, user, pointed_thing)
  58. if pointed_thing.type ~= "node" then return end
  59. local node = minetest.get_node(pointed_thing.under)
  60. if not node_in_group(node.name, data.liquid_source_name) then return end
  61. local charge = get_can_level(itemstack)
  62. if charge == data.can_capacity then return end
  63. if minetest.is_protected(pointed_thing.under, user:get_player_name()) then
  64. minetest.log("action", user:get_player_name().." tried to take "..node.name.." at protected position "..minetest.pos_to_string(pointed_thing.under).." with a "..data.can_name)
  65. minetest.chat_send_player(user:get_player_name(), "# Server: That is on someone else's land!")
  66. return
  67. end
  68. if node.name == "default:lava_source" then
  69. minetest.add_node(pointed_thing.under, {name="fire:basic_flame"})
  70. local pos = user:get_pos()
  71. minetest.sound_play("default_cool_lava", {pos = pos, max_hear_distance = 16, gain = 0.25}, true)
  72. if not heatdamage.is_immune(user:get_player_name()) then
  73. bucket.harm_player_after(user:get_player_name(), 2*500)
  74. end
  75. else
  76. minetest.remove_node(pointed_thing.under)
  77. end
  78. charge = charge + 1
  79. set_can_level(itemstack, charge)
  80. set_can_wear(itemstack, charge, data.can_capacity)
  81. return itemstack
  82. end,
  83. on_place = function(itemstack, user, pointed_thing)
  84. if not user or not user:is_player() then
  85. return
  86. end
  87. local pname = user:get_player_name()
  88. if pointed_thing.type ~= "node" then return end
  89. local pos = pointed_thing.under
  90. local node = minetest.get_node(pos)
  91. local def = minetest.reg_ns_nodes[node.name] or {}
  92. if def.on_rightclick and not user:get_player_control().sneak then
  93. return def.on_rightclick(pos, node, user, itemstack, pointed_thing)
  94. end
  95. if node_in_group(node.name, data.liquid_source_name) or node_in_group(node.name, data.liquid_flowing_name) then
  96. -- Do nothing, `pos' already set.
  97. elseif not def.buildable_to then
  98. pos = pointed_thing.above
  99. local node = minetest.get_node(pos)
  100. def = minetest.reg_ns_nodes[node.name] or {}
  101. if not def.buildable_to then return end
  102. end
  103. local charge = get_can_level(itemstack)
  104. if charge == 0 then return end
  105. -- Check against local ground level.
  106. local success, ground_level = rc.get_ground_level_at_pos(pos)
  107. if not success then
  108. minetest.chat_send_player(pname, "# Server: That position is in the Void!")
  109. return
  110. end
  111. -- Above 10000 XP, player can use buckets.
  112. -- Note: this will allow high-XP players to place lava (which ignores
  113. -- protection) above ground. If such a player decides to grief somebody,
  114. -- I guess you'll need to form a committee! (You can still use city blocks
  115. -- to protect builds.)
  116. local lxp = (xp.get_xp(pname, "digxp") >= 10000)
  117. if not lxp or sheriff.is_cheater(pname) then
  118. if pos.y > ground_level then
  119. minetest.chat_send_player(pname, "# Server: Don't do that above ground!")
  120. easyvend.sound_error(pname)
  121. return
  122. end
  123. end
  124. if minetest.is_protected(pos, pname) then
  125. minetest.log("action", pname .. " tried to place " .. data.place_name .. " at protected position " .. minetest.pos_to_string(pos) .. " with a "..data.can_name)
  126. minetest.chat_send_player(pname, "# Server: Not on somebody else's land!")
  127. return
  128. end
  129. if city_block:in_disallow_liquid_zone(pos, user) then
  130. minetest.chat_send_player(pname, "# Server: Don't do that in town!")
  131. easyvend.sound_error(pname)
  132. return
  133. end
  134. minetest.set_node(pos, {name=data.place_name})
  135. charge = charge - 1
  136. set_can_level(itemstack, charge)
  137. set_can_wear(itemstack, charge, data.can_capacity)
  138. -- Notify dirt.
  139. dirtspread.on_environment(pos)
  140. droplift.notify(pos)
  141. return itemstack
  142. end,
  143. })
  144. end
  145. cans.register_can({
  146. can_name = "cans:water_can",
  147. can_description = "Water Can",
  148. can_inventory_image = "technic_water_can.png",
  149. can_capacity = 16,
  150. liquid_source_name = {"default:water_source", "cw:water_source"},
  151. liquid_flowing_name = {"default:water_flowing", "cw:water_flowing"},
  152. place_name = "default:water_source",
  153. })
  154. minetest.register_craft({
  155. output = 'cans:water_can',
  156. recipe = {
  157. {'zinc:ingot', 'rubber:rubber_fiber','zinc:ingot'},
  158. {'carbon_steel:ingot', '', 'carbon_steel:ingot'},
  159. {'zinc:ingot', 'carbon_steel:ingot', 'zinc:ingot'},
  160. }
  161. })
  162. cans.register_can({
  163. can_name = "cans:river_water_can",
  164. can_description = "Salt Water Can",
  165. can_inventory_image = "technic_river_water_can.png",
  166. can_capacity = 16,
  167. liquid_source_name = "default:river_water_source",
  168. liquid_flowing_name = "default:river_water_flowing",
  169. place_name = "default:river_water_source",
  170. })
  171. minetest.register_craft({
  172. type = "shapeless",
  173. output = "cans:river_water_can",
  174. recipe = {"cans:water_can"},
  175. })
  176. minetest.register_craft({
  177. type = "shapeless",
  178. output = "cans:water_can",
  179. recipe = {"cans:river_water_can"},
  180. })
  181. cans.register_can({
  182. can_name = "cans:lava_can",
  183. can_description = "Lava Can",
  184. can_inventory_image = "technic_lava_can.png",
  185. can_capacity = 8,
  186. liquid_source_name = "default:lava_source",
  187. liquid_flowing_name = "default:lava_flowing",
  188. place_name = "default:lava_source",
  189. })
  190. minetest.register_craft({
  191. output = 'cans:lava_can',
  192. recipe = {
  193. {'zinc:ingot', 'stainless_steel:ingot','zinc:ingot'},
  194. {'stainless_steel:ingot', '', 'stainless_steel:ingot'},
  195. {'zinc:ingot', 'stainless_steel:ingot', 'zinc:ingot'},
  196. }
  197. })