Harden against unknown nodes

This commit is contained in:
teknomunk 2024-04-06 07:33:47 +00:00
parent 659a387256
commit ce9e3481af
2 changed files with 20 additions and 21 deletions

View file

@ -56,7 +56,7 @@ local function handle_cart_enter_exit(self, pos, next_dir, event)
local check_pos = pos + dir * check[1] + right * check[2] + up * check[3] local check_pos = pos + dir * check[1] + right * check[2] + up * check[3]
local node = minetest.get_node(check_pos) local node = minetest.get_node(check_pos)
local node_def = minetest.registered_nodes[node.name] local node_def = minetest.registered_nodes[node.name]
if node_def then
-- node-specific hook -- node-specific hook
local hook_name = "_mcl_minecarts_"..event..check[4] local hook_name = "_mcl_minecarts_"..event..check[4]
local hook = node_def[hook_name] local hook = node_def[hook_name]
@ -66,6 +66,7 @@ local function handle_cart_enter_exit(self, pos, next_dir, event)
hook = mcl_minecarts[event..check[4]] hook = mcl_minecarts[event..check[4]]
if hook then hook(check_pos, self, next_dir, node_def) end if hook then hook(check_pos, self, next_dir, node_def) end
end end
end
-- Handle cart-specific behaviors -- Handle cart-specific behaviors
local hook = self["_mcl_minecarts_"..event] local hook = self["_mcl_minecarts_"..event]
@ -85,11 +86,13 @@ local function handle_cart_node_watches(self, dtime)
for _,node_pos in ipairs(watches) do for _,node_pos in ipairs(watches) do
local node = minetest.get_node(node_pos) local node = minetest.get_node(node_pos)
local node_def = minetest.registered_nodes[node.name] local node_def = minetest.registered_nodes[node.name]
if node_def then
local hook = node_def._mcl_minecarts_node_on_step local hook = node_def._mcl_minecarts_node_on_step
if hook and hook(node_pos, self, dtime) then if hook and hook(node_pos, self, dtime) then
new_watches[#new_watches+1] = node_pos new_watches[#new_watches+1] = node_pos
end end
end end
end
staticdata.node_watches = new_watches staticdata.node_watches = new_watches
end end
@ -197,11 +200,9 @@ local function calculate_acceleration(self, staticdata)
acceleration = 0.6 acceleration = 0.6
elseif staticdata.velocity >= ( node_def._max_acceleration_velocity or max_vel ) then elseif staticdata.velocity >= ( node_def._max_acceleration_velocity or max_vel ) then
-- Standard friction -- Standard friction
else elseif node_def and node_def._rail_acceleration then
if node_def._rail_acceleration then
acceleration = node_def._rail_acceleration * 4 acceleration = node_def._rail_acceleration * 4
end end
end
-- Factor in gravity after everything else -- Factor in gravity after everything else
local gravity_strength = 2.45 --friction * 5 local gravity_strength = 2.45 --friction * 5
@ -860,11 +861,9 @@ function mcl_minecarts.place_minecart(itemstack, pointed_thing, placer)
end end
local function dropper_place_minecart(dropitem, pos) local function dropper_place_minecart(dropitem, pos)
local node = minetest.get_node(pos)
local nodedef = minetest.registered_nodes[node.name]
-- Don't try to place the minecart if pos isn't a rail -- Don't try to place the minecart if pos isn't a rail
if (nodedef.groups.rail or 0) == 0 then return false end local node = minetest.get_node(pos)
if minetest.get_item_group(node.name, "rail") == 0 then return false end
mcl_minecarts.place_minecart(dropitem, { mcl_minecarts.place_minecart(dropitem, {
above = pos, above = pos,

View file

@ -207,8 +207,8 @@ local function update_rail_connections(pos, opt)
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
local nodedef = minetest.registered_nodes[node.name] local nodedef = minetest.registered_nodes[node.name]
if not nodedef._mcl_minecarts then if not nodedef or not nodedef._mcl_minecarts then
minetest.log("warning", "attemting to rail connect "..node.name) minetest.log("warning", "attemting to rail connect to "..node.name)
return return
end end