import { world, BlockVolume, Entity } from "@minecraft/server";

var x = 0
var y = 0
var z = 0
var highestplayer = 0

//Starter
let zombies1 = ['rabbit_hide', 'feather', 'stick', 'rns:pebble', 'bumble:bad_bundle']
let rarezombies1 = ['hax:wood_broadsword', 'hax:wood_machete', 'hax:wood_cutlass', 'wooden_shovel', 'hax:wood_spade', 'wooden_axe', 'sev:makeshiftaxe', 'sev:flintaxe', 'sev:flint_broadsword']
let offhandzombies1 = ['dirt', 'sev:plantfiber', 'brown_mushroom', 'red_mushroom', 'sev:twine']

let skeleton1 = ['bone', 'bone_meal', 'white_dye', 'air', 'white_candle', 'cooked_cod']
let rareskeleton1 = ['hax:wood_scythe', 'hax:stone_scythe', 'hax:wood_dagger', 'hax:stone_dagger', 'hax:wood_kama', 'hax:stone_kama', 'hax:slime_katana', 'hax:wood_katana', 'hax:stone_katana']
let offhandskeleton1 = ['sand', 'bone', 'bone_meal', 'white_dye']

let tier1helmet = ['hax:slime_helmet', 'hax:slime_berserker_helmet', 'leather_helmet', 'chainmail_helmet', 'hax:chain_berserker_helmet', 'c8164:blindfold1', 'c8164:blindfold2', 'c8164:hat_sun']
let tier1helmetskele = ['hax:slime_helmet', 'hax:slime_knight_helmet', 'leather_helmet', 'chainmail_helmet', 'hax:chain_knight_helmet', 'c8164:blindfold1', 'c8164:blindfold2', 'c8164:hat_sun']
let tier1chestplate = ['leather_chestplate', 'hax:slime_chestplate', 'chainmail_chestplate']
let tier1leggings = ['leather_leggings', 'chainmail_leggings']
let tier1boots = ['leather_boots', 'chainmail_boots']

//Age 2

let zombies2 = ['add:scraped_hide', 'bumble:strawberry', 'cobblestone', 'rns:pebble', 'bumble:bad_bundle']
let rarezombies2 = ['hax:tin_broadsword', 'hax:tin_machete', 'hax:tin_cutlass', 'hax:copper_broadsword', 'hax:copper_longsword', 'hax:copper_machete', 'hax:copper_cutlass', 'stone_shovel', 'hax:tin_spade', 'hax:copper_spade', 'wooden_axe', 'sev:makeshiftaxe', 'sev:flintaxe', 'sev:flint_broadsword']
let offhandzombies2 = ['empty_map', 'torch', 'paper']

let skeleton2 = ['bone', 'brick', 'white_dye', 'air', 'white_candle', 'bumble:cooked_corn']
let rareskeleton2 = ['hax:tin_scythe', 'hax:copper_scythe', 'hax:tin_dagger', 'hax:copper_dagger', 'hax:tin_kama', 'hax:copper_kama', 'hax:slime_katana', 'hax:tin_katana', 'hax:copper_katana']
let offhandskeleton2 = ['arrow', 'flint', 'stick', 'feather', 'hax:wood_spear']

let tier2helmet = ['hax:tin_helmet', 'chainmail_helmet', 'hax:copper_helmet', 'golden_helmet']
let tier2chestplate = ['hax:tin_chestplate', 'chainmail_chestplate', 'hax:copper_chestplate', 'golden_chestplate']
let tier2leggings = ['hax:tin_leggings', 'chainmail_leggings', 'hax:copper_leggings', 'golden_leggings']
let tier2boots = ['hax:tin_boots', 'chainmail_boots', 'hax:copper_boots', 'golden_boots']

//Post Nether

let zombies3 = ['stone_sword', 'stone_shovel', 'stone_axe', 'golden_axe', 'golden_shovel', 'iron_shovel', 'beetroot_soup']
let rarezombies3 = ['iron_sword', 'iron_axe', 'diamond_shovel', 'water_bucket']
let offhandzombies3 = ['shears', 'carrot_on_a_stick', 'totem_of_undying', 'brush']

let skeleton3 = ['iron_sword', 'iron_axe', 'golden_shovel', 'air']
let rareskeleton3 = ['diamond_sword', 'golden_hoe', 'bow']
let offhandskeleton3 = ['white_banner', 'shield', 'lightning_rod']

