tell @a[team=nl_dev] "function next_level:custom_blocks/diamond_anvil/reroll/gear/roll_quality ok!"

loot spawn ~ ~ ~ loot next_level:quality/gear/anvil/reroll_quality
# store modifer numbers # 
    # Max Health # [0] 
execute store result score InputHealth nl.Quality run data get entity @e[limit=1,type=minecraft:item,sort=nearest,nbt={Item:{tag:{nl_Quality:1b}}}] Item.tag.AttributeModifiers.[0].Amount 100
    # Movement Speed # [1] 
execute store result score InputSpeed nl.Quality run data get entity @e[limit=1,type=minecraft:item,sort=nearest,nbt={Item:{tag:{nl_Quality:1b}}}] Item.tag.AttributeModifiers.[1].Amount 100
    # Armor # [2]
execute store result score InputArmor nl.Quality run data get entity @e[limit=1,type=minecraft:item,sort=nearest,nbt={Item:{tag:{nl_Quality:1b}}}] Item.tag.AttributeModifiers.[2].Amount 100
    # Mana # [3]
execute store result score InputMana nl.Quality run data get entity @e[limit=1,type=minecraft:item,sort=nearest,nbt={Item:{tag:{nl_Quality:1b}}}] Item.tag.AttributeModifiers.[3].Amount 100

    # Copy Armor # 
# Max Health # [0] 
execute store result score OutputHealth nl.Quality run data get entity @e[tag=UI_Reroll,limit=1,type=minecraft:item,distance=..1] Item.tag.AttributeModifiers.[0].Amount 100
# Movement Speed # [1] 
execute store result score OutputSpeed nl.Quality run data get entity @e[tag=UI_Reroll,limit=1,type=minecraft:item,distance=..1] Item.tag.AttributeModifiers.[1].Amount 100
# Armor # [2]
execute store result score OutputArmor nl.Quality run data get entity @e[tag=UI_Reroll,limit=1,type=minecraft:item,distance=..1] Item.tag.AttributeModifiers.[2].Amount 100
# Mana # [3]
execute store result score OutputMana nl.Quality run data get entity @e[tag=UI_Reroll,limit=1,type=minecraft:item,distance=..1] Item.tag.AttributeModifiers.[3].Amount 100

# add Buff #
scoreboard players operation OutputHealth nl.Quality += InputHealth nl.Quality
scoreboard players operation OutputSpeed nl.Quality += InputSpeed nl.Quality
scoreboard players operation OutputArmor nl.Quality += InputArmor nl.Quality
scoreboard players operation OutputMana nl.Quality += InputMana nl.Quality

# Store Result of Attribute changes back in copy of tool # 
execute store result entity @e[type=item,limit=1,distance=..1,sort=nearest,tag=UI_Reroll] Item.tag.AttributeModifiers.[0].Amount double .01 run scoreboard players get OutputHealth nl.Quality
execute store result entity @e[type=item,limit=1,distance=..1,sort=nearest,tag=UI_Reroll] Item.tag.AttributeModifiers.[1].Amount double .01 run scoreboard players get OutputSpeed nl.Quality
execute store result entity @e[type=item,limit=1,distance=..1,sort=nearest,tag=UI_Reroll] Item.tag.AttributeModifiers.[2].Amount double .01 run scoreboard players get OutputArmor nl.Quality
execute store result entity @e[type=item,limit=1,distance=..1,sort=nearest,tag=UI_Reroll] Item.tag.AttributeModifiers.[3].Amount double .01 run scoreboard players get OutputMana nl.Quality

# Find air for sign to write Lore # 
execute at @s run summon minecraft:area_effect_cloud ~ 250 ~ {NoGravity:1,Tags:["nl_FindAir"]}
execute as @e[tag=nl_FindAir] at @s run function next_level:quality/find_air

# Correct the Bonus Amount to be written # 
execute as @e[nbt={Item:{tag:{nl_Quality:1b}}}] unless entity @s[nbt={Item:{tag:{nl_Heroic:1b}}}] run function next_level:custom_blocks/diamond_anvil/reroll/gear/write_bonus
execute as @e[nbt={Item:{tag:{nl_Quality:1b,nl_Heroic:1b}}}] run function next_level:custom_blocks/diamond_anvil/reroll/gear/write_bonus_heroic

