Use on_repair when repairing tube (#37)

* Use on_repair when repairing tube

* Use tube.on_repair

* Verify was_node registration
This commit is contained in:
SX 2022-06-23 19:06:01 +03:00 committed by GitHub
parent 7b15bdbd1f
commit 75894355d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 7 deletions

View File

@ -64,29 +64,40 @@ pipeworks.register_tube("pipeworks:broken_tube", {
local itemstack = puncher:get_wielded_item() local itemstack = puncher:get_wielded_item()
local wieldname = itemstack:get_name() local wieldname = itemstack:get_name()
local playername = puncher:get_player_name() local playername = puncher:get_player_name()
local log_msg = playername.." struck a broken tube at "..minetest.pos_to_string(pos).."\n" local log_msg = playername.." struck a broken tube at "..minetest.pos_to_string(pos).."\n "
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local was_node = minetest.deserialize(meta:get_string("the_tube_was")) local was_node = minetest.deserialize(meta:get_string("the_tube_was"))
if not was_node then if not was_node then
pipeworks.logger(log_msg.." but it can't be repaired.") pipeworks.logger(log_msg.."but it can't be repaired.")
return return
end end
if not pipeworks.check_and_wear_hammer(puncher) then if not pipeworks.check_and_wear_hammer(puncher) then
if wieldname == "" then if wieldname == "" then
pipeworks.logger(log_msg.." by hand. It's not very effective.") pipeworks.logger(log_msg.."by hand. It's not very effective.")
if minetest.settings:get_bool("enable_damage") then if minetest.settings:get_bool("enable_damage") then
minetest.chat_send_player(playername,S("Broken tubes may be a bit sharp. Perhaps try with a hammer?")) minetest.chat_send_player(playername,S("Broken tubes may be a bit sharp. Perhaps try with a hammer?"))
puncher:set_hp(puncher:get_hp()-1) puncher:set_hp(puncher:get_hp()-1)
end end
else else
pipeworks.logger(log_msg.." with "..wieldname.." but that tool is too weak.") pipeworks.logger(log_msg.."with "..wieldname.." but that tool is too weak.")
end end
return return
end end
pipeworks.logger(log_msg.." with "..wieldname.." to repair it.") log_msg = log_msg.."with "..wieldname.." to repair it"
local nodedef = minetest.registered_nodes[was_node.name]
if nodedef then
pipeworks.logger(log_msg..".")
if nodedef.tube and nodedef.tube.on_repair then
nodedef.tube.on_repair(pos, was_node)
else
minetest.swap_node(pos, { name = was_node.name, param2 = was_node.param2 }) minetest.swap_node(pos, { name = was_node.name, param2 = was_node.param2 })
pipeworks.scan_for_tube_objects(pos) pipeworks.scan_for_tube_objects(pos)
end end
else
pipeworks.logger(log_msg.." but original node "..was_node.name.." is not registered anymore.")
minetest.chat_send_player(playername, S("This tube cannot be repaired."))
end
end
} }
}) })

View File

@ -184,6 +184,18 @@ pipeworks.register_tube("pipeworks:teleport_tube", {
pos.y = target[d].y pos.y = target[d].y
pos.z = target[d].z pos.z = target[d].z
return pipeworks.meseadjlist return pipeworks.meseadjlist
end,
on_repair = function(pos, node)
local meta = minetest.get_meta(pos)
local channel = meta:get_string("channel")
minetest.swap_node(pos, { name = node.name, param2 = node.param2 })
pipeworks.scan_for_tube_objects(pos)
if channel ~= "" then
local can_receive = meta:get_int("can_receive")
set_tube(pos, channel, can_receive)
local cr_description = (can_receive == 1) and "sending and receiving" or "sending"
meta:set_string("infotext", S("Teleportation Tube @1 on '@2'", cr_description, channel))
end
end end
}, },
on_construct = function(pos) on_construct = function(pos)