let tier3helmet = ['iron_helmet', 'chainmail_helmet']
let tier3chestplate = ['iron_chestplate', 'chainmail_chestplate']
let tier3leggings = ['iron_leggings', 'chainmail_leggings']
let tier3boots = ['iron_boots', 'chainmail_boots']

//Eye of Ender

let zombies4 = ['iron_sword', 'iron_shovel', 'iron_axe', 'golden_axe', 'golden_shovel', 'golden_carrot']
let rarezombies4 = ['diamond_sword', 'diamond_axe', 'diamond_shovel']
let offhandzombies4 = ['copper_bulb', 'experience_bottle', 'totem_of_undying', 'brush']

let skeleton4 = ['iron_sword', 'iron_axe', 'golden_shovel', 'air']
let rareskeleton4 = ['diamond_sword', 'golden_hoe', 'bow']
let offhandskeleton4 = ['white_banner', 'shield', 'totem_of_undying']

let tier4helmet = ['iron_helmet', 'chainmail_helmet']
let tier4chestplate = ['iron_chestplate', 'chainmail_chestplate']
let tier4leggings = ['iron_leggings', 'chainmail_leggings']
let tier4boots = ['iron_boots', 'chainmail_boots']

//Ender Dragon Defeat

let zombies5 = ['diamond_sword', 'diamond_shovel', 'diamond_axe', 'golden_axe', 'golden_shovel', 'golden_carrot']
let rarezombies5 = ['netherite_sword', 'netherite_axe', 'netherite_shovel']
let offhandzombies5 = ['warped_fungus_on_a_stick', 'experience_bottle', 'totem_of_undying', 'brush']

let skeleton5 = ['diamond_sword', 'diamond_axe', 'golden_shovel', 'air']
let rareskeleton5 = ['netherite_sword', 'golden_hoe', 'bow']
let offhandskeleton5 = ['shield', 'totem_of_undying']

let tier5helmet = ['iron_helmet', 'diamond_helmet']
let tier5chestplate = ['iron_chestplate', 'diamond_chestplate']
let tier5leggings = ['iron_leggings', 'diamond_leggings']
let tier5boots = ['iron_boots', 'diamond_boots']

//Pillagers

let pillager1 = ['crossbow']
let rarepillager1 = ['bow']
let offhandpillager = ['totem_of_undying', 'goat_horn', 'emerald']

let vindicator1 = ['iron_axe', 'iron_sword']
let rarevindicator1 = ['diamond_axe', 'diamond_sword']
let offhandvindicator = ['totem_of_undying', 'emerald']


let vex1 = ['iron_axe', 'iron_sword']
let rarevex1 = ['emerald', 'diamond_sword']
let offhandvex = ['totem_of_undying', 'emerald', 'emerald', 'emerald']

//Drowned

let drowned1 = ['fishing_rod', 'gold_sword', 'gold_axe']
let raredrowned1 = ['trident', 'iron_sword']
let offhanddrowned = ['lightning_rod', 'copper_ingot', 'nautilus_shell', 'sniffer_egg', 'kelp']



world.afterEvents.playerSpawn.subscribe((eventData) => {
    const source = eventData.player
    const firstspawn = eventData.initialSpawn
    if (firstspawn) {
        source.runCommandAsync('scoreboard objectives add page dummy')
        source.runCommandAsync('scoreboard players add @s page 0')

        source.runCommandAsync('scoreboard objectives add pillager_upg dummy')
        source.runCommandAsync('scoreboard players add @s pillager_upg 0')

        source.runCommandAsync('scoreboard objectives add drowned_upg dummy')
        source.runCommandAsync('scoreboard players add @s drowned_upg 0')
    }

})


