workaround for broken lua api with minetest.get_content_id *crashing* for non-existing nodes

This commit is contained in:
Sokomine 2024-01-11 18:48:37 +01:00
parent 9499987f68
commit d245b9a9ae
3 changed files with 40 additions and 35 deletions

View File

@ -19,30 +19,39 @@ moresnow.crazy_mode = true
-- end of configuration
--------------------------------------------------------------------------------
-- which shapes can we cover?
moresnow.shapes = {'top', 'fence_top', 'stair_top', 'slab_top',
'panel_top', 'micro_top', 'outer_stair_top', 'inner_stair_top',
'ramp_top', 'ramp_outer_top', 'ramp_inner_top'}
-- which snow node equivals which leaves node?
moresnow.nodetypes = {'snow','autumnleaves'}
-- adjustment for an annoying breaking change in the lua api
moresnow.get_cid = function(node_name)
if(not(node_name)
or (not(minetest.registered_nodes[node_name]) and not(minetest.registered_aliases[node_name]))) then
return nil
end
return minetest.get_content_id(node_name)
end
-- defines the on_construct function for falling/placed snow(balls)
dofile(minetest.get_modpath("moresnow")..'/snow_on_construct.lua');
-- which snow node equivals which leaves node?
moresnow.nodetypes = {'snow','autumnleaves'};
-- devines the 8 types of snow covers: general nodebox snow cover, stairs, slabs,
-- outer edge stair, inner edge stair, 3x homedecor shingles/technic cnc shapes
dofile(minetest.get_modpath("moresnow")..'/snow_cover_nodes.lua');
moresnow.build_translation_table();
-- some defines which fascilitate identification of nodes
moresnow.c_ignore = minetest.get_content_id( 'ignore' );
moresnow.c_air = minetest.get_content_id( 'air' );
moresnow.c_snow = minetest.get_content_id( 'default:snow' );
moresnow.c_snow_top = minetest.get_content_id( 'moresnow:snow_top' );
moresnow.c_snow_fence = minetest.get_content_id( 'moresnow:snow_fence_top' );
moresnow.c_snow_stair = minetest.get_content_id( 'moresnow:snow_stair_top' );
moresnow.c_snow_slab = minetest.get_content_id( 'moresnow:snow_slab_top' );
moresnow.c_snow_panel = minetest.get_content_id( 'moresnow:snow_panel_top' );
moresnow.c_snow_micro = minetest.get_content_id( 'moresnow:snow_micro_top' );
moresnow.c_snow_outer_stair = minetest.get_content_id( 'moresnow:snow_outer_stair_top' );
moresnow.c_snow_inner_stair = minetest.get_content_id( 'moresnow:snow_inner_stair_top' );
moresnow.c_snow_ramp_top = minetest.get_content_id( 'moresnow:snow_ramp_top' );
moresnow.c_snow_ramp_outer = minetest.get_content_id( 'moresnow:snow_ramp_outer_top' );
moresnow.c_snow_ramp_inner = minetest.get_content_id( 'moresnow:snow_ramp_inner_top' );
moresnow.c_ignore = moresnow.get_cid( 'ignore' );
moresnow.c_air = moresnow.get_cid( 'air' );
moresnow.c_snow = moresnow.get_cid( 'default:snow' );
-- create some suitable aliases
for _, v in ipairs(moresnow.shapes) do
moresnow['c_snow_'..v] = moresnow.get_cid('moresnow:snow_'..v)
end
-- takes a look at all defined nodes after startup and stores which shape they are;
-- this is important for finding the right snow cover to put on the shape below

View File

