
# -> Set IsFeelingDull
scoreboard players set @s OverlyRealistic.Player.IsFeelingDull 0
scoreboard players set @s[scores={OverlyRealistic.Player.Stats.MentalHealth=..30}] OverlyRealistic.Player.IsFeelingDull 1

# -> If player is in creative or spectator then return
execute unless entity @s[gamemode=survival] \
    unless entity @s[gamemode=adventure] run \
        return fail


# -> Loneliness operations/checks
function overly_realistic:players/mental_health/loneliness/player_nearby_check

# -> Power of friendship advancement
execute if score #or.is_player_nearby_sanity_edit OverlyRealistic.General.ArbitraryData matches 1 run \
    advancement grant @s only overly_realistic:advancements/livelyhood/the_power_of_friendship



# -> If player is low on sanity then perform operations
execute if score @s OverlyRealistic.Player.Stats.MentalHealth matches ..18 run \
    function overly_realistic:players/mental_health/low_mental_health_operations

# -> Add to sanity check timer
scoreboard players add @s OverlyRealistic.Player.SanityCheckTimer 1

# -> If sanity check timer < max then return (5 * SanityCheckTimerMax / 60 = the amount of mins until sanity checks are done)
execute unless score @s OverlyRealistic.Player.SanityCheckTimer matches 30.. run \
    return fail

# -> Reset sanity check timer
scoreboard players reset @s OverlyRealistic.Player.SanityCheckTimer



# -> Reset sanity edit value
scoreboard players set #or.sanity_edit_check OverlyRealistic.General.ArbitraryData 0

# -------------------------------------------------------------------------------------

# ----> Sanity checks


# --> Positive checks

# -> Player is on comforting blocks check
execute if predicate overly_realistic:player/misc/is_on_comforting_blocks run \
    scoreboard players add #or.sanity_edit_check OverlyRealistic.General.ArbitraryData 100

# -> (@NOTE: Use predicate for this)
# -> All high stats check
execute if score @s OverlyRealistic.Player.Stats.Temperature matches 25..85 \
    if score @s OverlyRealistic.Player.Stats.Thirst matches 73.. \
    if score @s OverlyRealistic.Player.Health matches 14.. \
    if score @s OverlyRealistic.Player.Hunger matches 17.. run \
        scoreboard players add #or.sanity_edit_check OverlyRealistic.General.ArbitraryData 800

# -> High hunger & thirst check
execute if score @s OverlyRealistic.Player.Hunger matches 16.. \
    if score @s OverlyRealistic.Player.Stats.Thirst matches 60.. run \
        scoreboard players add #or.sanity_edit_check OverlyRealistic.General.ArbitraryData 50

# -> High health check
execute if score @s OverlyRealistic.Player.Hunger matches 19.. run \
    scoreboard players add #or.sanity_edit_check OverlyRealistic.General.ArbitraryData 60

# -> Low loneliness check
execute unless score @s OverlyRealistic.Player.Stats.Loneliness matches 20.. \
    unless score #or.setting.loneliness OverlyRealistic.Settings.ArbitrarySetting matches 0 run \
        scoreboard players add #or.sanity_edit_check OverlyRealistic.General.ArbitraryData 350

# -> Inside check
execute unless score @s OverlyRealistic.PredicateValue.IsExposedToSkyLight matches 1 run \
    scoreboard players add #or.sanity_edit_check OverlyRealistic.General.ArbitraryData 100

# -> Feather crown check
execute if items entity @s armor.head *[custom_data~{IsFeatherCrown:1b}] run \
    scoreboard players add #or.sanity_edit_check OverlyRealistic.General.ArbitraryData 110

# -> High vitality check
execute if score @s OverlyRealistic.Player.Stats.Vitality matches 80.. run \
    scoreboard players add #or.sanity_edit_check OverlyRealistic.General.ArbitraryData 25

# -> Total positive sanity acquireable: 1595

# -> Function tag
function #overly_realistic:mechanics/player_sanity_add


# --> Negative checks

