gamerule commandBlockOutput false

###Sets scoreboards used for advancements, attacks, and resource spawning###
scoreboard objectives add nitSiteCraft dummy
scoreboard objectives add nitCastleCraft dummy
scoreboard objectives add nitHellAttacks dummy
scoreboard objectives add nitResourceTime dummy
scoreboard objectives add nitResourceSpawn dummy
scoreboard objectives add nitTaxSpawn dummy
scoreboard objectives add nitID dummy
scoreboard objectives add nitTemp dummy
scoreboard objectives add nitSpreadLim dummy
scoreboard objectives add nitRoadType dummy
scoreboard objectives add nitLightType dummy
scoreboard objectives add nitPostType dummy
scoreboard objectives add nitRaidProg dummy
scoreboard objectives add nitSuccess dummy

###Create the settler wagon###
place template nitk:wagon ~-7 ~-3 ~-7
tp @s ~ ~2 ~

###Place siteplanners at borders###
place template nitk:siteplanner_e ~7 ~ ~
place template nitk:siteplanner_w ~-7 ~ ~
place template nitk:siteplanner_n ~ ~ ~-7
place template nitk:siteplanner_s ~ ~ ~7

###Remove the charter###
clear @p written_book{title:"Town Charter"}

###Assigns the town's ID###
tag @a[distance=..20] add nitCitizen
execute store result score @s nitID run data get entity @s UUID[0]
scoreboard players operation @e[tag=nitStoneStor,limit=1,sort=nearest] nitID = @s nitID
scoreboard players operation @e[tag=nitLumberStor,limit=1,sort=nearest] nitID = @s nitID

###Assigns wagon color###
function nitk:wagon_color

###Adds forceload to ensure structure generation###
forceload add ~-100 ~-100 ~100 ~100

###Summons markers for hell attacks###
summon minecraft:marker ~ ~ ~ {Tags:[nitEntity,nitEntityTechnical,nitHellSite1,nitHellSite,smithed.entity,smithed.strict],Invulnerable:1b}
summon minecraft:marker ~ ~ ~ {Tags:[nitEntity,nitEntityTechnical,nitHellSite2,nitHellSite,smithed.entity,smithed.strict],Invulnerable:1b}
summon minecraft:marker ~ ~ ~ {Tags:[nitEntity,nitEntityTechnical,nitHellSite3,nitHellSite,smithed.entity,smithed.strict],Invulnerable:1b}
summon minecraft:marker ~ ~ ~ {Tags:[nitEntity,nitEntityTechnical,nitHellSite4,nitHellSite,smithed.entity,smithed.strict],Invulnerable:1b}
summon minecraft:marker ~ ~ ~ {Tags:[nitEntity,nitEntityTechnical,nitHellSite5,nitHellSite,smithed.entity,smithed.strict],Invulnerable:1b}
summon minecraft:marker ~ ~ ~ {Tags:[nitEntity,nitEntityTechnical,nitHellSite6,nitHellSite,smithed.entity,smithed.strict],Invulnerable:1b,NoGravity:1b}
scoreboard players set @e[tag=nitHellSite,limit=6,sort=nearest] nitHellAttacks 0
scoreboard players operation @e[tag=nitHellSite,limit=6,sort=nearest] nitID = @s nitID

###Spreads the hell attack markers for this town###
scoreboard players operation #nitID nitTemp = @s nitID
execute as @e[tag=nitHellSite,tag=!nitHellSite3,tag=!nitHellSite5,limit=4,sort=nearest] if score @s nitID = #nitID nitTemp run tag @s add nitID_match
scoreboard players set @s nitSpreadLim 0
function nitk:spread_hell
execute as @e[tag=nitHellSite,tag=nitID_match] at @s run function nitk:special/special_gen_ground
tag @e[tag=nitHellSite,tag=nitID_match] remove nitID_match

