init.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. cans = cans or {}
  2. local function set_can_wear(itemstack, level, max_level)
  3. local temp
  4. if level == 0 then
  5. temp = 0
  6. else
  7. temp = 65536 - math.floor(level / max_level * 65535)
  8. if temp > 65535 then temp = 65535 end
  9. if temp < 1 then temp = 1 end
  10. end
  11. itemstack:set_wear(temp)
  12. end
  13. local function get_can_level(itemstack)
  14. if itemstack:get_metadata() == "" then
  15. return 0
  16. else
  17. return tonumber(itemstack:get_metadata())
  18. end
  19. end
  20. local function set_can_level(itemstack, charge)
  21. itemstack:set_metadata(tostring(charge))
  22. end
  23. local function node_in_group(name, list)
  24. if type(list) == "string" then
  25. return (name == list)
  26. elseif type(list) == "table" then
  27. for k, v in ipairs(list) do
  28. if name == v then
  29. return true
  30. end
  31. end
  32. end
  33. return false
  34. end
  35. function cans.register_can(d)
  36. local data = table.copy(d)
  37. minetest.register_tool(data.can_name, {
  38. description = data.can_description,
  39. inventory_image = data.can_inventory_image,
  40. stack_max = 1,
  41. liquids_pointable = true,
  42. wear_represents = "liquid_amount",
  43. groups = {not_repaired_by_anvil = 1, disable_repair = 1},
  44. on_use = function(itemstack, user, pointed_thing)
  45. if pointed_thing.type ~= "node" then return end
  46. local node = minetest.get_node(pointed_thing.under)
  47. if not node_in_group(node.name, data.liquid_source_name) then return end
  48. local charge = get_can_level(itemstack)
  49. if charge == data.can_capacity then return end
  50. if minetest.is_protected(pointed_thing.under, user:get_player_name()) then
  51. 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)
  52. minetest.chat_send_player(user:get_player_name(), "# Server: That is on someone else's land!")
  53. return
  54. end
  55. if node.name == "default:lava_source" then
  56. minetest.add_node(pointed_thing.under, {name="fire:basic_flame"})
  57. local pos = user:getpos()
  58. minetest.sound_play("default_cool_lava", {pos = pos, max_hear_distance = 16, gain = 0.25})
  59. if not heatdamage.is_immune(user:get_player_name()) then
  60. bucket.harm_player_after(user:get_player_name(), 2)
  61. end
  62. else
  63. minetest.remove_node(pointed_thing.under)
  64. end
  65. charge = charge + 1
  66. set_can_level(itemstack, charge)
  67. set_can_wear(itemstack, charge, data.can_capacity)
  68. return itemstack
  69. end,
  70. on_place = function(itemstack, user, pointed_thing)
  71. if not user or not user:is_player() then
  72. return
  73. end
  74. if pointed_thing.type ~= "node" then return end
  75. local pos = pointed_thing.under
  76. local node = minetest.get_node(pos)
  77. local def = minetest.reg_ns_nodes[node.name] or {}
  78. if def.on_rightclick and not user:get_player_control().sneak then
  79. return def.on_rightclick(pos, node, user, itemstack, pointed_thing)
  80. end
  81. if node_in_group(node.name, data.liquid_source_name) or node_in_group(node.name, data.liquid_flowing_name) then
  82. -- Do nothing, `pos' already set.
  83. elseif not def.buildable_to then
  84. pos = pointed_thing.above
  85. local node = minetest.get_node(pos)
  86. def = minetest.reg_ns_nodes[node.name] or {}
  87. if not def.buildable_to then return end
  88. end
  89. local charge = get_can_level(itemstack)
  90. if charge == 0 then return end
  91. -- Check against local ground level.
  92. local success, ground_level = rc.get_ground_level_at_pos(pos)
  93. if not success then
  94. minetest.chat_send_player(user:get_player_name(), "# Server: That position is in the Void!")
  95. return
  96. end
  97. if pos.y > ground_level then
  98. minetest.chat_send_player(user:get_player_name(), "# Server: Don't do that above ground!")
  99. easyvend.sound_error(user:get_player_name())
  100. return
  101. end
  102. if minetest.is_protected(pos, user:get_player_name()) then
  103. 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)
  104. minetest.chat_send_player(user:get_player_name(), "# Server: Not on somebody else's land!")
  105. return
  106. end
  107. if city_block:in_city(pos) then
  108. minetest.chat_send_player(user:get_player_name(), "# Server: Don't do that in town!")
  109. easyvend.sound_error(user:get_player_name())
  110. return
  111. end
  112. minetest.set_node(pos, {name=data.place_name})
  113. charge = charge - 1
  114. set_can_level(itemstack, charge)
  115. set_can_wear(itemstack, charge, data.can_capacity)
  116. -- Notify dirt.
  117. dirtspread.on_environment(pos)
  118. droplift.notify(pos)
  119. return itemstack
  120. end,
  121. })
  122. end
  123. cans.register_can({
  124. can_name = "cans:water_can",
  125. can_description = "Water Can",
  126. can_inventory_image = "technic_water_can.png",
  127. can_capacity = 16,
  128. liquid_source_name = {"default:water_source", "cw:water_source"},
  129. liquid_flowing_name = {"default:water_flowing", "cw:water_flowing"},
  130. place_name = "default:water_source",
  131. })
  132. minetest.register_craft({
  133. output = 'cans:water_can',
  134. recipe = {
  135. {'zinc:ingot', 'rubber:rubber_fiber','zinc:ingot'},
  136. {'carbon_steel:ingot', '', 'carbon_steel:ingot'},
  137. {'zinc:ingot', 'carbon_steel:ingot', 'zinc:ingot'},
  138. }
  139. })
  140. cans.register_can({
  141. can_name = "cans:lava_can",
  142. can_description = "Lava Can",
  143. can_inventory_image = "technic_lava_can.png",
  144. can_capacity = 8,
  145. liquid_source_name = "default:lava_source",
  146. liquid_flowing_name = "default:lava_flowing",
  147. place_name = "default:lava_source",
  148. })
  149. minetest.register_craft({
  150. output = 'cans:lava_can',
  151. recipe = {
  152. {'zinc:ingot', 'stainless_steel:ingot','zinc:ingot'},
  153. {'stainless_steel:ingot', '', 'stainless_steel:ingot'},
  154. {'zinc:ingot', 'stainless_steel:ingot', 'zinc:ingot'},
  155. }
  156. })