Add legacy node conversion to vl_legacy and update rails.lua to use it

This commit is contained in:
teknomunk 2024-08-11 10:39:03 -05:00
parent 3cb53f26ef
commit c09e32ae27
3 changed files with 70 additions and 40 deletions

View File

@ -17,3 +17,19 @@ Arguments:
* `old`: Itemstring to be converted
* `new`: New item string
## `vl_legacy.convert_node(pos, node)`
Converts legacy nodes to newer versions.
Arguments:
* `pos`: Position of the node to attempt conversion
* `node`: Node definition to convert. The node will be loaded from map data if `nil`.
The node definition for the old node must contain the field `_vl_legacy_convert` with
a value that is either a `function(pos, node)` or `string` for this call to have any
affect. If a function is provided, the function is called with `pos` and `node` as
arguments. If a string is provided, a node name conversion will occur.
This mod provides an LBM and ABM that will automatically call this function for nodes
with `group:legacy` set.

View File

@ -35,6 +35,17 @@ function mod.convert_inventory(inv)
mod.convert_inventory_lists(lists)
inv:set_lists(lists)
end
function mod.convert_node(pos, node)
local node = node or minetest.get_node(pos)
local node_def = minetest.registered_nodes[node.name]
local convert = node_def._vl_legacy_convert_node
if type(convert) == "function" then
convert(pos, node)
elseif type(convert) == "string" then
node.name = convert
minetest.swap_node(pos, node)
end
end
minetest.register_on_joinplayer(function(player)
mod.convert_inventory(player:get_inventory())
@ -49,4 +60,16 @@ minetest.register_lbm({
mod.convert_inventory(meta:get_inventory())
end
})
minetest.register_lbm({
name = "vl_legacy:convert_nodes",
nodenames = "group:legacy",
run_at_every_load = true,
action = mod.convert_node,
})
minetest.register_abm({
label = "Convert Legacy Nodes",
nodenames = "group:legacy",
interval = 5,
chance = 1,
action = mod.convert_node,
})

View File

@ -373,28 +373,24 @@ end
local CURVY_RAILS_MAP = {
["mcl_minecarts:rail"] = "mcl_minecarts:rail_v2",
}
local function convert_legacy_curvy_rails(pos, node)
node.name = CURVY_RAILS_MAP[node.name]
if node.name then
minetest.swap_node(pos, node)
mod.update_rail_connections(pos, { legacy = true, ignore_neighbor_connections = true })
end
end
for old,new in pairs(CURVY_RAILS_MAP) do
local new_def = minetest.registered_nodes[new]
minetest.register_node(old, {
drawtype = "raillike",
inventory_image = new_def.inventory_image,
groups = { rail = 1 },
groups = { rail = 1, legacy = 1 },
tiles = { new_def.tiles[1], new_def.tiles[1], new_def.tiles[1], new_def.tiles[1] },
_vl_legacy_convert_node = convert_legacy_curvy_rails
})
vl_legacy.register_item_conversion(old, new)
end
minetest.register_lbm({
name = "mcl_minecarts:update_legacy_curvy_rails",
nodenames = mcl_util.table_keys(CURVY_RAILS_MAP),
run_at_every_load = true,
action = function(pos, node)
node.name = CURVY_RAILS_MAP[node.name]
if node.name then
minetest.swap_node(pos, node)
mod.update_rail_connections(pos, { legacy = true, ignore_neighbor_connections = true })
end
end
})
local STRAIGHT_RAILS_MAP ={
["mcl_minecarts:golden_rail"] = "mcl_minecarts:golden_rail_v2",
["mcl_minecarts:golden_rail_on"] = "mcl_minecarts:golden_rail_v2_on",
@ -403,39 +399,34 @@ local STRAIGHT_RAILS_MAP ={
["mcl_minecarts:detector_rail"] = "mcl_minecarts:detector_rail_v2",
["mcl_minecarts:detector_rail_on"] = "mcl_minecarts:detector_rail_v2_on",
}
local function convert_legacy_straight_rail(pos, node)
node.name = STRAIGHT_RAILS_MAP[node.name]
if node.name then
local connections = mod.get_rail_connections(pos, { legacy = true, ignore_neighbor_connections = true })
if not mod.HORIZONTAL_STANDARD_RULES[connections] then
-- Drop an immortal object at this location
local item_entity = minetest.add_item(pos, ItemStack(node.name))
if item_entity then
item_entity:get_luaentity()._immortal = true
end
-- This is a configuration that doesn't exist in the new rail
-- Replace with a standard rail
node.name = "mcl_minecarts:rail_v2"
end
minetest.swap_node(pos, node)
mod.update_rail_connections(pos, { legacy = true, ignore_neighbor_connections = true })
end
end
for old,new in pairs(STRAIGHT_RAILS_MAP) do
local new_def = minetest.registered_nodes[new]
minetest.register_node(old, {
drawtype = "raillike",
inventory_image = new_def.inventory_image,
groups = { rail = 1 },
groups = { rail = 1, legacy = 1 },
tiles = { new_def.tiles[1], new_def.tiles[1], new_def.tiles[1], new_def.tiles[1] },
_vl_legacy_convert_node = convert_legacy_straight_rail,
})
vl_legacy.register_item_conversion(old, new)
end
minetest.register_lbm({
name = "mcl_minecarts:update_legacy_straight_rails",
nodenames = mcl_util.table_keys(STRAIGHT_RAILS_MAP),
run_at_every_load = true,
action = function(pos, node)
node.name = STRAIGHT_RAILS_MAP[node.name]
if node.name then
local connections = mod.get_rail_connections(pos, { legacy = true, ignore_neighbor_connections = true })
if not mod.HORIZONTAL_STANDARD_RULES[connections] then
-- Drop an immortal object at this location
local item_entity = minetest.add_item(pos, ItemStack(node.name))
if item_entity then
item_entity:get_luaentity()._immortal = true
end
-- This is a configuration that doesn't exist in the new rail
-- Replace with a standard rail
node.name = "mcl_minecarts:rail_v2"
end
minetest.swap_node(pos, node)
mod.update_rail_connections(pos, { legacy = true, ignore_neighbor_connections = true })
end
end
})