##> python  .py

import os

Loman = {
    1: "I",
    2: "II",
    3: "III",
    4: "IV",
    5: "V",
    6: "VI",
    7: "VII",
    8: "VIII",
    9: "IX",
    10: "X"
}
MatList = [
    "Food",
    "Leather",
    "Cobblestone",
    "Chain",
    "Iron",
    "Diamond",
    "Netherite"
]
MatInfo = {
    "Food": [[30,60,150,300,500,1000,2000,3500,5000,7000],
             [["Avoid", "Zombie"],
              ["NightVision"],
              ["EmeraldHunter"],
              ["ImmidiateRespawn"],
              ["Infinite"],
              ["BonusEmerald", 1],
              ["Recovery", "Mending", 35, 100],
              ["Status", "Immunity", 1],
              ["Status", "Immunity", 1],
              ["FallenStar"]],
             "bread",
             "🍖"],
    "Leather": [[10,30,60,120,200,320,500,750,1000,1250],
             [["Status", "Swiftness", 5],
              ["DeleteItems"],
              ["DepthStrider", 3],
              ["WebBreaker"],
              ["Respiration", 1],
              ["NexusStatus", "Swiftness", 100],
              ["SaveShulkerBox"],
              ["Status", "Swiftness", 10],
              ["SoulSpeed", 150],
              ["Status", "Frenzy", 5]],
             "leather",
             "🟫"],
    "Cobblestone": [[10,30,80,150,300,600,1200,1800,2500,3200],
             [["Pickaxe", True],
              ["DeleteFish", 30],
              ["DeleteFish", 60],
              ["Pickaxe"],
              ["DeleteFish", 90],
              ["Pickaxe"],
              ["Status", "Health", 2],
              ["Status", "Health", 3],
              ["Ignite"],
              ["InfiniteStone"]],
             "cobblestone",
             "🪨"],
    "Chain": [[5,10,20,35,50,75,125,200,300,450],
             [["Bow", True],
              ["KBResistance", 2],
              ["Bow"],
              ["KBResistance", 4],
              ["Bow"],
              ["KBResistance", 6],
              ["Recovery", "Fortitude", 35, 100],
              ["KBResistance", 8],
              ["Bow"],
              ["SneakKBRes"]],
             "chain",
             "⛓"],
    "Iron": [[5,15,40,80,150,250,500,750,1000,1300],
             [["Enchant", "Blast Protection", 1],
              ["Avoid", "Blaze"],
              ["Enchant", "Fire Protection", 2],
              ["Avoid", "Ghast"],
              ["Status", "Syphon", 40],
              ["Enchant", "Blast Protection", 2],
              ["Status", "Syphon", 40],
              ["Enchant", "Fire Protection", 4],
              ["Avoid", "Blaze and Ghast", True],
              ["Status", "Syphon", 80]],
             "iron_ingot",
             "◎"],
    "Diamond": [[5,10,25,50,100,200,300,400,600,1000],
             [["Enchant", "Looting", 1],
              ["Magnet"],
              ["Enchant", "Looting", 2],
              ["ApplySlowness", 1],
              ["Enchant", "Looting", 3],
              ["Enchant", "Fortune", 1],
              ["ApplySlowness", 2],
              ["Enchant", "Fortune", 2],
              ["ApplySlowness", 3, "creepers"],
              ["ApplySlowness", 3, "creepers"]],
             "diamond",
             "💎"],
    "Netherite": [[1,5,10,20,30,45,60,80,100,125],
             [["Status", "Strength", 2],
              ["Status", "Strength", 3],
              ["Status", "Strength", 3],
              ["Status", "Strength", 3],
              ["Status", "Strength", 4],
              ["Status", "Master", 1],
              ["Status", "Master", 1],
              ["Status", "Master", 1],
              ["Status", "Master", 1],
              ["Status", "Master", 1]],
             "netherite_ingot",
             "💠"]
}
def Avoid(target, isreally = False):
    a = ""
    if isreally == True:
        a = "greatly"
    return f'{{"text":"{target}\'s detection range is {a} reduced.\\n\\n","color":"white"}}'
def Status(name, value):
    return f'{{"text":"{name}: +{value}\\n\\n","color":"white"}}'
def Enchant(name, value):
    return f'{{"text":"You gain {name} {Loman[value]}\\n\\n","color":"white"}}'
def Recovery(status, per, value):
    return f'{{"text":"You gain +{value} {status} while your health is below {per}%\\n\\n","color":"white"}}'
