
# -> Get alive player amount
execute store result score #or.total_player_amount OverlyRealistic.General.ArbitraryData \
    if entity @a[gamemode=!spectator]

# -> Reset players sleeping amount
scoreboard players set #or.players_sleeping_amount OverlyRealistic.General.ArbitraryData 0

# -> Get sleeping players
execute as @a[tag=!OverlyRealistic.Player.IsGhost] at @s \
    if block ~ ~ ~ #beds \
    anchored eyes \
    if block ^ ^ ^ #beds run \
        scoreboard players add #or.players_sleeping_amount OverlyRealistic.General.ArbitraryData 1
execute as @a[tag=OverlyRealistic.Player.IsSleeping_Dynamic] run \
    scoreboard players add #or.players_sleeping_amount OverlyRealistic.General.ArbitraryData 1

# -> Check if values match and there are players alive
execute if score #or.total_player_amount OverlyRealistic.General.ArbitraryData matches 1.. \
    unless score #or.total_player_amount OverlyRealistic.General.ArbitraryData = #or.players_sleeping_amount OverlyRealistic.General.ArbitraryData run \
        return fail

# -> Function tag
function #overly_realistic:mechanic_loops/loop_sleep

# -> Increase time
execute store result score #or.WorldTime OverlyRealistic.General.ArbitraryData run \
    time add 1s

# -> Add to players' in-game playtime
scoreboard players add @a OverlyRealistic.Player.PlayedForSeconds_InGame 60

# -> If WorldTime = 0-1000 then run the awake function
execute if predicate overly_realistic:world/time/is_early_day \
    as @a[gamemode=!spectator] \
    at @s run \
        return run \
            function overly_realistic:general/sleeping/sleep_awake_players

# -> Operate on sleeping players
execute as @a[tag=!OverlyRealistic.Player.IsGhost,gamemode=!spectator] at @s run \
    function overly_realistic:general/sleeping/sleeping_player_operations

# -> Add onto sleep time
scoreboard players add #or.world.sleep_time_0 OverlyRealistic.General.ArbitraryData 1
scoreboard players add #or.world.sleep_time_1 OverlyRealistic.General.ArbitraryData 1
scoreboard players add #or.world.sleep_time_2 OverlyRealistic.General.ArbitraryData 1
scoreboard players add #or.world.sleep_time_3 OverlyRealistic.General.ArbitraryData 1

# --> Offshoot loops

# -> 5t
execute if score #or.world.sleep_time_0 OverlyRealistic.General.ArbitraryData matches 5 run \
    function overly_realistic:general/sleeping/loops/sleep_loop_5t

# -> 15t
execute if score #or.world.sleep_time_1 OverlyRealistic.General.ArbitraryData matches 15 run \
    function overly_realistic:general/sleeping/loops/sleep_loop_15t

# -> 20t
execute if score #or.world.sleep_time_2 OverlyRealistic.General.ArbitraryData matches 20 run \
    function overly_realistic:general/sleeping/loops/sleep_loop_20t

# -> 80t
execute if score #or.world.sleep_time_3 OverlyRealistic.General.ArbitraryData matches 80.. run \
    function overly_realistic:general/sleeping/loops/sleep_loop_80t

# -> 100t
execute if score #or.world.sleep_time_4 OverlyRealistic.General.ArbitraryData matches 180.. run \
    function overly_realistic:general/sleeping/loops/sleep_loop_180t

# -> Loop
schedule function overly_realistic:general/sleeping/sleep_loop 1t