import { world, system, SystemAfterEvents, TicksPerSecond, ItemStack } from "@minecraft/server"

//hoglin, zoglin, and ender dragon code, sometimes launching player into the sky
world.afterEvents.entityHitEntity.subscribe(event => {
    const damager = event.damagingEntity
    const target = event.hitEntity
    const chance = Math.random()
    if ((damager?.typeId === 'minecraft:hoglin' || damager?.typeId === 'minecraft:zoglin' || damager?.typeId === 'minecraft:ender_dragon') && target?.typeId === 'minecraft:player' && chance < 0.5) {
        target.applyKnockback(0, 0, 0, 1.5)
    }
})
//entity can get levitation when melee attacked (any entity attack entity, applies to player)
world.afterEvents.entityHitEntity.subscribe(event => {
    const target = event.hitEntity
    const chance = Math.random()
    if (chance < 0.05) {
        target.addEffect('levitation', TicksPerSecond * 3, { showParticles: true, amplifier: 3 })
    }
})

//pig, villager and creeper sometimes spawning a lightning when getting hit
world.afterEvents.entityHurt.subscribe(event => {
    const player = event.damageSource.damagingEntity?.typeId === 'minecraft:player'
    const target = event.hurtEntity
    const chance = Math.random()
    const entity = event.damageSource.damagingEntity
    if (player && entity?.typeId !== "minecraft:fireball" && (target?.typeId === 'minecraft:pig' || target?.typeId === 'minecraft:villager_v2' || target?.typeId === 'minecraft:creeper') && chance < 0.2) {
        target.dimension.spawnEntity('lightning_bolt', target.location)
    }
})

//when passive mob is hit, it can either explode or get launched into the sky
world.afterEvents.entityHitEntity.subscribe(event => {
    const entity = event.hitEntity
    const allay = event.hitEntity.typeId === 'minecraft:allay'
    const armadillo = event.hitEntity.typeId === 'minecraft:armadillo'
    const axolotl = event.hitEntity.typeId === 'minecraft:axolotl'
    const bat = event.hitEntity.typeId === 'minecraft:bat'
    const camel = event.hitEntity.typeId === 'minecraft:camel'
    const cat = event.hitEntity.typeId === 'minecraft:cat'
    const chicken = event.hitEntity.typeId === 'minecraft:chicken'
    const cod = event.hitEntity.typeId === 'minecraft:cod'
    const cow = event.hitEntity.typeId === 'minecraft:cow'
    const donkey = event.hitEntity.typeId === 'minecraft:donkey'
    const frog = event.hitEntity.typeId === 'minecraft:frog'
    const glow = event.hitEntity.typeId === 'minecraft:glow_squid'
    const horse = event.hitEntity.typeId === 'minecraft:horse'
    const moosh = event.hitEntity.typeId === 'minecraft:mooshroom'
    const mule = event.hitEntity.typeId === 'minecraft:mule'
    const ocelot = event.hitEntity.typeId === 'minecraft:ocelot'
    const parrot = event.hitEntity.typeId === 'minecraft:parrot'
    const pig = event.hitEntity.typeId === 'minecraft:pig'
    const puffer = event.hitEntity.typeId === 'minecraft:pufferfish'
    const rabbit = event.hitEntity.typeId === 'minecraft:rabbit'
    const salmon = event.hitEntity.typeId === 'minecraft:salmon'
    const sheep = event.hitEntity.typeId === 'minecraft:sheep'
    const skele = event.hitEntity.typeId === 'minecraft:skeleton_horse'
    const sniff = event.hitEntity.typeId === 'minecraft:sniffer'
    const snowGolem = event.hitEntity.typeId === 'minecraft:snow_golem'
    const squid = event.hitEntity.typeId === 'minecraft:squid'
    const strider = event.hitEntity.typeId === 'minecraft:strider'
    const tadpole = event.hitEntity.typeId === 'minecraft:tadpole'
    const tropical = event.hitEntity.typeId === 'minecraft:tropicalfish'
    const turtle = event.hitEntity.typeId === 'minecraft:turtle'

    const player = event.damagingEntity.typeId === 'minecraft:player'
    const chance = Math.random()
    if ((allay || armadillo || axolotl || bat || camel || cat || chicken || cod || cow || donkey || frog || glow || horse || moosh || mule || ocelot || parrot || pig || puffer || rabbit || salmon || sheep || skele || sniff || snowGolem || squid || strider || tadpole || tropical || turtle) && player && chance < 0.1) {
        entity.dimension.createExplosion(entity.location, 5, { allowUnderwater: false, causesFire: true })
    }
})
world.afterEvents.entityHitEntity.subscribe(event => {
    const entity = event.hitEntity
    const armadillo = event.hitEntity.typeId === 'minecraft:armadillo'
    const camel = event.hitEntity.typeId === 'minecraft:camel'
    const cow = event.hitEntity.typeId === 'minecraft:cow'
    const donkey = event.hitEntity.typeId === 'minecraft:donkey'
    const horse = event.hitEntity.typeId === 'minecraft:horse'
    const moosh = event.hitEntity.typeId === 'minecraft:mooshroom'
    const mule = event.hitEntity.typeId === 'minecraft:mule'
    const rabbit = event.hitEntity.typeId === 'minecraft:rabbit'
    const skele = event.hitEntity.typeId === 'minecraft:skeleton_horse'
    const sniff = event.hitEntity.typeId === 'minecraft:sniffer'
    const snowGolem = event.hitEntity.typeId === 'minecraft:snow_golem'
    const strider = event.hitEntity.typeId === 'minecraft:strider'

    const player = event.damagingEntity.typeId === 'minecraft:player'
    const chance = Math.random()
    if ((armadillo || camel || cow || donkey || horse || moosh || mule || rabbit || skele || sniff || snowGolem || strider) && player && chance < 0.1) {
        entity.applyKnockback(0, 0, 0, 10)
    }
})

