###Adds requisite scores###
scoreboard players add @s nitResourceTime 1
scoreboard players set @s[scores={nitResourceTime=9..}] nitResourceTime 7

###Select visitor###
execute if entity @s[scores={nitResourceTime=7}] run loot spawn ~ ~ ~ loot nitk:command/town_square_random
execute if entity @s[scores={nitResourceTime=7}] as @e[type=item,distance=..1] run function nitk:recurs/town_square/visitor_determine

###Summon Visitor###
execute if entity @s[scores={nitResourceTime=7,nitResourceSpawn=1}] run summon wandering_trader ~ ~ ~ {Tags:[smithed.entity,smithed.strict,nitEntity,nitVisitor,nitVisitorCaravan],CustomName:'{"text":"Nomad Caravan","color":"green"}',NoAI:1b,NoGravity:1b,PersistenceRequired:1b,Invulnerable:1b,Rotation:[90.0f,0.0f]}
execute if entity @s[scores={nitResourceTime=7,nitResourceSpawn=2}] run summon mooshroom ~ ~ ~ {Tags:[smithed.entity,smithed.strict,nitEntity,nitVisitor,nitVisitorMooshroom],CustomName:'{"text":"Mooshroom Herd","color":"red"}',NoAI:1b,NoGravity:1b,PersistenceRequired:1b,Invulnerable:1b,Rotation:[90.0f,0.0f]}
execute if entity @s[scores={nitResourceTime=7,nitResourceSpawn=3}] run summon wolf ~ ~ ~ {Tags:[smithed.entity,smithed.strict,nitEntity,nitVisitor,nitVisitorWolf],CustomName:'{"text":"Pack of Wolves","color":"gray"}',NoAI:1b,NoGravity:1b,PersistenceRequired:1b,Invulnerable:1b,Rotation:[90.0f,0.0f]}
execute if entity @s[scores={nitResourceTime=7,nitResourceSpawn=4}] run summon witch ~ ~ ~ {Tags:[smithed.entity,smithed.strict,nitEntity,nitVisitor,nitVisitorWitch],CustomName:'{"text":"Witch Coven","color":"dark_purple"}',NoAI:1b,NoGravity:1b,PersistenceRequired:1b,Invulnerable:1b,Rotation:[90.0f,0.0f]}
execute if entity @s[scores={nitResourceTime=7,nitResourceSpawn=5}] run summon piglin ~ ~ ~ {Tags:[smithed.entity,smithed.strict,nitEntity,nitVisitor,nitVisitorPiglin],CustomName:'{"text":"Piglin Posse","color":"dark_red"}',NoAI:1b,NoGravity:1b,PersistenceRequired:1b,Invulnerable:1b,Rotation:[90.0f,0.0f],IsImmuneToZombification:1b}
execute if entity @s[scores={nitResourceTime=7}] run execute as @e[tag=nitVisitor,limit=1,sort=nearest] at @s facing entity @e[tag=nitFountainFace,limit=1,sort=nearest] feet run tp ~ ~ ~

###Anounces visitor###
execute if entity @s[scores={nitResourceTime=7,nitResourceSpawn=1..}] run tellraw @a[tag=nitCitizen,distance=..100] [{"text":"[۩]","color":"green","bold":true},{"text":": ","color":"white","bold":false},{"text":"A visitor has appeared at the stage of the Town Square!","color":"gold","bold":false}]
execute if entity @s[scores={nitResourceTime=7,nitResourceSpawn=1..}] run playsound event.raid.horn block @a ~ ~ ~ 10 2

###Sends away visitor/triggers consequence###
execute if entity @s[scores={nitResourceTime=8}] run tp @e[tag=nitVisitor,limit=1,sort=nearest] ~ -100 ~
execute if entity @s[scores={nitResourceTime=8}] run kill @e[tag=nitVisitor,limit=1,sort=nearest]
execute if entity @s[scores={nitResourceTime=8,nitResourceSpawn=1..2}] run tellraw @a[tag=nitCitizen,distance=..100] [{"text":"[۩]","color":"green","bold":true},{"text":": ","color":"white","bold":false},{"text":"The visitor has decided to leave your town.","color":"gold","bold":false}]
execute if entity @s[scores={nitResourceTime=8,nitResourceSpawn=3..5}] run tellraw @a[tag=nitCitizen,distance=..100] [{"text":"[۩]","color":"green","bold":true},{"text":": ","color":"white","bold":false},{"text":"Your inaction has angered the visitor. The town is under attack!","color":"red","bold":false}]
execute if entity @s[scores={nitResourceTime=8,nitResourceSpawn=3}] run execute as @e[tag=nitKTP,limit=1,sort=nearest] at @s run function nitk:recurs/town_square/setup_wolves
execute if entity @s[scores={nitResourceTime=8,nitResourceSpawn=4}] run execute as @e[tag=nitKTP,limit=1,sort=nearest] at @s run function nitk:recurs/town_square/setup_witches
execute if entity @s[scores={nitResourceTime=8,nitResourceSpawn=5}] run execute as @e[tag=nitKTP,limit=1,sort=nearest] at @s run function nitk:recurs/town_square/setup_piglins
scoreboard players set @s[scores={nitResourceTime=8}] nitResourceSpawn 0
scoreboard players set @s[scores={nitResourceTime=8}] nitResourceTime 0