world.afterEvents.entitySpawn.subscribe((eventData) => {
    let entity = eventData.entity
    let spawn = eventData.cause
    let randomnum = 0

    if (entity.typeId != "minecraft:item" || entity.typeId != "minecraft:xp_orb") {

        x = get_highest_age()
        y = get_highest_pillager()
        z = get_highest_drowned()

        //Volume to check for a mob spawner
        const boundingBox = new BlockVolume(
            { x: entity?.location.x - 3, y: entity?.location.y - 1, z: entity?.location.z - 3 },
            { x: entity?.location.x + 3, y: entity?.location.y + 1, z: entity?.location.z + 3 }
        )
        //Don't Spawn With Loot if spawned near Mob Spawners/Trial Chamber
        //LINE BELOW REQUIRES BETA API!!
        //if (entity.dimension.containsBlock(boundingBox, { includeTypes: ["minecraft:mob_spawner", "minecraft:trial_spawner"] }, true).valueOf() == false) {
        //Non Age related changes
        if (entity.typeId == "minecraft:creeper") {
            randomnum = Math.random()
            if (randomnum > 0.8) {
                entity.addEffect("wind_charged", 20000000, { showParticles: true })
            }
        }

        //Branching Paths

        //Pillagers 

        if (y == 1) {
            if (entity.typeId == "minecraft:pillager") {
                mob_tiered_equipment(entity, pillager1, rarepillager1, offhandpillager, 1, 0.3, 0.5)
            }

            if (entity.typeId == "minecraft:vindicator") {
                mob_tiered_equipment(entity, vindicator1, rarevindicator1, offhandvindicator, 1, 0.4, 0.2)
                mob_buffs(entity, 0.1)
            }
            if (entity.typeId == "minecraft:vex") {
                mob_tiered_equipment(entity, vex1, rarevex1, offhandvex, 1, 0.1, 0.8)
                mob_buffs(entity, 0.1)
            }
        }

        if (z == 1) {
            if (entity.typeId == "minecraft:drowned") {
                mob_tiered_equipment(entity, drowned1, raredrowned1, offhanddrowned, 0.5, 0.3, 0.5)
            }
        }

        //Mainline Progression

        //Specific Ages Changes
        if (x == 0) {
            //Zombies & Husks 1
            if (entity.typeId == "minecraft:husk" || entity.typeId == "minecraft:zombie" || entity.typeId == "hax:draugr") {
                mob_tiered_equipment(entity, zombies1, rarezombies1, offhandzombies1, 0.85, 0.2, 0.2)
                //entity, helmet_chance, chest_chance, leggings_chance, chance_to_have_any_equip, boots_chance, helmet, chestplate, leggings, boots
                mob_tiered_armor(entity, tier1helmet, tier1chestplate, tier1leggings, tier1boots, 0.5, 0.8, 0.1, 0.2, 0.5)

            }
            //Skele and Bogged
            if (entity.typeId == "minecraft:skeleton" || entity.typeId == "minecraft:bogged" || entity.typeId == "minecraft:stray" || entity.typeId == "hax:parched") {
                mob_tiered_equipment(entity, skeleton1, rareskeleton1, offhandskeleton1, 0.8, 0.3, 0.2)
                mob_tiered_armor(entity, tier1helmetskele, tier1chestplate, tier1leggings, tier1boots, 0.7, 0.7, 0.1, 0.1, 0.1)
            }

            //Spiders 1
            if (entity.typeId.includes("spider")) {
                randomnum = Math.random()
                if (randomnum > 0.9) {
                    entity.addEffect("weaving", 20000000, { showParticles: true })
                }
            }
        }

        //After Mining Iron
        if (x == 1) {
            //Chance to be Infested or Slimed Underground
            if (entity.location.y < 40) {
                randomnum = Math.random()
                if (randomnum > 0.7) {
                    randomnum = Math.random()
                    if (randomnum > 0.5) {
                        entity.addEffect("oozing", 20000000, { showParticles: true })
                    }
                    else {
                        entity.addEffect("infested", 20000000, { showParticles: true })
                    }
                }
            }

            //Zombies & Husks 2
            if (entity.typeId == "minecraft:husk" || entity.typeId == "minecraft:zombie") {
                mob_tiered_equipment(entity, zombies2, rarezombies2, offhandzombies2, 0.6, 0.2, 0.3)
                mob_tiered_armor(entity, tier2helmet, tier2chestplate, tier2leggings, tier2boots, 0.5, 0.5, 0.1, 0.2, 0.4)
            }

            //Skele and Bogged 2
            if (entity.typeId == "minecraft:skeleton" || entity.typeId == "minecraft:bogged" || entity.typeId == "minecraft:stray") {
                mob_tiered_equipment(entity, skeleton2, rareskeleton2, offhandskeleton2, 0.7, 0.3, 0.2)
                mob_tiered_armor(entity, tier2helmet, tier2chestplate, tier2leggings, tier2boots, 0.5, 0.5, 0.1, 0.2, 0.4)
            }

            //Spiders 2
            if (entity.typeId.includes("spider")) {
                randomnum = Math.random()
                if (randomnum > 0.7) {
                    entity.addEffect("weaving", 20000000, { showParticles: true })
                }
            }
        }

        //Entering the Nether
        if (x == 2) {
            //Chance to be Infested or Slimed Underground
            if (entity.location.y < 40) {
                randomnum = Math.random()
                if (randomnum > 0.6) {
                    randomnum = Math.random()
                    if (randomnum > 0.5) {
                        entity.addEffect("oozing", 20000000, { showParticles: true })
                    }
                    else {
                        entity.addEffect("infested", 20000000, { showParticles: true })
                    }
                }
            }

            //Zombies & Husks 3
            if (entity.typeId == "minecraft:husk" || entity.typeId == "minecraft:zombie") {
                mob_tiered_equipment(entity, zombies3, rarezombies3, offhandzombies3, 0.4, 0.1, 0.1)
                mob_tiered_armor(entity, tier3helmet, tier3chestplate, tier3leggings, tier3boots, 0.7, 0.5, 0.2, 0.3, 0.5)
            }

            //Skele and Bogged 3
            if (entity.typeId == "minecraft:skeleton" || entity.typeId == "minecraft:bogged" || entity.typeId == "minecraft:stray") {
                mob_tiered_equipment(entity, skeleton3, rareskeleton3, offhandskeleton3, 0.5, 0.3, 0.4)
                mob_tiered_armor(entity, tier3helmet, tier3chestplate, tier3leggings, tier3boots, 0.7, 0.5, 0.2, 0.3, 0.5)
            }
            //Spiders 3
            if (entity.typeId.includes("spider")) {
                randomnum = Math.random()
                if (randomnum > 0.5) {
                    entity.addEffect("weaving", 20000000, { showParticles: true })
                }
                mob_buffs(entity, 0.1)
            }
        }

        //Eye of Ender in Inventory
        if (x == 3) {
            //Chance to be Infested or Slimed Underground
            if (entity.location.y < 40) {
                randomnum = Math.random()
                if (randomnum > 0.6) {
                    randomnum = Math.random()
                    if (randomnum > 0.5) {
                        entity.addEffect("oozing", 20000000, { showParticles: true })
                    }
                    else {
                        entity.addEffect("infested", 20000000, { showParticles: true })
                    }
                }
            }

            //Zombies & Husks 3
            if (entity.typeId == "minecraft:husk" || entity.typeId == "minecraft:zombie") {
                mob_tiered_equipment(entity, zombies4, rarezombies4, offhandzombies4, 0.4, 0.1, 0.1)
                mob_tiered_armor(entity, tier4helmet, tier4chestplate, tier4leggings, tier4boots, 0.7, 0.5, 0.2, 0.3, 0.5)
                mob_buffs(entity, 0.1)
            }

            //Skele and Bogged 3
            if (entity.typeId == "minecraft:skeleton" || entity.typeId == "minecraft:bogged" || entity.typeId == "minecraft:stray") {
                mob_tiered_equipment(entity, skeleton4, rareskeleton4, offhandskeleton4, 0.5, 0.3, 0.4)
                mob_tiered_armor(entity, tier4helmet, tier4chestplate, tier4leggings, tier4boots, 0.7, 0.5, 0.2, 0.3, 0.5)
                mob_buffs(entity, 0.1)
            }
            //Spiders 3
            if (entity.typeId.includes("spider")) {
                randomnum = Math.random()
                if (randomnum > 0.5) {
                    entity.addEffect("weaving", 20000000, { showParticles: true })
                }
                mob_buffs(entity, 0.1)
            }
        }

        //Ender Dragon Defeated
        if (x == 4) {
            //Chance to be Infested or Slimed Underground
            if (entity.location.y < 40) {
                randomnum = Math.random()
                if (randomnum > 0.6) {
                    randomnum = Math.random()
                    if (randomnum > 0.5) {
                        entity.addEffect("oozing", 20000000, { showParticles: true })
                    }
                    else {
                        entity.addEffect("infested", 20000000, { showParticles: true })
                    }
                }
            }

            //Zombies & Husks 3
            if (entity.typeId == "minecraft:husk" || entity.typeId == "minecraft:zombie") {
                mob_tiered_equipment(entity, zombies5, rarezombies5, offhandzombies5, 0.8, 0.1, 0.2)
                mob_tiered_armor(entity, tier5helmet, tier5chestplate, tier5leggings, tier5boots, 1, 0.7, 0.5, 0.5, 0.5)
                mob_buffs(entity, 0.8)
            }

            //Skele and Bogged 3
            if (entity.typeId == "minecraft:skeleton" || entity.typeId == "minecraft:bogged" || entity.typeId == "minecraft:stray") {
                mob_tiered_equipment(entity, skeleton5, rareskeleton5, offhandskeleton5, 0.3, 0.2, 0.1)
                mob_tiered_armor(entity, tier5helmet, tier5chestplate, tier5leggings, tier5boots, 1, 0.7, 0.5, 0.5, 0.5)
                mob_buffs(entity, 0.5)
            }
            //Spiders 3
            if (entity.typeId.includes("spider")) {
                randomnum = Math.random()
                if (randomnum > 0.3) {
                    entity.addEffect("weaving", 20000000, { showParticles: true })
                }
                mob_buffs(entity, 0.4)
            }
        }
        //}
    }

})