//when hitting a villager, player might get nausia, darkness, or get stunned>=D
world.afterEvents.entityHitEntity.subscribe(event => {
    const villager = event.hitEntity?.typeId === 'minecraft:villager_v2'
    const player = event.damagingEntity?.typeId === 'minecraft:player'
    const damager = event.damagingEntity
    const chance = Math.random()
    if (player && villager) {
        damager.dimension.playSound("entity.mad_villager", damager.location, { volume: 1, pitch: 1 })
    }
    if (player && villager && chance < 0.07) {

        damager.addEffect('darkness', TicksPerSecond * 15, { showParticles: false })
        damager.addEffect('nausea', TicksPerSecond * 15, { showParticles: false })
        damager.addEffect('slowness', TicksPerSecond * 5, { showParticles: false, amplifier: 1 })
    }
})

//when hitting a wandering trader, player enters a boss battle >=D
world.afterEvents.entityHitEntity.subscribe(event => {
    const trader = event.hitEntity?.typeId === 'minecraft:wandering_trader'
    const player = event.damagingEntity?.typeId === 'minecraft:player'
    const target = event.hitEntity
    const damager = event.damagingEntity
    if (player && trader) {
        damager.dimension.playSound("entity.mad_villager", damager.location, { volume: 1, pitch: 1 })
    }
    if (player && trader) {
        damager.runCommand('effect @p levitation 1 200 true')
        damager.runCommand('gamemode s @p')

        target.addEffect('fire_resistance', TicksPerSecond * 1000000, { showParticles: false, amplifier: 255 })
        target.addEffect('water_breathing', TicksPerSecond * 1000000, { showParticles: false, amplifier: 255 })
        target.addEffect('slow_falling', TicksPerSecond * 1000000, { showParticles: false, amplifier: 1 })
        target.addEffect('speed', TicksPerSecond * 1000000, { showParticles: false, amplifier: 3 })
        target.addEffect('health_boost', TicksPerSecond * 1000000, { showParticles: false, amplifier: 9 })

        if (Math.round(Math.random() * 3) == 1) {
            damager.addEffect('darkness', TicksPerSecond * 10, { showParticles: false })
        }
        if (Math.round(Math.random() * 3) == 1) {
            damager.addEffect('blindness', TicksPerSecond * 10, { showParticles: false })
        }
        if (Math.round(Math.random() * 5) == 1) {
            damager.addEffect('nausea', TicksPerSecond * 10, { showParticles: false })
        }
        if (Math.round(Math.random() * 5) == 1) {
            damager.addEffect('bad_omen', TicksPerSecond * 10, { showParticles: false, amplifier: 1 })
        }
        if (Math.round(Math.random() * 5) == 1) {
            damager.addEffect('weakness', TicksPerSecond * 10, { showParticles: false, amplifier: 1 })
        }
        if (Math.round(Math.random() * 5) == 1) {
            damager.addEffect('infested', TicksPerSecond * 10, { showParticles: false, amplifier: 1 })
        }
        if (Math.round(Math.random() * 5) == 1) {
            damager.addEffect('oozing', TicksPerSecond * 10, { showParticles: false, amplifier: 1 })
        }
        if (Math.round(Math.random() * 5) == 1) {
            damager.addEffect('slowness', TicksPerSecond * 5, { showParticles: false, amplifier: 1 })
        }
        if (Math.round(Math.random() * 5) == 1) {
            damager.addEffect('fatal_poison', TicksPerSecond * 5, { showParticles: false, amplifier: 255 })
        }
        if (Math.round(Math.random() * 5) == 1) {
            damager.addEffect('hunger', TicksPerSecond * 5, { showParticles: false, amplifier: 255 })
        }
        if (Math.round(Math.random() * 5) == 1) {
            damager.addEffect('wither', TicksPerSecond * 5, { showParticles: false, amplifier: 255 })
        }
        if (Math.round(Math.random() * 5) == 1) {
            damager.addEffect('instant_damage', TicksPerSecond * 1, { showParticles: false, amplifier: 5 })
        }
        //wandering trader effects
        if (Math.round(Math.random() * 3) == 1) {
            target.addEffect('levitation', TicksPerSecond * 1, { showParticles: true, amplifier: 1 })
        }
        if (Math.round(Math.random() * 3) == 1) {
            target.addEffect('regeneration', TicksPerSecond * 2, { showParticles: true, amplifier: 4 })
        }
        if (Math.round(Math.random() * 3) == 1) {
            target.addEffect('resistance', TicksPerSecond * 1, { showParticles: true, amplifier: 255 })
        }
    }
})

