#checks the current daytime
execute as @a[tag=K] store result score @s KyuTime run time query daytime

#Set Danger's trades, also giving him Dusk's Draught if the time is between 13685 and 22335. Oh, and this does NOT work using predicates for the time, as the predicate doesn't seem to update frequently or consistently enough.
execute as @e[limit=1,type=villager,name=Danger,tag=!got,distance=..25,sort=nearest] run data merge entity @s {VillagerData:{level:5,profession:"minecraft:weaponsmith",type:"minecraft:taiga"},CareerLevel:1,Offers:{Recipes:[{buy:{id:"minecraft:feather",Count:6b},buyB:{id:"minecraft:emerald",Count:1b},sell:{id:"minecraft:potion",Count:1b,tag:{display:{Name:"{\"text\":\"Slowfall Potion\"}"},CustomPotionEffects:[{Id:28b,Amplifier:0b,Duration:600}],CustomPotionColor:12233896},HideFlags:1,Enchantments:[{id:"minecraft:lure",lvl:12}]}},{buy:{id:"minecraft:ender_chest",Count:1b},buyB:{id:"minecraft:red_mushroom",Count:64b},sell:{id:"minecraft:written_book",Count:1b,tag:{HideFlags:1,Enchantments:[{id:"minecraft:unbreaking",lvl:10}],display:{Name:"{\"text\":\"Portable Mushroom House\",\"color\":\"red\"}",Lore:["\"Mushy!\""]},title:"",author:"",pages:["{\"text\":\"Portable Mushroom\",\"color\":\"red\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/trigger KyuMushy set 1\"},\"extra\":[{\"text\":\"\\n-------------------\",\"color\":\"gray\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/trigger KyuMushy set 1\"}},{\"text\":\"\\nClick to summon a\\ncute mushroom sprout, which quickly grows into an entire mushroom house! Once used, the book will disappear into the chest inside of the mushroom house, and once taken out, the house will vanish.\",\"color\":\"black\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/trigger KyuMushy set 1\"}}]}"]},HideFlags:1,Enchantments:[{id:"minecraft:lure",lvl:12}]}},{buy:{id:"minecraft:diamond",Count:12b},buyB:{id:"anvil",Count:13b},sell:{id:"minecraft:golden_axe",Count:1b,tag:{RepairCost:9999,splintersaxe:1b,display:{Name:"{\"text\":\"Axe of Splinters\",\"color\":\"red\"}",Lore:["\"An extremely powerful, but fragile axe. It will shatter after one use.\"","\"If a mob is killed with it, it will drop unique loot based on the mob that was killed.\""]},Damage:4000,Enchantments:[{id:"minecraft:sharpness",lvl:200},{id:"minecraft:looting",lvl:2}]}}},{buy:{id:"minecraft:blaze_powder",Count:2b},buyB:{id:"iron_block",Count:15b},sell:{id:"minecraft:netherite_axe",Count:1b,tag:{HideFlags:2,display:{Name:"{\"text\":\"Battleaxe\",\"color\":\"red\"}",Lore:['[{"text":""}]','[{"text":""}]','[{"text":"","color":"#BC9249"},{"text":"When in Main Hand:","color":"#CCCCCC","italic":false}]','[{"text":"","color":"#BC9249"},{"text":" 13 Attack Damage","color":"#14A82D","italic":false}]','[{"text":"","color":"#BC9249"},{"text":" 1.0 Attack Speed","color":"#14A82D","italic":false}]']},Enchantments:[{id:"minecraft:looting",lvl:1s},{id:"minecraft:unbreaking",lvl:1s}],AttributeModifiers:[{AttributeName:"generic.attack_speed",Name:"generic.attack_speed",Amount:-.75,Operation:1,UUID:[I;6969,6969,6969,1219],Slot:"mainhand"},{AttributeName:"generic.attack_damage",Name:"generic.attack_damage",Amount:13,Operation:0,UUID:[I;6969,6969,6969,1220],Slot:"mainhand"}]}}},{buy:{id:"minecraft:wither_skeleton_skull",Count:1b},buyB:{id:"redstone_block",Count:64b},sell:{id:"minecraft:tipped_arrow",Count:10b,tag:{display:{Name:"{\"text\":\"Reaper's Arrow - Does not work on Dragon/Wither\",\"translate\":\"wig\",\"color\":\"dark_purple\"}"},HideFlags:1,Enchantments:[{id:"minecraft:luck_of_the_sea",lvl:1}],CustomPotionEffects:[{Id:20b,Amplifier:4b,Duration:32000}],CustomPotionColor:0}}},{buy:{id:"minecraft:wither_skeleton_skull",Count:1b},buyB:{id:"redstone_block",Count:64b},sell:{id:"minecraft:bow",Count:1b,tag:{display:{Name:"{\"text\":\"This doesn't do anything special, but it has a purple name! \",\"translate\":\"wig\",\"color\":\"dark_purple\"}"},CustomPotionEffects:[{Id:20b,Amplifier:4b,Duration:32000}],CustomPotionColor:0}}}]}}
execute if entity @a[tag=K,scores={KyuTime=13685..22335}] run execute as @e[limit=1,type=villager,name=Danger,tag=!got,distance=..25,sort=nearest] run data modify entity @s Offers.Recipes append value {buy:{id:"minecraft:netherite_scrap",Count:1b},buyB:{id:"minecraft:blaze_powder",Count:1b},sell:{id:"minecraft:potion",Count:1b,tag:{Potion:"minecraft:thick",CustomPotionColor:16252672,duskdraught:1b,display:{Name:"{\"text\":\"Dusk's Draught\"}",Lore:["\"Once consumed, grants a permanent resistance and speed buff\"","\"to the user any time they are on the surface during nighttime.\"","\"\"","\"'Brittle key not required'\""]},HideFlags:33,Enchantments:[{id:"minecraft:lure",lvl:10}]}}}