# -> (@NOTE: Use predicate for this)
# -> Low stats check
execute unless score @s OverlyRealistic.Player.Stats.Temperature matches 25..85 \
    if score @s OverlyRealistic.Player.Stats.Thirst matches ..30 \
    if score @s OverlyRealistic.Player.Hunger matches ..10 run \
        scoreboard players remove #or.sanity_edit_check OverlyRealistic.General.ArbitraryData 800

# -> Low hunger check
execute unless score @s OverlyRealistic.Player.Hunger matches 8.. run \
    scoreboard players remove #or.sanity_edit_check OverlyRealistic.General.ArbitraryData 60

# -> Low thirst check
execute unless score @s OverlyRealistic.Player.Stats.Thirst matches 25.. run \
    scoreboard players remove #or.sanity_edit_check OverlyRealistic.General.ArbitraryData 65

# -> Low health check
execute unless score @s OverlyRealistic.Player.Health matches 8.. run \
    scoreboard players remove #or.sanity_edit_check OverlyRealistic.General.ArbitraryData 100

# -> Vitality check
execute unless score @s OverlyRealistic.Player.Stats.Vitality matches 20.. run \
    scoreboard players remove #or.sanity_edit_check OverlyRealistic.General.ArbitraryData 120

# -> Low health check
execute unless score @s OverlyRealistic.Player.Health matches 7.. run \
    scoreboard players remove #or.sanity_edit_check OverlyRealistic.General.ArbitraryData 550

# -> Light level 0 check
execute if score @s OverlyRealistic.PredicateValue.IsInLightLevel0 matches 1 run \
    scoreboard players remove #or.sanity_edit_check OverlyRealistic.General.ArbitraryData 280

# -> Illness check
execute if entity @s[tag=OverlyRealistic.Player.IsSick] run \
    scoreboard players remove #or.sanity_edit_check OverlyRealistic.General.ArbitraryData 50

# -> Loneliness check (5 * 3 * LonelinessStat / 60 = amount of minutes for loneliness)
execute if score @s OverlyRealistic.Player.Stats.Loneliness matches 120.. \
    unless score #or.setting.loneliness OverlyRealistic.Settings.ArbitrarySetting matches 0 run \
        scoreboard players remove #or.sanity_edit_check OverlyRealistic.General.ArbitraryData 650

# -> Specific illness checks
execute if entity @s[tag=OverlyRealistic.Player.IsSick,scores={OverlyRealistic.Player.Status_IllnessType_2=1..2}] \
    if score #or.sanity_edit_check OverlyRealistic.General.ArbitraryData matches 200.. run \
        scoreboard players remove #or.sanity_edit_check OverlyRealistic.General.ArbitraryData 280

# -> Total negative sanity acquireable: -2675

# -> Function tag
function #overly_realistic:mechanics/player_sanity_remove



# ----> Sanity edit

# --> Positive edits

# -> +1
execute if score #or.sanity_edit_check OverlyRealistic.General.ArbitraryData matches 200..1800 run \
    return run \
        function overly_realistic:players/mental_health/edit_static/add_to_mental_health_by_1

# -> +2
execute if score #or.sanity_edit_check OverlyRealistic.General.ArbitraryData matches 1801.. run \
    return run \
        function overly_realistic:players/mental_health/edit_static/add_to_mental_health_by_2

# -> +3
#execute if score #or.sanity_edit_check OverlyRealistic.General.ArbitraryData matches 1851.. run \
    return run \
        function overly_realistic:players/mental_health/edit_static/add_to_mental_health_by_3

# --> Negative edits

# -> -1
execute if score #or.sanity_edit_check OverlyRealistic.General.ArbitraryData matches -1800..-200 run \
    return run \
        function overly_realistic:players/mental_health/edit_static/decrease_mental_health_by_1

# -> -2
execute if score #or.sanity_edit_check OverlyRealistic.General.ArbitraryData matches ..-1801 run \
    return run \
        function overly_realistic:players/mental_health/edit_static/decrease_mental_health_by_2

# -> -3
#execute if score #or.sanity_edit_check OverlyRealistic.General.ArbitraryData matches ..-1851 run \
    return run \
        function overly_realistic:players/mental_health/edit_static/decrease_mental_health_by_3