//if wandering trader dies
world.afterEvents.entityDie.subscribe(event => {
    const entity = event.deadEntity
    const source = event.damageSource?.damagingEntity?.typeId === 'minecraft:player'
    if (entity?.typeId === 'minecraft:wandering_trader' && source) {
        entity.runCommand('give @p stick')
        entity.runCommand('give @p netherite_hoe')
        entity.runCommand('give @p netherite_boots')
        entity.runCommand('give @p netherite_chestplate')
        entity.runCommand('give @p netherite_helmet')
        entity.runCommand('give @p netherite_leggings')
        entity.runCommand('give @p totem_of_undying')
        entity.runCommand('give @p totem_of_undying')
        entity.runCommand('give @p totem_of_undying')
        entity.runCommand('give @p omni_science:bagel')
        entity.runCommand('give @p omni_science:poop 64')
        entity.runCommand('give @p lodestone 64')
    }
})

//when hitting a witch, player enters a boss battle >=D
world.afterEvents.entityHitEntity.subscribe(event => {
    const witch = event.hitEntity?.typeId === 'minecraft:witch'
    const player = event.damagingEntity?.typeId === 'minecraft:player'
    const target = event.hitEntity
    const damager = event.damagingEntity
    if (player && witch) {
        target.runCommand('gamemode s @p')
        target.addEffect('fire_resistance', TicksPerSecond * 1000000, { showParticles: false, amplifier: 255 })
        target.addEffect('water_breathing', TicksPerSecond * 1000000, { showParticles: false, amplifier: 255 })
        target.addEffect('slow_falling', TicksPerSecond * 1000000, { showParticles: false, amplifier: 1 })
        target.addEffect('health_boost', TicksPerSecond * 1000000, { showParticles: false, amplifier: 9 })
        target.addEffect('regeneration', TicksPerSecond * 2, { showParticles: true, amplifier: 2 })

        if (Math.round(Math.random() * 5) == 1) {
            damager.addEffect('weakness', TicksPerSecond * 10, { showParticles: false, amplifier: 1 })
        }
        if (Math.round(Math.random() * 5) == 1) {
            damager.addEffect('infested', TicksPerSecond * 10, { showParticles: false, amplifier: 1 })
        }
        if (Math.round(Math.random() * 5) == 1) {
            damager.addEffect('oozing', TicksPerSecond * 10, { showParticles: false, amplifier: 1 })
        }
        if (Math.round(Math.random() * 5) == 1) {
            damager.addEffect('slowness', TicksPerSecond * 5, { showParticles: false, amplifier: 3 })
        }
        if (Math.round(Math.random() * 5) == 1) {
            damager.addEffect('poison', TicksPerSecond * 5, { showParticles: false, amplifier: 255 })
        }
        if (Math.round(Math.random() * 5) == 1) {
            damager.addEffect('fatal_poison', TicksPerSecond * 5, { showParticles: false, amplifier: 255 })
        }
        if (Math.round(Math.random() * 5) == 1) {
            damager.addEffect('hunger', TicksPerSecond * 5, { showParticles: false, amplifier: 255 })
        }
        if (Math.round(Math.random() * 5) == 1) {
            damager.addEffect('nausea', TicksPerSecond * 5, { showParticles: false, amplifier: 255 })
        }
        if (Math.round(Math.random() * 3) == 1) {
            damager.runCommand('summon evocation_fang')
        }
        if (Math.round(Math.random() * 3) == 1) {
            damager.runCommand('setblock ~~~ fire')
        }
        if (Math.round(Math.random() * 5) == 1) {
            damager.runCommand('damage @p 5 drowning')
        }
        if (Math.round(Math.random() * 3) == 1) {
            damager.runCommand('playsound mob.enderdragon.death @a ~~~ 1 1')
        }
        if (Math.round(Math.random() * 2) == 1) {
            damager.runCommand('playsound mob.ravager.roar @a ~~~ 1 1')
        }
        if (Math.round(Math.random() * 2) == 1) {
            damager.runCommand('playsound mob.wither.death @a ~~~ 1 1.69')
        }
        if (Math.round(Math.random() * 2) == 1) {
            damager.runCommand('playsound mob.ghast.scream @a ~~~ 1 0.56')
        }
        if (Math.round(Math.random() * 2) == 1) {
            damager.runCommand('playsound mob.ghast.affectionate_scream @a ~~~ 1 1')
        }
        //witch effects
        if (Math.round(Math.random() * 3) == 1) {
            target.addEffect('levitation', TicksPerSecond * 1, { showParticles: true, amplifier: 1 })
        }
        if (Math.round(Math.random() * 3) == 1) {
            target.addEffect('resistance', TicksPerSecond * 1, { showParticles: true, amplifier: 255 })
        }
        if (Math.round(Math.random() * 3) == 1) {
            target.addEffect('speed', TicksPerSecond * 2, { showParticles: true, amplifier: 255 })
        }
        if (Math.round(Math.random() * 3) == 1) {
            target.addEffect('invisibility', TicksPerSecond * 2, { showParticles: true, amplifier: 255 })
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('fill ~~~ ~10 ~0 ~10 fire')
        }
    }
})