function mob_tiered_equipment(entity, common_equip, rare_equip, offhand_equip, chance_to_have_any_equip, rare_equip_chance, off_hand_chance) {
    if (Math.random() > 1 - chance_to_have_any_equip) {
        if (Math.random() > 1 - rare_equip_chance) {
            entity.runCommandAsync('replaceitem entity @s slot.weapon.mainhand 0 ' + rare_equip[Math.floor((Math.random()) * rare_equip.length)])
        }
        else {
            entity.runCommandAsync('replaceitem entity @s slot.weapon.mainhand 0 ' + common_equip[Math.floor((Math.random()) * common_equip.length)])
        }
    }
    if (Math.random() > 1 - off_hand_chance) {
        entity.runCommandAsync('replaceitem entity @s slot.weapon.offhand 0 ' + offhand_equip[(Math.floor((Math.random()) * offhand_equip.length))])
    }
}

function mob_tiered_armor(entity, helmet, chestplate, leggings, boots, chance_to_have_any_equip, helmet_chance, chest_chance, leggings_chance, boots_chance) {
    if (Math.random() > 1 - chance_to_have_any_equip) {
        if (Math.random() > 1 - helmet_chance) {
            entity.runCommandAsync('replaceitem entity @s slot.armor.head 0 ' + helmet[(Math.floor((Math.random()) * helmet.length))])
        }
        if (Math.random() > 1 - chest_chance) {
            entity.runCommandAsync('replaceitem entity @s slot.armor.chest 0 ' + chestplate[(Math.floor((Math.random()) * chestplate.length))])
        }
        if (Math.random() > 1 - leggings_chance) {
            entity.runCommandAsync('replaceitem entity @s slot.armor.legs 0 ' + leggings[(Math.floor((Math.random()) * leggings.length))])
        }
        if (Math.random() > 1 - boots_chance) {
            entity.runCommandAsync('replaceitem entity @s slot.armor.feet 0 ' + boots[(Math.floor((Math.random()) * boots.length))])
        }
    }

}