@ -10,7 +10,7 @@ moresnow.snow_param2_offset = {};
-- homedecor 3d shingles and technic cnc items are handled here
moresnow.identify_special_slopes = function( new_name, homedecor_prefix, technic_postfix, param2_offset )
-- these nodes are only supported if homedecor and/or technic are installed
local c_new_snow_node = minetest.get_content_id( new_name );
local c_new_snow_node = moresnow.get_cid( new_name )
if( not( c_new_snow_node )) then
return;
end
@ -20,7 +20,7 @@ moresnow.identify_special_slopes = function( new_name, homedecor_prefix, technic
'tree','steelblock','bronzeblock','stainless_steel','marble','granite'};
for _,v in ipairs( homedecor_materials ) do
local id = minetest.get_content_id( homedecor_prefix..v );
local id = moresnow.get_cid( homedecor_prefix..v )
-- the node has to be registered at this point; thus, the soft-dependency on homedecor and technic
if( id and id ~= moresnow.c_ignore ) then
moresnow.snow_cover[ id ] = c_new_snow_node;
@ -32,7 +32,7 @@ moresnow.identify_special_slopes = function( new_name, homedecor_prefix, technic
prefix = 'technic:';
end
local id = minetest.get_content_id( prefix..v..technic_postfix );
local id = moresnow.get_cid( prefix..v..technic_postfix )
-- the node has to be registered at this point; thus, the soft-dependency on homedecor and technic
if( id and id ~= moresnow.c_ignore ) then
moresnow.snow_cover[ id ] = c_new_snow_node;
@ -53,7 +53,7 @@ moresnow.identify_stairs_and_slabs = function()
for n,v in pairs( minetest.registered_nodes ) do
local id = minetest.get_content_id( n );
local id = moresnow.get_cid( n )
if( not( id ) or moresnow.snow_cover[ id ] ) then
@ -192,7 +192,7 @@ end
minetest.after( 0, moresnow.identify_stairs_and_slabs );
-- no snow on lava or flowing water
moresnow.snow_cover[ minetest.get_content_id( 'default:lava_source') ] = moresnow.c_air;
moresnow.snow_cover[ minetest.get_content_id( 'default:lava_flowing') ] = moresnow.c_air;
moresnow.snow_cover[ minetest.get_content_id( 'default:water_flowing') ] = moresnow.c_air;
moresnow.snow_cover[ minetest.get_content_id( 'default:river_water_flowing')] = moresnow.c_air;
moresnow.snow_cover[ moresnow.get_cid( 'default:lava_source') ] = moresnow.c_air
moresnow.snow_cover[ moresnow.get_cid( 'default:lava_flowing') ] = moresnow.c_air
moresnow.snow_cover[ moresnow.get_cid( 'default:water_flowing') ] = moresnow.c_air
moresnow.snow_cover[ moresnow.get_cid( 'default:river_water_flowing')] = moresnow.c_air

View File

@ -3,24 +3,20 @@
moresnow.translation_table = {}
moresnow.build_translation_table = function()
local shapes = {'top', 'fence_top', 'stair_top', 'slab_top',
'panel_top', 'micro_top', 'outer_stair_top', 'inner_stair_top',
'ramp_top', 'ramp_outer_top', 'ramp_inner_top' };
for _,t in ipairs(moresnow.nodetypes) do
moresnow.translation_table[ t ] = {};
for _,v in ipairs( shapes ) do
local id1 = minetest.get_content_id( 'moresnow:snow_'..v );
local id2 = minetest.get_content_id( 'moresnow:'..t..'_'..v );
for _,v in ipairs( moresnow.shapes ) do
local id1 = moresnow.get_cid( 'moresnow:snow_'..v )
local id2 = moresnow.get_cid( 'moresnow:'..t..'_'..v )
if( id1 ) then
moresnow.translation_table[ t ][ id1 ] = id2;
end
end
local id1 = minetest.get_content_id( 'default:snow' );
local id2 = minetest.get_content_id( 'moresnow:'..t );
local id1 = moresnow.get_cid( 'default:snow' )
local id2 = moresnow.get_cid( 'moresnow:'..t )
if( id1 ) then
moresnow.translation_table[ t ][ id1 ] = id2;
end
@ -168,7 +164,7 @@ moresnow.on_construct_select_shape = function( pos, falling_node_name, default_n
return;
end
local res = moresnow.suggest_snow_type( minetest.get_content_id( node1.name ), node1.param2 );
local res = moresnow.suggest_snow_type( moresnow.get_cid( node1.name ), node1.param2 )
-- snow_top is a special node suitable for nodeboxes; BUT: it only looks acceptable if the
-- node below that nodebox/torch/fence/etc is a solid one
@ -190,7 +186,7 @@ moresnow.on_construct_select_shape = function( pos, falling_node_name, default_n
end
return { remove_node = true};
end
local new_id2 = moresnow.snow_cover[ minetest.get_content_id( node2.name )];
local new_id2 = moresnow.snow_cover[ moresnow.get_cid( node2.name )]
-- if the node below this one can't handle a normal snow cover, we can't put a snow top on our node either
if( not( new_id2 ) or new_id2 ~= moresnow.c_snow) then
return { remove_node = true};