//if witch dies
world.afterEvents.entityDie.subscribe(event => {
    const entity = event.deadEntity
    const source = event.damageSource?.damagingEntity?.typeId === 'minecraft:player'
    if (entity?.typeId === 'minecraft:witch' && source) {
        entity.runCommand('effect @a darkness 1 255 true')
        entity.runCommand('give @a golden_apple 15')
        entity.runCommand('give @p enchanted_golden_apple 10')
        entity.runCommand('give @p blaze_rod 10')
        entity.runCommand('give @p ender_pearl 10')
        entity.runCommand('give @p nether_star')
        entity.runCommand('give @p glowstone 20')
        entity.runCommand('give @a stick')
        entity.runCommand('give @a glass_bottle 6')
        entity.runCommand('give @a glowstone_dust 6')
        entity.runCommand('give @a redstone 15')
        entity.runCommand('give @a spider_eye 6')
        entity.runCommand('give @a gunpowder 20')
        entity.runCommand('give @a sugar 6')
        entity.runCommand('give @a dragon_breath 20')
    }
})

//when hitting an evoker, player enters a boss battle >=D
world.afterEvents.entityHitEntity.subscribe(event => {
    const evoker = event.hitEntity?.typeId === 'minecraft:evocation_illager'
    const player = event.damagingEntity?.typeId === 'minecraft:player'
    const target = event.hitEntity
    const damager = event.damagingEntity
    if (player && evoker) {

        //does not include instant_health and absorption
        damager.runCommand('gamemode a @s')
        target.addEffect('fire_resistance', TicksPerSecond * 1000000, { showParticles: false, amplifier: 255 })
        target.addEffect('water_breathing', TicksPerSecond * 1000000, { showParticles: false, amplifier: 255 })
        target.addEffect('haste', TicksPerSecond * 1000000, { showParticles: false, amplifier: 255 })
        target.addEffect('night_vision', TicksPerSecond * 1000000, { showParticles: false, amplifier: 255 })
        target.addEffect('saturation', TicksPerSecond * 1000000, { showParticles: false, amplifier: 255 })
        target.addEffect('conduit_power', TicksPerSecond * 1000000, { showParticles: false, amplifier: 255 })
        target.addEffect('bad_omen', TicksPerSecond * 1000000, { showParticles: false, amplifier: 255 })
        target.addEffect('strength', TicksPerSecond * 1000000, { showParticles: false, amplifier: 2 })
        target.addEffect('jump_boost', TicksPerSecond * 1000000, { showParticles: false, amplifier: 1 })
        target.addEffect('slow_falling', TicksPerSecond * 1000000, { showParticles: false, amplifier: 1 })
        target.addEffect('health_boost', TicksPerSecond * 1000000, { showParticles: false, amplifier: 9 })
        target.addEffect('resistance', TicksPerSecond * 1000000, { showParticles: true, amplifier: 1 })

        damager.addEffect('infested', TicksPerSecond * 10, { showParticles: false, amplifier: 1 })
        damager.addEffect('oozing', TicksPerSecond * 10, { showParticles: false, amplifier: 1 })
        damager.addEffect('weaving', TicksPerSecond * 10, { showParticles: false, amplifier: 1 })

        if (Math.round(Math.random() * 3) == 1) {
            damager.runCommand('summon evocation_fang')
        }
        if (Math.round(Math.random() * 3) == 1) {
            damager.runCommand('setblock ~~~ fire')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('summon ravager')
        }
        if (Math.round(Math.random() * 5) == 1) {
            for (let i = 0; i < 5; i++) {
                target.runCommand('summon vex')
            }
        }
        if (Math.round(Math.random() * 3) == 1) {
            target.runCommand('playanimation @e [type = vex] animation.ghast.scale a 600')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('summon witch')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('summon vindicator')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('summon pillager')
        }
        //evoker effects
        if (Math.round(Math.random() * 3) == 1) {
            target.addEffect('levitation', TicksPerSecond * 1, { showParticles: true, amplifier: 1 })
        }
        if (Math.round(Math.random() * 3) == 1) {
            target.addEffect('speed', TicksPerSecond * 10, { showParticles: true, amplifier: 5 })
        }
        if (Math.round(Math.random() * 3) == 1) {
            target.addEffect('regeneration', TicksPerSecond * 1, { showParticles: true, amplifier: 5 })
        }
    }
})

