David Leal
2020-06-12 a8daa417c485ee20716ec050d4c676b5c91af773
technic/tools/cans.lua
@@ -1,13 +1,110 @@
local water_can_max_load = 16
local lava_can_max_load = 8
local S = technic.getter
local function set_can_wear(itemstack, level, max_level)
   local temp
   if level == 0 then
      temp = 0
   else
      temp = 65536 - math.floor(level / max_level * 65535)
      if temp > 65535 then temp = 65535 end
      if temp < 1 then temp = 1 end
   end
   itemstack:set_wear(temp)
end
local function get_can_level(itemstack)
   if itemstack:get_metadata() == "" then
      return 0
   else
      return tonumber(itemstack:get_metadata())
   end
end
function technic.register_can(d)
   local data = {}
   for k, v in pairs(d) do data[k] = v end
   minetest.register_tool(data.can_name, {
      description = data.can_description,
      inventory_image = data.can_inventory_image,
      stack_max = 1,
      wear_represents = "content_level",
      liquids_pointable = true,
      on_use = function(itemstack, user, pointed_thing)
         if pointed_thing.type ~= "node" then return end
         local node = minetest.get_node(pointed_thing.under)
         if node.name ~= data.liquid_source_name then return end
         local charge = get_can_level(itemstack)
         if charge == data.can_capacity then return end
         if minetest.is_protected(pointed_thing.under, user:get_player_name()) then
            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)
            return
         end
         minetest.remove_node(pointed_thing.under)
         charge = charge + 1
         itemstack:set_metadata(tostring(charge))
         set_can_wear(itemstack, charge, data.can_capacity)
         return itemstack
      end,
      on_place = function(itemstack, user, pointed_thing)
         if pointed_thing.type ~= "node" then return end
         local pos = pointed_thing.under
         local node_name = minetest.get_node(pos).name
         local def = minetest.registered_nodes[node_name] or {}
         if def.on_rightclick and user and not user:get_player_control().sneak then
            return def.on_rightclick(pos, minetest.get_node(pos), user, itemstack, pointed_thing)
         end
         if not def.buildable_to or node_name == data.liquid_source_name then
            pos = pointed_thing.above
            node_name = minetest.get_node(pos).name
            def = minetest.registered_nodes[node_name] or {}
            -- Try to place node above the pointed source, or abort.
            if not def.buildable_to or node_name == data.liquid_source_name then return end
         end
         local charge = get_can_level(itemstack)
         if charge == 0 then return end
         if minetest.is_protected(pos, user:get_player_name()) then
            minetest.log("action", user:get_player_name().." tried to place "..data.liquid_source_name.." at protected position "..minetest.pos_to_string(pos).." with a "..data.can_name)
            return
         end
         minetest.set_node(pos, {name=data.liquid_source_name})
         charge = charge - 1
         itemstack:set_metadata(tostring(charge))
         set_can_wear(itemstack, charge, data.can_capacity)
         return itemstack
      end,
      on_refill = function(stack)
         stack:set_metadata(tostring(data.can_capacity))
         set_can_wear(stack, data.can_capacity, data.can_capacity)
         return stack
      end,
   })
