# Schedule Timer #

# Check Credits # 
execute as @a[scores={nl.Credits=1..}] run function next_level:setup/credits_page

# Update Player info # 
execute as @a run function next_level:player/global_update

# Rested Bonus #
scoreboard players set @a[scores={nl.WellRested=1..}] nl.WellRested 0

# Calculate Mana Regeneration # 
execute as @a if score @s nl.MP.Current < @s nl.MP.Max run function next_level:magic/calc_regen

# Scoreboard Failsafes # 

execute as @a unless score @s nl.Reroll matches 0.. run scoreboard players set @s nl.Reroll 0
execute as @a unless score @s nl.Rng matches 0.. run scoreboard players set @s nl.Rng 0
execute as @a unless score @s nl.CraftID matches 0.. run scoreboard players set @s nl.CraftID 0
execute as @a unless score @s nl.Recipe matches 0.. run scoreboard players set @s nl.Recipe 0
execute as @a unless score @s nl.RecipeID matches 0.. run scoreboard players set @s nl.RecipeID 0
execute as @a unless score @s nl.Enchant matches 0.. run scoreboard players set @s nl.Enchant 0
execute as @a unless score @s nl.HasteTimer matches 0.. run scoreboard players set @s nl.HasteTimer 0
execute as @a unless score @s nl.SoulTimer matches 0.. run scoreboard players set @s nl.SoulTimer 0
execute as @a unless score @s nl.DMG.Timer matches 0.. run scoreboard players set @s nl.DMG.Timer 0
execute as @a unless score @s nl.DMG.Bleed matches 0.. run scoreboard players set @s nl.DMG.Bleed 0
execute as @a unless score @s nl.DMG.Infect matches 0.. run scoreboard players set @s nl.DMG.Infect 0
execute as @a unless score @s nl.DMG.Leech matches 0.. run scoreboard players set @s nl.DMG.Leech 0
execute as @a unless score @s nl.DMG.Stun matches 0.. run scoreboard players set @s nl.DMG.Stun 0
execute as @a unless score @s nl.MP.Max matches 0.. run scoreboard players set @s nl.MP.Max 0
execute as @a unless score @s nl.MP.Base matches 0.. run scoreboard players set @s nl.MP.Base 0
execute as @a unless score @s nl.RegenTimer matches 0.. run scoreboard players set @s nl.RegenTimer 0
execute as @a unless score @s nl.RegenMax matches 0.. run scoreboard players set @s nl.RegenMax 0
execute as @a unless score @s nl.RegenBase matches 0.. run scoreboard players set @s nl.RegenBase 0

# Crafter Particles #
execute as @e[type=minecraft:armor_stand,tag=nl_Crafter] at @s if entity @e[type=minecraft:player,distance=..5] run particle minecraft:dust 1 1 0 1 ~ ~0.5 ~ 0.5 0.5 0.5 0 1
execute as @e[type=minecraft:armor_stand,tag=nl_Crafter] at @s if entity @e[type=minecraft:player,distance=..5] run particle minecraft:dust 1 0 1 1 ~ ~0.5 ~ 0.5 0.5 0.5 0 1
execute as @e[type=minecraft:armor_stand,tag=nl_Crafter] at @s if entity @e[type=minecraft:player,distance=..5] run particle minecraft:dust 1 0 0 1 ~ ~0.5 ~ 0.5 0.5 0.5 0 1
execute as @e[type=minecraft:armor_stand,tag=nl_Crafter] at @s if entity @e[type=minecraft:player,distance=..5] run particle minecraft:dust 0 1 0 1 ~ ~0.5 ~ 0.5 0.5 0.5 0 1
execute as @e[type=minecraft:armor_stand,tag=nl_Crystal] at @s if entity @e[type=minecraft:player,distance=..5] run particle minecraft:dust 1 1 1 1 ~ ~0.5 ~ 0.3 0.3 0.3 0 1

# Sneak Trigger #
scoreboard players set @a[scores={nl.DetectSneak=1..}] nl.DetectSneak 0

# Custom Status Effects #

execute at @e[scores={nl.DMG.Infect=1..}] as @e[distance=..5,limit=1,sort=random] unless score @s nl.DMG.Infect matches 1.. run function next_level:combat/enchantments/infect_others
execute as @e[tag=nl_Scanned] run function next_level:health_display/set_health_display

# Check for holding Magic Compass #
execute as @a[nbt={SelectedItem:{tag:{nl_ItemID:134b}}}] at @s run function next_level:compass/main
execute as @a[nbt={Inventory:[{Slot:-106b,tag:{nl_ItemID:134b}}]}] at @s run function next_level:compass/main

# Set Global Level # 
execute as @a store result score @s nl.GlobalLevel run xp query @s levels
scoreboard players set Global nl.GlobalLevel 0
execute store result score Players nl.GlobalLevel run list uuids
scoreboard players operation Global nl.GlobalLevel += @a nl.GlobalLevel
scoreboard players operation Global nl.GlobalLevel /= Players nl.GlobalLevel

# Captain Abilities #
execute as @e[tag=nl_Rally.Speed] at @s run effect give @e[type=#next_level:all_mobs,distance=..8] minecraft:speed 15 1   
execute as @e[tag=nl_Rally.Strength] at @s run effect give @e[type=#next_level:all_mobs,distance=..8] minecraft:strength 15 1   
execute as @e[tag=nl_Rally.Regen] at @s run effect give @e[type=#next_level:all_mobs,distance=..8] minecraft:regeneration 15 1   
execute as @e[tag=nl_Rally.Resist] at @s run effect give @e[type=#next_level:all_mobs,distance=..8] minecraft:resistance 15 1 

# Structures # 
execute as @a[nbt={Dimension:0}] at @s unless entity @e[tag=nl_StructureGen,distance=..8] run function next_level:structures/summon_marker

# Quality # 
execute as @a run function next_level:quality/main
scoreboard players set #Global nl.Timer 0 
