#Preps scoreboards
scoreboard players add @s nitDragonology 0
scoreboard players add @s nitDragonState 0
tag @a[distance=..4] add nitDragonologyBuilt
clear @a[tag=nitDragonologyBuilt] knowledge_book{nitref_dragonResearch:1b}
kill @e[type=item,nbt={Item:{tag:{nitref_dragonResearch:1b}}}]

#Research Status
scoreboard players set @s nitTemp 150
execute store result score @s nitResourceSpawn run data get entity @s Xp
scoreboard players operation @s nitResourceSpawn -= @s nitTemp
scoreboard players operation @s nitDragonology = @s nitResourceSpawn

data modify entity @s[scores={nitDragonState=0}] Offers.Recipes[1].xp set value 20
data modify entity @s[scores={nitDragonState=1}] Offers.Recipes[1].xp set value 10
data modify entity @s[scores={nitDragonState=2}] Offers.Recipes[1].xp set value 5
data modify entity @s[scores={nitDragonState=3}] Offers.Recipes[1].xp set value 2
data modify entity @s[scores={nitDragonState=4}] Offers.Recipes[1].xp set value 1

data modify entity @s[scores={nitDragonState=0}] Offers.Recipes[2].xp set value 30
data modify entity @s[scores={nitDragonState=1}] Offers.Recipes[2].xp set value 15
data modify entity @s[scores={nitDragonState=2}] Offers.Recipes[2].xp set value 7
data modify entity @s[scores={nitDragonState=3}] Offers.Recipes[2].xp set value 4
data modify entity @s[scores={nitDragonState=4}] Offers.Recipes[2].xp set value 2

#Achieved Research Levels
execute if entity @s[scores={nitDragonology=100..}] run playsound block.end_portal_frame.fill block @a ~ ~ ~ 3 1
execute if entity @s[scores={nitDragonology=100..}] run effect give @s instant_health 3 0 false
execute if entity @s[scores={nitDragonology=100..,nitDragonState=4}] run advancement grant @p only nitref:dragonology/armor
execute if entity @s[scores={nitDragonology=100..,nitDragonState=3}] run advancement grant @p only nitref:dragonology/trades
execute if entity @s[scores={nitDragonology=100..,nitDragonState=2}] run advancement grant @p only nitref:dragonology/egg
execute if entity @s[scores={nitDragonology=100..,nitDragonState=1}] run advancement grant @p only nitref:dragonology/head
execute if entity @s[scores={nitDragonology=100..,nitDragonState=0}] run advancement grant @p only nitref:dragonology/chest

#New trades when unlocked
execute if entity @s[tag=nitUnlocked,tag=!nitHasTrades] run data modify entity @s Offers.Recipes append value {buy:{id:"minecraft:ender_pearl",Count:16b},buyB:{id:"minecraft:dragon_head",Count:1b},sell:{id:"minecraft:iron_sword",Count:1b,tag:{display:{Name:'[{"text":"Dragontooth Sword","italic":false,"color":"aqua"}]'},Enchantments:[{id:infinity,lvl:1},{id:knockback,lvl:3},{id:looting,lvl:4},{id:sharpness,lvl:6},{id:sweeping,lvl:4}]}}}
execute if entity @s[tag=nitUnlocked,tag=!nitHasTrades] run data modify entity @s Offers.Recipes append value {buy:{id:"minecraft:ender_pearl",Count:16b},buyB:{id:"minecraft:dragon_breath",Count:1b},sell:{id:"minecraft:shield",Count:1b,tag:{BlockEntityTag:{Patterns:[{Pattern:flo,Color:10},{Pattern:cbo,Color:15},{Pattern:bl,Color:15},{Pattern:br,Color:15},{Pattern:tts,Color:7},{Pattern:bts,Color:7},{Pattern:bri,Color:15},{Pattern:gra,Color:15}],Base:7},AttributeModifiers:[{AttributeName:"generic.armor",Amount:10,Slot:offhand,Name:"generic.armor",UUID:[I;-122220,7427,74553,-14854]},{AttributeName:"generic.armor_toughness",Amount:5,Slot:offhand,Name:"generic.armor_toughness",UUID:[I;-122220,7527,74553,-15054]},{AttributeName:"generic.knockback_resistance",Amount:0.5,Slot:offhand,Name:"generic.knockback_resistance",UUID:[I;-122220,7627,74553,-15254]}],display:{Name:'[{"text":"Dragonscale Shield","italic":false,"color":"dark_purple"}]'},Enchantments:[{id:infinity,lvl:1}]}}}
execute if entity @s[tag=nitUnlocked,tag=!nitHasTrades] run data modify entity @s Offers.Recipes append value {buy:{id:"minecraft:ender_pearl",Count:16b},buyB:{id:"minecraft:dragon_egg",Count:1b},sell:{id:"minecraft:elytra",Count:1b,tag:{AttributeModifiers:[{AttributeName:"generic.armor",Amount:10,Slot:chest,Name:"generic.armor",UUID:[I;-122220,15227,74553,-30454]},{AttributeName:"generic.armor_toughness",Amount:5,Slot:chest,Name:"generic.armor_toughness",UUID:[I;-122220,15327,74553,-30654]},{AttributeName:"generic.max_health",Amount:4.0,Slot:chest,Name:"generic.max_health",UUID:[I;-122220,15427,74553,-30854]}],display:{Name:'[{"text":"Dragonscale Wingset","italic":false,"color":"light_purple"}]'},Enchantments:[{id:infinity,lvl:1},{id:unbreaking,lvl:3}]}}}
tag @s[tag=nitUnlocked,tag=!nitHasTrades] add nitHasTrades