end
technic.register_can({
   can_name = "technic:water_can",
   can_description = S("Water Can"),
   can_inventory_image = "technic_water_can.png",
   can_capacity = 16,
   liquid_source_name = "default:water_source",
   liquid_flowing_name = "default:water_flowing",
})
minetest.register_craft({
   output = 'technic:water_can 1',
   recipe = {
      {'technic:zinc_ingot', 'technic:rubber','technic:zinc_ingot'},
      {'default:steel_ingot', '', 'default:steel_ingot'},
      {'technic:zinc_ingot', 'default:steel_ingot', 'technic:zinc_ingot'},
      {'technic:carbon_steel_ingot', '', 'technic:carbon_steel_ingot'},
      {'technic:zinc_ingot', 'technic:carbon_steel_ingot', 'technic:zinc_ingot'},
   }
})
technic.register_can({
   can_name = "technic:lava_can",
   can_description = S("Lava Can"),
   can_inventory_image = "technic_lava_can.png",
   can_capacity = 8,
   liquid_source_name = "default:lava_source",
   liquid_flowing_name = "default:lava_flowing",
})
minetest.register_craft({
@@ -19,102 +116,20 @@
   }
})
minetest.register_tool("technic:water_can", {
   description = "Water Can",
   inventory_image = "technic_water_can.png",
   stack_max = 1,
   liquids_pointable = true,
   on_use = function(itemstack, user, pointed_thing)
      if pointed_thing.type ~= "node" then
               return end
      n = minetest.env:get_node(pointed_thing.under)
      item=itemstack:to_table()
      local load=nil
      if item["metadata"]=="" then load=0
      else load=tonumber(item["metadata"])
      end
      if n.name == "default:water_source" then
         if load+1<17 then
         minetest.env:add_node(pointed_thing.under, {name="air"})
          load=load+1;
         item["metadata"]=tostring(load)
         technic.set_RE_wear(item,load,water_can_max_load)
         itemstack:replace(item)
         end
         return itemstack
      end
      item=itemstack:to_table()
      if load==0 then return end
      if n.name == "default:water_flowing" then
         minetest.env:add_node(pointed_thing.under, {name="default:water_source"})
         load=load-1;
         item["metadata"]=tostring(load)
         technic.set_RE_wear(item,load,water_can_max_load)
         itemstack:replace(item)
         return itemstack
         end
      n = minetest.env:get_node(pointed_thing.above)
      if n.name == "air" then
         minetest.env:add_node(pointed_thing.above, {name="default:water_source"})
         load=load-1;
         item["metadata"]=tostring(load)
         technic.set_RE_wear(item,load,water_can_max_load)
         itemstack:replace(item)
         return itemstack
         end
   end,
technic.register_can({
   can_name = 'technic:river_water_can',
   can_description = S("River Water Can"),
   can_inventory_image = "technic_river_water_can.png",
   can_capacity = 16,
   liquid_source_name = "default:river_water_source",
   liquid_flowing_name = "default:river_water_flowing",
})
minetest.register_tool("technic:lava_can", {
   description = "Lava Can",
   inventory_image = "technic_lava_can.png",
   stack_max = 1,
   liquids_pointable = true,
   on_use = function(itemstack, user, pointed_thing)
      if pointed_thing.type ~= "node" then return end
      n = minetest.env:get_node(pointed_thing.under)
      item=itemstack:to_table()
      local load=nil
      if item["metadata"]=="" then load=0
      else load=tonumber(item["metadata"])
      end
      if n.name == "default:lava_source" then
         if load+1<17 then
         minetest.env:add_node(pointed_thing.under, {name="air"})
          load=load+1;
         item["metadata"]=tostring(load)
         technic.set_RE_wear(item,load,lava_can_max_load)
         itemstack:replace(item)
         end
         return itemstack
      end
      item=itemstack:to_table()
      if load==0 then return end
      if n.name == "default:lava_flowing" then
         minetest.env:add_node(pointed_thing.under, {name="default:lava_source"})
         load=load-1;
         item["metadata"]=tostring(load)
         technic.set_RE_wear(item,load,lava_can_max_load)
         itemstack:replace(item)
         return itemstack
         end
      n = minetest.env:get_node(pointed_thing.above)
      if n.name == "air" then
         minetest.env:add_node(pointed_thing.above, {name="default:lava_source"})
         load=load-1;
         item["metadata"]=tostring(load)
         technic.set_RE_wear(item,load,lava_can_max_load)
         itemstack:replace(item)
         return itemstack
         end
   end,
minetest.register_craft({
   output = 'technic:river_water_can 1',
   recipe = {
      {'technic:zinc_ingot', 'technic:rubber', 'technic:zinc_ingot'},
      {'default:steel_ingot', '', 'default:steel_ingot'},
      {'technic:zinc_ingot', 'default:steel_ingot', 'technic:zinc_ingot'},
   }
})