//if evoker dies
world.afterEvents.entityDie.subscribe(event => {
    const entity = event.deadEntity
    const source = event.damageSource?.damagingEntity?.typeId === 'minecraft:player'
    if (entity?.typeId === 'minecraft:evocation_illager' && source) {
        entity.runCommand('effect @a village_hero 1 255 true')
        entity.runCommand('gamemode s @a')
        entity.runCommand('give @p enchanted_golden_apple 64')
        entity.runCommand('give @p netherite_block 64')
        entity.runCommand('give @p diamond_block 64')
        entity.runCommand('give @p iron_block 64')
        entity.runCommand('give @p gold_block 64')
        entity.runCommand('give @p emerald_block 64')
        entity.runCommand('give @p blaze_rod 10')
        entity.runCommand('give @p ender_pearl 10')
        entity.runCommand('give @p totem_of_undying 10')
        entity.runCommand('give @p netherite_sword')
        entity.runCommand('give @p netherite_upgrade_smithing_template')
        entity.runCommand('give @p smithing_table 2')
        entity.runCommand('give @p undyed_shulker_box 3')
    }
})

//if player hits a blaze
world.afterEvents.entityHitEntity.subscribe(event => {
    const fireGolem = event.hitEntity?.typeId === 'minecraft:blaze'
    const player = event.damagingEntity?.typeId === 'minecraft:player'

    const hitPlayer = event.hitEntity?.typeId === 'minecraft:player'
    const hitterBlaze = event.damagingEntity?.typeId === 'minecraft:blaze'
    const target = event.hitEntity
    const damager = event.damagingEntity
    if (player && fireGolem) {
        damager.runCommand('setblock ~~~ fire')

        target.addEffect('water_breathing', TicksPerSecond * 1000000, { showParticles: false, amplifier: 255 })
        target.addEffect('speed', TicksPerSecond * 1000000, { showParticles: false, amplifier: 5 })
        target.addEffect('health_boost', TicksPerSecond * 1000000, { showParticles: false, amplifier: 4 })
        target.addEffect('strength', TicksPerSecond * 1000000, { showParticles: false, amplifier: 2 })

        if (Math.round(Math.random() * 3) == 1) {
            target.addEffect('regeneration', TicksPerSecond * 10, { showParticles: true, amplifier: 3 })
        }
    }
    //if blaze hits a player
    if (hitPlayer && hitterBlaze) {
        target.runCommand('fill ~~~ ~3 ~0 ~3 fire')

        damager.addEffect('water_breathing', TicksPerSecond * 1000000, { showParticles: false, amplifier: 255 })
        damager.addEffect('speed', TicksPerSecond * 1000000, { showParticles: false, amplifier: 5 })
        damager.addEffect('health_boost', TicksPerSecond * 1000000, { showParticles: false, amplifier: 4 })
        target.addEffect('strength', TicksPerSecond * 1000000, { showParticles: false, amplifier: 2 })

        if (Math.round(Math.random() * 3) == 1) {
            damager.addEffect('regeneration', TicksPerSecond * 10, { showParticles: true, amplifier: 3 })
        }
    }
})