#Summon an item from the below loot table, giving stranger 1 random trade based on that. If the time is between 13685 and 22335, also give him Dawn's Daylight.
loot spawn ~ ~ ~ loot minecraft:stranger
execute if entity @a[tag=K,scores={KyuTime=13685..22335}] run execute as @e[limit=1,type=villager,name=Stranger,tag=!got] run data modify entity @s Offers.Recipes append value {buy:{id:"minecraft:clock",Count:1b},buyB:{id:"minecraft:enchanting_table",Count:1b},sell:{id:"minecraft:clock",Count:1b,tag:{kyutrinket:1b,kyudayextender:1b,display:{Name:"{\"text\":\"Dawn's Daylight\"}",Lore:["\"If held at the start of the day,\"","\"extends the amount of daytime by 1:30.\"","\"\"","\"'Is it 1358 yet?'\""]},HideFlags:1,Enchantments:[{id:"minecraft:lure",lvl:10}]}}}
execute if entity @e[distance=..15,nbt={Item:{tag:{display:{Lore:["\"ENDER\""]}}}}] run execute as @e[limit=1,type=villager,name=Stranger,tag=!got,distance=..25,sort=nearest] run data merge entity @s {VillagerData:{level:5,profession:"minecraft:librarian",type:"minecraft:swamp"},CareerLevel:1,Offers:{Recipes:[{buy:{id:"minecraft:ender_eye",Count:1b},buyB:{id:"minecraft:glistering_melon_slice",Count:23b},sell:{id:"minecraft:black_banner",Count:1b,tag:{kyutrinket:1b,kyuvoidtrinket:1b,display:{Name:"{\"text\":\"Eye of the Void\"}",Lore:["\"Gives every non-farm-animal mob a small chance to\"","\"grant an ender pearl.\"","\"NOTE: PLACING THIS ITEM WILL BREAK IT. THE EFFECT \"","\"IS GAINED BY HOLDING IT IN YOUR INVENTORY.\""]},BlockEntityTag:{Base:0,Patterns:[{Pattern:mc,Color:9},{Pattern:flo,Color:15},{Pattern:tl,Color:10},{Pattern:tr,Color:10},{Pattern:bl,Color:10},{Pattern:cbo,Color:10},{Pattern:br,Color:10},{Pattern:bo,Color:15}]},HideFlags:1,Enchantments:[{id:"minecraft:lure",lvl:1}]},}}]}}
execute if entity @e[distance=..15,nbt={Item:{tag:{display:{Lore:["\"ZAMB\""]}}}}] run execute as @e[limit=1,type=villager,name=Stranger,tag=!got,distance=..25,sort=nearest] run data merge entity @s {VillagerData:{level:5,profession:"minecraft:librarian",type:"minecraft:swamp"},CareerLevel:1,Offers:{Recipes:[{buy:{id:"minecraft:allium",Count:4b},buyB:{id:"minecraft:gold_ingot",Count:5b},sell:{id:"minecraft:glowstone_dust",Count:1b,tag:{kyutrinket:1b,kyuspices:1b,display:{Name:"{\"text\":\"Suspicious Spices\"}",Lore:['[{"text":"Effect 1 - ","color":"dark_aqua"},{"text":"Saturation","color":"gold"}]','{"text":"All food items grant a tiny bit of extra saturation/hunger restoration.","color":"gold"}','{"text":"Zombie flesh in particular gains a massive boost,","color":"gold"}','{"text":"and the hunger effect it sometimes gives will be removed.","color":"gold"}','{"text":""}','[{"text":"Effect 2 - ","color":"dark_aqua"},{"text":"Regeneration","color":"light_purple"}]','{"text":"Golden apples will always heal an additional half a heart,","color":"light_purple"}','{"text":"and golden carrots have a small chance to heal you for half a heart.","color":"light_purple"}']},HideFlags:1,Enchantments:[{id:"minecraft:lure",lvl:2}]}}}]}}
execute if entity @e[distance=..15,nbt={Item:{tag:{display:{Lore:["\"LAVACHARM\""]}}}}] run execute as @e[limit=1,type=villager,name=Stranger,tag=!got,distance=..25,sort=nearest] run data merge entity @s {VillagerData:{level:5,profession:"minecraft:librarian",type:"minecraft:swamp"},CareerLevel:1,Offers:{Recipes:[{buy:{id:"minecraft:diamond_boots",Count:1b},buyB:{id:"minecraft:obsidian",Count:64b},sell:{id:"minecraft:leather_boots",Count:1b,tag:{kyutrinket:1b,kyulavaboots:1b,display:{color:8982758,Name:"{\"text\":\"Lava Wandering Boots\"}",Lore:["\"Cools lava underneath your feet,\"","\"turning it into obsidian.\""]},HideFlags:1,Enchantments:[{id:"minecraft:fire_protection",lvl:4},{id:"minecraft:unbreaking",lvl:3}],AttributeModifiers:[{AttributeName:"generic.armor",Name:"generic.armor",Amount:3,Operation:0,UUID:[I;6969,6969,6969,1221],Slot:"feet"},{AttributeName:"generic.armor_toughness",Name:"generic.armor_toughness",Amount:2,Operation:0,UUID:[I;6969,6969,6969,1222],Slot:"feet"}]}}}]}}
execute if entity @e[distance=..15,nbt={Item:{tag:{display:{Lore:["\"DUBLOON\""]}}}}] run execute as @e[limit=1,type=villager,name=Stranger,tag=!got,distance=..25,sort=nearest] run data merge entity @s {VillagerData:{level:5,profession:"minecraft:librarian",type:"minecraft:swamp"},CareerLevel:1,Offers:{Recipes:[{buy:{id:"minecraft:golden_apple",Count:1b},buyB:{id:"minecraft:ghast_tear",Count:1b},sell:{id:"minecraft:sunflower",Count:1b,tag:{kyutrinket:1b,kyudubloon:1b,display:{Name:"{\"text\":\"Lucky Dubloon\"}",Lore:["\"Allows you to access the rare drop table\"","\"randomly, at any time. Also gives +1 luck.\""]},HideFlags:1,Enchantments:[{id:"minecraft:lure",lvl:4}]}}}]}}
execute if entity @e[distance=..15,nbt={Item:{tag:{display:{Lore:["\"NATURE\""]}}}}] run execute as @e[limit=1,type=villager,name=Stranger,tag=!got,distance=..25,sort=nearest] run data merge entity @s {VillagerData:{level:5,profession:"minecraft:librarian",type:"minecraft:swamp"},CareerLevel:1,Offers:{Recipes:[{buy:{id:"minecraft:brown_wool",Count:4b},buyB:{id:"minecraft:golden_carrot",Count:12b},sell:{id:"minecraft:brown_banner",Count:1b,tag:{kyutrinket:1b,kyunaturegift:1b,display:{Name:"{\"text\":\"Nature's Gift\"}",Lore:["\"Disables the negative effects of random events\"","\"which effect animals, food, or crops.\"","\"NOTE: PLACING THIS ITEM WILL BREAK IT. THE EFFECT \"","\"IS GAINED BY HOLDING IT IN YOUR INVENTORY.\""]},BlockEntityTag:{Base:5,Patterns:[{Pattern:cr,Color:5},{Pattern:ts,Color:15},{Pattern:flo,Color:5},{Pattern:mr,Color:1},{Pattern:cbo,Color:15},{Pattern:mc,Color:10}]},HideFlags:1,Enchantments:[{id:"minecraft:lure",lvl:1}]},}}]}}
execute if entity @e[distance=..15,nbt={Item:{tag:{display:{Lore:["\"WITCHY\""]}}}}] run execute as @e[limit=1,type=villager,name=Stranger,tag=!got,distance=..25,sort=nearest] run data merge entity @s {VillagerData:{level:5,profession:"minecraft:librarian",type:"minecraft:swamp"},CareerLevel:1,Offers:{Recipes:[{buy:{id:"minecraft:golden_carrot",Count:12b},buyB:{id:"minecraft:iron_helmet",Count:1b},sell:{id:"minecraft:turtle_helmet",Count:1b,tag:{kyutrinket:1b,kyugoggles:1b,display:{Name:"{\"text\":\"Night Vision Goggles\"}",Lore:["\"Grants night vision, even when not worn...Odd.\""]},HideFlags:1,Enchantments:[{id:"minecraft:lure",lvl:6}]}}}]}}
execute if entity @e[distance=..15,nbt={Item:{tag:{display:{Lore:["\"WATER\""]}}}}] run execute as @e[limit=1,type=villager,name=Stranger,tag=!got,distance=..25,sort=nearest] run data merge entity @s {VillagerData:{level:5,profession:"minecraft:librarian",type:"minecraft:swamp"},CareerLevel:1,Offers:{Recipes:[{buy:{id:"minecraft:sponge",Count:8b},buyB:{id:"minecraft:enchanting_table",Count:1b},sell:{id:"minecraft:prismarine_shard",Count:1b,tag:{kyutrinket:1b,kyuenchantedshard:1b,display:{Name:"{\"text\":\"Enchanted Shard\"}",Lore:["\"Anyone who holds one of these\"","\"will be granted permanent water-breathing\"","\"and underwater mining haste.\""]},HideFlags:1,Enchantments:[{id:"minecraft:lure",lvl:7}]}}}]}}
execute if entity @e[distance=..15,nbt={Item:{tag:{display:{Lore:["\"KNOWLEDGE\""]}}}}] run execute as @e[limit=1,type=villager,name=Stranger,tag=!got,distance=..25,sort=nearest] run data merge entity @s {VillagerData:{level:5,profession:"minecraft:librarian",type:"minecraft:swamp"},CareerLevel:1,Offers:{Recipes:[{buy:{id:"minecraft:paper",Count:1b,tag:{kyusheet:1b,Enchantments:[{id:"minecraft:protection",lvl:1s}],HideFlags:1,display:{Name:"{\"text\":\"Magical Spell Sheet\"}",Lore:["\"Can be used as a reagant to acquire spells via the nether dungeon shop.\""]}}},buyB:{id:"minecraft:gold_block",Count:1b},sell:{id:"minecraft:book",Count:1b,tag:{kyutrinket:1b,kyuxptrinket:1b,display:{Name:"{\"text\":\"Pure Unrefined Knowledge\"}",Lore:["\"Passively grants xp, and gives additional\"","\"experience when killing mobs.\"","\"\"","\"'You know what I like a lot more than xp? KNOWLEDGE.'\""]},HideFlags:1,Enchantments:[{id:"minecraft:lure",lvl:10}]}}}]}}
#execute as @e[name=KyuAreaBlocks] run execute at @s positioned ~4 ~50 ~ run execute if entity @e[distance=..5,nbt={Item:{tag:{display:{Lore:["\"WITCHCLOCK\""]}}}}] run execute as @e[limit=1,type=villager,name=Stranger,tag=!got] run data merge entity @s {VillagerData:{level:5,profession:"minecraft:librarian",type:"minecraft:swamp"},CareerLevel:1,Offers:{Recipes:[{buy:{id:"minecraft:clock",Count:1b},buyB:{id:"minecraft:enchanting_table",Count:1b},sell:{id:"minecraft:clock",Count:1b,tag:{kyutrinket:1b,kyudayextender:1b,display:{Name:"{\"text\":\"Dawn's Daylight\"}",Lore:["\"If held at the start of the day,\"","\"extends the amount of daytime by 1:30.\"","\"\"","\"'Is it 1358 yet?'\""]},HideFlags:1,Enchantments:[{id:"minecraft:lure",lvl:10}]}}}]}}
execute if entity @a[tag=K,scores={KyuTime=13685..22335}] run execute as @e[limit=1,type=villager,name=Stranger,tag=!got,distance=..25,sort=nearest] run data modify entity @s Offers.Recipes append value {buy:{id:"minecraft:clock",Count:1b},buyB:{id:"minecraft:enchanting_table",Count:1b},sell:{id:"minecraft:clock",Count:1b,tag:{kyutrinket:1b,kyudayextender:1b,display:{Name:"{\"text\":\"Dawn's Daylight\"}",Lore:["\"If held at the start of the day,\"","\"extends the amount of daytime by 1:30.\"","\"\"","\"'Is it 1358 yet?'\""]},HideFlags:1,Enchantments:[{id:"minecraft:lure",lvl:10}]}}}

