2021-05-29 16:12:33 +02:00
local S = minetest.get_translator ( minetest.get_current_modname ( ) )
2019-03-08 00:51:45 +01:00
2021-04-18 16:03:23 +02:00
-- Spawn a stand entity
local function spawn_stand_entity ( pos , node )
local luaentity = minetest.add_entity ( pos , " mcl_armor_stand:armor_entity " ) : get_luaentity ( )
luaentity : update_rotation ( node or minetest.get_node ( pos ) )
return luaentity
end
2017-01-06 04:23:11 +01:00
2021-04-18 16:03:23 +02:00
-- Find a stand entity or spawn one
local function get_stand_entity ( pos , node )
for _ , obj in ipairs ( minetest.get_objects_inside_radius ( pos , 0 ) ) do
local luaentity = obj : get_luaentity ( )
if luaentity and luaentity.name == " mcl_armor_stand:armor_entity " then
return luaentity
2017-01-06 04:23:11 +01:00
end
end
2021-04-18 16:03:23 +02:00
return spawn_stand_entity ( pos , node )
2017-01-06 04:23:11 +01:00
end
2021-04-18 16:03:23 +02:00
-- Migrate the old inventory format
local function migrate_inventory ( inv )
inv : set_size ( " armor " , 5 )
local lists = inv : get_lists ( )
for name , element in pairs ( mcl_armor.elements ) do
local listname = " armor_ " .. name
local list = lists [ listname ]
if list then
inv : set_stack ( " armor " , element.index , list [ 1 ] )
inv : set_size ( listname , 0 )
2017-01-06 04:23:11 +01:00
end
end
end
2021-04-18 16:03:23 +02:00
-- Drop all armor on the ground when it got destroyed
local function drop_inventory ( pos )
local inv = minetest.get_meta ( pos ) : get_inventory ( )
for _ , stack in pairs ( inv : get_list ( " armor " ) ) do
2017-06-11 14:16:22 +02:00
if not stack : is_empty ( ) then
local p = { x = pos.x + math.random ( 0 , 10 ) / 10 - 0.5 , y = pos.y , z = pos.z + math.random ( 0 , 10 ) / 10 - 0.5 }
minetest.add_item ( p , stack )
end
end
end
2019-03-09 07:01:12 +01:00
-- TODO: The armor stand should be an entity
2020-02-18 16:00:02 +01:00
minetest.register_node ( " mcl_armor_stand:armor_stand " , {
2019-03-08 00:51:45 +01:00
description = S ( " Armor Stand " ) ,
2020-03-08 03:18:47 +01:00
_tt_help = S ( " Displays pieces of armor " ) ,
2019-03-08 00:51:45 +01:00
_doc_items_longdesc = S ( " An armor stand is a decorative object which can display different pieces of armor. Anything which players can wear as armor can also be put on an armor stand. " ) ,
2019-03-16 00:07:44 +01:00
_doc_items_usagehelp = S ( " Just place an armor item on the armor stand. To take the top piece of armor from the armor stand, select your hand and use the place key on the armor stand. " ) ,
2017-01-06 04:23:11 +01:00
drawtype = " mesh " ,
mesh = " 3d_armor_stand.obj " ,
2023-02-08 02:09:28 +01:00
inventory_image = " mcl_armor_stand_item.png " ,
wield_image = " mcl_armor_stand_item.png " ,
2017-06-11 14:21:52 +02:00
tiles = { " default_wood.png " , " mcl_stairs_stone_slab_top.png " } ,
2017-01-06 04:23:11 +01:00
paramtype = " light " ,
p aramtype2 = " facedir " ,
walkable = false ,
2017-05-30 17:18:19 +02:00
is_ground_content = false ,
2017-01-16 23:34:40 +01:00
stack_max = 16 ,
2017-01-06 04:23:11 +01:00
selection_box = {
type = " fixed " ,
fixed = { - 0.5 , - 0.5 , - 0.5 , 0.5 , 1.4 , 0.5 }
} ,
2019-03-09 07:01:12 +01:00
-- TODO: This should be breakable by 2 quick punches
2019-03-09 08:40:52 +01:00
groups = { handy = 1 , deco_block = 1 , dig_by_piston = 1 , attached_node = 1 } ,
2017-02-27 18:32:35 +01:00
_mcl_hardness = 2 ,
2017-02-11 18:46:23 +01:00
sounds = mcl_sounds.node_sound_wood_defaults ( ) ,
2017-01-06 04:23:11 +01:00
on_construct = function ( pos )
2021-04-18 16:03:23 +02:00
spawn_stand_entity ( pos )
end ,
on_destruct = function ( pos )
drop_inventory ( pos )
2017-01-06 04:23:11 +01:00
end ,
2017-02-19 02:09:49 +01:00
on_rightclick = function ( pos , node , clicker , itemstack , pointed_thing )
2019-02-08 21:59:01 +01:00
local protname = clicker : get_player_name ( )
2021-04-18 16:03:23 +02:00
2019-02-08 21:59:01 +01:00
if minetest.is_protected ( pos , protname ) then
minetest.record_protection_violation ( pos , protname )
2019-02-08 22:23:26 +01:00
return itemstack
2019-02-08 21:59:01 +01:00
end
2019-02-18 23:55:18 +01:00
2021-04-18 16:03:23 +02:00
return mcl_armor.equip ( itemstack , get_stand_entity ( pos , node ) . object , true )
2017-01-06 04:23:11 +01:00
end ,
2019-12-09 19:41:10 +01:00
on_rotate = function ( pos , node , user , mode )
if mode == screwdriver.ROTATE_FACE then
node.param2 = ( node.param2 + 1 ) % 4
minetest.swap_node ( pos , node )
2021-04-18 16:03:23 +02:00
get_stand_entity ( pos , node ) : update_rotation ( node )
2019-12-09 19:41:10 +01:00
return true
end
return false
end ,
2017-01-06 04:23:11 +01:00
} )
2020-02-18 16:00:02 +01:00
minetest.register_entity ( " mcl_armor_stand:armor_entity " , {
2021-04-18 16:03:23 +02:00
initial_properties = {
physical = true ,
visual = " mesh " ,
mesh = " 3d_armor_entity.obj " ,
visual_size = { x = 1 , y = 1 } ,
collisionbox = { - 0.1 , - 0.4 , - 0.1 , 0.1 , 1.3 , 0.1 } ,
pointable = false ,
textures = { " blank.png " } ,
timer = 0 ,
static_save = false ,
} ,
on_activate = function ( self )
self.object : set_armor_groups ( { immortal = 1 } )
self.node_pos = vector.round ( self.object : get_pos ( ) )
self.inventory = minetest.get_meta ( self.node_pos ) : get_inventory ( )
migrate_inventory ( self.inventory )
2021-04-18 16:20:32 +02:00
mcl_armor.update ( self.object )
2017-01-06 04:23:11 +01:00
end ,
on_step = function ( self , dtime )
2021-04-18 16:03:23 +02:00
if minetest.get_node ( self.node_pos ) . name ~= " mcl_armor_stand:armor_stand " then
2017-01-06 04:23:11 +01:00
self.object : remove ( )
end
end ,
2021-04-18 16:03:23 +02:00
update_armor = function ( self , info )
self.object : set_properties ( { textures = { info.texture } } )
end ,
update_rotation = function ( self , node )
self.object : set_yaw ( minetest.dir_to_yaw ( minetest.facedir_to_dir ( node.param2 ) ) )
end ,
2017-01-06 04:23:11 +01:00
} )
2019-02-18 23:55:18 +01:00
minetest.register_lbm ( {
label = " Respawn armor stand entities " ,
2020-02-18 16:00:02 +01:00
name = " mcl_armor_stand:respawn_entities " ,
nodenames = { " mcl_armor_stand:armor_stand " } ,
2019-02-18 23:55:18 +01:00
run_at_every_load = true ,
action = function ( pos , node )
2021-04-18 16:03:23 +02:00
spawn_stand_entity ( pos , node )
2019-02-18 23:55:18 +01:00
end ,
} )
2017-01-06 04:23:11 +01:00
minetest.register_craft ( {
2020-02-18 16:00:02 +01:00
output = " mcl_armor_stand:armor_stand " ,
2017-01-06 04:23:11 +01:00
recipe = {
2017-01-31 23:32:56 +01:00
{ " mcl_core:stick " , " mcl_core:stick " , " mcl_core:stick " } ,
{ " " , " mcl_core:stick " , " " } ,
2017-06-05 18:40:56 +02:00
{ " mcl_core:stick " , " mcl_stairs:slab_stone " , " mcl_core:stick " } ,
2017-01-06 04:23:11 +01:00
}
} )
2020-02-18 16:13:37 +01:00
-- Legacy handling
2020-02-18 16:00:02 +01:00
minetest.register_alias ( " 3d_armor_stand:armor_stand " , " mcl_armor_stand:armor_stand " )
2020-02-18 16:13:37 +01:00
minetest.register_entity ( " :3d_armor_stand:armor_entity " , {
on_activate = function ( self )
minetest.log ( " action " , " [mcl_armor_stand] Removing legacy entity: 3d_armor_stand:armor_entity " )
self.object : remove ( )
end ,
static_save = false ,
} )