//if player hits a breeze
world.afterEvents.entityHitEntity.subscribe(event => {
    const windGolem = event.hitEntity?.typeId === 'minecraft:breeze'
    const player = event.damagingEntity?.typeId === 'minecraft:player'

    const target = event.hitEntity
    const damager = event.damagingEntity
    if (player && windGolem) {
        damager.runCommand('effect @p levitation 1 50 true')

        target.addEffect('water_breathing', TicksPerSecond * 1000000, { showParticles: false, amplifier: 255 })
        target.addEffect('speed', TicksPerSecond * 1000000, { showParticles: false, amplifier: 255 })
        target.addEffect('health_boost', TicksPerSecond * 1000000, { showParticles: false, amplifier: 4 })

        if (Math.round(Math.random() * 3) == 1) {
            target.addEffect('regeneration', TicksPerSecond * 10, { showParticles: true, amplifier: 3 })
        }
    }
})

//if player hits an enderman
world.afterEvents.entityHitEntity.subscribe(event => {
    const enderMan = event.hitEntity?.typeId === 'minecraft:enderman'
    const player = event.damagingEntity?.typeId === 'minecraft:player'

    const hitPlayer = event.hitEntity?.typeId === 'minecraft:player'
    const hitterEnd = event.damagingEntity?.typeId === 'minecraft:enderman'
    const target = event.hitEntity
    const damager = event.damagingEntity
    if (player && enderMan) {
        target.runCommand('setblock ~~~ fire')

        target.addEffect('fire_resistance', TicksPerSecond * 1000000, { showParticles: false, amplifier: 255 })
        target.addEffect('speed', TicksPerSecond * 1000000, { showParticles: false, amplifier: 255 })
    }
    //if enderman hits a player
    if (hitPlayer && hitterEnd) {
        target.runCommand('setblock ~~~ fire')
        damager.addEffect('fire_resistance', TicksPerSecond * 1000000, { showParticles: false, amplifier: 255 })
        damager.addEffect('speed', TicksPerSecond * 1000000, { showParticles: false, amplifier: 255 })
    }
})