#execute at @s positioned ~ ~ ~ run execute if entity @e[distance=..5,nbt={Item:{tag:{display:{Lore:["\"STATUS\""]}}}}] run execute as @e[limit=1,type=villager,name=Stranger,tag=!got] run data merge entity @s {VillagerData:{level:5,profession:"minecraft:librarian",type:"minecraft:swamp"},CareerLevel:1,Offers:{Recipes:[{buy:{id:"minecraft:beetroot",Count:1b},buyB:{id:"minecraft:damaged_anvil",Count:1b},sell:{id:"minecraft:white_banner",Count:1b,tag:{display:{Name:"{\"text\":\"No-status\"}",Lore:["Disables the effects of the following status debuffs:"]},HideFlags:1,Enchantments:[{id:"minecraft:lure",lvl:9}]}}}]}}
#execute at @s positioned ~ ~ ~ run execute if entity @e[distance=..5,nbt={Item:{tag:{display:{Lore:["\"SPOODER\""]}}}}] run execute as @e[limit=1,type=villager,name=Stranger,tag=!got] run data merge entity @s {VillagerData:{level:5,profession:"minecraft:librarian",type:"minecraft:swamp"},CareerLevel:1,Offers:{Recipes:[{buy:{id:"minecraft:beetroot",Count:1b},buyB:{id:"minecraft:damaged_anvil",Count:1b},sell:{id:"minecraft:white_banner",Count:1b,tag:{display:{Name:"{\"text\":\"Spooder\"}",Lore:["spooooder"]},HideFlags:1,Enchantments:[{id:"minecraft:lure",lvl:10}]}}}]}}
#execute at @s positioned ~ ~ ~ run execute if entity @e[distance=..5,nbt={Item:{tag:{display:{Lore:["\"SLOWFALL\""]}}}}] run execute as @e[limit=1,type=villager,name=Stranger,tag=!got,distance=..25,sort=nearest] run data merge entity @s {VillagerData:{level:5,profession:"minecraft:librarian",type:"minecraft:swamp"},CareerLevel:1,Offers:{Recipes:[{buy:{id:"minecraft:emerald",Count:4b},buyB:{id:"minecraft:phantom_membrane",Count:4b},sell:{id:"minecraft:feather",Count:1b,tag:{kyutrinket:1b,kyuslowfall:1b,display:{Name:"{\"text\":\"Slowfall\"}",Lore:["\"Shortly after being equipped in the main hand,\"","\"grants slowfall for as long as it remains equipped.\""]},HideFlags:1,Enchantments:[{id:"minecraft:lure",lvl:11}]}}}]}}
#execute at @s positioned ~ ~ ~ run execute if entity @e[distance=..5,nbt={Item:{tag:{display:{Lore:["\"FOLLOWER\""]}}}}] run execute as @e[limit=1,type=villager,name=Stranger,tag=!got] run data merge entity @s {VillagerData:{level:5,profession:"minecraft:librarian",type:"minecraft:swamp"},CareerLevel:1,Offers:{Recipes:[{buy:{id:"minecraft:beetroot",Count:1b},buyB:{id:"minecraft:damaged_anvil",Count:1b},sell:{id:"minecraft:white_banner",Count:1b,tag:{display:{Name:"{\"text\":\"Follower\"}",Lore:["Woof woof. Bark bark?"]},HideFlags:1,Enchantments:[{id:"minecraft:lure",lvl:12}]}}}]}}

#Give the two villagers a tag to prevent them getting changed by future runs of the function
tag @e[limit=1,type=villager,name=Stranger,tag=!got,distance=..25,sort=nearest] add got
tag @e[limit=1,type=villager,name=Danger,tag=!got,distance=..25,sort=nearest] add got

#kill the item from the loot table spawn above
kill @e[type=item,distance=..10]
