Genericise can logic

Factor out the logic common to water and lava cans.  Provide it in the
form of a technic.register_can() function, which can be called by other
mods to register cans for other liquids.
This commit is contained in:
Zefram 2014-07-20 23:14:14 +01:00
parent cca72f43f1
commit 6771ad9d33

@ -1,26 +1,5 @@
local water_can_max_load = 16
local lava_can_max_load = 8
local S = technic.getter local S = technic.getter
minetest.register_craft({
output = 'technic:water_can 1',
recipe = {
{'technic:zinc_ingot', 'technic:rubber','technic:zinc_ingot'},
{'technic:carbon_steel_ingot', '', 'technic:carbon_steel_ingot'},
{'technic:zinc_ingot', 'technic:carbon_steel_ingot', 'technic:zinc_ingot'},
}
})
minetest.register_craft({
output = 'technic:lava_can 1',
recipe = {
{'technic:zinc_ingot', 'technic:stainless_steel_ingot','technic:zinc_ingot'},
{'technic:stainless_steel_ingot', '', 'technic:stainless_steel_ingot'},
{'technic:zinc_ingot', 'technic:stainless_steel_ingot', 'technic:zinc_ingot'},
}
})
local function set_can_wear(itemstack, level, max_level) local function set_can_wear(itemstack, level, max_level)
local temp local temp
if level == 0 then if level == 0 then
@ -33,113 +12,97 @@ local function set_can_wear(itemstack, level, max_level)
itemstack:set_wear(temp) itemstack:set_wear(temp)
end end
minetest.register_tool("technic:water_can", { function technic.register_can(d)
description = S("Water Can"), local data = {}
inventory_image = "technic_water_can.png", for k, v in pairs(d) do data[k] = v end
stack_max = 1, minetest.register_tool(data.can_name, {
wear_represents = "content_level", description = data.can_description,
liquids_pointable = true, inventory_image = data.can_inventory_image,
on_use = function(itemstack, user, pointed_thing) stack_max = 1,
if pointed_thing.type ~= "node" then wear_represents = "content_level",
return liquids_pointable = true,
end on_use = function(itemstack, user, pointed_thing)
node = minetest.get_node(pointed_thing.under) if pointed_thing.type ~= "node" then
return
local charge = nil
if itemstack:get_metadata() == "" then
charge = 0
else
charge = tonumber(itemstack:get_metadata())
end
if node.name == "default:water_source" then
if charge < water_can_max_load then
minetest.remove_node(pointed_thing.under)
charge = charge + 1
itemstack:set_metadata(tostring(charge))
set_can_wear(itemstack, charge, water_can_max_load)
end end
return itemstack node = minetest.get_node(pointed_thing.under)
end
if charge == 0 then
return
end
if node.name == "default:water_flowing" then local charge = nil
minetest.set_node(pointed_thing.under, {name="default:water_source"}) if itemstack:get_metadata() == "" then
charge = charge - 1 charge = 0
itemstack:set_metadata(tostring(charge)) else
set_can_wear(itemstack, charge, water_can_max_load) charge = tonumber(itemstack:get_metadata())
return itemstack end
end if node.name == data.liquid_source_name then
if charge < data.can_capacity then
minetest.remove_node(pointed_thing.under)
charge = charge + 1
itemstack:set_metadata(tostring(charge))
set_can_wear(itemstack, charge, data.can_capacity)
end
return itemstack
end
if charge == 0 then
return
end
node = minetest.get_node(pointed_thing.above) if node.name == data.liquid_flowing_name then
if node.name == "air" then minetest.set_node(pointed_thing.under, {name=data.liquid_source_name})
minetest.set_node(pointed_thing.above, {name="default:water_source"}) charge = charge - 1
charge = charge - 1; itemstack:set_metadata(tostring(charge))
itemstack:set_metadata(tostring(charge)) set_can_wear(itemstack, charge, data.can_capacity)
set_can_wear(itemstack, charge, water_can_max_load) return itemstack
return itemstack end
end
end, node = minetest.get_node(pointed_thing.above)
on_refill = function(stack) if node.name == "air" then
stack:set_metadata(tostring(water_can_max_load)) minetest.set_node(pointed_thing.above, {name=data.liquid_source_name})
set_can_wear(stack, water_can_max_load, water_can_max_load) charge = charge - 1
return stack itemstack:set_metadata(tostring(charge))
end, set_can_wear(itemstack, charge, data.can_capacity)
return itemstack
end
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_tool("technic:lava_can", { minetest.register_craft({
description = S("Lava Can"), output = 'technic:water_can 1',
inventory_image = "technic_lava_can.png", recipe = {
stack_max = 1, {'technic:zinc_ingot', 'technic:rubber','technic:zinc_ingot'},
wear_represents = "content_level", {'technic:carbon_steel_ingot', '', 'technic:carbon_steel_ingot'},
liquids_pointable = true, {'technic:zinc_ingot', 'technic:carbon_steel_ingot', 'technic:zinc_ingot'},
on_use = function(itemstack, user, pointed_thing) }
if pointed_thing.type ~= "node" then
return
end
node = minetest.get_node(pointed_thing.under)
local charge = 0
if itemstack:get_metadata() == "" then
charge = 0
else
charge = tonumber(itemstack:get_metadata())
end
if node.name == "default:lava_source" then
if charge < lava_can_max_load then
minetest.remove_node(pointed_thing.under)
charge = charge + 1
itemstack:set_metadata(tostring(charge))
set_can_wear(itemstack, charge, lava_can_max_load)
end
return itemstack
end
if charge == 0 then
return
end
if node.name == "default:lava_flowing" then
minetest.set_node(pointed_thing.under, {name="default:lava_source"})
charge = charge - 1
itemstack:set_metadata(tostring(charge))
set_can_wear(itemstack, charge, lava_can_max_load)
return itemstack
end
node = minetest.get_node(pointed_thing.above)
if node.name == "air" then
minetest.set_node(pointed_thing.above, {name="default:lava_source"})
charge = charge - 1
itemstack:set_metadata(tostring(charge))
set_can_wear(itemstack, charge, lava_can_max_load)
return itemstack
end
end,
on_refill = function(stack)
stack:set_metadata(tostring(lava_can_max_load))
set_can_wear(stack, lava_can_max_load, lava_can_max_load)
return stack
end,
}) })
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({
output = 'technic:lava_can 1',
recipe = {
{'technic:zinc_ingot', 'technic:stainless_steel_ingot','technic:zinc_ingot'},
{'technic:stainless_steel_ingot', '', 'technic:stainless_steel_ingot'},
{'technic:zinc_ingot', 'technic:stainless_steel_ingot', 'technic:zinc_ingot'},
}
})