//Not possible to detect what a mob is holding yet
/*
function enchantments(entity, chance_to_enchant, curse_of_vanishing_chance) {

    if (Math.random() > 1 - chance_to_enchant) {
        world.sendMessage("enchant")
        if (entity.testFor({ item: "minecraft:wooden_sword" }) == true || entity.testFor({ item: "minecraft:stone_sword" }) == true || entity.testFor({ item: "minecraft:iron_sword" }) == true || entity.testFor({ item: "minecraft:golden_sword" }) == true || entity.testFor({ item: "minecraft:diamond_sword" }) == true || entity.testFor({ item: "minecraft:netherite_sword" }) == true) {
            entity.runCommandAsync("enchant @e[r=0.3] " + sword_enchants[(Math.floor((Math.random()) * sword_enchants.length))] + " 1")
            world.sendMessage("1")
        }
        else if (entity.testFor({ item: "minecraft:wooden_axe" }) == true || entity.testFor({ item: "minecraft:stone_axe" }) == true || entity.testFor({ item: "minecraft:iron_axe" }) == true || entity.testFor({ item: "minecraft:golden_axe" }) == true || entity.testFor({ item: "minecraft:diamond_axe" }) == true || entity.testFor({ item: "minecraft:netherite_axe" }) == true) {
            entity.runCommandAsync("enchant @e[r=0.3] " + axe_enchants[(Math.floor((Math.random()) * axe_enchants.length))] + " 1")
            world.sendMessage("2")

        }
        else if (entity.testFor({ item: "minecraft:wooden_shovel" }) == true || entity.testFor({ item: "minecraft:stone_shovel" }) == true || entity.testFor({ item: "minecraft:iron_shovel" }) == true || entity.testFor({ item: "minecraft:golden_shovel" }) == true || entity.testFor({ item: "minecraft:diamond_shovel" }) == true || entity.testFor({ item: "minecraft:netherite_shovel" }) == true || entity.testFor({ item: "minecraft:wooden_hoe" }) == true || entity.testFor({ item: "minecraft:stone_hoe" }) == true || entity.testFor({ item: "minecraft:iron_hoe" }) == true || entity.testFor({ item: "minecraft:golden_hoe" }) == true || entity.testFor({ item: "minecraft:diamond_hoe" }) == true || entity.testFor({ item: "minecraft:netherite_hoe" }) == true) {
            entity.runCommandAsync("enchant @e[r=0.3] " + other_enchants[(Math.floor((Math.random()) * other_enchants.length))] + " 1")
            world.sendMessage("3")
        }

        if (Math.random() > 1 - curse_of_vanishing_chance) {
            item_enchant.addEnchantment("curse_of_vanishing")
        }
    }

}
*/