# Write new Attribute change to sign #
execute as @e[nbt={Item:{tag:{nl_Quality:1b}}}] unless entity @s[nbt={Item:{tag:{nl_Heroic:1b}}}] at @s run summon minecraft:armor_stand ~ ~ ~ {Small:1b,Marker:1b,Invisible:1b,Tags:["nl_QualityName"],CustomName:'{"text":" ","italic":false}'}
execute as @e[nbt={Item:{tag:{nl_Quality:1b}}}] unless entity @s[nbt={Item:{tag:{nl_Heroic:1b}}}] run data modify entity @e[limit=1,tag=nl_QualityName] CustomName set from entity @e[limit=1,nbt={Item:{tag:{nl_Quality:1b}}}] Item.tag.display.Name
execute as @e[nbt={Item:{tag:{nl_Quality:1b}}}] unless entity @s[nbt={Item:{tag:{nl_Heroic:1b}}}] at @e[tag=nl_FindAir] run setblock ~ ~-1 ~ minecraft:oak_sign{Text1:'["",{"text":"Quality: [","color":"dark_aqua","italic":false},{"selector":"@e[limit=1,tag=nl_QualityName]","italic":false},{"text":"]","color":"dark_aqua","italic":false}]',Text2:'["",{"text":" +","color":"blue","italic":false},{"score":{"name":"Whole","objective":"nl.Quality"},"color":"blue","italic":false},{"text":".","color":"blue","italic":false},{"score":{"name":"Decimal","objective":"nl.Quality"},"color":"blue","italic":false},{"selector":"@e[tag=nl_QualityArmor,limit=1]","color":"blue","italic":false}]',Text3:'["",{"text":" "}]',Text4:'["",{"text":"  ⚔ Minecraft:Next Level ⚔  ","color":"dark_gray","italic":false}]'}

execute as @e[nbt={Item:{tag:{nl_Quality:1b,nl_Heroic:1b}}}] as @s at @e[tag=nl_FindAir,limit=1] run setblock ~ ~-1 ~ minecraft:oak_sign{Text1:'["",{"text":"Quality: [","color":"dark_aqua","italic":false},{"text":"Heroic","color":"gold","italic":false},{"text":"]","color":"dark_aqua","italic":false}]',Text2:'["",{"text":" +","color":"gold","italic":false},{"score":{"name":"HPWhole","objective":"nl.Quality"},"color":"gold","italic":false},{"text":".","color":"gold","italic":false},{"score":{"name":"HPDecimal","objective":"nl.Quality"},"color":"gold","italic":false},{"text":" Max Health","color":"gold","italic":false}]',Text3:'["",{"text":" +","color":"gold","italic":false},{"score":{"name":"MPWhole","objective":"nl.Quality"},"color":"gold","italic":false},{"text":".","color":"gold","italic":false},{"score":{"name":"MPDecimal","objective":"nl.Quality"},"color":"gold","italic":false},{"text":" Max Mana","color":"gold","italic":false}]',Text4:'["",{"text":"  ⚔ Minecraft:Next Level ⚔  ","color":"dark_gray","italic":false}]'}


# Modify Lore from sign # 
data remove entity @e[tag=UI_Reroll,limit=1,type=minecraft:item,distance=..1] Item.tag.display.Lore[-1]
    # If Heroic # 
execute as @e[nbt={Item:{tag:{nl_Quality:1b,nl_Heroic:1b}}}] as @e[tag=UI_Reroll,limit=1,sort=nearest] at @e[tag=nl_FindAir] run data modify entity @s Item.tag.display.Lore append from block ~ ~-1 ~ Text1
execute as @e[nbt={Item:{tag:{nl_Quality:1b,nl_Heroic:1b}}}] as @e[tag=UI_Reroll,limit=1,sort=nearest] at @e[tag=nl_FindAir] run data modify entity @s Item.tag.display.Lore append from block ~ ~-1 ~ Text2
execute as @e[nbt={Item:{tag:{nl_Quality:1b,nl_Heroic:1b}}}] as @e[tag=UI_Reroll,limit=1,sort=nearest] at @e[tag=nl_FindAir] run data modify entity @s Item.tag.display.Lore append from block ~ ~-1 ~ Text3
execute as @e[nbt={Item:{tag:{nl_Quality:1b,nl_Heroic:1b}}}] as @e[tag=UI_Reroll,limit=1,sort=nearest] at @e[tag=nl_FindAir] run data modify entity @s Item.tag.display.Lore append value "{\"text\":\" \"}"
execute as @e[nbt={Item:{tag:{nl_Quality:1b,nl_Heroic:1b}}}] as @e[tag=UI_Reroll,limit=1,sort=nearest] at @e[tag=nl_FindAir] run data modify entity @s Item.tag.display.Lore append from block ~ ~-1 ~ Text4
    # If Normal # 