//if player hits a shulker
world.afterEvents.entityHitEntity.subscribe(event => {
    const shulk = event.hitEntity?.typeId === 'minecraft:shulker'
    const player = event.damagingEntity?.typeId === 'minecraft:player'

    const damager = event.damagingEntity
    const target = event.hitEntity
    if (player && shulk) {
        target.addEffect('poison', TicksPerSecond * 1, { showParticles: true, amplifier: 255 })

        if (Math.round(Math.random() * 3) == 1) {
            damager.runCommand('summon evocation_fang')
        }
        if (Math.round(Math.random() * 3) == 1) {
            damager.runCommand('summon snowball')
        }
        if (Math.round(Math.random() * 3) == 1) {
            damager.runCommand('summon wind_charge_projectile')
        }
        target.runCommandAsync('playanimation @e [type = wandering_trader] animation.ghast.scale a 600')
        target.runCommandAsync('playanimation @e [type = witch] animation.ghast.scale a 600')
        target.runCommandAsync('playanimation @e [type = evocation_illager] animation.ghast.scale a 600')
    }
})

//if shulker dies
world.afterEvents.entityDie.subscribe(event => {
    const entity = event.deadEntity
    const source = event.damageSource?.damagingEntity?.typeId === 'minecraft:player'

    if (entity?.typeId === 'minecraft:shulker' && source) {
        entity.runCommand('summon shulker')
        entity.runCommand('summon shulker')
        entity.runCommand('summon shulker')
        entity.runCommand('summon evocation_illager')
        entity.runCommand('summon witch')
        entity.runCommand('summon wandering_trader')
        entity.runCommand('give @p command_block_minecart')
    }
})

//if player hits ender dragon
world.afterEvents.entityHitEntity.subscribe(event => {
    const dragon = event.hitEntity?.typeId === 'minecraft:ender_dragon'
    const player = event.damagingEntity?.typeId === 'minecraft:player'

    const target = event.hitEntity
    if (player && dragon) {
        target.runCommand('gamemode s @a')
        target.runCommand('time set night')
        target.runCommand('difficulty hard')
        target.runCommand('weather thunder')

        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('effect @p levitation 1 50 true')
            target.runCommand('camerashake add @p 4 1 rotational')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('setblock ~~~ fire')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('setblock ~~~ lava')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('setblock ~~~ magma')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('setblock ~~~ dirt')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('setblock ~~~ end_portal')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('damage @p 5 sonic_boom')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('damage @p 5 fireworks')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('damage @p 5 fire_tick entity @r')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('damage @p 5 fall')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('damage @p 5 entity_attack entity @r')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('damage @p 5 entity_explosion entity @r')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('damage @p 5 fly_into_wall')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('damage @p 5 freezing')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('damage @p 5 magic')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('damage @p 5 lava')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('damage @p 5 block_explosion')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('damage @p 5 wither')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('damage @p 5 fire')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('damage @p 5 stalagmite')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('damage @p 5 stalactite')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('damage @p 5 void')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('damage @p 5 lightning')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('damage @p 5 drowning')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('damage @p 5 anvil')
        }
        if (Math.round(Math.random() * 5) == 1) {
            target.runCommand('damage @p 5 starve')
        }
    }
})

//if player kills ender dragon
world.afterEvents.entityDie.subscribe(event => {
    const entity = event.deadEntity
    const source = event.damageSource?.damagingEntity?.typeId === 'minecraft:player'
    if (entity?.typeId === 'minecraft:ender_dragon' && source) {
        entity.runCommand('fog @a remove hi')
        entity.runCommand('time set day')
        entity.runCommand('weather clear')
    }
})