function get_highest_age() {
    highestplayer = 0
    world.scoreboard.getObjective('page').getParticipants().forEach((player) => {
        if (world.scoreboard.getObjective('page').getScore(player) > highestplayer) {
            highestplayer = world.scoreboard.getObjective('page').getScore(player)
        }
    })

    return highestplayer
}


function get_highest_pillager() {
    highestplayer = 0
    world.scoreboard.getObjective('pillager_upg').getParticipants().forEach((player) => {
        if (world.scoreboard.getObjective('pillager_upg').getScore(player) > highestplayer) {
            highestplayer = world.scoreboard.getObjective('pillager_upg').getScore(player)
        }
    })

    return highestplayer
}

function get_highest_drowned() {
    highestplayer = 0
    world.scoreboard.getObjective('drowned_upg').getParticipants().forEach((player) => {
        if (world.scoreboard.getObjective('drowned_upg').getScore(player) > highestplayer) {
            highestplayer = world.scoreboard.getObjective('drowned_upg').getScore(player)
        }
    })

    return highestplayer
}

function mob_buffs(entity, chance) {
    if (Math.random() > 1 - chance) {
        let num = Math.floor((Math.random()) * 4)
        if (num == 0) {
            entity.addEffect("strength", 20000000)
        }
        if (num == 1) {
            entity.addEffect("speed", 20000000)
        }
        if (num == 2) {
            entity.addEffect("resistance", 20000000)
        }
        if (num == 3) {
            entity.addEffect("fire_resistance", 20000000)
        }
    }
}






world.afterEvents.entityHurt.subscribe((eventData) => {
    let attacker = eventData.damageSource.damagingEntity
    let victim = eventData.hurtEntity

    x = get_highest_age()

    if (attacker != undefined) {
        if (x == 1) {
            if (attacker.typeId == "minecraft:zombie") {
                if (Math.random() > 0.9) {
                    let num = Math.floor((Math.random()) * 3)
                    if (num == 0) {
                        victim.addEffect("poison", 100)
                    }
                    if (num == 1) {
                        victim.addEffect("weakness", 300)
                    }
                    if (num == 2) {
                        victim.addEffect("slowness", 100)
                    }
                }
            }
        }

        if (x == 2) {
            if (attacker.typeId == "minecraft:zombie") {
                if (Math.random() > 0.8) {
                    let num = Math.floor((Math.random()) * 3)
                    if (num == 0) {
                        victim.addEffect("poison", 100)
                    }
                    if (num == 1) {
                        victim.addEffect("weakness", 300)
                    }
                    if (num == 2)
                        victim.addEffect("slowness", 100)
                }
            }

            if (attacker.typeId == "minecraft:spider") {
                if (Math.random() > 0.8) {
                    let num = Math.floor((Math.random()) * 3)
                    if (num == 0) {
                        victim.addEffect("blindness", 60)
                    }
                    if (num == 1) {
                        victim.addEffect("infested", 300)
                    }
                    if (num == 2)
                        victim.addEffect("slowness", 100)
                }
            }
        }
    }
})

//Thanks HappyCannon :Emoji:
Entity.prototype.testFor = function (options) {
    return (this.runCommand(`testfor @s[hasitem={${'item=' + options.item}${(options.data !== undefined ? ',data=' + options.data : "")}${(options.location !== undefined ? ',location=' + options.location : "")}${(options.quantity !== undefined ? ',quantity=' + options.quantity : "")}${(options.slot !== undefined ? ',slot=' + options.slot : "")}}]`).successCount > 0 ? true : false)
}