# Recalculate Bordercraft progress from the sidebar/UI counts and re-sync all three dimensions.
# Formula:
#   Increases = Items + Advancements + Mobs + Bosses + (Structures * 10)
#   Border diameter = 1 + (Increases * 2)

# Ensure constants exist even in older worlds.
scoreboard players set TWO bc_tmp 2
scoreboard players set TEN bc_tmp 10
scoreboard players set TARGET bc_tmp 0
scoreboard players set STRUCT_TMP bc_tmp 0
scoreboard players set CUR bc_tmp 0
scoreboard players set DIFF bc_tmp 0

# Rebuild Increases from sidebar lines.
execute if score Items/1674 bc_side matches 0.. run scoreboard players operation TARGET bc_tmp += Items/1674 bc_side
execute if score Adv/125 bc_side matches 0.. run scoreboard players operation TARGET bc_tmp += Adv/125 bc_side
execute if score Mobs/88 bc_side matches 0.. run scoreboard players operation TARGET bc_tmp += Mobs/88 bc_side
execute unless score Mobs/88 bc_side matches 0.. if score Mobs/62 bc_side matches 0.. run scoreboard players operation TARGET bc_tmp += Mobs/62 bc_side
execute if score Bosses bc_side matches 0.. run scoreboard players operation TARGET bc_tmp += Bosses bc_side
execute if score Struct/22 bc_side matches 0.. run scoreboard players operation STRUCT_TMP bc_tmp = Struct/22 bc_side
scoreboard players operation STRUCT_TMP bc_tmp *= TEN bc_tmp
scoreboard players operation TARGET bc_tmp += STRUCT_TMP bc_tmp

scoreboard players operation Increases bc_border = TARGET bc_tmp

# Convert increases -> border diameter: 1 + 2*TARGET
scoreboard players operation TARGET bc_tmp *= TWO bc_tmp
scoreboard players add TARGET bc_tmp 1

# Sync Overworld
execute in minecraft:overworld store result score CUR bc_tmp run worldborder get
scoreboard players operation DIFF bc_tmp = TARGET bc_tmp
scoreboard players operation DIFF bc_tmp -= CUR bc_tmp
function bordercraft:reloadborder/ow_dispatch

# Sync Nether
execute in minecraft:the_nether store result score CUR bc_tmp run worldborder get
scoreboard players operation DIFF bc_tmp = TARGET bc_tmp
scoreboard players operation DIFF bc_tmp -= CUR bc_tmp
function bordercraft:reloadborder/nether_dispatch

# Sync End
execute in minecraft:the_end store result score CUR bc_tmp run worldborder get
scoreboard players operation DIFF bc_tmp = TARGET bc_tmp
scoreboard players operation DIFF bc_tmp -= CUR bc_tmp
function bordercraft:reloadborder/end_dispatch

tellraw @a ["",{"text":"Bordercraft: borders reloaded from scoreboard/UI progress.","color":"green"},{"text":"  (Increases: ","color":"gray"},{"score":{"name":"Increases","objective":"bc_border"},"color":"white"},{"text":", Size: ","color":"gray"},{"score":{"name":"TARGET","objective":"bc_tmp"},"color":"white"},{"text":")","color":"gray"}]
