init.lua 6.1 KB

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