execute as @e[nbt={Item:{tag:{nl_Quality:1b}}}] unless entity @s[nbt={Item:{tag:{nl_Heroic:1b}}}] as @e[tag=UI_Reroll,limit=1,sort=nearest] at @e[tag=nl_FindAir] run data modify entity @s Item.tag.display.Lore append from block ~ ~-1 ~ Text1
execute as @e[nbt={Item:{tag:{nl_Quality:1b}}}] unless entity @s[nbt={Item:{tag:{nl_Heroic:1b}}}] as @e[tag=UI_Reroll,limit=1,sort=nearest] at @e[tag=nl_FindAir] run data modify entity @s Item.tag.display.Lore append from block ~ ~-1 ~ Text2
execute as @e[nbt={Item:{tag:{nl_Quality:1b}}}] unless entity @s[nbt={Item:{tag:{nl_Heroic:1b}}}] as @e[tag=UI_Reroll,limit=1,sort=nearest] at @e[tag=nl_FindAir] run data modify entity @s Item.tag.display.Lore append from block ~ ~-1 ~ Text3
execute as @e[nbt={Item:{tag:{nl_Quality:1b}}}] unless entity @s[nbt={Item:{tag:{nl_Heroic:1b}}}] as @e[tag=UI_Reroll,limit=1,sort=nearest] at @e[tag=nl_FindAir] run data modify entity @s Item.tag.display.Lore append from block ~ ~-1 ~ Text4
    # If Unbreakable # 
execute as @e[nbt={Item:{tag:{nl_Quality:1b,nl_Unbreakable:1b}}}] run data remove entity @e[tag=UI_Reroll,limit=1,type=minecraft:item,distance=..1] Item.tag.display.Lore[-1]
execute as @e[nbt={Item:{tag:{nl_Quality:1b,nl_Unbreakable:1b}}}] run data remove entity @e[tag=UI_Reroll,limit=1,type=minecraft:item,distance=..1] Item.tag.display.Lore[-1]
execute as @e[nbt={Item:{tag:{nl_Quality:1b,nl_Unbreakable:1b}}}] run data remove entity @e[tag=UI_Reroll,limit=1,type=minecraft:item,distance=..1] Item.tag.display.Lore[-1]
execute as @e[nbt={Item:{tag:{nl_Quality:1b,nl_Unbreakable:1b}}}] as @e[tag=UI_Reroll,limit=1,sort=nearest] at @e[tag=nl_FindAir] run data modify entity @s Item.tag.display.Lore append value "{\"text\":\"This Item is now Unbreakable\",\"color\":\"gray\",\"italic\":false}"
execute as @e[nbt={Item:{tag:{nl_Quality:1b,nl_Unbreakable:1b}}}] as @e[tag=UI_Reroll,limit=1,sort=nearest] at @e[tag=nl_FindAir] run data modify entity @s Item.tag.display.Lore append value "{\"text\":\" \"}"
execute as @e[nbt={Item:{tag:{nl_Quality:1b,nl_Unbreakable:1b}}}] as @e[tag=UI_Reroll,limit=1,sort=nearest] at @e[tag=nl_FindAir] run data modify entity @s Item.tag.display.Lore append value "{\"text\":\"  ⚔ Minecraft:Next Level ⚔  \",\"color\":\"dark_gray\",\"italic\":false}"
execute as @e[nbt={Item:{tag:{nl_Quality:1b,nl_Unbreakable:1b}}}] run data merge entity @e[tag=UI_Reroll,type=minecraft:item,limit=1] {Item:{tag:{Unbreakable:1b}}}

# Reset Stored Modifiers #
scoreboard players set OutputHealth nl.Quality 0
scoreboard players set OutputSpeed nl.Quality 0
scoreboard players set OutputArmor nl.Quality 0
scoreboard players set OutputMana nl.Quality 0
scoreboard players set InputHealth nl.Quality 0
scoreboard players set InputSpeed nl.Quality 0
scoreboard players set InputArmor nl.Quality 0
scoreboard players set InputMana nl.Quality 0

# Re-add Stored Enchantments and Custom Name#
execute as @e[tag=nl_StoreEnchant,limit=1,sort=nearest] run data modify entity @e[tag=UI_Reroll,type=minecraft:item,limit=1] Item.tag.Enchantments set from entity @e[tag=nl_StoreEnchant,limit=1] HandItems.[0].tag.Enchantments
execute as @e[tag=nl_StoreEnchant,limit=1,sort=nearest] run data modify entity @e[tag=UI_Reroll,type=minecraft:item,limit=1] Item.tag.display.Name[0] insert 0 from entity @e[tag=nl_StoreEnchant,limit=1] HandItems.[0].tag.display.Name

# Add check to stop re-processing #
data merge entity @e[tag=UI_Reroll,type=minecraft:item,limit=1] {Item:{tag:{nl_QualityCheck:1b}}}

# Clear sign, entities etc. # 
execute at @e[tag=nl_FindAir] run setblock ~ ~-1 ~ air
kill @e[tag=nl_FindAir]
kill @e[tag=nl_QualityArmor]
kill @e[tag=nl_QualityName]
kill @e[nbt={Item:{tag:{nl_Quality:1b}}}]
kill @e[tag=nl_StoreEnchant]