def BonusEmerald(value):
    num = ""
    if value > 1:
        num = "s"
    a = f'You gain {value} Emerald{num}!'
    return f'{{"text":"{a}\\n\\n","color":"white"}}'
def DepthStrider(value):
    num = ""
    if value < 3:
        num = str(-value*33)
    else:
        num = "no"
    a = f'You now receive {num} movement penalty in water.'
    return f'{{"text":"{a}\\n\\n","color":"white"}}'
def Respiration(value):
    a = f'You can now breathe underwater for {value*100}% longer.'
    return f'{{"text":"{a}\\n\\n","color":"white"}}'
def NexusStatus(status, value):
    return f'{{"text":"You gain +{value} {status} while in Nexus.\\n\\n","color":"white"}}'
def SoulSpeed(value):
    a = f'Standing on soul sand or soul soil increases your Swiftness by {value}% for a short time.'
    return f'{{"text":"{a}\\n\\n","color":"white"}}'
def Pickaxe(first = False):
    a = ""
    if first == False:
        a = "Your pickaxe levels up!"
    else:
        a = "When you craft a wooden pickaxe, it evolves."
    return f'{{"text":"{a}\\n\\n","color":"white"}}'
def DeleteFish(value):
    a = f'{value}% chance for silverfish not to spawn.'
    return f'{{"text":"{a}\\n\\n","color":"white"}}'
def Bow(first = False):
    a = ""
    if first == False:
        a = "Your bow levels up!"
    else:
        a = "When you craft a bow, it evolves."
    return f'{{"text":"{a}\\n\\n","color":"white"}}'
def KBResistance(value):
    num = value*10
    if num > 100:
        num = 100
    a = f'You receive {num}% less knockback.'
    return f'{{"text":"{a}\\n\\n","color":"white"}}'
def Haste(value):
    a = f'You gain Haste {Loman[value]} (while not holding a special sword).'
    return f'{{"text":"{a}\\n\\n","color":"white"}}'
def ApplySlowness(value, target = "none"):
    num = ""
    if target == "none":
        num = "enemies"
    else:
        num = target
    a  =f'Apply Slowness {Loman[value]} to nearby {num}.'
    return f'{{"text":"{a}\\n\\n","color":"white"}}'
Lore = {
    "Avoid": Avoid,
    "NightVision": '{"text":"You gain Night Vision.\\n\\n","color":"white"}',
    "EmeraldHunter": '{"text":"Enemies now have a small chance to drop an Emerald. Creepers have twice the chance.\\n\\n","color":"white"}',
    "ImmidiateRespawn": '{"text":"You can now revive where you died.\\n\\n","color":"white"}',
    "Infinite": '{"text":"You no longer consume arrows when shooting a bow.\\n\\n","color":"white"}',
    "Status": Status,
    "Enchant": Enchant,
    "Recovery": Recovery,
    "BonusEmerald": BonusEmerald,
    "FallenStar": '{"text":"You now receive \\"Fallen Star\\"\\n(which grants you immunity to levitation).\\n\\n","color":"white"}',
    "DeleteItems": '{"text":"Clears most non-block items from your inventory.\\n\\n","color":"white"}',
    "DepthStrider": DepthStrider,
    "WebBreaker": '{"text":"You can now destroy cobwebs with your body.\\n\\n","color":"white"}',
    "Respiration": Respiration,
    "NexusStatus": NexusStatus,
    "SaveShulkerBox": '{"text":"Dropped shulker boxes are immune to explosions and fire.\\n\\n","color":"white"}',
    "SoulSpeed": SoulSpeed,
    "Pickaxe": Pickaxe,
    "DeleteFish": DeleteFish,
    "Ignite": '{"text":"Ignite coal blocks in your inventory.\\nBurning them generates Coal, Iron, Diamonds, and Netherite.\\n\\n","color":"white"}',
    "InfiniteStone": '{"text":"You can now receive \\"Infinite Cobblestone\\".\\n\\n","color":"white"}',
    "Bow": Bow,
    "KBResistance": KBResistance,
    "SneakKBRes": '{"text":"(Active at Immunity 6+) While sneaking, you negate\\nall damage and knockback, but cannot deal damage or mine blocks. (When the effect expires, all active resistance effects are removed.)\\n\\n","color":"white"}',
    "Haste": Haste,
    "Magnet": '{"text":"Pull nearby items toward you.\\n\\n","color":"white"}',
    "ApplySlowness": ApplySlowness
}

