tag @s add meme

#model missing? spawn a new one
execute unless entity @e[tag=player_model,type=armor_stand,distance=..100] run function bd:control/summon_model

#find our model
execute as @e[tag=player_model,type=armor_stand] if score @s playerID = @e[tag=meme,limit=1] playerID run tag @s add my_model

#note: char_direction is important

#make sure we have the proper model
execute if entity @s[tag=arm_disconnected] as @e[limit=1,sort=nearest,type=armor_stand,tag=my_model,tag=has_arm] run function bd:control/char_model_no_arm
execute if entity @s[tag=!arm_disconnected] as @e[limit=1,sort=nearest,type=armor_stand,tag=my_model,tag=!has_arm] run function bd:control/char_model_arm

#come, tp to me
scoreboard players set @e[limit=1,sort=nearest,tag=my_model,type=armor_stand] lifespan 10
tp @e[limit=1,sort=nearest,tag=my_model] ~ ~-.8 ~

#correct direction by exploiting armor stand jank
#--done because armor stands are very lazy when it comes to showing what direction they're truly facing
#--this prevents the character model from getting stuck facing the wrong way visually
#--(I say "prevent" but it doesn't work 100%...  it stops the visual error *most* of the time)
execute if score global dCorrect matches 1 as @e[limit=1,sort=nearest,tag=my_model,type=armor_stand] at @s run tp @s ~ ~ ~ ~ 45
execute if score global dCorrect matches 4 as @e[limit=1,sort=nearest,tag=my_model,type=armor_stand] at @s run tp @s ~ ~ ~ ~ -45

#model is rotated to be in the proper direction
execute if entity @s[scores={lastDirection=1}] unless score global oTimer matches 9 as @e[limit=1,sort=nearest,tag=my_model,type=armor_stand] run data merge entity @s {Rotation:[0f]}
execute if entity @s[scores={lastDirection=2}] unless score global oTimer matches 9 as @e[limit=1,sort=nearest,tag=my_model,type=armor_stand] run data merge entity @s {Rotation:[45f]}
execute if entity @s[scores={lastDirection=3}] unless score global oTimer matches 9 as @e[limit=1,sort=nearest,tag=my_model,type=armor_stand] run data merge entity @s {Rotation:[90f]}
execute if entity @s[scores={lastDirection=4}] unless score global oTimer matches 9 as @e[limit=1,sort=nearest,tag=my_model,type=armor_stand] run data merge entity @s {Rotation:[135f]}
execute if entity @s[scores={lastDirection=5}] unless score global oTimer matches 9 as @e[limit=1,sort=nearest,tag=my_model,type=armor_stand] run data merge entity @s {Rotation:[180f]}
execute if entity @s[scores={lastDirection=6}] unless score global oTimer matches 9 as @e[limit=1,sort=nearest,tag=my_model,type=armor_stand] run data merge entity @s {Rotation:[225f]}
execute if entity @s[scores={lastDirection=7}] unless score global oTimer matches 9 as @e[limit=1,sort=nearest,tag=my_model,type=armor_stand] run data merge entity @s {Rotation:[270f]}
execute if entity @s[scores={lastDirection=8}] unless score global oTimer matches 9 as @e[limit=1,sort=nearest,tag=my_model,type=armor_stand] run data merge entity @s {Rotation:[315f]}

#my_model tag stays, removed later
tag @s remove meme