init.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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:getpos()
  60. minetest.sound_play("default_cool_lava", {pos = pos, max_hear_distance = 16, gain = 0.25})
  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. if pointed_thing.type ~= "node" then return end
  77. local pos = pointed_thing.under
  78. local node = minetest.get_node(pos)
  79. local def = minetest.reg_ns_nodes[node.name] or {}
  80. if def.on_rightclick and not user:get_player_control().sneak then
  81. return def.on_rightclick(pos, node, user, itemstack, pointed_thing)
  82. end
  83. if node_in_group(node.name, data.liquid_source_name) or node_in_group(node.name, data.liquid_flowing_name) then
  84. -- Do nothing, `pos' already set.
  85. elseif not def.buildable_to then
  86. pos = pointed_thing.above
  87. local node = minetest.get_node(pos)
  88. def = minetest.reg_ns_nodes[node.name] or {}
  89. if not def.buildable_to then return end
  90. end
  91. local charge = get_can_level(itemstack)
  92. if charge == 0 then return end
  93. -- Check against local ground level.
  94. local success, ground_level = rc.get_ground_level_at_pos(pos)
  95. if not success then
  96. minetest.chat_send_player(user:get_player_name(), "# Server: That position is in the Void!")
  97. return
  98. end
  99. if pos.y > ground_level then
  100. minetest.chat_send_player(user:get_player_name(), "# Server: Don't do that above ground!")
  101. easyvend.sound_error(user:get_player_name())
  102. return
  103. end
  104. if minetest.is_protected(pos, user:get_player_name()) then
  105. minetest.log("action", user:get_player_name().." tried to place "..data.place_name.." at protected position "..minetest.pos_to_string(pos).." with a "..data.can_name)
  106. minetest.chat_send_player(user:get_player_name(), "# Server: Not on somebody else's land!")
  107. return
  108. end
  109. if city_block:in_city(pos) then
  110. minetest.chat_send_player(user:get_player_name(), "# Server: Don't do that in town!")
  111. easyvend.sound_error(user:get_player_name())
  112. return
  113. end
  114. minetest.set_node(pos, {name=data.place_name})
  115. charge = charge - 1
  116. set_can_level(itemstack, charge)
  117. set_can_wear(itemstack, charge, data.can_capacity)
  118. -- Notify dirt.
  119. dirtspread.on_environment(pos)
  120. droplift.notify(pos)
  121. return itemstack
  122. end,
  123. })
  124. end
  125. cans.register_can({
  126. can_name = "cans:water_can",
  127. can_description = "Water Can",
  128. can_inventory_image = "technic_water_can.png",
  129. can_capacity = 16,
  130. liquid_source_name = {"default:water_source", "cw:water_source"},
  131. liquid_flowing_name = {"default:water_flowing", "cw:water_flowing"},
  132. place_name = "default:water_source",
  133. })
  134. minetest.register_craft({
  135. output = 'cans:water_can',
  136. recipe = {
  137. {'zinc:ingot', 'rubber:rubber_fiber','zinc:ingot'},
  138. {'carbon_steel:ingot', '', 'carbon_steel:ingot'},
  139. {'zinc:ingot', 'carbon_steel:ingot', 'zinc:ingot'},
  140. }
  141. })
  142. cans.register_can({
  143. can_name = "cans:lava_can",
  144. can_description = "Lava Can",
  145. can_inventory_image = "technic_lava_can.png",
  146. can_capacity = 8,
  147. liquid_source_name = "default:lava_source",
  148. liquid_flowing_name = "default:lava_flowing",
  149. place_name = "default:lava_source",
  150. })
  151. minetest.register_craft({
  152. output = 'cans:lava_can',
  153. recipe = {
  154. {'zinc:ingot', 'stainless_steel:ingot','zinc:ingot'},
  155. {'stainless_steel:ingot', '', 'stainless_steel:ingot'},
  156. {'zinc:ingot', 'stainless_steel:ingot', 'zinc:ingot'},
  157. }
  158. })