import { world, EntityDamageCause, GameMode } from "@minecraft/server";

world.afterEvents.entityHitEntity.subscribe(eventData => {
  const damager = eventData.damagingEntity;
  const target = eventData.hitEntity;
  const { x, y, z } = damager.location;
  const damagerEquippable = damager.getComponent("equippable");
  if (!damagerEquippable) return;
  const itemUsed = damagerEquippable.getEquipment("Mainhand");
  const customWeapons = [
    "nicothekid:blazing_staff",
    "nicothekid:curserer_staff",
    "nicothekid:dripstone_club",
    "nicothekid:magma_club",
    "nicothekid:piglin_fire_staff",
    "nicothekid:piglin_sledge_hammer",
    "nicothekid:powder_staff",
    "nicothekid:scorpion_gauntlet",
    "nicothekid:scorpion_telson",
    "nicothekid:staff_of_the_sea",
    "nicothekid:wooden_club"
  ]
  if (itemUsed === undefined) return;
  if (customWeapons.includes(itemUsed.typeId)) {
    if (itemUsed.typeId === "nicothekid:blazing_staff") {
      if (!target.matches({ excludeFamilies:["blaze", "magmacube", "strider", "fire_bird", "soul_fire_bird", "magma_golem", "nether_wisp", "nether_soul_wisp"] })) {
        const pitchRange = Math.random() * (2.2 - 1.8) + 1.8;
        damager.playSound("mob.ghast.fireball", { pitch: pitchRange, location: damager.location, volume: 0.5 });
        if (target.typeId === "minecraft:blaze") {
          target.applyDamage( 2, { cause: "override", damagingEntity: damager });
          target.applyKnockback( x, z, 0.0, 0.0 );
          target.addEffect("instant_health", 2, {amplifier: 0, showParticles: false });
        }
        else {
          target.applyDamage( 3, { cause: "override", damagingEntity: damager });
          target.applyKnockback( x, z, 0.0, 0.0 );
          target.addEffect("instant_health", 2, {amplifier: 0, showParticles: false });
        }
      } 
      else {
        target.setOnFire( 4, true );
      }
      const itemEnchantable = itemUsed.getComponent("enchantable");
      const unbreakingLevel = itemEnchantable?.getEnchantment("unbreaking")?.level ?? 0;
      const breakChance = 100 / (unbreakingLevel + 1);
      const randomizeChance = Math.random() * 100;
      if (breakChance < randomizeChance) return;
      const itemDurability = itemUsed.getComponent("durability");
      if (!itemDurability) return;
      const itemMaxDurability = itemDurability.maxDurability;
      const itemDurabilityDamage = itemDurability.damage;
      const durabilityCalculation = itemMaxDurability - itemDurabilityDamage;
      let damageAmount = 0;
      if (damager.typeId === "minecraft:player") {
        if (!damager.matches({ gameMode: GameMode.creative })) {
          damageAmount = 3;
        }
        else return;
      }
      else {
        damageAmount = 2;
      };
      const currentDurability = durabilityCalculation - damageAmount;
      if (currentDurability <= 0) {
        damager.playSound("random.break", { pitch: 0.9, location: damager.location, volume: 1 });
        damagerEquippable.setEquipment("Mainhand", null);
      }
      else {
        itemDurability.damage += damageAmount;
        damagerEquippable.setEquipment("Mainhand", itemUsed)
      }
    }
    if (itemUsed.typeId === "nicothekid:curserer_staff") {
      const itemEnchantable = itemUsed.getComponent("enchantable");
      const unbreakingLevel = itemEnchantable?.getEnchantment("unbreaking")?.level ?? 0;
      const breakChance = 100 / (unbreakingLevel + 1);
      const randomizeChance = Math.random() * 100;
      if (breakChance < randomizeChance) return;
      const itemDurability = itemUsed.getComponent("durability");
      if (!itemDurability) return;
      const itemMaxDurability = itemDurability.maxDurability;
      const itemDurabilityDamage = itemDurability.damage;
      const durabilityCalculation = itemMaxDurability - itemDurabilityDamage;
      let damageAmount = 0;
      if (damager.typeId === "minecraft:player") {
        if (!damager.matches({ gameMode: GameMode.creative })) {
          damageAmount = 3;
        }
        else return;
      }
      else {
        damageAmount = 2;
      };
      const currentDurability = durabilityCalculation - damageAmount;
      if (currentDurability <= 0) {
        damager.playSound("random.break", { pitch: 0.9, location: damager.location, volume: 1 });
        damagerEquippable.setEquipment("Mainhand", null);
      }
      else {
        itemDurability.damage += damageAmount;
        damagerEquippable.setEquipment("Mainhand", itemUsed)
      }
    }
    if (itemUsed.typeId === "nicothekid:dripstone_club") {
      const { x, z } = damager.getViewDirection();
      let knockbackChance = Math.random();
      let extraDamageChance = Math.random();
      if (knockbackChance <= 0.15) {
        target.applyKnockback( x, z, 1, 0.1 );
        damager.playSound("game.player.attack.strong", { pitch: 1.2, location: damager.location, volume: 1.2 });
      }
      if (extraDamageChance <= 0.05) {
        target.applyDamage( 2, { cause: "override", damagingEntity: damager });
        target.applyKnockback( x, z, 0.0, 0.0 );
        damager.playSound("game.player.attack.strong", { pitch: 0.9, location: damager.location, volume: 1.2 });
      }
    }
    if (itemUsed.typeId === "nicothekid:magma_club") {
      const { x, z } = damager.getViewDirection();
      let knockbackChance = Math.random();
      let fireChance = Math.random();
      if (knockbackChance <= 0.15) {
        target.applyKnockback( x, z, 1, 0.1 );
        damager.playSound("game.player.attack.strong", { pitch: 1.2, location: damager.location, volume: 1.2 });
      }
      if (fireChance <= 0.05) {
        target.setOnFire( 2, true );
      }
    }
    if (itemUsed.typeId === "nicothekid:piglin_fire_staff") {
      if (!target.matches({ excludeFamilies:[ "blaze", "magmacube", "strider", "fire_bird", "soul_fire_bird", "magma_golem", "nether_wisp", "nether_soul_wisp" ] })) {
        const pitchRange = Math.random() * (2.2 - 1.8) + 1.8;
        damager.playSound("mob.ghast.fireball", { pitch: pitchRange, location: damager.location, volume: 0.5 });
        if (target.typeId === "minecraft:blaze") {
          target.applyDamage( 2, { cause: "override", damagingEntity: damager });
          target.applyKnockback( x, z, 0.0, 0.0 );
          target.addEffect("instant_health", 2, {amplifier: 0, showParticles: false });
        }
        else {
          target.applyDamage( 3, { cause: "override", damagingEntity: damager });
          target.applyKnockback( x, z, 0.0, 0.0 );
          target.addEffect("instant_health", 2, {amplifier: 0, showParticles: false });
        }
      }
      else {
        target.setOnFire( 2, true );
      }
      const itemEnchantable = itemUsed.getComponent("enchantable");
      const unbreakingLevel = itemEnchantable?.getEnchantment("unbreaking")?.level ?? 0;
      const breakChance = 100 / (unbreakingLevel + 1);
      const randomizeChance = Math.random() * 100;
      if (breakChance < randomizeChance) return;
      const itemDurability = itemUsed.getComponent("durability");
      if (!itemDurability) return;
      const itemMaxDurability = itemDurability.maxDurability
      const itemDurabilityDamage = itemDurability.damage
      const durabilityCalculation = itemMaxDurability - itemDurabilityDamage;
      let damageAmount = 0;
      if (damager.typeId === "minecraft:player") {
        if (!damager.matches({ gameMode: GameMode.creative })) {
          damageAmount = 2;
        }
        else return;
      }
      else {
        damageAmount = 1;
      };
      const currentDurability = durabilityCalculation - damageAmount;
      if (currentDurability <= 0) {
        damager.playSound("random.break", { pitch: 0.9, location: damager.location, volume: 1 });
        damagerEquippable.setEquipment("Mainhand", null);
      }
      else {
        itemDurability.damage += damageAmount;
        damagerEquippable.setEquipment("Mainhand", itemUsed)
      }
    }
    if (itemUsed.typeId === "nicothekid:piglin_sledge_hammer") {
      if (!target.matches({ excludeFamilies:[ "golem", "elder_golem", "irongolem", "snowgolem" ] })) {
        target.applyDamage( 5, { cause: "override", damagingEntity: damager });
        target.applyKnockback( x, z, 0.0, 0.0 );
        damager.addEffect("slowness", 80, {amplifier: 2, showParticles: false });
        damager.addEffect("weakness", 80, {amplifier: 4, showParticles: false });
        damager.playSound("item.nicothekid_piglin_sledge_hammer.use", { pitch: 0.3, location: damager.location, volume: 0.8 });
      }
      else {
        damager.addEffect("slowness", 80, {amplifier: 2, showParticles: false });
        damager.addEffect("weakness", 80, {amplifier: 4, showParticles: false });
        damager.playSound("item.nicothekid_piglin_sledge_hammer.use", { pitch: 0.5, location: damager.location, volume: 0.8 });
      }
    }
    if (itemUsed.typeId === "nicothekid:powder_staff") {
      const itemEnchantable = itemUsed.getComponent("enchantable");
      const unbreakingLevel = itemEnchantable?.getEnchantment("unbreaking")?.level ?? 0;
      const breakChance = 100 / (unbreakingLevel + 1);
      const randomizeChance = Math.random() * 100;
      if (breakChance < randomizeChance) return;
      const itemDurability = itemUsed.getComponent("durability");
      if (!itemDurability) return;
      const itemMaxDurability = itemDurability.maxDurability;
      const itemDurabilityDamage = itemDurability.damage;
      const durabilityCalculation = itemMaxDurability - itemDurabilityDamage;
      let damageAmount = 0;
      if (damager.typeId === "minecraft:player") {
        if (!damager.matches({ gameMode: GameMode.creative })) {
          damageAmount = 3;
        }
        else return;
      }
      else {
        damageAmount = 2;
      };
      const currentDurability = durabilityCalculation - damageAmount;
      if (currentDurability <= 0) {
        damager.playSound("random.break", { pitch: 0.9, location: damager.location, volume: 1 });
        damagerEquippable.setEquipment("Mainhand", null);
      }
      else {
        itemDurability.damage += damageAmount;
        damagerEquippable.setEquipment("Mainhand", itemUsed)
      }
    }
    if (itemUsed.typeId === "nicothekid:scorpion_gauntlet") {
      if (target.matches({ excludeFamilies:[ "golem", "elder_golem", "irongolem", "snowgolem" ] })) {
        const pitchRange = Math.random() * (1.2 - 0.8) + 0.8;
        target.addEffect("fatal_poison", 300, {amplifier: 0, showParticles: true });
        damager.playSound("mob.bee.sting", { pitch: pitchRange, location: damager.location, volume: 0.5 });
      }
      else return;
    }
    if (itemUsed.typeId === "nicothekid:scorpion_telson") {
      if (target.matches({ excludeFamilies:[ "golem", "elder_golem", "irongolem", "snowgolem" ] })) {
        let inflictChance = Math.random();
        if (inflictChance <= 0.03) {
          if (damager.typeId === "minecraft:player") {
            target.addEffect("poison", 300, {amplifier: 0, showParticles: true });
            damager.playSound("mob.bee.sting", { pitch: 1.0, location: damager.location, volume: 0.8 });
            if (!damager.matches({ gameMode: GameMode.creative })) {
              const decrementItem = itemUsed.amount == 1 ? undefined : (itemUsed.amount--, itemUsed);
              damagerEquippable.setEquipment("Mainhand", decrementItem)
            }
          }
          else {
            target.addEffect("poison", 300, {amplifier: 0, showParticles: true });
            damager.playSound("mob.bee.sting", { pitch: 1.0, location: damager.location, volume: 0.8 });
            const decrementItem = itemUsed.amount == 1 ? undefined : (itemUsed.amount--, itemUsed);
            damagerEquippable.setEquipment("Mainhand", decrementItem)
          }
        }
      }
      else return;
    }
    if (itemUsed.typeId === "nicothekid:staff_of_the_sea") {
      const pitchRange = Math.random() * (2.4 - 0.8) + 0.8;
      if (!target.matches({ excludeFamilies:[ "blaze", "magmacube", "strider", "enderman", "fire_bird", "soul_fire_bird", "magma_golem", "nether_wisp", "nether_soul_wisp" ] })) {
        if (target.typeId === "minecraft:magma_cube" || target.typeId === "nicothekid:magma_golem") {
          target.applyDamage( 2, { cause: "override", damagingEntity: damager });
          target.applyKnockback( x, z, 0.0, 0.0 );
          damager.playSound("random.fizz", { pitch: pitchRange, location: damager.location, volume: 0.7 });
        }
        else if (target.typeId === "minecraft:strider" || target.typeId === "minecraft:enderman") {
          target.applyDamage( 2, { cause: "override", damagingEntity: damager });
          target.applyKnockback( x, z, 0.0, 0.0 );
        }
        else {
          target.applyDamage( 3, { cause: "override", damagingEntity: damager });
          target.applyKnockback( x, z, 0.0, 0.0 );
          damager.playSound("random.fizz", { pitch: pitchRange, location: damager.location, volume: 0.7 });
        }
      }
      if (target.hasComponent("minecraft:onfire")) {
        target.extinguishFire( true );
        damager.playSound("random.fizz", { pitch: pitchRange, location: damager.location, volume: 0.8 });
      }
      const itemEnchantable = itemUsed.getComponent("enchantable");
      const unbreakingLevel = itemEnchantable?.getEnchantment("unbreaking")?.level ?? 0;
      const breakChance = 100 / (unbreakingLevel + 1);
      const randomizeChance = Math.random() * 100;
      if (breakChance < randomizeChance) return;
      const itemDurability = itemUsed.getComponent("durability");
      if (!itemDurability) return;
      const itemMaxDurability = itemDurability.maxDurability;
      const itemDurabilityDamage = itemDurability.damage;
      const durabilityCalculation = itemMaxDurability - itemDurabilityDamage;
      let damageAmount = 0;
      if (damager.typeId === "minecraft:player") {
        if (!damager.matches({ gameMode: GameMode.creative })) {
          damageAmount = 3;
        }
        else return;
      }
      else {
        damageAmount = 2;
      };
      const currentDurability = durabilityCalculation - damageAmount;
      if (currentDurability <= 0) {
        damager.playSound("random.break", { pitch: 0.9, location: damager.location, volume: 1 });
        damagerEquippable.setEquipment("Mainhand", null);
      }
      else {
        itemDurability.damage += damageAmount;
        damagerEquippable.setEquipment("Mainhand", itemUsed)
      }
    }
    if (itemUsed.typeId === "nicothekid:wooden_club") {
      const { x, z } = damager.getViewDirection();
      let knockbackChance = Math.random();
      if (knockbackChance <= 0.15) {
        target.applyKnockback( x, z, 1, 0.1 );
        damager.playSound("game.player.attack.strong", { pitch: 1.2, location: damager.location, volume: 0.8 });
      }
    }
  }
  else return;
});
