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

system.beforeEvents.watchdogTerminate.subscribe(data => {
    data.cancel = true;
});

world.afterEvents.itemCompleteUse.subscribe(eventData => {
  const player = eventData.source;
  const itemUsed = eventData.itemStack;
	const customFoods = [
    "nicothekid:amethyst_apple",
    "nicothekid:apple_pie",
    "nicothekid:bamboo_tumbler_chocolate",
    "nicothekid:bamboo_tumbler_glow_berry",
    "nicothekid:bamboo_tumbler_milk",
    "nicothekid:bamboo_tumbler_sweet_berry",
    "nicothekid:blazing_apple",
    "nicothekid:cactus_stew",
    "nicothekid:carrot_soup",
    "nicothekid:cheese",
    "nicothekid:cheese_bread",
    "nicothekid:chocolate_bar",
    "nicothekid:crimson_stew",
    "nicothekid:diamond_apple",
    "nicothekid:egg_sandwich",
    "nicothekid:emerald_apple",
    "nicothekid:glow_berry_cookie",
    "nicothekid:glow_berry_pie",
    "nicothekid:glow_lichen_stew",
    "nicothekid:iron_apple",
    "nicothekid:jar_chocolate_full",
    "nicothekid:jar_chocolate_half",
    "nicothekid:jar_glow_berry_juice_full",
    "nicothekid:jar_glow_berry_juice_half",
    "nicothekid:jar_honey_full",
    "nicothekid:jar_honey_half",
    "nicothekid:jar_milk_full",
    "nicothekid:jar_milk_half",
    "nicothekid:jar_sweet_berry_juice_full",
    "nicothekid:jar_sweet_berry_juice_half",
    "nicothekid:lapis_lazuli_apple",
    "nicothekid:netherite_apple",
    "nicothekid:pea_stew",
    "nicothekid:potato_soup",
    "nicothekid:pumpkin_slice",
    "nicothekid:seagrass_stew",
    "nicothekid:soup",
    "nicothekid:sweet_berry_cookie",
    "nicothekid:sweet_berry_pie",
    "nicothekid:warped_stew"
  ]
	if (customFoods.includes(itemUsed.typeId)) {
    if (itemUsed.typeId === "nicothekid:amethyst_apple") {
      player.addEffect("night_vision", 1200, {amplifier: 0, showParticles: true })
      player.addEffect("regeneration", 100, {amplifier: 1, showParticles: true })
    }
    if (itemUsed.typeId === "nicothekid:apple_pie") {
      player.addEffect("instant_health", 200, {amplifier: 1, showParticles: true })
    }
    if (itemUsed.typeId === "nicothekid:bamboo_tumbler_chocolate") {
      player.removeEffect("slowness")
    }
    if (itemUsed.typeId === "nicothekid:bamboo_tumbler_glow_berry") {
      player.removeEffect("blindness")
    }
    if (itemUsed.typeId === "nicothekid:bamboo_tumbler_milk") {
      player.removeEffect("nausea")
      player.removeEffect("fatal_poison")
      player.removeEffect("levitation")
      player.removeEffect("poison")
      player.removeEffect("weakness")
    }
    if (itemUsed.typeId === "nicothekid:bamboo_tumbler_sweet_berry") {
      player.removeEffect("hunger")
    }
    if (itemUsed.typeId === "nicothekid:blazing_apple") {
      player.addEffect("fire_resistance", 1200, {amplifier: 0, showParticles: true })
      player.addEffect("regeneration", 100, {amplifier: 1, showParticles: true })
    }
    if (itemUsed.typeId === "nicothekid:cactus_stew") {
      let nauseaChance = Math.random();
      if (nauseaChance <= 0.5) {
        player.addEffect("nausea", 300, {amplifier: 0, showParticles: true })
      }
    }
    if (itemUsed.typeId === "nicothekid:carrot_soup") {
      player.removeEffect("hunger")
      player.removeEffect("nausea")
    }
    if (itemUsed.typeId === "nicothekid:cheese") {
      let nauseaChance = Math.random();
      if (nauseaChance <= 0.1) {
        player.addEffect("nausea", 300, {amplifier: 0, showParticles: true })
      }
    }
    if (itemUsed.typeId === "nicothekid:cheese_bread") {
      let regenerationChance = Math.random();
      if (regenerationChance <= 0.05) {
        player.addEffect("regeneration", 200, {amplifier: 0, showParticles: true })
      }
    }
    if (itemUsed.typeId === "nicothekid:chocolate_bar") {
      let speedChance = Math.random();
      if (speedChance <= 0.5) {
        player.addEffect("speed", 400, {amplifier: 0, showParticles: true })
      }
    }
    if (itemUsed.typeId === "nicothekid:crimson_stew") {
      let nauseaChance = Math.random();
      let poisonChance = Math.random();
      if (nauseaChance <= 0.25) {
        player.addEffect("nausea", 300, {amplifier: 0, showParticles: true })
      }
      if (poisonChance <= 0.1) {
        player.addEffect("poison", 200, {amplifier: 0, showParticles: true })
      }
    }
    if (itemUsed.typeId === "nicothekid:diamond_apple") {
      player.addEffect("resistance", 3600, {amplifier: 2, showParticles: true })
      player.addEffect("absorption", 1200, {amplifier: 1, showParticles: true })
      player.addEffect("health_boost", 600, {amplifier: 0, showParticles: true })
      player.addEffect("regeneration", 300, {amplifier: 1, showParticles: true })
    }
    if (itemUsed.typeId === "nicothekid:egg_sandwich") {
      let regenerationChance = Math.random();
      if (regenerationChance <= 0.05) {
        player.addEffect("regeneration", 100, {amplifier: 0, showParticles: true })
      }
    }
    if (itemUsed.typeId === "nicothekid:emerald_apple") {
      player.addEffect("haste", 1200, {amplifier: 1, showParticles: true })
      player.addEffect("regeneration", 300, {amplifier: 1, showParticles: true })
    }
    if (itemUsed.typeId === "nicothekid:glow_berry_cookie") {
      player.addEffect("night_vision", 200, {amplifier: 0, showParticles: true })
    }
    if (itemUsed.typeId === "nicothekid:glow_berry_pie") {
      player.addEffect("night_vision", 900, {amplifier: 0, showParticles: true })
    }
    if (itemUsed.typeId === "nicothekid:glow_lichen_stew") {
      player.addEffect("night_vision", 500, {amplifier: 0, showParticles: true })
      player.addEffect("mining_fatigue", 400, {amplifier: 0, showParticles: true })
    }
    if (itemUsed.typeId === "nicothekid:iron_apple") {
      player.addEffect("strength", 1200, {amplifier: 1, showParticles: true })
      player.addEffect("regeneration", 300, {amplifier: 1, showParticles: true })
    }
    if (itemUsed.typeId === "nicothekid:jar_chocolate_full") {
      player.removeEffect("slowness")
    }
    if (itemUsed.typeId === "nicothekid:jar_chocolate_half") {
      player.removeEffect("slowness")
    }
    if (itemUsed.typeId === "nicothekid:jar_glow_berry_juice_full") {
      player.removeEffect("blindness")
    }
    if (itemUsed.typeId === "nicothekid:jar_glow_berry_juice_half") {
      player.removeEffect("blindness")
    }
    if (itemUsed.typeId === "nicothekid:jar_honey_full") {
      player.removeEffect("poison")
      player.removeEffect("fatal_poison")
    }
    if (itemUsed.typeId === "nicothekid:jar_honey_half") {
      player.removeEffect("poison")
      player.removeEffect("fatal_poison")
    }
    if (itemUsed.typeId === "nicothekid:jar_milk_full") {
      player.removeEffect("fatal_poison")
      player.removeEffect("levitation")
      player.removeEffect("mining_fatigue")
      player.removeEffect("nausea")
      player.removeEffect("poison")
      player.removeEffect("weakness")
    }
    if (itemUsed.typeId === "nicothekid:jar_milk_half") {
      player.removeEffect("fatal_poison")
      player.removeEffect("levitation")
      player.removeEffect("mining_fatigue")
      player.removeEffect("nausea")
      player.removeEffect("poison")
      player.removeEffect("weakness")
    }
    if (itemUsed.typeId === "nicothekid:jar_sweet_berry_juice_full") {
      player.removeEffect("hunger")
    }
    if (itemUsed.typeId === "nicothekid:jar_sweet_berry_juice_half") {
      player.removeEffect("hunger")
    }
    if (itemUsed.typeId === "nicothekid:lapis_lazuli_apple") {
      player.addEffect("speed", 1200, {amplifier: 1, showParticles: true })
      player.addEffect("regeneration", 300, {amplifier: 1, showParticles: true })
    }
    if (itemUsed.typeId === "nicothekid:netherite_apple") {
      player.addEffect("resistance", 4800, {amplifier: 2, showParticles: true })
      player.addEffect("absorption", 3600, {amplifier: 1, showParticles: true })
      player.addEffect("strength", 3600, {amplifier: 1, showParticles: true })
      player.addEffect("regeneration", 1200, {amplifier: 1, showParticles: true })
    }
    if (itemUsed.typeId === "nicothekid:pea_stew") {
      let nauseaChance = Math.random();
      if (nauseaChance <= 0.1) {
        player.addEffect("nausea", 200, {amplifier: 0, showParticles: true })
      }
    }
    if (itemUsed.typeId === "nicothekid:potato_soup") {
      player.removeEffect("hunger")
      player.removeEffect("nausea")
    }
    if (itemUsed.typeId === "nicothekid:pumpkin_slice") {
      let nauseaChance = Math.random();
      if (nauseaChance <= 0.25) {
        player.addEffect("nausea", 300, {amplifier: 0, showParticles: true })
      }
    }
    if (itemUsed.typeId === "nicothekid:seagrass_stew") {
      let nauseaChance = Math.random();
      if (nauseaChance <= 0.25) {
        player.addEffect("nausea", 300, {amplifier: 0, showParticles: true })
      }
    }
    if (itemUsed.typeId === "nicothekid:soup") {
      player.removeEffect("hunger")
    }
    if (itemUsed.typeId === "nicothekid:sweet_berry_cookie") {
      player.addEffect("regeneration", 200, {amplifier: 0, showParticles: true })
    }
    if (itemUsed.typeId === "nicothekid:sweet_berry_pie") {
      player.addEffect("regeneration", 300, {amplifier: 0, showParticles: true })
    }
    if (itemUsed.typeId === "nicothekid:warped_stew") {
      let nauseaChance = Math.random();
      let poisonChance = Math.random();
      if (nauseaChance <= 0.25) {
        player.addEffect("nausea", 300, {amplifier: 0, showParticles: true })
      }
      if (poisonChance <= 0.1) {
        player.addEffect("poison", 200, {amplifier: 0, showParticles: true })
      }
    }
	}
	else return;
});
world.beforeEvents.itemUseOn.subscribe(eventData => {
  const player = eventData.source;
  const itemUsed = eventData.itemStack;
  const block = eventData.block;
  const blockLocation = block.location;
  const playerEquippable = player.getComponent("equippable");
  const statePillarAxis = block.permutation.getState("pillar_axis");
  const stateAll = block.permutation.getAllStates();
  const strippedWoods = [
    "minecraft:stripped_acacia_log",
    "minecraft:stripped_acacia_wood",
    "minecraft:stripped_bamboo_block",
    "minecraft:stripped_birch_log",
    "minecraft:stripped_birch_wood",
    "minecraft:stripped_cherry_log",
    "minecraft:stripped_cherry_wood",
    "minecraft:stripped_crimson_hyphae",
    "minecraft:stripped_crimson_stem",
    "minecraft:stripped_dark_oak_log",
    "minecraft:stripped_dark_oak_wood",
    "minecraft:stripped_jungle_log",
    "minecraft:stripped_jungle_wood",
    "minecraft:stripped_mangrove_log",
    "minecraft:stripped_mangrove_wood",
    "minecraft:stripped_oak_log",
    "minecraft:stripped_oak_wood",
    "minecraft:stripped_spruce_log",
    "minecraft:stripped_spruce_wood",
    "minecraft:stripped_warped_hyphae",
    "minecraft:stripped_warped_stem"
  ]
  function decreaseItem () {
    if (!player.matches({ gameMode: GameMode.creative })) {
      const decrementItem = itemUsed.amount == 1 ? undefined : (itemUsed.amount--, itemUsed);
      playerEquippable.setEquipment("Mainhand", decrementItem)
    }
  }
  if (itemUsed === undefined) return;
  if (strippedWoods.includes(block.typeId)) {
    if (itemUsed.typeId === "minecraft:bone_meal") {
      system.run(() => {
        decreaseItem ();
        block.dimension.playSound("item.bone_meal.use", blockLocation, { pitch: 1.0, volume: 1.0 });
        block.dimension.spawnParticle("nicothekid:barked_2_particle", { x: blockLocation.x, y: blockLocation.y, z: blockLocation.z });
        if (block.permutation.matches("minecraft:stripped_acacia_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:acacia_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_acacia_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:acacia_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_bamboo_block")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:bamboo_block", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_birch_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:birch_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_birch_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:birch_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_cherry_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:cherry_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_cherry_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:cherry_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_crimson_stem")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:crimson_stem", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_crimson_hyphae")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:crimson_hyphae", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_dark_oak_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:dark_oak_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_dark_oak_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:dark_oak_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_jungle_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:jungle_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_jungle_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:jungle_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_mangrove_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:mangrove_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_mangrove_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:mangrove_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_oak_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:oak_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_oak_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:oak_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_spruce_log")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:spruce_log", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_spruce_wood")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:spruce_wood", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_warped_stem")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:warped_stem", { "pillar_axis": statePillarAxis } ));
          return;
        }
        if (block.permutation.matches("minecraft:stripped_warped_hyphae")) {
          block.setPermutation(BlockPermutation.resolve("minecraft:warped_hyphae", { "pillar_axis": statePillarAxis } ));
          return;
        }
      });
    }
    else return;
  }
  else return;
});