//if a player hits a piglin
world.afterEvents.entityHitEntity.subscribe(event => {
    const piglin = event.hitEntity?.typeId === 'minecraft:piglin'
    const piglinBrute = event.hitEntity?.typeId === 'minecraft:piglin_brute'
    const zomPig = event.hitEntity?.typeId === 'minecraft:zombie_pigman'
    const player = event.damagingEntity?.typeId === 'minecraft:player'
    const target = event.hitEntity
    //const damager = event.damagingEntity
    if (player && piglin) {
        target.runCommand('summon piglin')
    }
    if (player && piglinBrute) {
        target.runCommand('summon piglin_brute')
    }
    if (player && zomPig) {
        target.runCommand('summon zombie_pigman')
    }
})

//if specific nether mobs die
world.afterEvents.entityDie.subscribe(event => {
    const entity = event.deadEntity
    const source = event.damageSource?.damagingEntity?.typeId === 'minecraft:player'
    if (entity?.typeId === 'minecraft:hoglin' && source) {
        entity.runCommand('summon zoglin')
    }
    if (entity?.typeId === 'minecraft:zoglin' && source) {
        entity.runCommand('summon hoglin')
    }
    if (entity?.typeId === 'minecraft:magma_cube' && source) {
        entity.runCommand('summon lightning_bolt')
    }
    if (entity?.typeId === 'minecraft:ghast' && source) {
        entity.runCommand('summon ghast')
        entity.runCommand('summon ghast')
    }
    if (entity?.typeId === 'minecraft:wither_skeleton' && source) {
        entity.runCommand('summon skeleton')
        entity.runCommand('summon skeleton')
    }
})



//entities on fire leave a fire trail behind them
system.runInterval(() => {
    world.getDimension('overworld').getEntities().forEach(
        (data) => {
            const onFire = data.hasComponent('minecraft:onfire')
            const { x, y, z } = data.location
            if (onFire) {
                data.runCommand(`fill ${x} ${y} ${z} ${x} ${y} ${z} fire [] replace air`)
            }
        }
    )
}, 20)

//tnt phantom hitting a player
world.afterEvents.entityHitEntity.subscribe(event => {
    const tnt_phantom = event.damagingEntity.typeId === 'omni_science:tnt_phantom'
    const damager = event.damagingEntity
    if (tnt_phantom) {
        damager.dimension.createExplosion(damager.location, 5, { allowUnderwater: true, causesFire: true })
    }
})

//mecha giga steve
world.afterEvents.entityHitEntity.subscribe((data) => {
    let player = data.hitEntity
    let mechasteve = data.damagingEntity
    let knockback = mechasteve.getViewDirection()

    if (mechasteve.typeId == "omni_science:mecha_giga_steve") {
        player.applyKnockback(knockback.x, knockback.z, 3, 1.6)
    }
})
world.afterEvents.entityDie.subscribe((data) => {
    try {
        const mechasteve = data?.deadEntity;
        const dim = mechasteve?.dimension;
        const location = mechasteve?.location;

        if (mechasteve.typeId === "omni_science:mecha_giga_steve") {
            dim.createExplosion(location, 105, { causesFire: false });
            mechasteve.runCommandAsync(`playsound mob.wither.death @a ~ ~ ~ 100 0.7`)
        }
    } catch { }
})
//entity milking
world.afterEvents.itemUse.subscribe(event => {
    const item = event.itemStack
    const player = event.source
    const entities = player.getEntitiesFromViewDirection({ maxDistance: 7 }).forEach(event => {
        const entity = event.entity
        const inventory = player.getComponent('inventory').container
        if ((entity.typeId !== 'minecraft:cow' || entity.typeId !== 'minecraft:goat') && item?.typeId === 'minecraft:bucket') {
            player.dimension.playSound('mob.cow.milk', player.location)
            player.runCommand(`clear @s bucket 0 1`)
            if (inventory.emptySlotsCount > 1) {
                inventory.addItem(new ItemStack('minecraft:milk_bucket', 1))
            }
            else if (inventory.emptySlotsCount === 0) {
                player.dimension.spawnItem(new ItemStack('minecraft:milk_bucket', 1), player.location)
            }
        }
    })
})