###Summons markers for natural structures###
summon minecraft:marker ~ ~ ~ {Tags:[nitEntity,nitEntityTechnical,nitEndVault,nitKSpecialFeature,smithed.entity,smithed.strict],Invulnerable:1b,NoGravity:1b}
summon minecraft:marker ~ ~ ~ {Tags:[nitEntity,nitEntityTechnical,nitCrucible,nitKSpecialFeature,smithed.entity,smithed.strict],Invulnerable:1b,NoGravity:1b}
summon minecraft:marker ~ ~ ~ {Tags:[nitEntity,nitEntityTechnical,nitHaroldCage,nitKSpecialFeature,smithed.entity,smithed.strict],Invulnerable:1b,NoGravity:1b}
summon minecraft:marker ~ ~ ~ {Tags:[nitEntity,nitEntityTechnical,nitAlignPuz,nitKSpecialFeature,smithed.entity,smithed.strict],Invulnerable:1b,NoGravity:1b}
scoreboard players operation @e[tag=nitKSpecialFeature,limit=4,sort=nearest] nitID = @s nitID

###Spreads and builds the natural structures for this town###
scoreboard players operation #nitID nitTemp = @s nitID
execute as @e[tag=nitKSpecialFeature,limit=4,sort=nearest] if score @s nitID = #nitID nitTemp run tag @s add nitID_match
scoreboard players set @s nitSpreadLim 0
function nitk:spread_features
execute as @e[tag=nitKSpecialFeature,tag=nitID_match] at @s run function nitk:special/generate

execute as @s[tag=nitDistanceWarn] run tellraw @s [{"text":"[۩]","color":"green","bold":true},{"text":": ","color":"white","bold":false},{"text":"Beware, some features may spawn very close to your town...","color":"red","bold":false}]
tag @s remove nitDistanceWarn

###Summons the town planner###
execute as @e[tag=nitTownPlanner,limit=1,sort=nearest] at @s unless entity @e[type=villager,tag=nitKTP,distance=..2] run summon villager ~ ~ ~ {VillagerData:{profession:cartographer,level:4,type:"plains"},Invulnerable:1,PersistenceRequired:1,NoAI:1,CustomName:'{"text":"Town Planner"}',Offers:{Recipes:[{buy:{id:"minecraft:feather",Count:1},sell:{id:"minecraft:written_book",Count:1},maxUses:9999999},{buy:{id:gold_ingot,Count:4},sell:{id:"minecraft:paper",Count:1b,tag:{nitk_buildingPermit:1b,HideFlags:1,display:{Lore:['{"text":"The final ingredient","color":"gray","italic":false}','{"text":"in new town structures.","color":"gray","italic":false}'],Name:'{"text":"Building Permit","color":"blue","italic":false}'},Enchantments:[{id:"minecraft:protection",lvl:0}]}},maxUses:9999999},{buy:{id:gold_ingot,Count:24},buyB:{id:"minecraft:paper",Count:1b,tag:{nitk_buildingPermit:1b,HideFlags:1,display:{Lore:['{"text":"The final ingredient","color":"gray","italic":false}','{"text":"in new town structures.","color":"gray","italic":false}'],Name:'{"text":"Building Permit","color":"blue","italic":false}'},Enchantments:[{id:"minecraft:protection",lvl:0}]}},sell:{id:"minecraft:paper",Count:6b,tag:{nitk_castlePermit:1b,HideFlags:1,display:{Lore:['{"text":"For preparing sites","color":"gray","italic":false}','{"text":"for a unified fortress.","color":"gray","italic":false}'],Name:'{"text":"Castle Permit","color":"blue","italic":false}'},Enchantments:[{id:"minecraft:protection",lvl:0}]}},maxUses:1}]},Tags:[nitCitizen,nitKTP,smithed.entity,smithed.strict,nitEntity]}
scoreboard players operation @e[type=villager,tag=nitKTP,limit=1,sort=nearest] nitID = @s nitID
scoreboard players operation @e[tag=nitTownPlanner,limit=1,sort=nearest] nitID = @s nitID
function nitk:debug/update_all

###Starts process for checking if command blocks are enabled###
tag @s[tag=!nitCBEnabled] add nitCBDisabled
setblock ~ 200 ~ repeating_command_block{Command:"function nitk:command_block_check",auto:1b} replace

###Removes forceloaded chunks###
forceload remove ~-100 ~-100 ~100 ~100
tellraw @s [{"text":"[۩]","color":"green","bold":true},{"text":": ","color":"white","bold":false},{"text":"You have successfully settled at this location!","color":"white","bold":false}]