base = os.path.dirname(os.path.abspath(__file__))
reached = os.path.abspath(
    os.path.join(base, "..", "collections", "reached")
)
advcollection = os.path.abspath(
    os.path.join(base, "..", "..", "advancements", "collection")
)
collection = os.path.abspath(
    os.path.join(base, "..", "collections")
)
os.makedirs(reached, exist_ok=True)
os.makedirs(collection, exist_ok=True)

filename0 = os.path.join(advcollection, f"root.json")
with open(filename0, "w", encoding="utf-8") as f:
    f.write(f'{{\n  "display": {{\n    "icon": {{\n      "item": "minecraft:chest"\n    }},\n    "title": "Collection",\n')
    f.write(f'    "description": "",\n    "background": "minecraft:textures/block/stripped_oak_log.png",\n')
    f.write('    "show_toast": false,\n    "announce_to_chat": false,\n    "hidden": false\n  },\n  "criteria": {\n    "root": {\n      "trigger": "minecraft:impossible"\n    }\n  }\n}')

for i in range (0, len(MatList)):
    mat = MatList[i]
    Info = MatInfo[mat]
    
    for j in range(0, 10):
        reach = Info[0][j]
        next = ""
        if j < 9:
            x = Info[0][j+1]
            next = f'{{"text":"Next Bonus: ","color":"gray"}},{{"text":"{x} {mat}","color":"aqua"}},{{"text":" required in total\\n","color":"gray"}}'
        else:
            next = '{"text":"You\'ve reached the maximum collection!\\n","color":"gray"}'
        
        LoreInfo = Info[1][j]
        function = Lore[LoreInfo[0]]
        lore = ""
        if callable(function):
            lore = function(*LoreInfo[1:])
        else:
            lore = function
        
        icon = Info[2]
        mark = Info[3]
        
        mat_l = mat.lower()
        mat_u = mat.upper()
        filename = os.path.join(reached, f"{mat_l}{j+1}.mcfunction")
        with open(filename, "w", encoding="utf-8") as f:
            f.write(f'tellraw @s {{"text":"","extra":[{{"text":"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n","color":"dark_gray"}},{{"text":"{mark} {mat_u} COLLECTION\\n","color":"gold","bold":true}},{{"text":"You collected ","color":"gray"}},{{"text":"{reach} {mat}","color":"green"}},{{"text":" !\\n\\n","color":"gray"}},{lore},{next},{{"text":"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━","color":"dark_gray"}}]}}\n')
            f.write("execute at @s run playsound entity.player.levelup master @s ~ ~ ~ 1 1 1\n")
            f.write(f'tag @s add {mat}{j+1}\n')
        
        a = ""
        if j == 0:
            a = f'hearing:collection/root'
        else:
            a = f'hearing:collection/{mat_l}_{j}'
        filename2 = os.path.join(advcollection, f"{mat_l}_{j+1}.json")
        with open(filename2, "w", encoding="utf-8") as f:
            f.write(f'{{\n  "parent": "{a}",\n  "display": {{\n    "icon": {{\n      "item": "minecraft:{icon}"\n    }},\n    "title": "{mat} Collection",\n')
            f.write(f'    "description": {{"text":"","extra":[{{"text":"{mat} {Loman[j+1]}\\n","color":"gold","bold":true}},{{"text":"Collect ","color":"gray"}},{{"text":"{reach} {mat}","color":"green"}},{{"text":": \\n\\n","color":"gray"}},{lore}]}},\n')
            f.write('    "show_toast": false,\n    "announce_to_chat": false,\n    "hidden": false\n  },\n  "criteria": {\n    "unlock": {\n      "trigger": "minecraft:impossible"\n    }\n  }\n}')

filename3 = os.path.join(collection, "check.mcfunction")
with open(filename3, "w", encoding="utf-8") as f:
    for i in range (0, len(MatList)):
        mat = MatList[i]
        Info = MatInfo[mat]
        
        mat_l = mat.lower()
        mat_u = mat.upper()

        for j in range(0, 10):
            reach = Info[0][j]
            f.write(f'execute as @s[tag=!{mat}{j+1},scores={{{mat}={reach}..}}] run function hearing:collections/reached/{mat_l}{j+1}\n')
            f.write(f'execute as @s[scores={{{mat}={reach}..}}] run advancement grant @s only hearing:collection/{mat_l}_{j+1}\n')
            if mat != "Netherite" and j == 9:
                f.write('\n')