import { world, system, EntityComponentTypes, ItemStack, ItemLockMode } from "@minecraft/server"

const time = seconds => seconds * 20

function getPlayerAnimationState(player) {
    return {
        timer: player.getDynamicProperty("ks_pa:timer"),
        initiateCall: player.getDynamicProperty("ks_pa:initiate_call"),
        loopIdle: player.getDynamicProperty("ks_pa:loop_idle"),
        loopIdleEvent: player.getDynamicProperty("ks_pa:loop_idle_event"),
        idleEventDelay: player.getDynamicProperty("ks_pa:idle_event_delay"),
        finishIdleEvent: player.getDynamicProperty("ks_pa:finish_idle_event"),
        loopIdleSword: player.getDynamicProperty("ks_pa:loop_idle_sword"),
        loopIdleLowHealth: player.getDynamicProperty("ks_pa:loop_idle_low_health"),
        loopWalk: player.getDynamicProperty("ks_pa:loop_walk"),
        loopWalkSword: player.getDynamicProperty("ks_pa:loop_walk_sword"),
        loopWalkLowHealth: player.getDynamicProperty("ks_pa:loop_walk_low_health"),
        loopSprint: player.getDynamicProperty("ks_pa:loop_sprint"),
        loopSprintSword: player.getDynamicProperty("ks_pa:loop_sprint_sword"),
        loopSprintLowHealth: player.getDynamicProperty("ks_pa:loop_sprint_low_health"),
        loopSneakStill: player.getDynamicProperty("ks_pa:loop_sneak_still"),
        loopSneak: player.getDynamicProperty("ks_pa:loop_sneak"),
        loopClimb: player.getDynamicProperty("ks_pa:loop_climb"),
        loopClimbStill: player.getDynamicProperty("ks_pa:loop_climb_still"),
        loopJump: player.getDynamicProperty("ks_pa:loop_jump"),
        loopFall: player.getDynamicProperty("ks_pa:loop_fall"),
        loopLand: player.getDynamicProperty("ks_pa:loop_land"),
        loopFlyStill: player.getDynamicProperty("ks_pa:loop_fly_still"),
        loopFly: player.getDynamicProperty("ks_pa:loop_fly"),
        loopFastFly: player.getDynamicProperty("ks_pa:loop_fast_fly"),
        loopSwimStill: player.getDynamicProperty("ks_pa:loop_swim_still"),
        loopSwim: player.getDynamicProperty("ks_pa:loop_swim"),
        loopCrawlStill: player.getDynamicProperty("ks_pa:loop_crawl_still"),
        loopCrawl: player.getDynamicProperty("ks_pa:loop_crawl"),
        loopRideMinecart: player.getDynamicProperty("ks_pa:loop_ride_minecart"),
        loopRideMinecartMove: player.getDynamicProperty("ks_pa:loop_ride_minecart_move"),
        testholdSword: player.getDynamicProperty("ks_pa:test_hold_sword"),
        testPlayerVelocity: player.getDynamicProperty("ks_pa:test_player_velocity"),
        canCrawl: player.getDynamicProperty("ks_pa:can_crawl"),
    };
}

// function testForNoCustomArmCase(player) {
//   let states = getPlayerAnimationState(player);
//   //console.warn(states.loopRideMinecartMove, states.loopRideMinecart, states.loopClimb, states.loopClimbStill, states.loopSwimStill, states.loopSwim, states.loopCrawlStill, states.loopCrawl)
//   if (states.loopRideMinecartMove == false && states.loopRideMinecart == false && states.loopClimb == false && states.loopClimbStill == false && states.loopSwimStill == false && states.loopSwim == false && states.loopCrawlStill == false && states.loopCrawl == false )
//   {
//   }
// }

function safeCheckCustomArms(player) {
  if(!player.isInWater) {
    // player.setDynamicProperty("ks_pa:loop_swim", false);
    // player.setDynamicProperty("ks_pa:loop_swim_still", false);
    if (!player.hasTag('ks_pa_initiation_swim')) { player.setDynamicProperty("ks_pa:initiate_call", false); player.addTag('ks_pa_initiation_swim')}
  }
  else if (player.isInWater){
    player.removeTag('ks_pa_initiation_swim');
  }
  if(!player.isClimbing) {
    // player.setDynamicProperty("ks_pa:loop_climb", false);
    // player.setDynamicProperty("ks_pa:loop_climb_still", false);
    if (!player.hasTag('ks_pa_initiation_climb')) { player.setDynamicProperty("ks_pa:initiate_call", false); player.addTag('ks_pa_initiation_climb')}
  }
  else if (player.isClimbing){
    player.removeTag('ks_pa_initiation_climb');
  }
  const blockAbove = player.dimension.getBlock({
    x: Math.floor(player.location.x),
    y: Math.floor(player.location.y) + 1,
    z: Math.floor(player.location.z)
  })
  let crawlPossible = !player.getDynamicProperty("ks_pa:ride_minecart") && blockAbove?.typeId != "minecraft:air" && blockAbove?.typeId != "minecraft:ladder" && blockAbove?.typeId != "minecraft:large_fern" && blockAbove?.typeId != "minecraft:tall_grass" && blockAbove?.typeId != "minecraft:vine" && blockAbove?.typeId != "minecraft:wooden_door" && blockAbove?.typeId != "minecraft:spruce_door" && blockAbove?.typeId != "minecraft:birch_door" && blockAbove?.typeId != "minecraft:jungle_door" && blockAbove?.typeId != "minecraft:acacia_door" && blockAbove?.typeId != "minecraft:dark_oak_door" && blockAbove?.typeId != "minecraft:iron_door" && blockAbove?.typeId != "minecraft:stone_button" && blockAbove?.typeId != "minecraft:wooden_button" && blockAbove?.typeId != "minecraft:spruce_button" && blockAbove?.typeId != "minecraft:birch_button" && blockAbove?.typeId != "minecraft:jungle_button" && blockAbove?.typeId != "minecraft:acacia_button" && blockAbove?.typeId != "minecraft:dark_oak_button" && blockAbove?.typeId != "minecraft:mangrove_button" && blockAbove?.typeId != "minecraft:crimson_button" && blockAbove?.typeId != "minecraft:warped_button" && blockAbove?.typeId != "minecraft:lever" && !blockAbove?.permutation.getState("open_bit") && !player.isInWater
  if(!crawlPossible){
    // player.setDynamicProperty("ks_pa:can_crawl", false);
    // player.setDynamicProperty("ks_pa:loop_crawl", false);
    // player.setDynamicProperty("ks_pa:loop_crawl_still", false);
    if (!player.hasTag('ks_pa_initiation_crawl')) { player.setDynamicProperty("ks_pa:initiate_call", false); player.addTag('ks_pa_initiation_crawl')}
  }
  else if (crawlPossible){
    player.removeTag('ks_pa_initiation_crawl');
  }
  if (!player.isInWater && !player.isClimbing && !crawlPossible){
    if (player.getComponent("minecraft:equippable").getEquipment("Offhand")?.typeId.includes("ks_pa:")){
      player.getComponent(EntityComponentTypes.Equippable).setEquipment("Offhand", undefined);
    }
  }
}

const new_arm1 = new ItemStack('ks_pa:new_arm1_item', 1);
const new_arm2 = new ItemStack('ks_pa:new_arm2_item', 1);
const new_arm3 = new ItemStack('ks_pa:new_arm3_item', 1);
const new_arm4 = new ItemStack('ks_pa:new_arm4_item', 1);
const new_arm5 = new ItemStack('ks_pa:new_arm5_item', 1);
system.runInterval(() => {
  world.getPlayers().forEach(player => {
    //let customArmsOff = testForNoCustomArmCase(player);
    let states = getPlayerAnimationState(player);
    let changed = player.getDynamicProperty("ks_pa:changed");
    const equipmentCompPlayer = player.getComponent(EntityComponentTypes.Equippable);

    const offHand = player.getComponent("minecraft:equippable").getEquipment("Offhand")
    let first = player.getDynamicProperty("ks_pa:first");
    let third = player.getDynamicProperty("ks_pa:third");
    let camera = player.getDynamicProperty("ks_pa:camera");
    let angle = player.getDynamicProperty("ks_pa:angle") || "back";

    // Get animation settings
    let swordAnimations = player.getDynamicProperty("ks_pa:idle_sword_animation");
    let lowHealthAnimation = player.getDynamicProperty("ks_pa:low_health_animation");
    let standAnimation = player.getDynamicProperty("ks_pa:idle_animation");
    let walkAnimation = player.getDynamicProperty("ks_pa:walk_animation");
    let sprintAnimation = player.getDynamicProperty("ks_pa:sprint_animation");
    let attackAnimations = player.getDynamicProperty("ks_pa:attack_animations");
    let minecartAnimation = player.getDynamicProperty("ks_pa:minecart_animation");
    let jumpAnimation = player.getDynamicProperty("ks_pa:jump_animation");
    let landAnimation = player.getDynamicProperty("ks_pa:land_animation");
    let flyAnimation = player.getDynamicProperty("ks_pa:fly_animation");
    let climbAnimation = player.getDynamicProperty("ks_pa:climb_animation");
    let fallAnimation = player.getDynamicProperty("ks_pa:fall_animation");
    let sneakAnimation = player.getDynamicProperty("ks_pa:sneak_animation");
    let swimAnimation = player.getDynamicProperty("ks_pa:swim_animation");
    let crawlAnimation = player.getDynamicProperty("ks_pa:crawl_animation");
    let stretchAnimation = player.getDynamicProperty("ks_pa:stretch_animation");

    let enableAll = world.getDynamicProperty("ks_pa:enableAll");
    let disablePlayers = (world.getDynamicProperty("ks_pa:disablePlayers") || "").split(",").filter(p => p);
    let enablePlayers = (world.getDynamicProperty("ks_pa:enablePlayers") || "").split(",").filter(p => p);

    let playerName = player.name;

    if (!enableAll && disablePlayers.length === 0 && enablePlayers.length === 0) {
      if (offHand?.typeId.startsWith("ks_pa:")) {
        equipmentCompPlayer.setEquipment("Offhand", undefined);
      }
      return;
    }

    if (enablePlayers.length !== 0 && !enablePlayers.includes(playerName)) {
      if (offHand?.typeId.startsWith("ks_pa:")) {
        equipmentCompPlayer.setEquipment("Offhand", undefined);
      }
      return;
    }

    if (disablePlayers.length !== 0 && disablePlayers.includes(playerName)) {
      if (offHand?.typeId.startsWith("ks_pa:")) {
        equipmentCompPlayer.setEquipment("Offhand", undefined);
      }
      return;
    }
    if (camera) {
      switch (angle) {
        case "back": // Vista trasera
          player.runCommand('camera @s set minecraft:free ease 0.2 linear pos ^^+6^-3 facing ~~~');
          break;
        case "shoulder_left": // Vista shoulder
          player.runCommand('camera @s set minecraft:free ease 0.2 linear pos ^-1^+2.5^-2.5 facing ^^^+10');
          break;
        case "shoulder_right": // Vista shoulder
          player.runCommand('camera @s set minecraft:free ease 0.2 linear pos ^1^+2.5^-2.5 facing ^^^+10');
          break;
        case "front": // Vista frontal
          player.runCommand('camera @s set minecraft:free ease 0.2 linear pos ^^+4^+5 facing ~~~');
          break;
        case "left": // Vista lateral izquierda
          player.runCommand('camera @s set minecraft:free ease 0.2 linear pos ^-6^+1^ facing ~~~');
          break;

        case "right": // Vista lateral derecha
          player.runCommand('camera @s set minecraft:free ease 0.2 linear pos ^+6^+1^ facing ~~~');
          break;

        case "top": // Vista cenital (desde arriba)
          player.runCommand('camera @s set minecraft:free ease 0.2 linear pos ~~+16~ facing ~~~');
          break;
      }
    } else {
      player.runCommand('camera @s clear');
    }

    if (!first && !third) {
      player.playAnimation("animation.humanoid.base_pose", { blendOutTime: 0.2, stopExpression: "query.log", controller: "first_base_layer" })
      player.playAnimation("animation.humanoid.base_pose", { blendOutTime: 0.2, stopExpression: "query.log", controller: "first_animate_loop" })
      player.playAnimation("animation.humanoid.base_pose", { blendOutTime: 0.2, stopExpression: "query.log", controller: "first_animate_once" })
      player.playAnimation("animation.humanoid.base_pose", { blendOutTime: 0.2, stopExpression: "query.log", controller: "third_animate_loop" })
      player.playAnimation("animation.humanoid.base_pose", { blendOutTime: 0.2, stopExpression: "query.log", controller: "third_sword_attack_layer" })
      player.playAnimation("animation.humanoid.base_pose", { blendOutTime: 0.2, stopExpression: "query.log", controller: "third_crawl_layer" })
      player.playAnimation("animation.humanoid.base_pose", { blendOutTime: 0.2, stopExpression: "query.log", controller: "third_crawl_legs_layer" })
      player.playAnimation("animation.humanoid.base_pose", { blendOutTime: 0.2, stopExpression: "query.log", controller: "third_sneaking_layer" })
      player.playAnimation("animation.humanoid.base_pose", { blendOutTime: 0.2, stopExpression: "query.log", controller: "third_base_layer" })
      player.playAnimation("animation.humanoid.base_pose", { blendOutTime: 0.2, stopExpression: "query.log", controller: "third_animate_once" })
    }
    if (!first && third) {
      player.playAnimation("animation.humanoid.base_pose", { blendOutTime: 0.2, stopExpression: "query.log", controller: "first_base_layer" })
      player.playAnimation("animation.humanoid.base_pose", { blendOutTime: 0.2, stopExpression: "query.log", controller: "first_animate_loop" })
      player.playAnimation("animation.humanoid.base_pose", { blendOutTime: 0.2, stopExpression: "query.log", controller: "first_animate_once" })
    }
    if (first && !third) {
      player.playAnimation("animation.humanoid.base_pose", { blendOutTime: 0.2, stopExpression: "query.log", controller: "third_animate_loop" })
      player.playAnimation("animation.humanoid.base_pose", { blendOutTime: 0.2, stopExpression: "query.log", controller: "third_sword_attack_layer" })
      player.playAnimation("animation.humanoid.base_pose", { blendOutTime: 0.2, stopExpression: "query.log", controller: "third_crawl_layer" })
      player.playAnimation("animation.humanoid.base_pose", { blendOutTime: 0.2, stopExpression: "query.log", controller: "third_crawl_legs_layer" })
      player.playAnimation("animation.humanoid.base_pose", { blendOutTime: 0.2, stopExpression: "query.log", controller: "third_sneaking_layer" })
      player.playAnimation("animation.humanoid.base_pose", { blendOutTime: 0.2, stopExpression: "query.log", controller: "third_base_layer" })
      player.playAnimation("animation.humanoid.base_pose", { blendOutTime: 0.2, stopExpression: "query.log", controller: "third_animate_once" })
    }
    // if (!third && changed) {
    //   states.loopIdleSword = false
    //   player.setDynamicProperty('ks_pa:loop_idle_sword', false);
    //   states.loopIdleLowHealth = false
    //   player.setDynamicProperty('ks_pa:loop_idle_low_health', false);
    //   states.loopWalk = false
    //   player.setDynamicProperty('ks_pa:loop_walk', false);
    //   states.loopWalkSword = false
    //   player.setDynamicProperty('ks_pa:loop_walk_sword', false);
    //   states.loopWalkLowHealth = false
    //   player.setDynamicProperty('ks_pa:loop_walk_low_health', false);
    //   states.loopSprint = false
    //   player.setDynamicProperty('ks_pa:loop_sprint', false);
    //   states.loopSprintSword = false
    //   player.setDynamicProperty('ks_pa:loop_sprint_sword', false);
    //   states.loopSprintLowHealth = false
    //   player.setDynamicProperty('ks_pa:loop_sprint_low_health', false);
    //   states.loopSneakStill = false
    //   player.setDynamicProperty('ks_pa:loop_sneak_still', false);
    //   states.loopSneak = false
    //   player.setDynamicProperty('ks_pa:loop_sneak', false);
    //   states.loopClimb = false
    //   player.setDynamicProperty('ks_pa:loop_climb', false);
    //   states.loopClimbStill = false
    //   player.setDynamicProperty('ks_pa:loop_climb_still', false);
    //   states.loopJump = false
    //   player.setDynamicProperty('ks_pa:loop_jump', false);
    //   states.loopFall = false
    //   player.setDynamicProperty('ks_pa:loop_fall', false);
    //   states.loopLand = false
    //   player.setDynamicProperty('ks_pa:loop_land', false);
    //   states.loopFlyStill = false
    //   player.setDynamicProperty('ks_pa:loop_fly_still', false);
    //   states.loopFly = false
    //   player.setDynamicProperty('ks_pa:loop_fly', false);
    //   states.loopFastFly = false
    //   player.setDynamicProperty('ks_pa:loop_fast_fly', false);
    //   states.loopSwimStill = false
    //   player.setDynamicProperty('ks_pa:loop_swim_still', false);
    //   states.loopSwim = false
    //   player.setDynamicProperty('ks_pa:loop_swim', false);
    //   states.loopCrawlStill = false
    //   player.setDynamicProperty('ks_pa:loop_crawl_still', false);
    //   states.loopCrawl = false
    //   player.setDynamicProperty('ks_pa:loop_crawl', false);
    //   states.loopRideMinecart = false
    //   player.setDynamicProperty('ks_pa:loop_ride_minecart', false);
    //   states.initiateCall = false
    //   player.setDynamicProperty('ks_pa:initiate_call', false);

    //   states.loopIdle = false
    //   player.setDynamicProperty('ks_pa:loop_idle', false);
    //   states.loopIdleEvent = false
    //   player.setDynamicProperty('ks_pa:loop_idle_event', false);

    //   states.idleEventDelay = 0
    //   player.setDynamicProperty('ks_pa:idle_event_delay', 0);

    //   states.finishIdleEvent = false
    //   player.setDynamicProperty('ks_pa:finish_idle_event', false);
    //   if ( !player.hasTag('ks_pa_attack_anim') ) player.playAnimation("animation.humanoid.base_pose", { blendOutTime: 0.2, stopExpression: "query.log", controller: "third_animate_loop" })
    //   player.playAnimation("animation.ks_pa.humanoid.move", { blendOutTime: 0.2, stopExpression: "query.log", controller: "third_animate_loop" })
    //   return;
    // }

    const mainHand = player.getComponent("minecraft:inventory").container.getItem(player.selectedSlotIndex)

    const holdSword = mainHand?.typeId == "minecraft:wooden_sword" || mainHand?.typeId == "minecraft:stone_sword" || mainHand?.typeId == "minecraft:iron_sword" || mainHand?.typeId == "minecraft:diamond_sword" || mainHand?.typeId == "minecraft:golden_sword" || mainHand?.typeId == "minecraft:netherite_sword" || (mainHand?.typeId == "minecraft:shield" || offHand?.typeId == "minecraft:shield")

    const getPlayerVelocity = Math.sqrt(player.getVelocity().x ** 2 + player.getVelocity().z ** 2)

    states.testPlayerVelocity = getPlayerVelocity
    player.setDynamicProperty('ks_pa:test_player_velocity', getPlayerVelocity);

    if (holdSword) {
      states.testholdSword = true
      player.setDynamicProperty('ks_pa:test_hold_sword', true);
    } else {
      states.testholdSword = false
      player.setDynamicProperty('ks_pa:test_hold_sword', false);
    }
    if (first && ((states.loopRideMinecartMove == true && minecartAnimation) || (states.loopRideMinecart == true && minecartAnimation) || (states.loopClimb == true && climbAnimation) || (states.loopClimbStill == true && climbAnimation) || (states.loopSwimStill == true && swimAnimation) || (states.loopSwim == true && swimAnimation) || (states.loopCrawlStill == true && crawlAnimation) || (states.loopCrawl == true && crawlAnimation)) && (!offHand || offHand?.typeId.startsWith("ks_pa:"))) {
      if (player.hasTag("new_arm1")) {
        [new_arm1].forEach(item => {
          item.lockMode = ItemLockMode.slot;
          item.keepOnDeath = true;
        });
        equipmentCompPlayer.setEquipment("Offhand", new_arm1);
      } else if (player.hasTag("new_arm2")) {
        [new_arm2].forEach(item => {
          item.lockMode = ItemLockMode.slot;
          item.keepOnDeath = true;
        });
        equipmentCompPlayer.setEquipment("Offhand", new_arm2);
      } else if (player.hasTag("new_arm3")) {
        [new_arm3].forEach(item => {
          item.lockMode = ItemLockMode.slot;
          item.keepOnDeath = true;
        });
        equipmentCompPlayer.setEquipment("Offhand", new_arm3);
      } else if (player.hasTag("new_arm4")) {
        [new_arm4].forEach(item => {
          item.lockMode = ItemLockMode.slot;
          item.keepOnDeath = true;
        });
        equipmentCompPlayer.setEquipment("Offhand", new_arm4);
      } else if (player.hasTag("new_arm5")) {
        [new_arm5].forEach(item => {
          item.lockMode = ItemLockMode.slot;
          item.keepOnDeath = true;
        });
        equipmentCompPlayer.setEquipment("Offhand", new_arm5);
      }
    } else if (offHand?.typeId.includes("ks_pa:")) {
      equipmentCompPlayer.setEquipment("Offhand", undefined);
    }

    const blockInView = player.getBlockFromViewDirection({ maxDistance: 8 })
    const entityInView = player.getEntitiesFromViewDirection({ excludeTypes: ["ks_pa:air_tester"], maxDistance: 8 })[0]

    let tester = player.dimension.getEntities({ type: "ks_pa:air_tester" })[0]
    if (!holdSword && !(mainHand?.typeId == "minecraft:shield" || offHand?.typeId == "minecraft:shield") || blockInView || entityInView) {
      if (tester) tester.triggerEvent("ks_pa:despawn")
    } else {
      if (!tester) {
        tester = player.dimension.spawnEntity("ks_pa:air_tester", { x: player.location.x, y: player.location.y, z: player.location.z })
      } else {
        tester.teleport({ x: player.location.x + player.getViewDirection().x * 2, y: player.location.y + 1 + player.getViewDirection().y * 2, z: player.location.z + player.getViewDirection().z * 2 }, player.dimension
        )
      }
    }
    const minecarts = player.dimension.getEntities({
      type: "minecraft:minecart",
      location: player.location,
      maxDistance: 1,
    });
    const isRidingMinecart = minecarts.some((minecart) => {
      const rideable = minecart.getComponent("minecraft:rideable");
      return rideable.getRiders().some((rider) => rider.id === player.id);
  });
  if (player.getDynamicProperty("ks_pa:ride_minecart") !== isRidingMinecart) {
      player.setDynamicProperty("ks_pa:ride_minecart", isRidingMinecart);
  }
  
  //console.warn(states.loopSwim, swimAnimation);

    const blockAbove = player.dimension.getBlock({
      x: Math.floor(player.location.x),
      y: Math.floor(player.location.y) + 1,
      z: Math.floor(player.location.z)
    })

    states.canCrawl = !player.getDynamicProperty("ks_pa:ride_minecart") && blockAbove?.typeId != "minecraft:air" && blockAbove?.typeId != "minecraft:ladder" && blockAbove?.typeId != "minecraft:large_fern" && blockAbove?.typeId != "minecraft:tall_grass" && blockAbove?.typeId != "minecraft:vine" && blockAbove?.typeId != "minecraft:wooden_door" && blockAbove?.typeId != "minecraft:spruce_door" && blockAbove?.typeId != "minecraft:birch_door" && blockAbove?.typeId != "minecraft:jungle_door" && blockAbove?.typeId != "minecraft:acacia_door" && blockAbove?.typeId != "minecraft:dark_oak_door" && blockAbove?.typeId != "minecraft:iron_door" && blockAbove?.typeId != "minecraft:stone_button" && blockAbove?.typeId != "minecraft:wooden_button" && blockAbove?.typeId != "minecraft:spruce_button" && blockAbove?.typeId != "minecraft:birch_button" && blockAbove?.typeId != "minecraft:jungle_button" && blockAbove?.typeId != "minecraft:acacia_button" && blockAbove?.typeId != "minecraft:dark_oak_button" && blockAbove?.typeId != "minecraft:mangrove_button" && blockAbove?.typeId != "minecraft:crimson_button" && blockAbove?.typeId != "minecraft:warped_button" && blockAbove?.typeId != "minecraft:lever" && !blockAbove?.permutation.getState("open_bit") && !player.isInWater
    player.setDynamicProperty('ks_pa:can_crawl', states.canCrawl);
    if (states.initiateCall == false) {
      states.initiateCall = true
      states.loopIdle = true
      player.setDynamicProperty('ks_pa:initiate_call', true);
      player.setDynamicProperty('ks_pa:loop_idle', true);
    } else if (states.initiateCall == true) {
      if (states.loopIdle == true || states.loopIdleSword == true) {
        if (states.loopIdleEvent == true) {
          callIdleEvent(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
        }
      }
      if (states.loopJump == true) {
        callJump(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      } else if (states.loopFall == true) {
        callFall(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      }

      if (states.loopIdle == true) {
        callIdle(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      } else if (states.loopIdleSword == true && swordAnimations) {
        callIdleSword(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      } else if (states.loopIdleLowHealth == true) {
        callIdleLowHealth(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      } else if (states.loopWalk == true) {
        callWalk(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      } else if (states.loopWalkSword == true && swordAnimations) {
        callWalkSword(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      } else if (states.loopWalkLowHealth == true) {
        callWalkLowHealth(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      } else if (states.loopSprint == true) {
        callSprint(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      } else if (states.loopSprintSword == true && swordAnimations) {
        callSprintSword(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      } else if (states.loopSprintLowHealth == true) {
        callSprintLowHealth(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      } else if (states.loopLand == true) {
        callLand(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      } else if (states.loopSneakStill == true) {
        callSneakStill(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      } else if (states.loopSneak == true) {
        callSneak(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      } else if (states.loopClimb == true) {
        callClimb(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      } else if (states.loopClimbStill == true) {
        callClimbStill(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      } else if (states.loopFlyStill == true) {
        callFlyStill(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      } else if (states.loopFly == true) {
        callFly(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      } else if (states.loopFastFly == true) {
        callFastFly(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      } else if (states.loopSwimStill == true) {
        callSwimStill(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      } else if (states.loopSwim == true) {
        callSwim(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      } else if (states.loopCrawlStill == true) {
        callCrawlStill(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      } else if (states.loopCrawl == true) {
        callCrawl(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      } else if (states.loopRideMinecart == true) {
        callRideMinecart(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      } else if (states.loopRideMinecartMove == true) {
        callRideMinecartMove(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation)
      }
    }

    if ((states.loopIdle == true || states.loopIdleSword == true) && states.finishIdleEvent == false && initiateBreakBlock == false && attackDelay == false && !player.isFlying && !player.isInWater && !player.isSwimming && !player.hasTag("on_fire")) {
      if (states.idleEventDelay == time(16)) {
        states.loopIdleEvent = true
        player.setDynamicProperty('ks_pa:loop_idle_event', true);

        if (stretchAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_event", "animation.ks_pa.player.first.idle_event", 0.2, "third_animate_loop", "first_animate_loop");
        else {
          states.finishIdleEvent = true;
          player.setDynamicProperty('ks_pa:finish_idle_event', true);
        }
        system.runTimeout(() => {
          if (states.loopIdleEvent == true) {
            states.finishIdleEvent = true
            player.setDynamicProperty('ks_pa:finish_idle_event', true);
          }
        }, time(3.1))
      }
      states.idleEventDelay += 1
      player.setDynamicProperty('ks_pa:idle_event_delay', states.idleEventDelay);
    } else {
      states.finishIdleEvent = false
      player.setDynamicProperty('ks_pa:finish_idle_event', false);
      states.loopIdleEvent = false
      player.setDynamicProperty('ks_pa:loop_idle_event', false);
      states.idleEventDelay = 0
      player.setDynamicProperty('ks_pa:idle_event_delay', 0);
    }
    const health = player.getComponent("minecraft:health").currentValue;
    states = getPlayerAnimationState(player);
    if (third) {
    if (attackAnimations && states.testholdSword) {
        player.playAnimation("animation.ks_pa.player.third.sword_attack_layer", {
            stopExpression: "query.is_first_person",
            controller: "third_sword_attack_layer"
        });
    } else if (!player.hasTag('ks_pa_attack_anim') && states.testholdSword && !attackAnimations) {
        player.playAnimation("animation.humanoid.base_pose", {
            stopExpression: "query.log",
            controller: "third_sword_attack_layer"
        });
    } else if (states.canCrawl && crawlAnimation) {
        player.playAnimation("animation.ks_pa.player.third.crawl_layer", {
            stopExpression: "!query.is_crawling",
            controller: "third_crawl_layer"
        });
        player.playAnimation("animation.ks_pa.player.third.crawl_legs_layer", {
            stopExpression: "!query.is_crawling",
            controller: "third_crawl_legs_layer"
        });
    } else if (player.isSneaking && sneakAnimation) {
        player.playAnimation("animation.ks_pa.player.third.sneaking_layer", {
            stopExpression: "!query.is_sneaking",
            controller: "third_sneaking_layer"
        });
    } else if (player.isSneaking && !sneakAnimation) {
        player.playAnimation("animation.humanoid.base_pose", {
            stopExpression: "query.log",
            controller: "third_sneaking_layer"
        });
    } else if (!player.hasTag('ks_pa_attack_anim')) {
        if (
            states.loopIdle ||
            (player.isFlying && flyAnimation) ||
            (!player.isFlying && walkAnimation && states.testPlayerVelocity > 0.12 && !player.isSprinting && (health > 10 || !lowHealthAnimation)) ||
            (!player.isFlying && sprintAnimation && player.isSprinting && (health > 10 || !lowHealthAnimation)) ||
            (!player.isFlying && lowHealthAnimation && health <= 10)
        ) {
            player.playAnimation("animation.ks_pa.player.third.base_layer", {
                stopExpression: "query.log",
                controller: "third_base_layer"
            });
        } else {
            player.playAnimation("animation.humanoid.base_pose", {
                stopExpression: "query.any_animation_finished",
                controller: "third_base_layer"
            });
        }
    }
}

    if (first &&
      !player.hasTag('ks_pa_attack_anim') &&
      states.loopIdleEvent == false &&
      !player.isSneaking &&
      ((states.loopIdle == true && standAnimation) ||
      (player.isInWater && swimAnimation) ||
      (player.isFlying && flyAnimation) ||
      (!player.isFlying && walkAnimation && states.testPlayerVelocity > 0.12 && !player.isSprinting && (player.getComponent("minecraft:health").currentValue > 10 || !lowHealthAnimation)) ||
      (!player.isFlying && sprintAnimation && player.isSprinting && (player.getComponent("minecraft:health").currentValue > 10 || !lowHealthAnimation)) ||
      (!player.isFlying && lowHealthAnimation && player.getComponent("minecraft:health").currentValue <= 10) ||
      (player.getDynamicProperty("ks_pa:ride_minecart") && minecartAnimation) ||
      (states.testholdSword && swordAnimations && (player.getComponent("minecraft:health").currentValue > 10 || !lowHealthAnimation)) ||
      (player.isJumping && !player.isFalling && !player.isInWater && !player.isSwimming && jumpAnimation) ||
      (player.isFalling && !player.isInWater && fallAnimation) ||
      (player.isClimbing && !player.isOnGround && !player.isInWater && !player.isSwimming && climbAnimation) ||
      (states.canCrawl == true && crawlAnimation) ||
      (states.loopIdleEvent == true && stretchAnimation)
    ))
      { player.playAnimation("animation.ks_pa.player.first.base_layer", { stopExpression: "query.log", controller: "first_base_layer" }) }
    if (first &&
      (player.hasTag('ks_pa_attack_anim') ||
      (states.loopIdle == true && !standAnimation) ||
      (player.isInWater && !swimAnimation) ||
      (player.isSneaking && !sneakAnimation && !player.isInWater) ||
      (player.isFlying && !flyAnimation && !player.isInWater) ||
      (!player.isFlying && !walkAnimation && states.testPlayerVelocity > 0.12 && !player.isSprinting && ((player.getComponent("minecraft:health").currentValue > 10 && lowHealthAnimation) || !lowHealthAnimation) && !player.isInWater) ||
      (!player.isFlying && !sprintAnimation && player.isSprinting && ((player.getComponent("minecraft:health").currentValue > 10 && lowHealthAnimation) || !lowHealthAnimation) && !player.isInWater) ||
      (!player.isFlying && !lowHealthAnimation && player.getComponent("minecraft:health").currentValue <= 10 && ((!walkAnimation && states.testPlayerVelocity > 0.12 && !player.isSprinting && !player.isInWater) || (!sprintAnimation && player.isSprinting && !player.isInWater))) ||
      (player.getDynamicProperty("ks_pa:ride_minecart") && !minecartAnimation && !player.isInWater) ||
      (states.testholdSword && !swordAnimations && (player.getComponent("minecraft:health").currentValue > 10 || !lowHealthAnimation) && !player.isInWater) ||
      (player.isJumping && !player.isFalling && !player.isInWater && !player.isSwimming && !jumpAnimation) ||
      (player.isFalling && !player.isInWater && !fallAnimation) ||
      (player.isClimbing && !player.isOnGround && !player.isInWater && !player.isSwimming && !climbAnimation) ||
      (states.canCrawl == true && !crawlAnimation && !player.isInWater) ||
      (states.loopIdleEvent == true && !stretchAnimation)
    ))
      { player.playAnimation("animation.humanoid.base_pose", { stopExpression: "query.any_animation_finished", controller: "first_base_layer" }) }
    if (first) { player.playAnimation("animation.ks_pa.player.first.left_arm_layer", { stopExpression: "query.log", controller: "first_left_arm_layer" }) }
    if (first) { player.playAnimation("animation.ks_pa.player.first.left_arm_layer", { stopExpression: "query.log", controller: "first_left_arm_layer" }) }
    if (first && player.hasTag('ks_pa_attack_anim')) { player.playAnimation("animation.ks_pa.player.first.attack_rotation_layer", { stopExpression: "variable.attack_time == 0", controller: "first_attack_rotation_layer" }) }
    let timeout = system.runTimeout(() => {
      let safeCheck = safeCheckCustomArms(player);
      system.clearRun(timeout);
    }, 1)
  })
})


function playPlayerAnimation(player, third, first, thirdAnim, firstAnim, blendOutTime, controllerTp, controllerFp) {
  if (third) player.playAnimation(thirdAnim, { blendOutTime, stopExpression: "query.log", controller: controllerTp });
  if (first) player.playAnimation(firstAnim, { blendOutTime, stopExpression: "query.log", controller: controllerFp });
}

function callRideMinecart(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const velocity = states.testPlayerVelocity;
  const isRidingMinecart = player.getDynamicProperty("ks_pa:ride_minecart") && minecartAnimation;
  const health = player.getComponent("minecraft:health").currentValue;
  const canMove = velocity >= 0.12;
  const isIdle = velocity < 0.01;
  const holdingSword = states.testholdSword && swordAnimations;
  const isGrounded = player.isOnGround && !player.isClimbing;
  const idle = isIdle && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const walk = canMove && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const sneak = velocity < 0.04 && player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const sneakMove = velocity >= 0.04 && player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const climb = player.isClimbing && !player.isOnGround && !player.isInWater && !player.isSwimming;
  const jump = player.isJumping && !player.isFalling && !player.isInWater && !player.isSwimming && states.loopJump == false && states.loopFall == false;
  const fall = player.isFalling && !player.isInWater && states.loopJump == false && states.loopFall == false;
  const flyStand = velocity < 0.12 && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const fly = velocity >= 0.12 && !player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const flyFast = velocity >= 0.12 && player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const swimStand = player.isInWater && velocity < 0.12;
  const swim = player.isInWater && velocity >= 0.12;
  const crawlStand = velocity < 0.04 && states.canCrawl == true;
  const crawl = velocity >= 0.04 && states.canCrawl == true;
  if (isRidingMinecart && !isIdle) {
    states.loopRideMinecart = false
    player.setDynamicProperty('ks_pa:loop_ride_minecart', false);
    states.loopRideMinecartMove = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_move", "animation.ks_pa.humanoid_fpp.minecart_move", 0.28, "third_animate_loop", "first_animate_loop");
  }

  if (isRidingMinecart && isIdle) return;

  if (idle && !holdingSword && (health > 10 || !lowHealthAnimation) && !states.loopIdleEvent) {
    states.loopRideMinecart = false
    player.setDynamicProperty('ks_pa:loop_ride_minecart', false);
    states.loopIdle = true
    player.setDynamicProperty('ks_pa:loop_idle', true);
    if(standAnimation && !player.isSneaking) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
    else {
      if ( !player.hasTag('ks_pa_attack_anim') ) {
        playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
        if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
      }
    }
  } else if (idle && holdingSword && health > 10) {
    states.loopRideMinecart = false
    player.setDynamicProperty('ks_pa:loop_ride_minecart', false);
    states.loopIdleSword = true
    player.setDynamicProperty('ks_pa:loop_idle_sword', true);
    if (third) { player.playAnimation("animation.ks_pa.player.third.idle_sword", { blendOutTime: 0.28, stopExpression: "query.log", controller: "third_animate_loop" }) }
  } else if (idle && !holdingSword && health <= 10 && lowHealthAnimation) {
    states.loopRideMinecart = false;
    player.setDynamicProperty('ks_pa:loop_ride_minecart', false);
    states.loopIdleLowHealth = true;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_low_health", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (walk && health > 10 && !player.isSprinting && holdingSword) {
    states.loopRideMinecart = false
    player.setDynamicProperty('ks_pa:loop_ride_minecart', false);
    states.loopWalkSword = true
    player.setDynamicProperty('ks_pa:loop_walk_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.walk_sword", "animation.ks_pa.player.first.walk", 0.38, "third_animate_loop", "first_animate_loop");
  } else if (walk && health <= 10 && !player.isSprinting && lowHealthAnimation) {
    states.loopRideMinecart = false
    player.setDynamicProperty('ks_pa:loop_ride_minecart', false);
    states.loopWalkLowHealth = true
    player.setDynamicProperty('ks_pa:loop_walk_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.walk_low_health", "animation.ks_pa.player.first.walk_low_health", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (player.isSprinting && walk && (health > 10 || !lowHealthAnimation) && !holdingSword) {
    states.loopRideMinecart = false
    player.setDynamicProperty('ks_pa:loop_ride_minecart', false);
    states.loopSprint = true
    player.setDynamicProperty('ks_pa:loop_sprint', true);
    if (sprintAnimation)
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sprint", "animation.ks_pa.player.first.sprint", 0.32, "third_animate_loop", "first_animate_loop");
    else
      if (!player.hasTag('ks_pa_attack_anim')) {
        if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
      }
  }

  if (sneak) {
    states.loopRideMinecart = false
    player.setDynamicProperty('ks_pa:loop_ride_minecart', false);
    states.loopSneakStill = true
    player.setDynamicProperty('ks_pa:loop_sneak_still', true);
    if (third) {
      if (sneakAnimation) player.playAnimation("animation.ks_pa.player.third.sneak_still", { blendOutTime: 0.36, stopExpression: "query.log", controller: "third_animate_loop" })
    }
    else {
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.sneaking", "animation.ks_pa.player.sneaking", 0.38, "third_animate_loop", "first_animate_loop");
      if (!player.hasTag('ks_pa_attack_anim') && third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (sneakMove) {
    states.loopRideMinecart = false
    player.setDynamicProperty('ks_pa:loop_ride_minecart', false);
    states.loopSneak = true
    player.setDynamicProperty('ks_pa:loop_sneak', true);
    if (sneakAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sneak", "animation.ks_pa.player.first.sneak", 0.3, "third_animate_loop", "first_animate_loop");
    else {
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.sneaking", "animation.ks_pa.player.sneaking", 0.38, "third_animate_loop", "first_animate_loop");
      if (!player.hasTag('ks_pa_attack_anim') && third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (climb) {
    states.loopRideMinecart = false
    player.setDynamicProperty('ks_pa:loop_ride_minecart', false);
    states.loopClimb = true
    player.setDynamicProperty('ks_pa:loop_climb', true);
    if (climbAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.climb", "animation.ks_pa.player.first.climb", 0.48, "third_animate_loop", "first_animate_loop");
    else {
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (!player.hasTag('ks_pa_attack_anim') && third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (jump) {
    states.loopJump = true
    player.setDynamicProperty('ks_pa:loop_jump', true);
    if ( jumpAnimation ) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.jump_start_layer", "animation.ks_pa.player.first.jump_start_layer", 0.26, "third_animate_once", "first_animate_once");
  }

  if (fall) {
    states.loopFall = true
    player.setDynamicProperty('ks_pa:loop_fall', true);
    if (fallAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.falling_long_layer", "animation.ks_pa.player.first.falling_long_layer", 0.24, "third_animate_once", "first_animate_once");
  }

  if (flyStand) {
    states.loopRideMinecart = false
    player.setDynamicProperty('ks_pa:loop_ride_minecart', false);
    states.loopFlyStill = true
    player.setDynamicProperty('ks_pa:loop_fly_still', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly_still", "animation.ks_pa.player.first.fly_still", 0.58, "third_animate_loop", "first_animate_loop");
    else {
      if ( !player.hasTag('ks_pa_attack_anim') ) {
        playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
        if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
      }
    }
  } else if (fly) {
    states.loopRideMinecart = false
    player.setDynamicProperty('ks_pa:loop_ride_minecart', false);
    states.loopFly = true
    player.setDynamicProperty('ks_pa:loop_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  } else if (flyFast) {
    states.loopRideMinecart = false
    player.setDynamicProperty('ks_pa:loop_ride_minecart', false);
    states.loopFastFly = true
    player.setDynamicProperty('ks_pa:loop_fast_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fast_fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  }

  if (swimStand) {
    states.loopRideMinecart = false
    player.setDynamicProperty('ks_pa:loop_ride_minecart', false);
    states.loopSwimStill = true
    player.setDynamicProperty('ks_pa:loop_swim_still', true);
    if (swimAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim_still", "animation.ks_pa.player.first.swim_still", 0.0, "third_animate_loop", "first_animate_loop");
  } if (swim) {
    states.loopRideMinecart = false
    player.setDynamicProperty('ks_pa:loop_ride_minecart', false);
    states.loopSwim = true
    player.setDynamicProperty('ks_pa:loop_swim', true);
    if (swimAnimation) {
      //playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim", "animation.ks_pa.player.first.swim", 0.0, "third_animate_loop", "first_animate_loop");
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "first_animate_loop" }) }
    }
  }

  if (crawlStand) {
    states.loopRideMinecart = false
    player.setDynamicProperty('ks_pa:loop_ride_minecart', false);
    states.loopCrawlStill = true
    player.setDynamicProperty('ks_pa:loop_crawl_still', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl_still", "animation.ks_pa.player.first.crawl_still", 0.38, "third_animate_loop", "first_animate_loop");
  } else if (crawl) {
    states.loopRideMinecart = false
    player.setDynamicProperty('ks_pa:loop_ride_minecart', false);
    states.loopCrawl = true
    player.setDynamicProperty('ks_pa:loop_crawl', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl", "animation.ks_pa.player.first.crawl", 0.38, "third_animate_loop", "first_animate_loop");
  }
}

function callRideMinecartMove(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const velocity = states.testPlayerVelocity;
  const isRidingMinecart = player.getDynamicProperty("ks_pa:ride_minecart") && minecartAnimation;
  const health = player.getComponent("minecraft:health").currentValue;
  const canMove = velocity >= 0.12;
  const isIdle = velocity < 0.01;
  const holdingSword = states.testholdSword && swordAnimations;
  const isGrounded = player.isOnGround && !player.isClimbing;
  const idleMinecart = isIdle && isRidingMinecart;
  const idle = isIdle && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const walk = canMove && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const sneak = velocity < 0.04 && player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const sneakMove = velocity >= 0.04 && player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const climb = player.isClimbing && !player.isOnGround && !player.isInWater && !player.isSwimming;
  const jump = player.isJumping && !player.isFalling && !player.isInWater && !player.isSwimming && states.loopJump == false && states.loopFall == false;
  const fall = player.isFalling && !player.isInWater && states.loopJump == false && states.loopFall == false;
  const flyStand = velocity < 0.12 && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const fly = velocity >= 0.12 && !player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const flyFast = velocity >= 0.12 && player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const swimStand = player.isInWater && velocity < 0.12;
  const swim = player.isInWater && velocity >= 0.12;
  const crawlStand = velocity < 0.04 && states.canCrawl == true;
  const crawl = velocity >= 0.04 && states.canCrawl == true;
  if (idleMinecart) {
    states.loopRideMinecartMove = false;
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', false);
    states.loopRideMinecart = true;
    player.setDynamicProperty('ks_pa:loop_ride_minecart', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_idle", "animation.ks_pa.humanoid_fpp.minecart_idle", 0.28, "third_animate_loop", "first_animate_loop");
  }

  if (isRidingMinecart && !isIdle) return;

  if (idle && !holdingSword && (health > 10 || !lowHealthAnimation) && !states.loopIdleEvent) {
    states.loopRideMinecartMove = false;
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', false);
    states.loopIdle = true;
    player.setDynamicProperty('ks_pa:loop_idle', true);
    if(standAnimation && !player.isSneaking) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
    else if ( !player.hasTag('ks_pa_attack_anim') ) {
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (idle && holdingSword && health > 10) {
    states.loopRideMinecartMove = false
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', false);
    states.loopIdleSword = true
    player.setDynamicProperty('ks_pa:loop_idle_sword', true);
    if (third) { player.playAnimation("animation.ks_pa.player.third.idle_sword", { blendOutTime: 0.28, stopExpression: "query.log", controller: "third_animate_loop" }) }
  } else if (idle && !holdingSword && health <= 10 && lowHealthAnimation) {
    states.loopRideMinecartMove = false;
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', false);
    states.loopIdleLowHealth = true;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_low_health", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (walk && health > 10 && !player.isSprinting && holdingSword) {
    states.loopRideMinecartMove = false;
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', false);
    states.loopWalkSword = true;
    player.setDynamicProperty('ks_pa:loop_walk_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.walk_sword", "animation.ks_pa.player.first.walk", 0.38, "third_animate_loop", "first_animate_loop");
  } else if (walk && health <= 10 && !player.isSprinting && lowHealthAnimation) {
    states.loopRideMinecartMove = false;
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', false);
    states.loopWalkLowHealth = true;
    player.setDynamicProperty('ks_pa:loop_walk_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.walk_low_health", "animation.ks_pa.player.first.walk_low_health", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (player.isSprinting && walk && (health > 10 || !lowHealthAnimation) && !holdingSword) {
    states.loopRideMinecartMove = false;
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', false);
    states.loopSprint = true;
    player.setDynamicProperty('ks_pa:loop_sprint', true);
    if (sprintAnimation)
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sprint", "animation.ks_pa.player.first.sprint", 0.32, "third_animate_loop", "first_animate_loop");
    else
      if (!player.hasTag('ks_pa_attack_anim') && third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (sneak) {
    states.loopRideMinecartMove = false
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', false);
    states.loopSneakStill = true
    player.setDynamicProperty('ks_pa:loop_sneak_still', true);
        if (third) {
      if (sneakAnimation) player.playAnimation("animation.ks_pa.player.third.sneak_still", { blendOutTime: 0.36, stopExpression: "query.log", controller: "third_animate_loop" })
    }
    else {
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.sneaking", "animation.ks_pa.player.sneaking", 0.38, "third_animate_loop", "first_animate_loop");
      if (!player.hasTag('ks_pa_attack_anim') && third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (sneakMove) {
    states.loopRideMinecartMove = false;
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', false);
    states.loopSneak = true;
    player.setDynamicProperty('ks_pa:loop_sneak', true);
    if (sneakAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sneak", "animation.ks_pa.player.first.sneak", 0.3, "third_animate_loop", "first_animate_loop");
    else {
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.sneaking", "animation.ks_pa.player.sneaking", 0.38, "third_animate_loop", "first_animate_loop");
      if (!player.hasTag('ks_pa_attack_anim') && third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (climb) {
    states.loopRideMinecartMove = false;
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', false);
    states.loopClimb = true;
    player.setDynamicProperty('ks_pa:loop_climb', true);
    if (climbAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.climb", "animation.ks_pa.player.first.climb", 0.48, "third_animate_loop", "first_animate_loop");
    else if ( !player.hasTag('ks_pa_attack_anim') ) {
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (jump) {
    states.loopJump = true
    player.setDynamicProperty('ks_pa:loop_jump', true);
    if ( jumpAnimation ) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.jump_start_layer", "animation.ks_pa.player.first.jump_start_layer", 0.26, "third_animate_once", "first_animate_once");
  }

  if (fall) {
    states.loopFall = true;
    player.setDynamicProperty('ks_pa:loop_fall', true);
    if (fallAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.falling_long_layer", "animation.ks_pa.player.first.falling_long_layer", 0.24, "third_animate_once", "first_animate_once");
  }

  if (flyStand) {
    states.loopRideMinecartMove = false;
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', false);
    states.loopFlyStill = true;
    player.setDynamicProperty('ks_pa:loop_fly_still', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly_still", "animation.ks_pa.player.first.fly_still", 0.58, "third_animate_loop", "first_animate_loop");
    else if ( !player.hasTag('ks_pa_attack_anim') ) {
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (fly) {
    states.loopRideMinecartMove = false;
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', false);
    states.loopFly = true;
    player.setDynamicProperty('ks_pa:loop_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  } else if (flyFast) {
    states.loopRideMinecartMove = false;
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', false);
    states.loopFastFly = true;
    player.setDynamicProperty('ks_pa:loop_fast_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fast_fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  }

  if (swimStand) {
    states.loopRideMinecartMove = false;
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', false);
    states.loopSwimStill = true;
    player.setDynamicProperty('ks_pa:loop_swim_still', true);
    if (swimAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim_still", "animation.ks_pa.player.first.swim_still", 0.0, "third_animate_loop", "first_animate_loop");
  } if (swim) {
    states.loopRideMinecartMove = false;
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', false);
    states.loopSwim = true;
    player.setDynamicProperty('ks_pa:loop_swim', true);
    if (swimAnimation) {
      //playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim", "animation.ks_pa.player.first.swim", 0.0, "third_animate_loop", "first_animate_loop");
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "first_animate_loop" }) }
    }
  }

  if (crawlStand) {
    states.loopRideMinecartMove = false;
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', false);
    states.loopCrawlStill = true;
    player.setDynamicProperty('ks_pa:loop_crawl_still', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl_still", "animation.ks_pa.player.first.crawl_still", 0.38, "third_animate_loop", "first_animate_loop");
  } else if (crawl) {
    states.loopRideMinecartMove = false;
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', false);
    states.loopCrawl = true;
    player.setDynamicProperty('ks_pa:loop_crawl', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl", "animation.ks_pa.player.first.crawl", 0.38, "third_animate_loop", "first_animate_loop");
  }
}

function callIdle(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const velocity = states.testPlayerVelocity;
  const isRidingMinecart = player.getDynamicProperty("ks_pa:ride_minecart") && minecartAnimation;
  const health = player.getComponent("minecraft:health").currentValue;
  const canMove = velocity >= 0.12;
  const isIdle = velocity < 0.01;
  const holdingSword = states.testholdSword && swordAnimations;
  const isGrounded = player.isOnGround && !player.isClimbing;
  const idleMinecart = isIdle && isRidingMinecart;
  const idle = isIdle && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const walk = canMove && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const sneak = velocity < 0.04 && player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const sneakMove = velocity >= 0.04 && player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const climb = player.isClimbing && !player.isOnGround && !player.isInWater && !player.isSwimming;
  const jump = player.isJumping && !player.isFalling && !player.isInWater && !player.isSwimming && states.loopJump == false && states.loopFall == false;
  const fall = player.isFalling && !player.isInWater && states.loopJump == false && states.loopFall == false;
  const flyStand = velocity < 0.12 && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const fly = velocity >= 0.12 && !player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const flyFast = velocity >= 0.12 && player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const swimStand = player.isInWater && velocity < 0.12;
  const swim = player.isInWater && velocity >= 0.12;
  const crawlStand = velocity < 0.04 && states.canCrawl == true;
  const crawl = velocity >= 0.04 && states.canCrawl == true;
  if (!standAnimation && !states.loopIdleEvent){
    if ( !player.hasTag('ks_pa_attack_anim') ) {
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }
  if (isRidingMinecart && isIdle) {
    states.loopIdle = false
    player.setDynamicProperty('ks_pa:loop_idle', false);
    states.loopRideMinecart = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_idle", "animation.ks_pa.humanoid_fpp.minecart_idle", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (isRidingMinecart && !isIdle) {
    states.loopIdle = false
    player.setDynamicProperty('ks_pa:loop_idle', false);
    states.loopRideMinecartMove = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_move", "animation.ks_pa.humanoid_fpp.minecart_move", 0.28, "third_animate_loop", "first_animate_loop");
  }

  if (idle && holdingSword && health > 10) {
    states.loopIdle = false
    player.setDynamicProperty('ks_pa:loop_idle', false);
    states.loopIdleSword = true;
    player.setDynamicProperty('ks_pa:loop_idle_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_sword", "animation.ks_pa.player.first.idle_sword", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (idle && !holdingSword && health <= 10 && lowHealthAnimation) {
    states.loopIdle = false
    player.setDynamicProperty('ks_pa:loop_idle', false);
    states.loopIdleLowHealth = true
    player.setDynamicProperty('ks_pa:loop_idle_low_health', true);
    if (third) { player.playAnimation("animation.ks_pa.player.third.idle_low_health", { blendOutTime: 0.38, controller: "third_animate_loop" }) }
    if (first) { player.playAnimation("animation.ks_pa.player.first.idle", { blendOutTime: 0.38, stopExpression: "query.log", controller: "first_animate_loop" }) }
  }

  if (walk && !player.isSprinting ) {
    states.loopIdle = false
    player.setDynamicProperty('ks_pa:loop_idle', false);
    states.loopWalk = true
    player.setDynamicProperty('ks_pa:loop_walk', true);
    if (walkAnimation){
      if (third) { player.playAnimation("animation.ks_pa.player.third.walk", { blendOutTime: 0.42, stopExpression: "query.log", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.walk", { blendOutTime: 0.38, stopExpression: "query.log", controller: "first_animate_loop" }) }
    }
    else if (!player.hasTag('ks_pa_attack_anim')) {
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (player.isSprinting && walk && (health > 10 || !lowHealthAnimation) && !holdingSword) {
    states.loopIdle = false
    player.setDynamicProperty('ks_pa:loop_idle', false);
    states.loopSprint = true
    player.setDynamicProperty('ks_pa:loop_sprint', true);
    if (sprintAnimation)
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sprint", "animation.ks_pa.player.first.sprint", 0.32, "third_animate_loop", "first_animate_loop");
else if (!player.hasTag('ks_pa_attack_anim')) {
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (sneak) {
    states.loopIdle = false
    player.setDynamicProperty('ks_pa:loop_idle', false);
    states.loopSneakStill = true
    player.setDynamicProperty('ks_pa:loop_sneak_still', true);
        if (third) {
      if (sneakAnimation) player.playAnimation("animation.ks_pa.player.third.sneak_still", { blendOutTime: 0.36, stopExpression: "query.log", controller: "third_animate_loop" })
    }
    else {
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.sneaking", "animation.ks_pa.player.sneaking", 0.38, "third_animate_loop", "first_animate_loop");
      if (!player.hasTag('ks_pa_attack_anim') && third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (climb) {
    states.loopIdle = false
    player.setDynamicProperty('ks_pa:loop_idle', false);
    states.loopClimb = true
    player.setDynamicProperty('ks_pa:loop_climb', true);
    if (climbAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.climb", "animation.ks_pa.player.first.climb", 0.48, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (jump) {
    states.loopJump = true
    player.setDynamicProperty('ks_pa:loop_jump', true);
    if ( jumpAnimation ) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.jump_start_layer", "animation.ks_pa.player.first.jump_start_layer", 0.26, "third_animate_once", "first_animate_once");
  }

  if (flyStand) {
    states.loopIdle = false
    player.setDynamicProperty('ks_pa:loop_idle', false);
    states.loopFlyStill = true
    player.setDynamicProperty('ks_pa:loop_fly_still', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly_still", "animation.ks_pa.player.first.fly_still", 0.58, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (fly) {
    states.loopIdle = false
    player.setDynamicProperty('ks_pa:loop_idle', false);
    states.loopFly = true
    player.setDynamicProperty('ks_pa:loop_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  } else if (flyFast) {
    states.loopIdle = false
    player.setDynamicProperty('ks_pa:loop_idle', false);
    states.loopFastFly = true
    player.setDynamicProperty('ks_pa:loop_fast_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fast_fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  }

  if (swimStand) {
    states.loopIdle = false
    player.setDynamicProperty('ks_pa:loop_idle', false);
    states.loopSwimStill = true
    player.setDynamicProperty('ks_pa:loop_swim_still', true);
    if (swimAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim_still", "animation.ks_pa.player.first.swim_still", 0.0, "third_animate_loop", "first_animate_loop");
  } if (swim) {
    states.loopIdle = false
    player.setDynamicProperty('ks_pa:loop_idle', false);
    states.loopSwim = true
    player.setDynamicProperty('ks_pa:loop_swim', true);
    if (swimAnimation) {
      //playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim", "animation.ks_pa.player.first.swim", 0.0, "third_animate_loop", "first_animate_loop");
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "first_animate_loop" }) }
    }
  }

  if (crawlStand) {
    states.loopIdle = false
    player.setDynamicProperty('ks_pa:loop_idle', false);
    states.loopCrawlStill = true
    player.setDynamicProperty('ks_pa:loop_crawl_still', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl_still", "animation.ks_pa.player.first.crawl_still", 0.38, "third_animate_loop", "first_animate_loop");
  } else if (crawl) {
    states.loopIdle = false
    player.setDynamicProperty('ks_pa:loop_idle', false);
    states.loopCrawl = true
    player.setDynamicProperty('ks_pa:loop_crawl', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl", "animation.ks_pa.player.first.crawl", 0.38, "third_animate_loop", "first_animate_loop");
  }
}

function callIdleEvent(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  if (states.finishIdleEvent == true && states.testholdSword == false && player.getComponent("minecraft:health").currentValue > 10 && !lowHealthAnimation) {
    if(standAnimation && !player.isSneaking) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (states.finishIdleEvent == true && states.testholdSword == true && player.getComponent("minecraft:health").currentValue > 10) {
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_sword", "animation.ks_pa.player.first.idle_sword", 0.28, "third_animate_loop", "first_animate_loop");
  }
}

function callIdleSword(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const velocity = states.testPlayerVelocity;
  const isRidingMinecart = player.getDynamicProperty("ks_pa:ride_minecart") && minecartAnimation;
  const health = player.getComponent("minecraft:health").currentValue;
  const canMove = velocity >= 0.12;
  const isIdle = velocity < 0.01;
  const holdingSword = states.testholdSword && swordAnimations;
  const isGrounded = player.isOnGround && !player.isClimbing;
  const idle = isIdle && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const walk = canMove && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const sneak = velocity < 0.04 && player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const climb = player.isClimbing && !player.isOnGround && !player.isInWater && !player.isSwimming;
  const jump = player.isJumping && !player.isFalling && !player.isInWater && !player.isSwimming && states.loopJump == false && states.loopFall == false;
  const flyStand = velocity < 0.12 && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const fly = velocity >= 0.12 && !player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const flyFast = velocity >= 0.12 && player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const swimStand = player.isInWater && velocity < 0.12;
  const swim = player.isInWater && velocity >= 0.12;
  const crawlStand = velocity < 0.04 && states.canCrawl == true;
  const crawl = velocity >= 0.04 && states.canCrawl == true;
  if (isRidingMinecart && isIdle) {
    states.loopIdleSword = false
    player.setDynamicProperty('ks_pa:loop_idle_sword', false);
    states.loopRideMinecart = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_idle", "animation.ks_pa.humanoid_fpp.minecart_idle", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (isRidingMinecart && !isIdle) {
    states.loopIdleSword = false
    player.setDynamicProperty('ks_pa:loop_idle_sword', false);
    states.loopRideMinecartMove = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_move", "animation.ks_pa.humanoid_fpp.minecart_move", 0.28, "third_animate_loop", "first_animate_loop");
  }

  if (idle && !holdingSword && (health > 10 || !lowHealthAnimation) && !states.loopIdleEvent) {
    states.loopIdleSword = false
    player.setDynamicProperty('ks_pa:loop_idle_sword', false);
    states.loopIdle = true
    player.setDynamicProperty('ks_pa:loop_idle', true);
    if(standAnimation && !player.isSneaking) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (idle && !holdingSword && health <= 10 && lowHealthAnimation) {
    states.loopIdleSword = false;
    player.setDynamicProperty('ks_pa:loop_idle_sword', false);
    states.loopIdleLowHealth = true;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_low_health", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (walk && health > 10 && !player.isSprinting && holdingSword) {
    states.loopIdleSword = false
    player.setDynamicProperty('ks_pa:loop_idle_sword', false);
    states.loopWalkSword = true
    player.setDynamicProperty('ks_pa:loop_walk_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.walk_sword", "animation.ks_pa.player.first.walk", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (player.isSprinting && walk && health > 10) {
    states.loopIdleSword = false
    player.setDynamicProperty('ks_pa:loop_idle_sword', false);
    states.loopSprintSword = true
    player.setDynamicProperty('ks_pa:loop_sprint_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sprint_sword", "animation.ks_pa.player.first.sprint_sword", 0.32, "third_animate_loop", "first_animate_loop");
  }

  if (sneak) {
    states.loopIdleSword = false
    player.setDynamicProperty('ks_pa:loop_idle_sword', false);
    states.loopSneakStill = true
    player.setDynamicProperty('ks_pa:loop_sneak_still', true);
        if (third) {
      if (sneakAnimation) player.playAnimation("animation.ks_pa.player.third.sneak_still", { blendOutTime: 0.36, stopExpression: "query.log", controller: "third_animate_loop" })
    }
    else {
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.sneaking", "animation.ks_pa.player.sneaking", 0.38, "third_animate_loop", "first_animate_loop");
      if (!player.hasTag('ks_pa_attack_anim') && third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (climb) {
    states.loopIdleSword = false
    player.setDynamicProperty('ks_pa:loop_idle_sword', false);
    states.loopClimb = true
    player.setDynamicProperty('ks_pa:loop_climb', true);
    if (climbAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.climb", "animation.ks_pa.player.first.climb", 0.48, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (jump) {
    states.loopJump = true
    player.setDynamicProperty('ks_pa:loop_jump', true);
    if ( jumpAnimation ) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.jump_start_layer", "animation.ks_pa.player.first.jump_start_layer", 0.26, "third_animate_once", "first_animate_once");
  }

  if (flyStand) {
    states.loopIdleSword = false
    player.setDynamicProperty('ks_pa:loop_idle_sword', false);
    states.loopFlyStill = true
    player.setDynamicProperty('ks_pa:loop_fly_still', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly_still", "animation.ks_pa.player.first.fly_still", 0.58, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (fly) {
    states.loopIdleSword = false
    player.setDynamicProperty('ks_pa:loop_idle_sword', false);
    states.loopFly = true
    player.setDynamicProperty('ks_pa:loop_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  } else if (flyFast) {
    states.loopIdleSword = false
    player.setDynamicProperty('ks_pa:loop_idle_sword', false);
    states.loopFastFly = true
    player.setDynamicProperty('ks_pa:loop_fast_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fast_fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  }

  if (swimStand) {
    states.loopIdleSword = false
    player.setDynamicProperty('ks_pa:loop_idle_sword', false);
    states.loopSwimStill = true
    player.setDynamicProperty('ks_pa:loop_swim_still', true);
    if (swimAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim_still", "animation.ks_pa.player.first.swim_still", 0.0, "third_animate_loop", "first_animate_loop");
  } if (swim) {
    states.loopIdleSword = false
    player.setDynamicProperty('ks_pa:loop_idle_sword', false);
    states.loopSwim = true
    player.setDynamicProperty('ks_pa:loop_swim', true);
    if (swimAnimation) {
      //playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim", "animation.ks_pa.player.first.swim", 0.0, "third_animate_loop", "first_animate_loop");
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "first_animate_loop" }) }
    }
  }

  if (crawlStand) {
    states.loopIdleSword = false
    player.setDynamicProperty('ks_pa:loop_idle_sword', false);
    states.loopCrawlStill = true
    player.setDynamicProperty('ks_pa:loop_crawl_still', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl_still", "animation.ks_pa.player.first.crawl_still", 0.38, "third_animate_loop", "first_animate_loop");
  } else if (crawl) {
    states.loopIdleSword = false
    player.setDynamicProperty('ks_pa:loop_idle_sword', false);
    states.loopCrawl = true
    player.setDynamicProperty('ks_pa:loop_crawl', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl", "animation.ks_pa.player.first.crawl", 0.38, "third_animate_loop", "first_animate_loop");
  }
}

function callIdleLowHealth(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const velocity = states.testPlayerVelocity;
  const health = player.getComponent("minecraft:health").currentValue;
  const canMove = velocity >= 0.12;
  const isIdle = velocity < 0.01;
  const holdingSword = states.testholdSword && swordAnimations;
  const isGrounded = player.isOnGround && !player.isClimbing;
  const idle = isIdle && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const walk = canMove && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const sneak = velocity < 0.04 && player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const climb = player.isClimbing && !player.isOnGround && !player.isInWater && !player.isSwimming;
  const jump = player.isJumping && !player.isFalling && !player.isInWater && !player.isSwimming && states.loopJump == false && states.loopFall == false;
  const flyStand = velocity < 0.12 && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const fly = velocity >= 0.12 && !player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const flyFast = velocity >= 0.12 && player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const swimStand = player.isInWater && velocity < 0.12;
  const swim = player.isInWater && velocity >= 0.12;
  const crawlStand = velocity < 0.04 && states.canCrawl == true;
  const crawl = velocity >= 0.04 && states.canCrawl == true;
  if (idle && health <= 10 && lowHealthAnimation) {
    states.timer++;
    player.setDynamicProperty('ks_pa:timer', states.timer);
    if(states.timer % 17 == 0) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_low_health", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
  }
  if (idle && !holdingSword && (health > 10 || !lowHealthAnimation) && !states.loopIdleEvent) {
    states.loopIdleLowHealth = false;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', false);
    states.loopIdle = true;
    player.setDynamicProperty('ks_pa:loop_idle', true);
    states.timer = 0;
    player.setDynamicProperty('ks_pa:timer', states.timer);
    if (third) { player.playAnimation("animation.ks_pa.player.third.idle", { blendOutTime: 0.28, stopExpression: "query.log", controller: "third_animate_loop" }) }
    if (first) { player.playAnimation("animation.ks_pa.player.first.idle", { blendOutTime: 0.38, stopExpression: "query.log", controller: "first_animate_loop" }) }
  }

  if (walk && health <= 10 && !player.isSprinting && lowHealthAnimation) {
    states.loopIdleLowHealth = false;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', false);
    states.loopWalkLowHealth = true;
    player.setDynamicProperty('ks_pa:loop_walk_low_health', true);
    states.timer = 0;
    player.setDynamicProperty('ks_pa:timer', states.timer);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.walk_low_health", "animation.ks_pa.player.first.walk_low_health", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (player.isSprinting && walk && health <= 10 && lowHealthAnimation) {
    states.loopIdleLowHealth = false;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', false);
    states.loopSprintLowHealth = true;
    player.setDynamicProperty('ks_pa:loop_sprint_low_health', true);
    states.timer = 0;
    player.setDynamicProperty('ks_pa:timer', states.timer);
    if (third) { player.playAnimation("animation.ks_pa.player.third.sprint_low_health", { blendOutTime: 0.34, stopExpression: "query.log", controller: "third_animate_loop" }) }
    if (first) { player.playAnimation("animation.ks_pa.player.first.sprint_low_health", { blendOutTime: 0.28, stopExpression: "query.log", controller: "first_animate_loop" }) }
  }

  if (sneak) {
    states.loopIdleLowHealth = false;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', false);
    states.loopSneakStill = true;
    player.setDynamicProperty('ks_pa:loop_sneak_still', true);
    states.timer = 0;
    player.setDynamicProperty('ks_pa:timer', states.timer);
        if (third) {
      if (sneakAnimation) player.playAnimation("animation.ks_pa.player.third.sneak_still", { blendOutTime: 0.36, stopExpression: "query.log", controller: "third_animate_loop" })
    }
    else {
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.sneaking", "animation.ks_pa.player.sneaking", 0.38, "third_animate_loop", "first_animate_loop");
      if (!player.hasTag('ks_pa_attack_anim') && third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (climb) {
    states.loopIdleLowHealth = false;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', false);
    states.loopClimb = true;
    player.setDynamicProperty('ks_pa:loop_climb', true);
    states.timer = 0;
    player.setDynamicProperty('ks_pa:timer', states.timer);
    if (climbAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.climb", "animation.ks_pa.player.first.climb", 0.48, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (jump) {
    states.loopIdleLowHealth = false;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', false);
    states.loopJump = true;
    player.setDynamicProperty('ks_pa:loop_jump', true);
    states.timer = 0;
    player.setDynamicProperty('ks_pa:timer', states.timer);
    if ( jumpAnimation ) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.jump_start_layer", "animation.ks_pa.player.first.jump_start_layer", 0.26, "third_animate_once", "first_animate_once");
  }

  if (flyStand) {
    states.loopIdleLowHealth = false;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', false);
    states.loopFlyStill = true;
    player.setDynamicProperty('ks_pa:loop_fly_still', true);
    states.timer = 0;
    player.setDynamicProperty('ks_pa:timer', states.timer);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly_still", "animation.ks_pa.player.first.fly_still", 0.58, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (fly) {
    states.loopIdleLowHealth = false;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', false);
    states.loopFly = true;
    player.setDynamicProperty('ks_pa:loop_fly', true);
    states.timer = 0;
    player.setDynamicProperty('ks_pa:timer', states.timer);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  } else if (flyFast) {
    states.loopIdleLowHealth = false;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', false);
    states.loopFastFly = true;
    player.setDynamicProperty('ks_pa:loop_fast_fly', true);
    states.timer = 0;
    player.setDynamicProperty('ks_pa:timer', states.timer);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fast_fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  }

  if (swimStand) {
    states.loopIdleLowHealth = false;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', false);
    states.loopSwimStill = true;
    player.setDynamicProperty('ks_pa:loop_swim_still', true);
    states.timer = 0;
    player.setDynamicProperty('ks_pa:timer', states.timer);
    if (swimAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim_still", "animation.ks_pa.player.first.swim_still", 0.0, "third_animate_loop", "first_animate_loop");
  } if (swim) {
    states.loopIdleLowHealth = false;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', false);
    states.loopSwim = true;
    player.setDynamicProperty('ks_pa:loop_swim', true);
    states.timer = 0;
    player.setDynamicProperty('ks_pa:timer', states.timer);
    if (swimAnimation) {
      //playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim", "animation.ks_pa.player.first.swim", 0.0, "third_animate_loop", "first_animate_loop");
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "first_animate_loop" }) }
    }
  }

  if (crawlStand) {
    states.loopIdleLowHealth = false;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', false);
    states.loopCrawlStill = true;
    player.setDynamicProperty('ks_pa:loop_crawl_still', true);
    states.timer = 0;
    player.setDynamicProperty('ks_pa:timer', states.timer);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl_still", "animation.ks_pa.player.first.crawl_still", 0.38, "third_animate_loop", "first_animate_loop");
  } else if (crawl) {
    states.loopIdleLowHealth = false;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', false);
    states.loopCrawl = true;
    player.setDynamicProperty('ks_pa:loop_crawl', true);
    states.timer = 0;
    player.setDynamicProperty('ks_pa:timer', states.timer);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl", "animation.ks_pa.player.first.crawl", 0.38, "third_animate_loop", "first_animate_loop");
  }
}

function callWalk(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const velocity = states.testPlayerVelocity;
  const isRidingMinecart = player.getDynamicProperty("ks_pa:ride_minecart") && minecartAnimation;
  const health = player.getComponent("minecraft:health").currentValue;
  const canMove = velocity >= 0.12;
  const isIdle = velocity < 0.01;
  const holdingSword = states.testholdSword && swordAnimations;
  const isGrounded = player.isOnGround && !player.isClimbing;
  const idle = isIdle && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const walk = canMove && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const sneak = velocity < 0.04 && player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const sneakMove = velocity >= 0.04 && player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const climb = player.isClimbing && !player.isOnGround && !player.isInWater && !player.isSwimming;
  const jump = player.isJumping && !player.isFalling && !player.isInWater && !player.isSwimming && states.loopJump == false && states.loopFall == false;
  const fall = player.isFalling && !player.isInWater && states.loopJump == false && states.loopFall == false;
  const flyStand = velocity < 0.12 && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const fly = velocity >= 0.12 && !player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const flyFast = velocity >= 0.12 && player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const swimStand = player.isInWater && velocity < 0.12;
  const swim = player.isInWater && velocity >= 0.12;
  const crawlStand = velocity < 0.04 && states.canCrawl == true;
  const crawl = velocity >= 0.04 && states.canCrawl == true;
  if (isRidingMinecart && isIdle) {
    states.loopWalk = false
    player.setDynamicProperty('ks_pa:loop_walk', false);
    states.loopRideMinecart = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_idle", "animation.ks_pa.humanoid_fpp.minecart_idle", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (isRidingMinecart && !isIdle) {
    states.loopWalk = false
    player.setDynamicProperty('ks_pa:loop_walk', false);
    states.loopRideMinecartMove = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_move", "animation.ks_pa.humanoid_fpp.minecart_move", 0.28, "third_animate_loop", "first_animate_loop");
  }

  if (idle && !holdingSword && (health > 10 || !lowHealthAnimation) && !states.loopIdleEvent) {
    states.loopWalk = false
    player.setDynamicProperty('ks_pa:loop_walk', false);
    states.loopIdle = true
    player.setDynamicProperty('ks_pa:loop_idle', true);
    if(standAnimation && !player.isSneaking) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (idle && holdingSword && health > 10) {
    states.loopWalk = false
    player.setDynamicProperty('ks_pa:loop_walk', false);
    states.loopIdleSword = true
    player.setDynamicProperty('ks_pa:loop_idle_sword', true);
    if (third) { player.playAnimation("animation.ks_pa.player.third.idle_sword", { blendOutTime: 0.28, stopExpression: "query.log", controller: "third_animate_loop" }) }
  } else if (idle && !holdingSword && health <= 10 && lowHealthAnimation) {
    states.loopWalk = false;
    player.setDynamicProperty('ks_pa:loop_walk', false);
    states.loopIdleLowHealth = true;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_low_health", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (walk && health > 10 && !player.isSprinting && holdingSword) {
    states.loopWalk = false
    player.setDynamicProperty('ks_pa:loop_walk', false);
    states.loopWalkSword = true
    player.setDynamicProperty('ks_pa:loop_walk_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.walk_sword", "animation.ks_pa.player.first.walk", 0.38, "third_animate_loop", "first_animate_loop");
  } else if (walk && health <= 10 && !player.isSprinting && lowHealthAnimation) {
    states.loopWalk = false
    player.setDynamicProperty('ks_pa:loop_walk', false);
    states.loopWalkLowHealth = true
    player.setDynamicProperty('ks_pa:loop_walk_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.walk_low_health", "animation.ks_pa.player.first.walk_low_health", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (player.isSprinting && walk && (health > 10 || !lowHealthAnimation) && !holdingSword) {
    states.loopWalk = false
    player.setDynamicProperty('ks_pa:loop_walk', false);
    states.loopSprint = true
    player.setDynamicProperty('ks_pa:loop_sprint', true);
    if (sprintAnimation)
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sprint", "animation.ks_pa.player.first.sprint", 0.32, "third_animate_loop", "first_animate_loop");
else if (!player.hasTag('ks_pa_attack_anim')) {
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (sneak) {
    states.loopWalk = false
    player.setDynamicProperty('ks_pa:loop_walk', false);
    states.loopSneakStill = true
    player.setDynamicProperty('ks_pa:loop_sneak_still', true);
        if (third) {
      if (sneakAnimation) player.playAnimation("animation.ks_pa.player.third.sneak_still", { blendOutTime: 0.36, stopExpression: "query.log", controller: "third_animate_loop" })
    }
    else {
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.sneaking", "animation.ks_pa.player.sneaking", 0.38, "third_animate_loop", "first_animate_loop");
      if (!player.hasTag('ks_pa_attack_anim') && third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (sneakMove) {
    states.loopWalk = false
    player.setDynamicProperty('ks_pa:loop_walk', false);
    states.loopSneak = true
    player.setDynamicProperty('ks_pa:loop_sneak', true);
    if (sneakAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sneak", "animation.ks_pa.player.first.sneak", 0.3, "third_animate_loop", "first_animate_loop");
    else {
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.sneaking", "animation.ks_pa.player.sneaking", 0.38, "third_animate_loop", "first_animate_loop");
      if (!player.hasTag('ks_pa_attack_anim') && third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (climb) {
    states.loopWalk = false
    player.setDynamicProperty('ks_pa:loop_walk', false);
    states.loopClimb = true
    player.setDynamicProperty('ks_pa:loop_climb', true);
    if (climbAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.climb", "animation.ks_pa.player.first.climb", 0.48, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (jump) {
    states.loopJump = true
    player.setDynamicProperty('ks_pa:loop_jump', true);
    if ( jumpAnimation ) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.jump_start_layer", "animation.ks_pa.player.first.jump_start_layer", 0.26, "third_animate_once", "first_animate_once");
  }

  if (fall) {
    states.loopFall = true
    player.setDynamicProperty('ks_pa:loop_fall', true);
    if (fallAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.falling_long_layer", "animation.ks_pa.player.first.falling_long_layer", 0.24, "third_animate_once", "first_animate_once");
  }

  if (flyStand) {
    states.loopWalk = false
    player.setDynamicProperty('ks_pa:loop_walk', false);
    states.loopFlyStill = true
    player.setDynamicProperty('ks_pa:loop_fly_still', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly_still", "animation.ks_pa.player.first.fly_still", 0.58, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (fly) {
    states.loopWalk = false
    player.setDynamicProperty('ks_pa:loop_walk', false);
    states.loopFly = true
    player.setDynamicProperty('ks_pa:loop_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  } else if (flyFast) {
    states.loopWalk = false
    player.setDynamicProperty('ks_pa:loop_walk', false);
    states.loopFastFly = true
    player.setDynamicProperty('ks_pa:loop_fast_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fast_fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  }

  if (swimStand) {
    states.loopWalk = false
    player.setDynamicProperty('ks_pa:loop_walk', false);
    states.loopSwimStill = true
    player.setDynamicProperty('ks_pa:loop_swim_still', true);
    if (swimAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim_still", "animation.ks_pa.player.first.swim_still", 0.0, "third_animate_loop", "first_animate_loop");
    else if (!swimAnimation) {
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim_still", { blendOutTime: 0.0, stopExpression: "query.is_in_water", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim_still", { blendOutTime: 0.0, stopExpression: "query.is_in_water", controller: "first_animate_loop" }) }
    }
  } if (swim) {
    states.loopWalk = false
    player.setDynamicProperty('ks_pa:loop_walk', false);
    states.loopSwim = true
    player.setDynamicProperty('ks_pa:loop_swim', true);
    if (swimAnimation) {
      //playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim", "animation.ks_pa.player.first.swim", 0.0, "third_animate_loop", "first_animate_loop");
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "first_animate_loop" }) }
    }
    else if (!swimAnimation) {
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim", { blendOutTime: 0.0, stopExpression: "query.is_in_water && query.modified_move_speed < 0.1", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim", { blendOutTime: 0.0, stopExpression: "query.is_in_water && query.modified_move_speed < 0.1", controller: "first_animate_loop" }) }
    }
  }

  if (crawlStand) {
    states.loopWalk = false
    player.setDynamicProperty('ks_pa:loop_walk', false);
    states.loopCrawlStill = true
    player.setDynamicProperty('ks_pa:loop_crawl_still', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl_still", "animation.ks_pa.player.first.crawl_still", 0.38, "third_animate_loop", "first_animate_loop");
  } else if (crawl) {
    states.loopWalk = false
    player.setDynamicProperty('ks_pa:loop_walk', false);
    states.loopCrawl = true
    player.setDynamicProperty('ks_pa:loop_crawl', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl", "animation.ks_pa.player.first.crawl", 0.38, "third_animate_loop", "first_animate_loop");
  }
}

function callWalkSword(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const velocity = states.testPlayerVelocity;
  const isRidingMinecart = player.getDynamicProperty("ks_pa:ride_minecart") && minecartAnimation;
  const health = player.getComponent("minecraft:health").currentValue;
  const canMove = velocity >= 0.12;
  const isIdle = velocity < 0.01;
  const holdingSword = states.testholdSword && swordAnimations;
  const isGrounded = player.isOnGround && !player.isClimbing;
  const idle = isIdle && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const walk = canMove && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const sneak = velocity < 0.04 && player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const sneakMove = velocity >= 0.04 && player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const climb = player.isClimbing && !player.isOnGround && !player.isInWater && !player.isSwimming;
  const jump = player.isJumping && !player.isFalling && !player.isInWater && !player.isSwimming && states.loopJump == false && states.loopFall == false;
  const fall = player.isFalling && !player.isInWater && states.loopJump == false && states.loopFall == false;
  const flyStand = velocity < 0.12 && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const fly = velocity >= 0.12 && !player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const flyFast = velocity >= 0.12 && player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const swimStand = player.isInWater && velocity < 0.12;
  const swim = player.isInWater && velocity >= 0.12;
  const crawlStand = velocity < 0.04 && states.canCrawl == true;
  const crawl = velocity >= 0.04 && states.canCrawl == true;
  if (isRidingMinecart && isIdle) {
    states.loopWalkSword = false
    player.setDynamicProperty('ks_pa:loop_walk_sword', false);
    states.loopRideMinecart = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_idle", "animation.ks_pa.humanoid_fpp.minecart_idle", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (isRidingMinecart && !isIdle) {
    states.loopWalkSword = false
    player.setDynamicProperty('ks_pa:loop_walk_sword', false);
    states.loopRideMinecartMove = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_move", "animation.ks_pa.humanoid_fpp.minecart_move", 0.28, "third_animate_loop", "first_animate_loop");
  }

  if (idle && !holdingSword && (health > 10 || !lowHealthAnimation) && !states.loopIdleEvent) {
    states.loopWalkSword = false
    player.setDynamicProperty('ks_pa:loop_walk_sword', false);
    states.loopIdle = true
    player.setDynamicProperty('ks_pa:loop_idle', true);
    if(standAnimation && !player.isSneaking) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (idle && holdingSword && health > 10) {
    states.loopWalkSword = false
    player.setDynamicProperty('ks_pa:loop_walk_sword', false);
    states.loopIdleSword = true
    player.setDynamicProperty('ks_pa:loop_idle_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_sword", "animation.ks_pa.player.first.idle_sword", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (idle && !holdingSword && health <= 10 && lowHealthAnimation) {
    states.loopWalkSword = false;
    player.setDynamicProperty('ks_pa:loop_walk_sword', false);
    states.loopIdleLowHealth = true;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_low_health", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (walk && !player.isSprinting && !holdingSword && (health > 10 || !lowHealthAnimation)) {
    states.loopWalkSword = false
    player.setDynamicProperty('ks_pa:loop_walk_sword', false);
    states.loopWalk = true
    player.setDynamicProperty('ks_pa:loop_walk', true);
    if (walkAnimation){
      if (third) { player.playAnimation("animation.ks_pa.player.third.walk", { blendOutTime: 0.42, stopExpression: "query.log", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.walk", { blendOutTime: 0.38, stopExpression: "query.log", controller: "first_animate_loop" }) }
    }
    else if (!player.hasTag('ks_pa_attack_anim')) {
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (walk && health <= 10 && !player.isSprinting && lowHealthAnimation) {
    states.loopWalkSword = false
    player.setDynamicProperty('ks_pa:loop_walk_sword', false);
    states.loopWalkLowHealth = true
    player.setDynamicProperty('ks_pa:loop_walk_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.walk_low_health", "animation.ks_pa.player.first.walk_low_health", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (player.isSprinting && walk && health > 10 && holdingSword) {
    states.loopWalkSword = false
    player.setDynamicProperty('ks_pa:loop_walk_sword', false);
    states.loopSprintSword = true
    player.setDynamicProperty('ks_pa:loop_sprint_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sprint_sword", "animation.ks_pa.player.first.sprint_sword", 0.32, "third_animate_loop", "first_animate_loop");
  }

  if (sneak) {
    states.loopWalkSword = false
    player.setDynamicProperty('ks_pa:loop_walk_sword', false);
    states.loopSneakStill = true
    player.setDynamicProperty('ks_pa:loop_sneak_still', true);
        if (third) {
      if (sneakAnimation) player.playAnimation("animation.ks_pa.player.third.sneak_still", { blendOutTime: 0.36, stopExpression: "query.log", controller: "third_animate_loop" })
    }
    else {
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.sneaking", "animation.ks_pa.player.sneaking", 0.38, "third_animate_loop", "first_animate_loop");
      if (!player.hasTag('ks_pa_attack_anim') && third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (sneakMove) {
    states.loopWalkSword = false
    player.setDynamicProperty('ks_pa:loop_walk_sword', false);
    states.loopSneak = true
    player.setDynamicProperty('ks_pa:loop_sneak', true);
    if (sneakAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sneak", "animation.ks_pa.player.first.sneak", 0.3, "third_animate_loop", "first_animate_loop");
    else {
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.sneaking", "animation.ks_pa.player.sneaking", 0.38, "third_animate_loop", "first_animate_loop");
      if (!player.hasTag('ks_pa_attack_anim') && third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (climb) {
    states.loopWalkSword = false
    player.setDynamicProperty('ks_pa:loop_walk_sword', false);
    states.loopClimb = true
    player.setDynamicProperty('ks_pa:loop_climb', true);
    if (climbAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.climb", "animation.ks_pa.player.first.climb", 0.48, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (jump) {
    states.loopJump = true
    player.setDynamicProperty('ks_pa:loop_jump', true);
    if ( jumpAnimation ) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.jump_start_layer", "animation.ks_pa.player.first.jump_start_layer", 0.26, "third_animate_once", "first_animate_once");
  }

  if (fall) {
    states.loopFall = true
    player.setDynamicProperty('ks_pa:loop_fall', true);
    if (fallAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.falling_long_layer", "animation.ks_pa.player.first.falling_long_layer", 0.24, "third_animate_once", "first_animate_once");
  }

  if (flyStand) {
    states.loopWalkSword = false
    player.setDynamicProperty('ks_pa:loop_walk_sword', false);
    states.loopFlyStill = true
    player.setDynamicProperty('ks_pa:loop_fly_still', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly_still", "animation.ks_pa.player.first.fly_still", 0.58, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (fly) {
    states.loopWalkSword = false
    player.setDynamicProperty('ks_pa:loop_walk_sword', false);
    states.loopFly = true
    player.setDynamicProperty('ks_pa:loop_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  } else if (flyFast) {
    states.loopWalkSword = false
    player.setDynamicProperty('ks_pa:loop_walk_sword', false);
    states.loopFastFly = true
    player.setDynamicProperty('ks_pa:loop_fast_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fast_fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  }

  if (swimStand) {
    states.loopWalkSword = false
    player.setDynamicProperty('ks_pa:loop_walk_sword', false);
    states.loopSwimStill = true
    player.setDynamicProperty('ks_pa:loop_swim_still', true);
    if (swimAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim_still", "animation.ks_pa.player.first.swim_still", 0.0, "third_animate_loop", "first_animate_loop");
    else if (!swimAnimation) {
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim_still", { blendOutTime: 0.0, stopExpression: "query.is_in_water", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim_still", { blendOutTime: 0.0, stopExpression: "query.is_in_water", controller: "first_animate_loop" }) }
    }
  } if (swim) {
    states.loopWalkSword = false
    player.setDynamicProperty('ks_pa:loop_walk_sword', false);
    states.loopSwim = true
    player.setDynamicProperty('ks_pa:loop_swim', true);
    if (swimAnimation) {
      //playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim", "animation.ks_pa.player.first.swim", 0.0, "third_animate_loop", "first_animate_loop");
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "first_animate_loop" }) }
    }
    else if (!swimAnimation) {
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim", { blendOutTime: 0.0, stopExpression: "query.is_in_water && query.modified_move_speed < 0.1", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim", { blendOutTime: 0.0, stopExpression: "query.is_in_water && query.modified_move_speed < 0.1", controller: "first_animate_loop" }) }
    }
  }

  if (crawlStand) {
    states.loopWalkSword = false
    player.setDynamicProperty('ks_pa:loop_walk_sword', false);
    states.loopCrawlStill = true
    player.setDynamicProperty('ks_pa:loop_crawl_still', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl_still", "animation.ks_pa.player.first.crawl_still", 0.38, "third_animate_loop", "first_animate_loop");
  } else if (crawl) {
    states.loopWalkSword = false
    player.setDynamicProperty('ks_pa:loop_walk_sword', false);
    states.loopCrawl = true
    player.setDynamicProperty('ks_pa:loop_crawl', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl", "animation.ks_pa.player.first.crawl", 0.38, "third_animate_loop", "first_animate_loop");
  }
}

function callWalkLowHealth(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const velocity = states.testPlayerVelocity;
  const isRidingMinecart = player.getDynamicProperty("ks_pa:ride_minecart") && minecartAnimation;
  const health = player.getComponent("minecraft:health").currentValue;
  const canMove = velocity >= 0.12;
  const isIdle = velocity < 0.01;
  const holdingSword = states.testholdSword && swordAnimations;
  const isGrounded = player.isOnGround && !player.isClimbing;
  const idle = isIdle && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const walk = canMove && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const sneak = velocity < 0.04 && player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const sneakMove = velocity >= 0.04 && player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const climb = player.isClimbing && !player.isOnGround && !player.isInWater && !player.isSwimming;
  const jump = player.isJumping && !player.isFalling && !player.isInWater && !player.isSwimming && states.loopJump == false && states.loopFall == false;
  const fall = player.isFalling && !player.isInWater && states.loopJump == false && states.loopFall == false;
  const flyStand = velocity < 0.12 && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const fly = velocity >= 0.12 && !player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const flyFast = velocity >= 0.12 && player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const swimStand = player.isInWater && velocity < 0.12;
  const swim = player.isInWater && velocity >= 0.12;
  const crawlStand = velocity < 0.04 && states.canCrawl == true;
  const crawl = velocity >= 0.04 && states.canCrawl == true;
  if (isRidingMinecart && isIdle) {
    states.loopWalkLowHealth = false
    player.setDynamicProperty('ks_pa:loop_walk_low_health', false);
    states.loopRideMinecart = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_idle", "animation.ks_pa.humanoid_fpp.minecart_idle", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (isRidingMinecart && !isIdle) {
    states.loopWalkLowHealth = false
    player.setDynamicProperty('ks_pa:loop_walk_low_health', false);
    states.loopRideMinecartMove = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_move", "animation.ks_pa.humanoid_fpp.minecart_move", 0.28, "third_animate_loop", "first_animate_loop");
  }

  if (idle && !holdingSword && health <= 10 && lowHealthAnimation) {
    states.loopWalkLowHealth = false;
    player.setDynamicProperty('ks_pa:loop_walk_low_health', false);
    states.loopIdleLowHealth = true;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_low_health", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (walk && !player.isSprinting && (health > 10 || !lowHealthAnimation)) {
    states.loopWalkLowHealth = false
    player.setDynamicProperty('ks_pa:loop_walk_low_health', false);
    states.loopWalk = true
    player.setDynamicProperty('ks_pa:loop_walk', true);
    if (walkAnimation){
      if (third) { player.playAnimation("animation.ks_pa.player.third.walk", { blendOutTime: 0.42, stopExpression: "query.log", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.walk", { blendOutTime: 0.38, stopExpression: "query.log", controller: "first_animate_loop" }) }
    }
    else if (!player.hasTag('ks_pa_attack_anim')) {
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (walk && player.isSprinting && health <= 10 && lowHealthAnimation) {
    states.loopWalkLowHealth = false
    player.setDynamicProperty('ks_pa:loop_walk_low_health', false);
    states.loopSprintLowHealth = true
    player.setDynamicProperty('ks_pa:loop_sprint_low_health', true);
    if (third) { player.playAnimation("animation.ks_pa.player.third.sprint_low_health", { blendOutTime: 0.34, stopExpression: "query.log", controller: "third_animate_loop" }) }
    if (first) { player.playAnimation("animation.ks_pa.player.first.sprint_low_health", { blendOutTime: 0.28, stopExpression: "query.log", controller: "first_animate_loop" }) }
  }

  if (sneak) {
    states.loopWalkLowHealth = false
    player.setDynamicProperty('ks_pa:loop_walk_low_health', false);
    states.loopSneakStill = true
    player.setDynamicProperty('ks_pa:loop_sneak_still', true);
        if (third) {
      if (sneakAnimation) player.playAnimation("animation.ks_pa.player.third.sneak_still", { blendOutTime: 0.36, stopExpression: "query.log", controller: "third_animate_loop" })
    }
    else {
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.sneaking", "animation.ks_pa.player.sneaking", 0.38, "third_animate_loop", "first_animate_loop");
      if (!player.hasTag('ks_pa_attack_anim') && third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (sneakMove) {
    states.loopWalkLowHealth = false
    player.setDynamicProperty('ks_pa:loop_walk_low_health', false);
    states.loopSneak = true
    player.setDynamicProperty('ks_pa:loop_sneak', true);
    if (sneakAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sneak", "animation.ks_pa.player.first.sneak", 0.3, "third_animate_loop", "first_animate_loop");
    else {
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.sneaking", "animation.ks_pa.player.sneaking", 0.38, "third_animate_loop", "first_animate_loop");
      if (!player.hasTag('ks_pa_attack_anim') && third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (climb) {
    states.loopWalkLowHealth = false
    player.setDynamicProperty('ks_pa:loop_walk_low_health', false);
    states.loopClimb = true
    player.setDynamicProperty('ks_pa:loop_climb', true);
    if (climbAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.climb", "animation.ks_pa.player.first.climb", 0.48, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (jump) {
    states.loopJump = true
    player.setDynamicProperty('ks_pa:loop_jump', true);
    if ( jumpAnimation ) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.jump_start_layer", "animation.ks_pa.player.first.jump_start_layer", 0.26, "third_animate_once", "first_animate_once");
  }

  if (fall) {
    states.loopFall = true
    player.setDynamicProperty('ks_pa:loop_fall', true);
    if (fallAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.falling_long_layer", "animation.ks_pa.player.first.falling_long_layer", 0.24, "third_animate_once", "first_animate_once");
  }

  if (flyStand) {
    states.loopWalkLowHealth = false
    player.setDynamicProperty('ks_pa:loop_walk_low_health', false);
    states.loopFlyStill = true
    player.setDynamicProperty('ks_pa:loop_fly_still', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly_still", "animation.ks_pa.player.first.fly_still", 0.58, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (fly) {
    states.loopWalkLowHealth = false
    player.setDynamicProperty('ks_pa:loop_walk_low_health', false);
    states.loopFly = true
    player.setDynamicProperty('ks_pa:loop_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  } else if (flyFast) {
    states.loopWalkLowHealth = false
    player.setDynamicProperty('ks_pa:loop_walk_low_health', false);
    states.loopFastFly = true
    player.setDynamicProperty('ks_pa:loop_fast_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fast_fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  }

  if (swimStand) {
    states.loopWalkLowHealth = false
    player.setDynamicProperty('ks_pa:loop_walk_low_health', false);
    states.loopSwimStill = true
    player.setDynamicProperty('ks_pa:loop_swim_still', true);
    if (swimAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim_still", "animation.ks_pa.player.first.swim_still", 0.0, "third_animate_loop", "first_animate_loop");
    else if (!swimAnimation) {
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim_still", { blendOutTime: 0.0, stopExpression: "query.is_in_water", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim_still", { blendOutTime: 0.0, stopExpression: "query.is_in_water", controller: "first_animate_loop" }) }
    }
  } if (swim) {
    states.loopWalkLowHealth = false
    player.setDynamicProperty('ks_pa:loop_walk_low_health', false);
    states.loopSwim = true
    player.setDynamicProperty('ks_pa:loop_swim', true);
    if (swimAnimation) {
      //playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim", "animation.ks_pa.player.first.swim", 0.0, "third_animate_loop", "first_animate_loop");
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "first_animate_loop" }) }
    }
    else if (!swimAnimation) {
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim", { blendOutTime: 0.0, stopExpression: "query.is_in_water && query.modified_move_speed < 0.1", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim", { blendOutTime: 0.0, stopExpression: "query.is_in_water && query.modified_move_speed < 0.1", controller: "first_animate_loop" }) }
    }
  }

  if (crawlStand) {
    states.loopWalkLowHealth = false
    player.setDynamicProperty('ks_pa:loop_walk_low_health', false);
    states.loopCrawlStill = true
    player.setDynamicProperty('ks_pa:loop_crawl_still', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl_still", "animation.ks_pa.player.first.crawl_still", 0.38, "third_animate_loop", "first_animate_loop");
  } else if (crawl) {
    states.loopWalkLowHealth = false
    player.setDynamicProperty('ks_pa:loop_walk_low_health', false);
    states.loopCrawl = true
    player.setDynamicProperty('ks_pa:loop_crawl', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl", "animation.ks_pa.player.first.crawl", 0.38, "third_animate_loop", "first_animate_loop");
  }
}

function callSprint(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const velocity = states.testPlayerVelocity;
  const isRidingMinecart = player.getDynamicProperty("ks_pa:ride_minecart") && minecartAnimation;
  const health = player.getComponent("minecraft:health").currentValue;
  const canMove = velocity >= 0.12;
  const isIdle = velocity < 0.01;
  const holdingSword = states.testholdSword && swordAnimations;
  const isGrounded = player.isOnGround && !player.isClimbing;
  const idle = isIdle && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const walk = canMove && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const sneak = velocity < 0.04 && player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const sneakMove = velocity >= 0.04 && player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const climb = player.isClimbing && !player.isOnGround && !player.isInWater && !player.isSwimming;
  const jump = player.isJumping && !player.isFalling && !player.isInWater && !player.isSwimming && states.loopJump == false && states.loopFall == false;
  const fall = player.isFalling && !player.isInWater && states.loopJump == false && states.loopFall == false;
  const flyStand = velocity < 0.12 && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const fly = velocity >= 0.12 && !player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const flyFast = velocity >= 0.12 && player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const swimStand = player.isInWater && velocity < 0.12;
  const swim = player.isInWater && velocity >= 0.12;
  const crawlStand = velocity < 0.04 && states.canCrawl == true;
  const crawl = velocity >= 0.04 && states.canCrawl == true;
  if (isRidingMinecart && isIdle) {
    states.loopSprint = false
    player.setDynamicProperty('ks_pa:loop_sprint', false);
    states.loopRideMinecart = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_idle", "animation.ks_pa.humanoid_fpp.minecart_idle", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (isRidingMinecart && !isIdle) {
    states.loopSprint = false
    player.setDynamicProperty('ks_pa:loop_sprint', false);
    states.loopRideMinecartMove = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_move", "animation.ks_pa.humanoid_fpp.minecart_move", 0.28, "third_animate_loop", "first_animate_loop");
  }

  if (idle && !holdingSword && (health > 10 || !lowHealthAnimation) && !states.loopIdleEvent) {
    states.loopSprint = false
    player.setDynamicProperty('ks_pa:loop_sprint', false);
    states.loopIdle = true
    player.setDynamicProperty('ks_pa:loop_idle', true);
    if(standAnimation && !player.isSneaking) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (idle && !holdingSword && health <= 10 && lowHealthAnimation) {
    states.loopSprint = false;
    player.setDynamicProperty('ks_pa:loop_sprint', false);
    states.loopIdleLowHealth = true;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_low_health", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (player.isSprinting && walk && health > 10 && holdingSword) {
    states.loopSprint = false
    player.setDynamicProperty('ks_pa:loop_sprint', false);
    states.loopSprintSword = true
    player.setDynamicProperty('ks_pa:loop_sprint_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sprint_sword", "animation.ks_pa.player.first.sprint_sword", 0.32, "third_animate_loop", "first_animate_loop");
  } else if (walk && !player.isSprinting && health <= 10 && lowHealthAnimation) {
    states.loopSprint = false
    player.setDynamicProperty('ks_pa:loop_sprint', false);
    states.loopSprintLowHealth = true
    player.setDynamicProperty('ks_pa:loop_sprint_low_health', true);
    if (third) { player.playAnimation("animation.ks_pa.player.third.sprint_low_health", { blendOutTime: 0.34, stopExpression: "query.log", controller: "third_animate_loop" }) }
    if (first) { player.playAnimation("animation.ks_pa.player.first.sprint_low_health", { blendOutTime: 0.28, stopExpression: "query.log", controller: "first_animate_loop" }) }
  }

  if (sneak) {
    states.loopSprint = false
    player.setDynamicProperty('ks_pa:loop_sprint', false);
    states.loopSneakStill = true
    player.setDynamicProperty('ks_pa:loop_sneak_still', true);
        if (third) {
      if (sneakAnimation) player.playAnimation("animation.ks_pa.player.third.sneak_still", { blendOutTime: 0.36, stopExpression: "query.log", controller: "third_animate_loop" })
    }
    else {
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.sneaking", "animation.ks_pa.player.sneaking", 0.38, "third_animate_loop", "first_animate_loop");
      if (!player.hasTag('ks_pa_attack_anim') && third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (sneakMove) {
    states.loopSprint = false
    player.setDynamicProperty('ks_pa:loop_sprint', false);
    states.loopSneak = true
    player.setDynamicProperty('ks_pa:loop_sneak', true);
    if (sneakAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sneak", "animation.ks_pa.player.first.sneak", 0.3, "third_animate_loop", "first_animate_loop");
    else {
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.sneaking", "animation.ks_pa.player.sneaking", 0.38, "third_animate_loop", "first_animate_loop");
      if (!player.hasTag('ks_pa_attack_anim') && third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (climb) {
    states.loopSprint = false
    player.setDynamicProperty('ks_pa:loop_sprint', false);
    states.loopClimb = true
    player.setDynamicProperty('ks_pa:loop_climb', true);
    if (climbAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.climb", "animation.ks_pa.player.first.climb", 0.48, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (jump) {
    states.loopJump = true
    player.setDynamicProperty('ks_pa:loop_jump', true);
    if ( jumpAnimation ) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.jump_start_layer", "animation.ks_pa.player.first.jump_start_layer", 0.26, "third_animate_once", "first_animate_once");
  }

  if (fall) {
    states.loopFall = true
    player.setDynamicProperty('ks_pa:loop_fall', true);
    if (fallAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.falling_long_layer", "animation.ks_pa.player.first.falling_long_layer", 0.24, "third_animate_once", "first_animate_once");
  }

  if (flyStand) {
    states.loopSprint = false
    player.setDynamicProperty('ks_pa:loop_sprint', false);
    states.loopFlyStill = true
    player.setDynamicProperty('ks_pa:loop_fly_still', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly_still", "animation.ks_pa.player.first.fly_still", 0.58, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (fly) {
    states.loopSprint = false
    player.setDynamicProperty('ks_pa:loop_sprint', false);
    states.loopFly = true
    player.setDynamicProperty('ks_pa:loop_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  } else if (flyFast) {
    states.loopSprint = false
    player.setDynamicProperty('ks_pa:loop_sprint', false);
    states.loopFastFly = true
    player.setDynamicProperty('ks_pa:loop_fast_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fast_fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  }

  if (swimStand) {
    states.loopSprint = false
    player.setDynamicProperty('ks_pa:loop_sprint', false);
    states.loopSwimStill = true
    player.setDynamicProperty('ks_pa:loop_swim_still', true);
    if (swimAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim_still", "animation.ks_pa.player.first.swim_still", 0.0, "third_animate_loop", "first_animate_loop");
    else if (!swimAnimation) {
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim_still", { blendOutTime: 0.0, stopExpression: "query.is_in_water", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim_still", { blendOutTime: 0.0, stopExpression: "query.is_in_water", controller: "first_animate_loop" }) }
    }
  } if (swim) {
    states.loopSprint = false
    player.setDynamicProperty('ks_pa:loop_sprint', false);
    states.loopSwim = true
    player.setDynamicProperty('ks_pa:loop_swim', true);
    if (swimAnimation) {
      //playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim", "animation.ks_pa.player.first.swim", 0.0, "third_animate_loop", "first_animate_loop");
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "first_animate_loop" }) }
    }
    else if (!swimAnimation) {
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim", { blendOutTime: 0.0, stopExpression: "query.is_in_water && query.modified_move_speed < 0.1", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim", { blendOutTime: 0.0, stopExpression: "query.is_in_water && query.modified_move_speed < 0.1", controller: "first_animate_loop" }) }
    }
  }

  if (crawlStand) {
    states.loopSprint = false
    player.setDynamicProperty('ks_pa:loop_sprint', false);
    states.loopCrawlStill = true
    player.setDynamicProperty('ks_pa:loop_crawl_still', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl_still", "animation.ks_pa.player.first.crawl_still", 0.38, "third_animate_loop", "first_animate_loop");
  } else if (crawl) {
    states.loopSprint = false
    player.setDynamicProperty('ks_pa:loop_sprint', false);
    states.loopCrawl = true
    player.setDynamicProperty('ks_pa:loop_crawl', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl", "animation.ks_pa.player.first.crawl", 0.38, "third_animate_loop", "first_animate_loop");
  }
}

function callSprintSword(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const velocity = states.testPlayerVelocity;
  const isRidingMinecart = player.getDynamicProperty("ks_pa:ride_minecart") && minecartAnimation;
  const health = player.getComponent("minecraft:health").currentValue;
  const canMove = velocity >= 0.12;
  const isIdle = velocity < 0.01;
  const holdingSword = states.testholdSword && swordAnimations;
  const isGrounded = player.isOnGround && !player.isClimbing;
  const idle = isIdle && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const walk = canMove && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const sneak = velocity < 0.04 && player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const sneakMove = velocity >= 0.04 && player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const climb = player.isClimbing && !player.isOnGround && !player.isInWater && !player.isSwimming;
  const jump = player.isJumping && !player.isFalling && !player.isInWater && !player.isSwimming && states.loopJump == false && states.loopFall == false;
  const fall = player.isFalling && !player.isInWater && states.loopJump == false && states.loopFall == false;
  const flyStand = velocity < 0.12 && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const fly = velocity >= 0.12 && !player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const flyFast = velocity >= 0.12 && player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const swimStand = player.isInWater && velocity < 0.12;
  const swim = player.isInWater && velocity >= 0.12;
  const crawlStand = velocity < 0.04 && states.canCrawl == true;
  const crawl = velocity >= 0.04 && states.canCrawl == true;
  if (isRidingMinecart && isIdle) {
    states.loopSprintSword = false
    player.setDynamicProperty('ks_pa:loop_sprint_sword', false);
    states.loopRideMinecart = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_idle", "animation.ks_pa.humanoid_fpp.minecart_idle", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (isRidingMinecart && !isIdle) {
    states.loopSprintSword = false
    player.setDynamicProperty('ks_pa:loop_sprint_sword', false);
    states.loopRideMinecartMove = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_move", "animation.ks_pa.humanoid_fpp.minecart_move", 0.28, "third_animate_loop", "first_animate_loop");
  }

  if (idle && !holdingSword && (health > 10 || !lowHealthAnimation) && !states.loopIdleEvent) {
    states.loopSprintSword = false
    player.setDynamicProperty('ks_pa:loop_sprint_sword', false);
    states.loopIdle = true
    player.setDynamicProperty('ks_pa:loop_idle', true);
    if(standAnimation && !player.isSneaking) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (idle && holdingSword && health > 10) {
    states.loopSprintSword = false
    player.setDynamicProperty('ks_pa:loop_sprint_sword', false);
    states.loopIdleSword = true
    player.setDynamicProperty('ks_pa:loop_idle_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_sword", "animation.ks_pa.player.first.idle_sword", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (idle && !holdingSword && health <= 10 && lowHealthAnimation) {
    states.loopSprintSword = false;
    player.setDynamicProperty('ks_pa:loop_sprint_sword', false);
    states.loopIdleLowHealth = true;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_low_health", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (player.isSprinting && walk && (health > 10 || !lowHealthAnimation) && !holdingSword) {
    states.loopSprintSword = false
    player.setDynamicProperty('ks_pa:loop_sprint_sword', false);
    states.loopSprint = true
    player.setDynamicProperty('ks_pa:loop_sprint', true);
    if (sprintAnimation)
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sprint", "animation.ks_pa.player.first.sprint", 0.32, "third_animate_loop", "first_animate_loop");
else if (!player.hasTag('ks_pa_attack_anim')) {
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (walk && !player.isSprinting && health <= 10 && lowHealthAnimation) {
    states.loopSprintSword = false
    player.setDynamicProperty('ks_pa:loop_sprint_sword', false);
    states.loopSprintLowHealth = true
    player.setDynamicProperty('ks_pa:loop_sprint_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sprint_low_health", "animation.ks_pa.player.first.sprint_low_health", 0.28, "third_animate_loop", "first_animate_loop");
  }

  if (sneak) {
    states.loopSprintSword = false
    player.setDynamicProperty('ks_pa:loop_sprint_sword', false);
    states.loopSneakStill = true
    player.setDynamicProperty('ks_pa:loop_sneak_still', true);
        if (third) {
      if (sneakAnimation) player.playAnimation("animation.ks_pa.player.third.sneak_still", { blendOutTime: 0.36, stopExpression: "query.log", controller: "third_animate_loop" })
    }
    else {
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.sneaking", "animation.ks_pa.player.sneaking", 0.38, "third_animate_loop", "first_animate_loop");
      if (!player.hasTag('ks_pa_attack_anim') && third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (sneakMove) {
    states.loopSprintSword = false
    player.setDynamicProperty('ks_pa:loop_sprint_sword', false);
    states.loopSneak = true
    player.setDynamicProperty('ks_pa:loop_sneak', true);
    if (sneakAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sneak", "animation.ks_pa.player.first.sneak", 0.3, "third_animate_loop", "first_animate_loop");
    else {
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.sneaking", "animation.ks_pa.player.sneaking", 0.38, "third_animate_loop", "first_animate_loop");
      if (!player.hasTag('ks_pa_attack_anim') && third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (climb) {
    states.loopSprintSword = false
    player.setDynamicProperty('ks_pa:loop_sprint_sword', false);
    states.loopClimb = true
    player.setDynamicProperty('ks_pa:loop_climb', true);
    if (climbAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.climb", "animation.ks_pa.player.first.climb", 0.48, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (jump) {
    states.loopJump = true
    player.setDynamicProperty('ks_pa:loop_jump', true);
    if ( jumpAnimation ) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.jump_start_layer", "animation.ks_pa.player.first.jump_start_layer", 0.26, "third_animate_once", "first_animate_once");
  }

  if (fall) {
    states.loopFall = true
    player.setDynamicProperty('ks_pa:loop_fall', true);
    if (fallAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.falling_long_layer", "animation.ks_pa.player.first.falling_long_layer", 0.24, "third_animate_once", "first_animate_once");
  }

  if (flyStand) {
    states.loopSprintSword = false
    player.setDynamicProperty('ks_pa:loop_sprint_sword', false);
    states.loopFlyStill = true
    player.setDynamicProperty('ks_pa:loop_fly_still', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly_still", "animation.ks_pa.player.first.fly_still", 0.58, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (fly) {
    states.loopJump = false
    player.setDynamicProperty('ks_pa:loop_jump', false);
    states.loopSprintSword = true
    player.setDynamicProperty('ks_pa:loop_sprint_sword', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  } else if (flyFast) {
    states.loopSprintSword = false
    player.setDynamicProperty('ks_pa:loop_sprint_sword', false);
    states.loopFastFly = true
    player.setDynamicProperty('ks_pa:loop_fast_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fast_fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  }

  if (swimStand) {
    states.loopSprintSword = false
    player.setDynamicProperty('ks_pa:loop_sprint_sword', false);
    states.loopSwimStill = true
    player.setDynamicProperty('ks_pa:loop_swim_still', true);
    if (swimAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim_still", "animation.ks_pa.player.first.swim_still", 0.0, "third_animate_loop", "first_animate_loop");
    else if (!swimAnimation) {
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim_still", { blendOutTime: 0.0, stopExpression: "query.is_in_water", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim_still", { blendOutTime: 0.0, stopExpression: "query.is_in_water", controller: "first_animate_loop" }) }
    }
  } if (swim) {
    states.loopSprintSword = false
    player.setDynamicProperty('ks_pa:loop_sprint_sword', false);
    states.loopSwim = true
    player.setDynamicProperty('ks_pa:loop_swim', true);
    if (swimAnimation) {
      //playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim", "animation.ks_pa.player.first.swim", 0.0, "third_animate_loop", "first_animate_loop");
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "first_animate_loop" }) }
    }
    else if (!swimAnimation) {
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim", { blendOutTime: 0.0, stopExpression: "query.is_in_water && query.modified_move_speed < 0.1", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim", { blendOutTime: 0.0, stopExpression: "query.is_in_water && query.modified_move_speed < 0.1", controller: "first_animate_loop" }) }
    }
  }

  if (crawlStand) {
    states.loopSprintSword = false
    player.setDynamicProperty('ks_pa:loop_sprint_sword', false);
    states.loopCrawlStill = true
    player.setDynamicProperty('ks_pa:loop_crawl_still', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl_still", "animation.ks_pa.player.first.crawl_still", 0.38, "third_animate_loop", "first_animate_loop");
  } else if (crawl) {
    states.loopSprintSword = false
    player.setDynamicProperty('ks_pa:loop_sprint_sword', false);
    states.loopCrawl = true
    player.setDynamicProperty('ks_pa:loop_crawl', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl", "animation.ks_pa.player.first.crawl", 0.38, "third_animate_loop", "first_animate_loop");
  }
}

function callSprintLowHealth(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const velocity = states.testPlayerVelocity;
  const isRidingMinecart = player.getDynamicProperty("ks_pa:ride_minecart") && minecartAnimation;
  const health = player.getComponent("minecraft:health").currentValue;
  const canMove = velocity >= 0.12;
  const isIdle = velocity < 0.01;
  const holdingSword = states.testholdSword && swordAnimations;
  const isGrounded = player.isOnGround && !player.isClimbing;
  const idle = isIdle && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const walk = canMove && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const sneak = velocity < 0.04 && player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const sneakMove = velocity >= 0.04 && player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const climb = player.isClimbing && !player.isOnGround && !player.isInWater && !player.isSwimming;
  const jump = player.isJumping && !player.isFalling && !player.isInWater && !player.isSwimming && states.loopJump == false && states.loopFall == false;
  const fall = player.isFalling && !player.isInWater && states.loopJump == false && states.loopFall == false;
  const flyStand = velocity < 0.12 && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const fly = velocity >= 0.12 && !player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const flyFast = velocity >= 0.12 && player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const swimStand = player.isInWater && velocity < 0.12;
  const swim = player.isInWater && velocity >= 0.12;
  const crawlStand = velocity < 0.04 && states.canCrawl == true;
  const crawl = velocity >= 0.04 && states.canCrawl == true;
  if (isRidingMinecart && isIdle) {
    states.loopSprintLowHealth = false
    player.setDynamicProperty('ks_pa:loop_sprint_low_health', false);
    states.loopRideMinecart = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_idle", "animation.ks_pa.humanoid_fpp.minecart_idle", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (isRidingMinecart && !isIdle) {
    states.loopSprintLowHealth = false
    player.setDynamicProperty('ks_pa:loop_sprint_low_health', false);
    states.loopRideMinecartMove = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_move", "animation.ks_pa.humanoid_fpp.minecart_move", 0.28, "third_animate_loop", "first_animate_loop");
  }

  if (idle && health <= 10 && lowHealthAnimation) {
    states.loopSprintLowHealth = false;
    player.setDynamicProperty('ks_pa:loop_sprint_low_health', false);
    states.loopIdleLowHealth = true;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_low_health", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (player.isSprinting && walk && (health > 10 || !lowHealthAnimation) && !holdingSword) {
    states.loopSprintLowHealth = false
    player.setDynamicProperty('ks_pa:loop_sprint_low_health', false);
    states.loopSprint = true
    player.setDynamicProperty('ks_pa:loop_sprint', true);
    if (sprintAnimation)
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sprint", "animation.ks_pa.player.first.sprint", 0.32, "third_animate_loop", "first_animate_loop");
else if (!player.hasTag('ks_pa_attack_anim')) {
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (player.isSprinting && walk && health > 10 && holdingSword) {
    states.loopSprintLowHealth = false
    player.setDynamicProperty('ks_pa:loop_sprint_low_health', false);
    states.loopSprintSword = true
    player.setDynamicProperty('ks_pa:loop_sprint_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sprint_sword", "animation.ks_pa.player.first.sprint_sword", 0.32, "third_animate_loop", "first_animate_loop");
  }

  if (sneak) {
    states.loopSprintLowHealth = false
    player.setDynamicProperty('ks_pa:loop_sprint_low_health', false);
    states.loopSneakStill = true
    player.setDynamicProperty('ks_pa:loop_sneak_still', true);
        if (third) {
      if (sneakAnimation) player.playAnimation("animation.ks_pa.player.third.sneak_still", { blendOutTime: 0.36, stopExpression: "query.log", controller: "third_animate_loop" })
    }
    else {
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.sneaking", "animation.ks_pa.player.sneaking", 0.38, "third_animate_loop", "first_animate_loop");
      if (!player.hasTag('ks_pa_attack_anim') && third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (sneakMove) {
    states.loopSprintLowHealth = false
    player.setDynamicProperty('ks_pa:loop_sprint_low_health', false);
    states.loopSneak = true
    player.setDynamicProperty('ks_pa:loop_sneak', true);
    if (sneakAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sneak", "animation.ks_pa.player.first.sneak", 0.3, "third_animate_loop", "first_animate_loop");
    else {
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.sneaking", "animation.ks_pa.player.sneaking", 0.38, "third_animate_loop", "first_animate_loop");
      if (!player.hasTag('ks_pa_attack_anim') && third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (climb) {
    states.loopSprintLowHealth = false
    player.setDynamicProperty('ks_pa:loop_sprint_low_health', false);
    states.loopClimb = true
    player.setDynamicProperty('ks_pa:loop_climb', true);
    if (climbAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.climb", "animation.ks_pa.player.first.climb", 0.38, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (jump) {
    states.loopJump = true
    player.setDynamicProperty('ks_pa:loop_jump', true);
    if ( jumpAnimation ) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.jump_start_layer", "animation.ks_pa.player.first.jump_start_layer", 0.26, "third_animate_once", "first_animate_once");
  }

  if (fall) {
    states.loopFall = true
    player.setDynamicProperty('ks_pa:loop_fall', true);
    if (fallAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.falling_long_layer", "animation.ks_pa.player.first.falling_long_layer", 0.24, "third_animate_once", "first_animate_once");
  }

  if (flyStand) {
    states.loopSprintLowHealth = false
    player.setDynamicProperty('ks_pa:loop_sprint_low_health', false);
    states.loopFlyStill = true
    player.setDynamicProperty('ks_pa:loop_fly_still', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly_still", "animation.ks_pa.player.first.fly_still", 0.58, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (fly) {
    states.loopSprintLowHealth = false
    player.setDynamicProperty('ks_pa:loop_sprint_low_health', false);
    states.loopFly = true
    player.setDynamicProperty('ks_pa:loop_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  } else if (flyFast) {
    states.loopSprintLowHealth = false
    player.setDynamicProperty('ks_pa:loop_sprint_low_health', false);
    states.loopFastFly = true
    player.setDynamicProperty('ks_pa:loop_fast_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fast_fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  }

  if (swimStand) {
    states.loopSprintLowHealth = false
    player.setDynamicProperty('ks_pa:loop_sprint_low_health', false);
    states.loopSwimStill = true
    player.setDynamicProperty('ks_pa:loop_swim_still', true);
    if (swimAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim_still", "animation.ks_pa.player.first.swim_still", 0.0, "third_animate_loop", "first_animate_loop");
    else if (!swimAnimation) {
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim_still", { blendOutTime: 0.0, stopExpression: "query.is_in_water", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim_still", { blendOutTime: 0.0, stopExpression: "query.is_in_water", controller: "first_animate_loop" }) }
    }
  } if (swim) {
    states.loopSprintLowHealth = false
    player.setDynamicProperty('ks_pa:loop_sprint_low_health', false);
    states.loopSwim = true
    player.setDynamicProperty('ks_pa:loop_swim', true);
    if (swimAnimation) {
      //playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim", "animation.ks_pa.player.first.swim", 0.0, "third_animate_loop", "first_animate_loop");
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "first_animate_loop" }) }
    }
    else if (!swimAnimation) {
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim", { blendOutTime: 0.0, stopExpression: "query.is_in_water && query.modified_move_speed < 0.1", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim", { blendOutTime: 0.0, stopExpression: "query.is_in_water && query.modified_move_speed < 0.1", controller: "first_animate_loop" }) }
    }
  }

  if (crawlStand) {
    states.loopSprintLowHealth = false
    player.setDynamicProperty('ks_pa:loop_sprint_low_health', false);
    states.loopCrawlStill = true
    player.setDynamicProperty('ks_pa:loop_crawl_still', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl_still", "animation.ks_pa.player.first.crawl_still", 0.38, "third_animate_loop", "first_animate_loop");
  } else if (crawl) {
    states.loopSprintLowHealth = false
    player.setDynamicProperty('ks_pa:loop_sprint_low_health', false);
    states.loopCrawl = true
    player.setDynamicProperty('ks_pa:loop_crawl', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl", "animation.ks_pa.player.first.crawl", 0.38, "third_animate_loop", "first_animate_loop");
  }
}

function callSneakStill(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const velocity = states.testPlayerVelocity;
  const isRidingMinecart = player.getDynamicProperty("ks_pa:ride_minecart") && minecartAnimation;
  const health = player.getComponent("minecraft:health").currentValue;
  const isIdle = velocity < 0.01;
  const holdingSword = states.testholdSword && swordAnimations;
  const isGrounded = player.isOnGround && !player.isClimbing;
  const idle = isIdle && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const sneakMove = velocity >= 0.04 && player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const jump = player.isJumping && !player.isFalling && !player.isInWater && !player.isSwimming && states.loopJump == false && states.loopFall == false;
  const fall = player.isFalling && !player.isInWater && states.loopJump == false && states.loopFall == false;
  const flyStand = velocity < 0.12 && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const fly = velocity >= 0.12 && !player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const flyFast = velocity >= 0.12 && player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const swimStand = player.isInWater && velocity < 0.12;
  const crawlStand = velocity < 0.04 && states.canCrawl == true;
  const crawl = velocity >= 0.04 && states.canCrawl == true;
  if (isRidingMinecart && isIdle) {
    states.loopSneakStill = false
    player.setDynamicProperty('ks_pa:loop_sneak_still', false);
    states.loopRideMinecart = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_idle", "animation.ks_pa.humanoid_fpp.minecart_idle", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (isRidingMinecart && !isIdle) {
    states.loopSneakStill = false
    player.setDynamicProperty('ks_pa:loop_sneak_still', false);
    states.loopRideMinecartMove = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_move", "animation.ks_pa.humanoid_fpp.minecart_move", 0.28, "third_animate_loop", "first_animate_loop");
  }

  if (idle && !holdingSword && (health > 10 || !lowHealthAnimation) && !states.loopIdleEvent) {
    states.loopSneakStill = false
    player.setDynamicProperty('ks_pa:loop_sneak_still', false);
    states.loopIdle = true
    player.setDynamicProperty('ks_pa:loop_idle', true);
    if(standAnimation && !player.isSneaking) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (idle && holdingSword && health > 10) {
    states.loopSneakStill = false
    player.setDynamicProperty('ks_pa:loop_sneak_still', false);
    states.loopIdleSword = true
    player.setDynamicProperty('ks_pa:loop_idle_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_sword", "animation.ks_pa.player.first.idle_sword", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (idle && !holdingSword && health <= 10 && lowHealthAnimation) {
    states.loopSneakStill = false;
    player.setDynamicProperty('ks_pa:loop_sneak_still', false);
    states.loopIdleLowHealth = true;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_low_health", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (sneakMove) {
    states.loopSneakStill = false
    player.setDynamicProperty('ks_pa:loop_sneak_still', false);
    states.loopSneak = true
    player.setDynamicProperty('ks_pa:loop_sneak', true);
    if (sneakAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sneak", "animation.ks_pa.player.first.sneak", 0.3, "third_animate_loop", "first_animate_loop");
    else {
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.sneaking", "animation.ks_pa.player.sneaking", 0.38, "third_animate_loop", "first_animate_loop");
      if (!player.hasTag('ks_pa_attack_anim') && third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (jump) {
    states.loopJump = true
    player.setDynamicProperty('ks_pa:loop_jump', true);
    if ( jumpAnimation ) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.jump_start_layer", "animation.ks_pa.player.first.jump_start_layer", 0.26, "third_animate_once", "first_animate_once");
  }

  if (fall) {
    states.loopFall = true
    player.setDynamicProperty('ks_pa:loop_fall', true);
    if (fallAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.falling_long_layer", "animation.ks_pa.player.first.falling_long_layer", 0.24, "third_animate_once", "first_animate_once");
  }

  if (flyStand) {
    states.loopSneakStill = false
    player.setDynamicProperty('ks_pa:loop_sneak_still', false);
    states.loopFlyStill = true
    player.setDynamicProperty('ks_pa:loop_fly_still', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly_still", "animation.ks_pa.player.first.fly_still", 0.58, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (fly) {
    states.loopSneakStill = false
    player.setDynamicProperty('ks_pa:loop_sneak_still', false);
    states.loopFly = true
    player.setDynamicProperty('ks_pa:loop_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  } else if (flyFast) {
    states.loopSneakStill = false
    player.setDynamicProperty('ks_pa:loop_sneak_still', false);
    states.loopFastFly = true
    player.setDynamicProperty('ks_pa:loop_fast_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fast_fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  }

  if (swimStand) {
    states.loopSneakStill = false
    player.setDynamicProperty('ks_pa:loop_sneak_still', false);
    states.loopSwimStill = true
    player.setDynamicProperty('ks_pa:loop_swim_still', true);
    if (swimAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim_still", "animation.ks_pa.player.first.swim_still", 0.0, "third_animate_loop", "first_animate_loop");
  }

  if (crawlStand) {
    states.loopSneakStill = false
    player.setDynamicProperty('ks_pa:loop_sneak_still', false);
    states.loopCrawlStill = true
    player.setDynamicProperty('ks_pa:loop_crawl_still', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl_still", "animation.ks_pa.player.first.crawl_still", 0.38, "third_animate_loop", "first_animate_loop");
  } else if (crawl) {
    states.loopSneakStill = false
    player.setDynamicProperty('ks_pa:loop_sneak_still', false);
    states.loopCrawl = true
    player.setDynamicProperty('ks_pa:loop_crawl', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl", "animation.ks_pa.player.first.crawl", 0.38, "third_animate_loop", "first_animate_loop");
  }
}

function callSneak(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const velocity = states.testPlayerVelocity;
  const isRidingMinecart = player.getDynamicProperty("ks_pa:ride_minecart") && minecartAnimation;
  const health = player.getComponent("minecraft:health").currentValue;
  const canMove = velocity >= 0.12;
  const isIdle = velocity < 0.01;
  const holdingSword = states.testholdSword && swordAnimations;
  const isGrounded = player.isOnGround && !player.isClimbing;
  const idle = isIdle && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const walk = canMove && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const sneak = velocity < 0.04 && player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const jump = player.isJumping && !player.isFalling && !player.isInWater && !player.isSwimming && states.loopJump == false && states.loopFall == false;
  const fall = player.isFalling && !player.isInWater && states.loopJump == false && states.loopFall == false;
  const flyStand = velocity < 0.12 && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const fly = velocity >= 0.12 && !player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const flyFast = velocity >= 0.12 && player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const swimStand = player.isInWater && velocity < 0.12;
  const swim = player.isInWater && velocity >= 0.12;
  const crawlStand = velocity < 0.04 && states.canCrawl == true;
  const crawl = velocity >= 0.04 && states.canCrawl == true;
  if (isRidingMinecart && isIdle) {
    states.loopSneak = false
    player.setDynamicProperty('ks_pa:loop_sneak', false);
    states.loopRideMinecart = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_idle", "animation.ks_pa.humanoid_fpp.minecart_idle", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (isRidingMinecart && !isIdle) {
    states.loopSneak = false
    player.setDynamicProperty('ks_pa:loop_sneak', false);
    states.loopRideMinecartMove = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_move", "animation.ks_pa.humanoid_fpp.minecart_move", 0.28, "third_animate_loop", "first_animate_loop");
  }

  if (idle && !holdingSword && (health > 10 || !lowHealthAnimation) && !states.loopIdleEvent) {
    states.loopSneak = false
    player.setDynamicProperty('ks_pa:loop_sneak', false);
    states.loopIdle = true
    player.setDynamicProperty('ks_pa:loop_idle', true);
    if(standAnimation && !player.isSneaking) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (idle && holdingSword && health > 10) {
    states.loopSneak = false
    player.setDynamicProperty('ks_pa:loop_sneak', false);
    states.loopIdleSword = true
    player.setDynamicProperty('ks_pa:loop_idle_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_sword", "animation.ks_pa.player.first.idle_sword", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (idle && !holdingSword && health <= 10 && lowHealthAnimation) {
    states.loopSneak = false;
    player.setDynamicProperty('ks_pa:loop_sneak', false);
    states.loopIdleLowHealth = true;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_low_health", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (walk && (health > 10 || !lowHealthAnimation) && !holdingSword) {
    states.loopSneak = false
    player.setDynamicProperty('ks_pa:loop_sneak', false);
    states.loopWalk = true
    player.setDynamicProperty('ks_pa:loop_walk', true);
    if (walkAnimation){
      if (third) { player.playAnimation("animation.ks_pa.player.third.walk", { blendOutTime: 0.42, stopExpression: "query.log", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.walk", { blendOutTime: 0.38, stopExpression: "query.log", controller: "first_animate_loop" }) }
    }
    else if (!player.hasTag('ks_pa_attack_anim')) {
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (walk && health > 10 && holdingSword) {
    states.loopSneak = false
    player.setDynamicProperty('ks_pa:loop_sneak', false);
    states.loopWalkSword = true
    player.setDynamicProperty('ks_pa:loop_walk_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.walk_sword", "animation.ks_pa.player.first.walk", 0.38, "third_animate_loop", "first_animate_loop");
  } else if (walk && health <= 10 && lowHealthAnimation) {
    states.loopSneak = false
    player.setDynamicProperty('ks_pa:loop_sneak', false);
    states.loopWalkLowHealth = true
    player.setDynamicProperty('ks_pa:loop_walk_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.walk_low_health", "animation.ks_pa.player.first.walk_low_health", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (sneak) {
    states.loopSneak = false
    player.setDynamicProperty('ks_pa:loop_sneak', false);
    states.loopSneakStill = true
    player.setDynamicProperty('ks_pa:loop_sneak_still', true);
        if (third) {
      if (sneakAnimation) player.playAnimation("animation.ks_pa.player.third.sneak_still", { blendOutTime: 0.36, stopExpression: "query.log", controller: "third_animate_loop" })
    }
    else {
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.sneaking", "animation.ks_pa.player.sneaking", 0.38, "third_animate_loop", "first_animate_loop");
      if (!player.hasTag('ks_pa_attack_anim') && third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (jump) {
    states.loopJump = true
    player.setDynamicProperty('ks_pa:loop_jump', true);
    if ( jumpAnimation ) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.jump_start_layer", "animation.ks_pa.player.first.jump_start_layer", 0.26, "third_animate_once", "first_animate_once");
  }

  if (fall) {
    states.loopFall = true
    player.setDynamicProperty('ks_pa:loop_fall', true);
    if (fallAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.falling_long_layer", "animation.ks_pa.player.first.falling_long_layer", 0.24, "third_animate_once", "first_animate_once");
  }

  if (flyStand) {
    states.loopSneak = false
    player.setDynamicProperty('ks_pa:loop_sneak', false);
    states.loopFlyStill = true
    player.setDynamicProperty('ks_pa:loop_fly_still', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly_still", "animation.ks_pa.player.first.fly_still", 0.58, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (fly) {
    states.loopSneak = false
    player.setDynamicProperty('ks_pa:loop_sneak', false);
    states.loopFly = true
    player.setDynamicProperty('ks_pa:loop_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  } else if (flyFast) {
    states.loopSneak = false
    player.setDynamicProperty('ks_pa:loop_sneak', false);
    states.loopFastFly = true
    player.setDynamicProperty('ks_pa:loop_fast_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fast_fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  }

  if (swimStand) {
    states.loopSneak = false
    player.setDynamicProperty('ks_pa:loop_sneak', false);
    states.loopSwimStill = true
    player.setDynamicProperty('ks_pa:loop_swim_still', true);
    if (swimAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim_still", "animation.ks_pa.player.first.swim_still", 0.0, "third_animate_loop", "first_animate_loop");
  } if (swim) {
    states.loopSneak = false
    player.setDynamicProperty('ks_pa:loop_sneak', false);
    states.loopSwim = true
    player.setDynamicProperty('ks_pa:loop_swim', true);
    if (swimAnimation) {
      //playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim", "animation.ks_pa.player.first.swim", 0.0, "third_animate_loop", "first_animate_loop");
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "first_animate_loop" }) }
    }
  }

  if (crawlStand) {
    states.loopSneak = false
    player.setDynamicProperty('ks_pa:loop_sneak', false);
    states.loopCrawlStill = true
    player.setDynamicProperty('ks_pa:loop_crawl_still', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl_still", "animation.ks_pa.player.first.crawl_still", 0.38, "third_animate_loop", "first_animate_loop");
  } else if (crawl) {
    states.loopSneak = false
    player.setDynamicProperty('ks_pa:loop_sneak', false);
    states.loopCrawl = true
    player.setDynamicProperty('ks_pa:loop_crawl', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl", "animation.ks_pa.player.first.crawl", 0.38, "third_animate_loop", "first_animate_loop");
  }
}

function callClimb(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const velocity = states.testPlayerVelocity;
  const isRidingMinecart = player.getDynamicProperty("ks_pa:ride_minecart") && minecartAnimation;
  const isIdle = velocity < 0.01;
  const fall = player.isFalling && !player.isInWater && states.loopJump == false && states.loopFall == false;
  const flyStand = velocity < 0.12 && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const fly = velocity >= 0.12 && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  if (isRidingMinecart && isIdle) {
    states.loopClimb = false
    player.setDynamicProperty('ks_pa:loop_climb', false);
    states.loopRideMinecart = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_idle", "animation.ks_pa.humanoid_fpp.minecart_idle", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (isRidingMinecart && !isIdle) {
    states.loopClimb = false
    player.setDynamicProperty('ks_pa:loop_climb', false);
    states.loopRideMinecartMove = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_move", "animation.ks_pa.humanoid_fpp.minecart_move", 0.28, "third_animate_loop", "first_animate_loop");
  }

  if (flyStand) {
    states.loopClimb = false
    player.setDynamicProperty('ks_pa:loop_climb', false);
    states.loopFlyStill = true
    player.setDynamicProperty('ks_pa:loop_fly_still', true);
    if (third) { player.playAnimation("animation.ks_pa.player.third.fly_still", { blendOutTime: 0.58, stopExpression: "query.log", controller: "third_animate_loop" }) }
  } else if (fly) {
    states.loopClimb = false
    player.setDynamicProperty('ks_pa:loop_climb', false);
    states.loopFly = true
    player.setDynamicProperty('ks_pa:loop_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  }

  if (player.isFalling && !player.isClimbing) {
    states.loopClimb = false
    player.setDynamicProperty('ks_pa:loop_climb', false);
    states.loopFall = true
    player.setDynamicProperty('ks_pa:loop_fall', true);
    states.loopLand = true
    player.setDynamicProperty('ks_pa:loop_land', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.walk", "animation.ks_pa.player.first.walk", 0.38, "third_animate_loop", "first_animate_loop");
    if (fallAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.falling_long_layer", "animation.ks_pa.player.first.falling_long_layer", 0.24, "third_animate_once", "first_animate_once");
  }

  if (player.isOnGround || (!player.isFalling && !player.isClimbing)) {
    states.loopClimb = false
    player.setDynamicProperty('ks_pa:loop_climb', false);
    states.loopLand = true
    player.setDynamicProperty('ks_pa:loop_land', true);
    if ( landAnimation ) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.jump_end_layer", "animation.ks_pa.player.first.jump_end_layer", 0.32, "third_animate_once", "first_animate_once");
  }

  if (player.isClimbing && player.isSneaking) {
    states.loopClimb = false
    player.setDynamicProperty('ks_pa:loop_climb', false);
    states.loopClimbStill = true
    player.setDynamicProperty('ks_pa:loop_climb_still', true);
    if (climbAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.climb_still", "animation.ks_pa.player.first.climb_still", 0.48, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }
}

function callClimbStill(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const velocity = states.testPlayerVelocity;
  const isRidingMinecart = player.getDynamicProperty("ks_pa:ride_minecart") && minecartAnimation;
  const isIdle = velocity < 0.01;
  const fall = player.isFalling && !player.isInWater && states.loopJump == false && states.loopFall == false;
  const flyStand = velocity < 0.12 && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const fly = velocity >= 0.12 && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  if (isRidingMinecart && isIdle) {
    states.loopClimbStill = false
    player.setDynamicProperty('ks_pa:loop_climb_still', false);
    states.loopRideMinecart = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_idle", "animation.ks_pa.humanoid_fpp.minecart_idle", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (isRidingMinecart && !isIdle) {
    states.loopClimbStill = false
    player.setDynamicProperty('ks_pa:loop_climb_still', false);
    states.loopRideMinecartMove = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_move", "animation.ks_pa.humanoid_fpp.minecart_move", 0.28, "third_animate_loop", "first_animate_loop");
  }

  if (flyStand) {
    states.loopClimbStill = false
    player.setDynamicProperty('ks_pa:loop_climb_still', false);
    states.loopFlyStill = true
    player.setDynamicProperty('ks_pa:loop_fly_still', true);
    if (third) { player.playAnimation("animation.ks_pa.player.third.fly_still", { blendOutTime: 0.58, stopExpression: "query.log", controller: "third_animate_loop" }) }
  } else if (fly) {
    states.loopClimbStill = false
    player.setDynamicProperty('ks_pa:loop_climb_still', false);
    states.loopFly = true
    player.setDynamicProperty('ks_pa:loop_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  }

  if (player.isFalling && !player.isClimbing) {
    states.loopClimbStill = false
    player.setDynamicProperty('ks_pa:loop_climb_still', false);
    states.loopFall = true
    player.setDynamicProperty('ks_pa:loop_fall', true);
    states.loopLand = true
    player.setDynamicProperty('ks_pa:loop_land', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.walk", "animation.ks_pa.player.first.walk", 0.38, "third_animate_loop", "first_animate_loop");
    if (fallAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.falling_long_layer", "animation.ks_pa.player.first.falling_long_layer", 0.24, "third_animate_once", "first_animate_once");
  }

  if (player.isOnGround || (!player.isFalling && !player.isClimbing)) {
    states.loopClimbStill = false
    player.setDynamicProperty('ks_pa:loop_climb_still', false);
    states.loopLand = true
    player.setDynamicProperty('ks_pa:loop_land', true);
    if ( landAnimation ) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.jump_end_layer", "animation.ks_pa.player.first.jump_end_layer", 0.32, "third_animate_once", "first_animate_once");
  }

  if (player.isClimbing && !player.isSneaking) {
    states.loopClimbStill = false
    player.setDynamicProperty('ks_pa:loop_climb_still', false);
    states.loopClimb = true
    player.setDynamicProperty('ks_pa:loop_climb', true);
    if (climbAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.climb", "animation.ks_pa.player.first.climb", 0.48, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }
}

function callJump(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const fall = player.isFalling && !player.isInWater && states.loopJump == false && states.loopFall == false;
  if (player.isFlying || player.isInWater) {
    states.loopJump = false
    player.setDynamicProperty('ks_pa:loop_jump', false);
  }

  if (fall) {
    states.loopJump = false
    player.setDynamicProperty('ks_pa:loop_jump', false);
    states.loopFall = true
    player.setDynamicProperty('ks_pa:loop_fall', true);
    if (fallAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.falling_long_layer", "animation.ks_pa.player.first.falling_long_layer", 0.24, "third_animate_once", "first_animate_once");
  }

  if (player.isOnGround && !player.isInWater && !player.isSwimming) {
    states.loopJump = false
    player.setDynamicProperty('ks_pa:loop_jump', false);
    states.loopFall = false
    player.setDynamicProperty('ks_pa:loop_fall', false);
    if ( landAnimation ) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.jump_end_layer", "animation.ks_pa.player.first.jump_end_layer", 0.32, "third_animate_once", "first_animate_once");
  }
}
// HERE FIX
function callFall(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const jump = player.isJumping && !player.isFalling && !player.isInWater && !player.isSwimming && states.loopJump == false && states.loopFall == false;
  if (player.isOnGround || player.isInWater || player.isSwimming) {
    states.loopFall = false;
    player.setDynamicProperty('ks_pa:loop_fall', false);
    states.loopLand = true;
    player.setDynamicProperty('ks_pa:loop_land', true);
    if ( landAnimation ) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.jump_end_layer", "animation.ks_pa.player.first.jump_end_layer", 0.32, "third_animate_once", "first_animate_once");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_once", "first_animate_once");
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_once", "first_animate_once");
    }
  }

  if (jump) {
    states.loopJump = true
    player.setDynamicProperty('ks_pa:loop_jump', true);
    if ( jumpAnimation ) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.jump_start_layer", "animation.ks_pa.player.first.jump_start_layer", 0.26, "third_animate_once", "first_animate_once");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_once", "first_animate_once");
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_once", "first_animate_once");
    }
  }
}

function callLand(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const velocity = states.testPlayerVelocity;
  const swimStand = player.isInWater && velocity < 0.12;
  const swim = player.isInWater && velocity >= 0.12;
  const isRidingMinecart = player.getDynamicProperty("ks_pa:ride_minecart") && minecartAnimation;
  const health = player.getComponent("minecraft:health").currentValue;
  const canMove = velocity >= 0.12;
  const isIdle = velocity < 0.01;
  const holdingSword = states.testholdSword && swordAnimations;
  const isGrounded = player.isOnGround && !player.isClimbing;
  const idle = isIdle && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const walk = canMove && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const sneak = velocity < 0.04 && player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const climb = player.isClimbing && !player.isOnGround && !player.isInWater && !player.isSwimming;
  if (isRidingMinecart && isIdle) {
    states.loopLand = false
    player.setDynamicProperty('ks_pa:loop_land', false);
    states.loopRideMinecart = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_idle", "animation.ks_pa.humanoid_fpp.minecart_idle", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (isRidingMinecart && !isIdle) {
    states.loopLand = false
    player.setDynamicProperty('ks_pa:loop_land', false);
    states.loopRideMinecartMove = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_move", "animation.ks_pa.humanoid_fpp.minecart_move", 0.28, "third_animate_loop", "first_animate_loop");
  }

  if (idle && !holdingSword && (health > 10 || !lowHealthAnimation) && !states.loopIdleEvent) {
    states.loopLand = false
    player.setDynamicProperty('ks_pa:loop_land', false);
    states.loopIdle = true
    player.setDynamicProperty('ks_pa:loop_idle', true);
    if(standAnimation && !player.isSneaking) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (idle && holdingSword && health > 10) {
    states.loopLand = false
    player.setDynamicProperty('ks_pa:loop_land', false);
    states.loopIdleSword = true
    player.setDynamicProperty('ks_pa:loop_idle_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_sword", "animation.ks_pa.player.first.idle_sword", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (idle && health <= 10 && lowHealthAnimation) {
    states.loopLand = false;
    player.setDynamicProperty('ks_pa:loop_land', false);
    states.loopIdleLowHealth = true;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_low_health", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (walk && (health > 10 || !lowHealthAnimation) && !player.isSprinting && !holdingSword) {
    states.loopLand = false
    player.setDynamicProperty('ks_pa:loop_land', false);
    states.loopWalk = true
    player.setDynamicProperty('ks_pa:loop_walk', true);
    if (walkAnimation){
      if (third) { player.playAnimation("animation.ks_pa.player.third.walk", { blendOutTime: 0.42, stopExpression: "query.log", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.walk", { blendOutTime: 0.38, stopExpression: "query.log", controller: "first_animate_loop" }) }
    }
    else if (!player.hasTag('ks_pa_attack_anim')) {
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (walk && health > 10 && !player.isSprinting && holdingSword) {
    states.loopLand = false
    player.setDynamicProperty('ks_pa:loop_land', false);
    states.loopWalkSword = true
    player.setDynamicProperty('ks_pa:loop_walk_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.walk_sword", "animation.ks_pa.player.first.walk", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (player.isSprinting && walk && (health > 10 || !lowHealthAnimation) && !holdingSword) {
    states.loopWalk = false
    player.setDynamicProperty('ks_pa:loop_walk', false);
    states.loopSprint = true
    player.setDynamicProperty('ks_pa:loop_sprint', true);
    if (sprintAnimation)
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sprint", "animation.ks_pa.player.first.sprint", 0.32, "third_animate_loop", "first_animate_loop");
else if (!player.hasTag('ks_pa_attack_anim')) {
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (walk && health <= 10 && !player.isSprinting && lowHealthAnimation) {
    states.loopWalk = false
    player.setDynamicProperty('ks_pa:loop_walk', false);
    states.loopWalkLowHealth = true
    player.setDynamicProperty('ks_pa:loop_walk_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.walk_low_health", "animation.ks_pa.player.first.walk_low_health", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (player.isSprinting && walk && health > 10 && holdingSword) {
    states.loopLand = false
    player.setDynamicProperty('ks_pa:loop_land', false);
    states.loopSprintSword = true
    player.setDynamicProperty('ks_pa:loop_sprint_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sprint_sword", "animation.ks_pa.player.first.sprint_sword", 0.32, "third_animate_loop", "first_animate_loop");
  }

  if (sneak) {
    states.loopLand = false
    player.setDynamicProperty('ks_pa:loop_land', false);
    states.loopSneakStill = true
    player.setDynamicProperty('ks_pa:loop_sneak_still', true);
        if (third) {
      if (sneakAnimation) player.playAnimation("animation.ks_pa.player.third.sneak_still", { blendOutTime: 0.36, stopExpression: "query.log", controller: "third_animate_loop" })
    }
    else {
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.sneaking", "animation.ks_pa.player.sneaking", 0.38, "third_animate_loop", "first_animate_loop");
      if (!player.hasTag('ks_pa_attack_anim') && third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (swimStand) {
    states.loopLand = false
    player.setDynamicProperty('ks_pa:loop_land', false);
    states.loopSwimStill = true
    player.setDynamicProperty('ks_pa:loop_swim_still', true);
    if (swimAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim_still", "animation.ks_pa.player.first.swim_still", 0.0, "third_animate_loop", "first_animate_loop");
  } if (swim) {
    states.loopLand = false
    player.setDynamicProperty('ks_pa:loop_land', false);
    states.loopSwim = true
    player.setDynamicProperty('ks_pa:loop_swim', true);
    if (swimAnimation) {
      //playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim", "animation.ks_pa.player.first.swim", 0.0, "third_animate_loop", "first_animate_loop");
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "first_animate_loop" }) }
    }
  }

  if (climb) {
    states.loopLand = false
    player.setDynamicProperty('ks_pa:loop_land', false);
    states.loopClimb = true
    player.setDynamicProperty('ks_pa:loop_climb', true);
    if (climbAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.climb", "animation.ks_pa.player.first.climb", 0.48, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }
}

function callFlyStill(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const velocity = states.testPlayerVelocity;
  const isRidingMinecart = player.getDynamicProperty("ks_pa:ride_minecart") && minecartAnimation;
  const health = player.getComponent("minecraft:health").currentValue;
  const canMove = velocity >= 0.12;
  const isIdle = velocity < 0.01;
  const holdingSword = states.testholdSword && swordAnimations;
  const isGrounded = player.isOnGround && !player.isClimbing;
  const idle = isIdle && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const walk = canMove && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const fall = player.isFalling && !player.isInWater && states.loopJump == false && states.loopFall == false;
  const fly = velocity >= 0.12 && !player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const flyFast = velocity >= 0.12 && player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  // if (!flyAnimation){
  //   states.loopFlyStill = false;
  //   states.loopIdle = true;
  // }
  if (isRidingMinecart && isIdle) {
    states.loopFlyStill = false
    player.setDynamicProperty('ks_pa:loop_fly_still', false);
    states.loopRideMinecart = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_idle", "animation.ks_pa.humanoid_fpp.minecart_idle", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (isRidingMinecart && !isIdle) {
    states.loopFlyStill = false
    player.setDynamicProperty('ks_pa:loop_fly_still', false);
    states.loopRideMinecartMove = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_move", "animation.ks_pa.humanoid_fpp.minecart_move", 0.28, "third_animate_loop", "first_animate_loop");
  }

  if (idle && !holdingSword && (health > 10 || !lowHealthAnimation) && !states.loopIdleEvent) {
    states.loopFlyStill = false
    player.setDynamicProperty('ks_pa:loop_fly_still', false);
    states.loopIdle = true
    player.setDynamicProperty('ks_pa:loop_idle', true);
    if(standAnimation && !player.isSneaking) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (idle && holdingSword && health > 10) {
    states.loopFlyStill = false
    player.setDynamicProperty('ks_pa:loop_fly_still', false);
    states.loopIdleSword = true
    player.setDynamicProperty('ks_pa:loop_idle_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_sword", "animation.ks_pa.player.first.idle_sword", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (idle && health <= 10 && lowHealthAnimation) {
    states.loopFlyStill = false;
    player.setDynamicProperty('ks_pa:loop_fly_still', false);
    states.loopIdleLowHealth = true;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_low_health", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (walk && (health > 10 || !lowHealthAnimation) && !player.isSprinting && !holdingSword) {
    states.loopFlyStill = false
    player.setDynamicProperty('ks_pa:loop_fly_still', false);
    states.loopWalk = true
    player.setDynamicProperty('ks_pa:loop_walk', true);
    if (walkAnimation){
      if (third) { player.playAnimation("animation.ks_pa.player.third.walk", { blendOutTime: 0.42, stopExpression: "query.log", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.walk", { blendOutTime: 0.38, stopExpression: "query.log", controller: "first_animate_loop" }) }
    }
    else if (!player.hasTag('ks_pa_attack_anim')) {
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (walk && health > 10 && !player.isSprinting && holdingSword) {
    states.loopFlyStill = false
    player.setDynamicProperty('ks_pa:loop_fly_still', false);
    states.loopWalkSword = true
    player.setDynamicProperty('ks_pa:loop_walk_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.walk_sword", "animation.ks_pa.player.first.walk", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (player.isSprinting && walk && (health > 10 || !lowHealthAnimation) && !holdingSword) {
    states.loopFlyStill = false
    player.setDynamicProperty('ks_pa:loop_fly_still', false);
    states.loopSprint = true
    player.setDynamicProperty('ks_pa:loop_sprint', true);
    if (sprintAnimation)
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sprint", "animation.ks_pa.player.first.sprint", 0.32, "third_animate_loop", "first_animate_loop");
else if (!player.hasTag('ks_pa_attack_anim')) {
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (walk && health <= 10 && !player.isSprinting && lowHealthAnimation) {
    states.loopFlyStill = false
    player.setDynamicProperty('ks_pa:loop_fly_still', false);
    states.loopWalkLowHealth = true
    player.setDynamicProperty('ks_pa:loop_walk_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.walk_low_health", "animation.ks_pa.player.first.walk_low_health", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (player.isSprinting && walk && health > 10 && holdingSword) {
    states.loopFlyStill = false
    player.setDynamicProperty('ks_pa:loop_fly_still', false);
    states.loopSprintSword = true
    player.setDynamicProperty('ks_pa:loop_sprint_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sprint_sword", "animation.ks_pa.player.first.sprint_sword", 0.32, "third_animate_loop", "first_animate_loop");
  }

  if (fly) {
    states.loopFlyStill = false
    player.setDynamicProperty('ks_pa:loop_fly_still', false);
    states.loopFly = true
    player.setDynamicProperty('ks_pa:loop_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  } else if (flyFast) {
    states.loopFlyStill = false
    player.setDynamicProperty('ks_pa:loop_fly_still', false);
    states.loopFastFly = true
    player.setDynamicProperty('ks_pa:loop_fast_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fast_fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  }

  if (fall) {
    states.loopFlyStill = false
    player.setDynamicProperty('ks_pa:loop_fly_still', false);
    states.loopFall = true
    player.setDynamicProperty('ks_pa:loop_fall', true);
    if (fallAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.falling_long_layer", "animation.ks_pa.player.first.falling_long_layer", 0.24, "third_animate_once", "first_animate_once");
  }

  if (player.isOnGround && !player.isFlying) {
    if ( landAnimation ) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.jump_end_layer", "animation.ks_pa.player.first.jump_end_layer", 0.32, "third_animate_once", "first_animate_once");
  }
}

function callFly(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const velocity = states.testPlayerVelocity;
  const isRidingMinecart = player.getDynamicProperty("ks_pa:ride_minecart") && minecartAnimation;
  const health = player.getComponent("minecraft:health").currentValue;
  const canMove = velocity >= 0.12;
  const isIdle = velocity < 0.01;
  const holdingSword = states.testholdSword && swordAnimations;
  const isGrounded = player.isOnGround && !player.isClimbing;
  const idle = isIdle && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const walk = canMove && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const fall = player.isFalling && !player.isInWater && states.loopJump == false && states.loopFall == false;
  const flyStand = velocity < 0.12 && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const flyFast = velocity >= 0.12 && player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  if (isRidingMinecart && isIdle) {
    states.loopFly = false
    player.setDynamicProperty('ks_pa:loop_fly', false);
    states.loopRideMinecart = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_idle", "animation.ks_pa.humanoid_fpp.minecart_idle", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (isRidingMinecart && !isIdle) {
    states.loopFly = false
    player.setDynamicProperty('ks_pa:loop_fly', false);
    states.loopRideMinecartMove = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_move", "animation.ks_pa.humanoid_fpp.minecart_move", 0.28, "third_animate_loop", "first_animate_loop");
  }

  if (idle && !holdingSword && (health > 10 || !lowHealthAnimation) && !states.loopIdleEvent) {
    states.loopFly = false
    player.setDynamicProperty('ks_pa:loop_fly', false);
    states.loopIdle = true
    player.setDynamicProperty('ks_pa:loop_idle', true);
    if(standAnimation && !player.isSneaking) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (idle && holdingSword && health > 10) {
    states.loopFly = false
    player.setDynamicProperty('ks_pa:loop_fly', false);
    states.loopIdleSword = true
    player.setDynamicProperty('ks_pa:loop_idle_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_sword", "animation.ks_pa.player.first.idle_sword", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (idle && health <= 10 && lowHealthAnimation) {
    states.loopFly = false;
    player.setDynamicProperty('ks_pa:loop_fly', false);
    states.loopIdleLowHealth = true;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_low_health", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (walk && (health > 10 || !lowHealthAnimation) && !player.isSprinting && !holdingSword) {
    states.loopFly = false
    player.setDynamicProperty('ks_pa:loop_fly', false);
    states.loopWalk = true
    player.setDynamicProperty('ks_pa:loop_walk', true);
    if (walkAnimation){
      if (third) { player.playAnimation("animation.ks_pa.player.third.walk", { blendOutTime: 0.42, stopExpression: "query.log", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.walk", { blendOutTime: 0.38, stopExpression: "query.log", controller: "first_animate_loop" }) }
    }
    else if (!player.hasTag('ks_pa_attack_anim')) {
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (walk && health > 10 && !player.isSprinting && holdingSword) {
    states.loopFly = false
    player.setDynamicProperty('ks_pa:loop_fly', false);
    states.loopWalkSword = true
    player.setDynamicProperty('ks_pa:loop_walk_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.walk_sword", "animation.ks_pa.player.first.walk", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (player.isSprinting && walk && (health > 10 || !lowHealthAnimation) && !holdingSword) {
    states.loopFly = false
    player.setDynamicProperty('ks_pa:loop_fly', false);
    states.loopSprint = true
    player.setDynamicProperty('ks_pa:loop_sprint', true);
    if (sprintAnimation)
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sprint", "animation.ks_pa.player.first.sprint", 0.32, "third_animate_loop", "first_animate_loop");
else if (!player.hasTag('ks_pa_attack_anim')) {
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (walk && health <= 10 && !player.isSprinting && lowHealthAnimation) {
    states.loopFly = false
    player.setDynamicProperty('ks_pa:loop_fly', false);
    states.loopWalkLowHealth = true
    player.setDynamicProperty('ks_pa:loop_walk_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.walk_low_health", "animation.ks_pa.player.first.walk_low_health", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (player.isSprinting && walk && health > 10 && holdingSword) {
    states.loopFly = false
    player.setDynamicProperty('ks_pa:loop_fly', false);
    states.loopSprintSword = true
    player.setDynamicProperty('ks_pa:loop_sprint_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sprint_sword", "animation.ks_pa.player.first.sprint_sword", 0.32, "third_animate_loop", "first_animate_loop");
  }

  if (flyStand) {
    states.loopFly = false
    player.setDynamicProperty('ks_pa:loop_fly', false);
    states.loopFlyStill = true
    player.setDynamicProperty('ks_pa:loop_fly_still', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly_still", "animation.ks_pa.player.first.fly_still", 0.58, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (flyFast) {
    states.loopFly = false
    player.setDynamicProperty('ks_pa:loop_fly', false);
    states.loopFastFly = true
    player.setDynamicProperty('ks_pa:loop_fast_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fast_fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  }

  if (fall) {
    states.loopFall = true
    player.setDynamicProperty('ks_pa:loop_fall', true);
    if (fallAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.falling_long_layer", "animation.ks_pa.player.first.falling_long_layer", 0.24, "third_animate_once", "first_animate_once");
  }

  if (player.isOnGround && !player.isFlying) {
    if ( landAnimation ) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.jump_end_layer", "animation.ks_pa.player.first.jump_end_layer", 0.32, "third_animate_once", "first_animate_once");
  }
}

function callFastFly(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const velocity = states.testPlayerVelocity;
  const isRidingMinecart = player.getDynamicProperty("ks_pa:ride_minecart") && minecartAnimation;
  const health = player.getComponent("minecraft:health").currentValue;
  const canMove = velocity >= 0.12;
  const isIdle = velocity < 0.01;
  const holdingSword = states.testholdSword && swordAnimations;
  const isGrounded = player.isOnGround && !player.isClimbing;
  const idle = isIdle && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const walk = canMove && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;const fall = player.isFalling && !player.isInWater && states.loopJump == false && states.loopFall == false;
  const flyStand = velocity < 0.12 && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  if (isRidingMinecart && isIdle) {
    states.loopFastFly = false
    player.setDynamicProperty('ks_pa:loop_fast_fly', false);
    states.loopRideMinecart = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_idle", "animation.ks_pa.humanoid_fpp.minecart_idle", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (isRidingMinecart && !isIdle) {
    states.loopFastFly = false
    player.setDynamicProperty('ks_pa:loop_fast_fly', false);
    states.loopRideMinecartMove = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_move", "animation.ks_pa.humanoid_fpp.minecart_move", 0.28, "third_animate_loop", "first_animate_loop");
  }

  if (idle && !holdingSword && (health > 10 || !lowHealthAnimation) && !states.loopIdleEvent) {
    states.loopFastFly = false
    player.setDynamicProperty('ks_pa:loop_fast_fly', false);
    states.loopIdle = true
    player.setDynamicProperty('ks_pa:loop_idle', true);
    if(standAnimation && !player.isSneaking) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (idle && holdingSword && health > 10) {
    states.loopFastFly = false
    player.setDynamicProperty('ks_pa:loop_fast_fly', false);
    states.loopIdleSword = true
    player.setDynamicProperty('ks_pa:loop_idle_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_sword", "animation.ks_pa.player.first.idle_sword", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (idle && health <= 10 && lowHealthAnimation) {
    states.loopFastFly = false;
    player.setDynamicProperty('ks_pa:loop_fast_fly', false);
    states.loopIdleLowHealth = true;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_low_health", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (walk && (health > 10 || !lowHealthAnimation) && !player.isSprinting && !holdingSword) {
    states.loopFastFly = false
    player.setDynamicProperty('ks_pa:loop_fast_fly', false);
    states.loopWalk = true
    player.setDynamicProperty('ks_pa:loop_walk', true);
    if (walkAnimation){
      if (third) { player.playAnimation("animation.ks_pa.player.third.walk", { blendOutTime: 0.42, stopExpression: "query.log", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.walk", { blendOutTime: 0.38, stopExpression: "query.log", controller: "first_animate_loop" }) }
    }
    else if (!player.hasTag('ks_pa_attack_anim')) {
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (walk && health > 10 && !player.isSprinting && holdingSword) {
    states.loopFastFly = false
    player.setDynamicProperty('ks_pa:loop_fast_fly', false);
    states.loopWalkSword = true
    player.setDynamicProperty('ks_pa:loop_walk_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.walk_sword", "animation.ks_pa.player.first.walk", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (player.isSprinting && walk && (health > 10 || !lowHealthAnimation) && !holdingSword) {
    states.loopFastFly = false
    player.setDynamicProperty('ks_pa:loop_fast_fly', false);
    states.loopSprint = true
    player.setDynamicProperty('ks_pa:loop_sprint', true);
    if (sprintAnimation)
      playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sprint", "animation.ks_pa.player.first.sprint", 0.32, "third_animate_loop", "first_animate_loop");
else if (!player.hasTag('ks_pa_attack_anim')) {
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (walk && health <= 10 && !player.isSprinting && lowHealthAnimation) {
    states.loopFastFly = false
    player.setDynamicProperty('ks_pa:loop_fast_fly', false);
    states.loopWalkLowHealth = true
    player.setDynamicProperty('ks_pa:loop_walk_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.walk_low_health", "animation.ks_pa.player.first.walk_low_health", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (player.isSprinting && walk && health > 10 && holdingSword) {
    states.loopFastFly = false
    player.setDynamicProperty('ks_pa:loop_fast_fly', false);
    states.loopSprintSword = true
    player.setDynamicProperty('ks_pa:loop_sprint_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.sprint_sword", "animation.ks_pa.player.first.sprint_sword", 0.32, "third_animate_loop", "first_animate_loop");
  }

  if (flyStand) {
    states.loopFastFly = false
    player.setDynamicProperty('ks_pa:loop_fast_fly', false);
    states.loopFlyStill = true
    player.setDynamicProperty('ks_pa:loop_fly_still', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly_still", "animation.ks_pa.player.first.fly_still", 0.58, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (fall) {
    states.loopFall = true
    player.setDynamicProperty('ks_pa:loop_fall', true);
    if (fallAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.falling_long_layer", "animation.ks_pa.player.first.falling_long_layer", 0.24, "third_animate_once", "first_animate_once");
  }

  if (player.isOnGround && !player.isFlying) {
    if ( landAnimation ) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.jump_end_layer", "animation.ks_pa.player.first.jump_end_layer", 0.32, "third_animate_once", "first_animate_once");
  }
}

function callSwimStill(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const velocity = states.testPlayerVelocity;
  const isRidingMinecart = player.getDynamicProperty("ks_pa:ride_minecart") && minecartAnimation;
  const isIdle = velocity < 0.01;
  const climb = player.isClimbing && !player.isOnGround && !player.isInWater && !player.isSwimming;
  const flyStand = velocity < 0.12 && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const fly = velocity >= 0.12 && !player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const flyFast = velocity >= 0.12 && player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const swim = player.isInWater && velocity >= 0.12;
  const crawlStand = velocity < 0.04 && states.canCrawl == true;
  if (isRidingMinecart && isIdle) {
    states.loopSwimStill = false
    player.setDynamicProperty('ks_pa:loop_swim_still', false);
    states.loopRideMinecart = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_idle", "animation.ks_pa.humanoid_fpp.minecart_idle", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (isRidingMinecart && !isIdle) {
    states.loopSwimStill = false
    player.setDynamicProperty('ks_pa:loop_swim_still', false);
    states.loopRideMinecartMove = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_move", "animation.ks_pa.humanoid_fpp.minecart_move", 0.28, "third_animate_loop", "first_animate_loop");
  }

  if (!player.isInWater && !player.isFlying) {
    states.loopSwimStill = false
    player.setDynamicProperty('ks_pa:loop_swim_still', false);
    states.loopLand = true
    player.setDynamicProperty('ks_pa:loop_land', true);
    if ( landAnimation ) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.jump_end_layer", "animation.ks_pa.player.first.jump_end_layer", 0.32, "third_animate_once", "first_animate_once");
  }

  if (flyStand) {
    states.loopSwimStill = false
    player.setDynamicProperty('ks_pa:loop_swim_still', false);
    states.loopFlyStill = true
    player.setDynamicProperty('ks_pa:loop_fly_still', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly_still", "animation.ks_pa.player.first.fly_still", 0.58, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (fly) {
    states.loopSwimStill = false
    player.setDynamicProperty('ks_pa:loop_swim_still', false);
    states.loopFly = true
    player.setDynamicProperty('ks_pa:loop_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  } else if (flyFast) {
    states.loopSwimStill = false
    player.setDynamicProperty('ks_pa:loop_swim_still', false);
    states.loopFastFly = true
    player.setDynamicProperty('ks_pa:loop_fast_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fast_fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  }

  if (climb) {
    states.loopSwimStill = false
    player.setDynamicProperty('ks_pa:loop_swim_still', false);
    states.loopClimb = true
    player.setDynamicProperty('ks_pa:loop_climb', true);
    if (climbAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.climb", "animation.ks_pa.player.first.climb", 0.48, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (swim) {
    states.loopSwimStill = false
    player.setDynamicProperty('ks_pa:loop_swim_still', false);
    states.loopSwim = true
    player.setDynamicProperty('ks_pa:loop_swim', true);
    if (swimAnimation) {
      //playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim", "animation.ks_pa.player.first.swim", 0.0, "third_animate_loop", "first_animate_loop");
      if (third) { player.playAnimation("animation.ks_pa.player.third.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.swim", { blendOutTime: 0.0, stopExpression: "!query.is_in_water || query.modified_move_speed < 0.1", controller: "first_animate_loop" }) }
    }
  }

  if (crawlStand) {
    states.loopSwimStill = false
    player.setDynamicProperty('ks_pa:loop_swim_still', false);
    states.loopCrawlStill = true
    player.setDynamicProperty('ks_pa:loop_crawl_still', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl_still", "animation.ks_pa.player.first.crawl_still", 0.38, "third_animate_loop", "first_animate_loop");
  }
}

function callSwim(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const velocity = states.testPlayerVelocity;
  const isRidingMinecart = player.getDynamicProperty("ks_pa:ride_minecart") && minecartAnimation;
  const isIdle = velocity < 0.01;
  const climb = player.isClimbing && !player.isOnGround && !player.isInWater && !player.isSwimming;
  const flyStand = velocity < 0.12 && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const fly = velocity >= 0.12 && !player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const flyFast = velocity >= 0.12 && player.isSprinting && player.isFlying && !player.isFalling && !player.isClimbing && !player.isInWater && !player.isSwimming;
  const swimStand = player.isInWater && velocity < 0.12;
  const crawl = velocity >= 0.04 && states.canCrawl == true;
  if (isRidingMinecart && isIdle) {
    states.loopSwim = false
    player.setDynamicProperty('ks_pa:loop_swim', false);
    states.loopRideMinecart = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_idle", "animation.ks_pa.humanoid_fpp.minecart_idle", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (isRidingMinecart && !isIdle) {
    states.loopSwim = false
    player.setDynamicProperty('ks_pa:loop_swim', false);
    states.loopRideMinecartMove = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_move", "animation.ks_pa.humanoid_fpp.minecart_move", 0.28, "third_animate_loop", "first_animate_loop");
  }

  if (!player.isInWater && !player.isFlying) {
    states.loopSwim = false
    player.setDynamicProperty('ks_pa:loop_swim', false);
    states.loopLand = true
    player.setDynamicProperty('ks_pa:loop_land', true);
    if ( landAnimation ) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.jump_end_layer", "animation.ks_pa.player.first.jump_end_layer", 0.32, "third_animate_once", "first_animate_once");
  }

  if (flyStand) {
    states.loopSwim = false
    player.setDynamicProperty('ks_pa:loop_swim', false);
    states.loopFlyStill = true
    player.setDynamicProperty('ks_pa:loop_fly_still', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly_still", "animation.ks_pa.player.first.fly_still", 0.58, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (fly) {
    states.loopSwim = false
    player.setDynamicProperty('ks_pa:loop_swim', false);
    states.loopFly = true
    player.setDynamicProperty('ks_pa:loop_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  } else if (flyFast) {
    states.loopSwim = false
    player.setDynamicProperty('ks_pa:loop_swim', false);
    states.loopFastFly = true
    player.setDynamicProperty('ks_pa:loop_fast_fly', true);
    if (flyAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.fast_fly", "animation.ks_pa.player.first.fly", 0.58, "third_animate_loop", "first_animate_loop");
  }

  if (climb) {
    states.loopSwim = false
    player.setDynamicProperty('ks_pa:loop_swim', false);
    states.loopClimb = true
    player.setDynamicProperty('ks_pa:loop_climb', true);
    if (climbAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.climb", "animation.ks_pa.player.first.climb", 0.48, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  }

  if (swimStand) {
    states.loopSwim = false
    player.setDynamicProperty('ks_pa:loop_swim', false);
    states.loopSwimStill = true
    player.setDynamicProperty('ks_pa:loop_swim_still', true);
    if (swimAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.swim_still", "animation.ks_pa.player.first.swim_still", 0.0, "third_animate_loop", "first_animate_loop");
  }

  if (crawl) {
    states.loopSwim = false
    player.setDynamicProperty('ks_pa:loop_swim', false);
    states.loopCrawl = true
    player.setDynamicProperty('ks_pa:loop_crawl', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl", "animation.ks_pa.player.first.crawl", 0.38, "third_animate_loop", "first_animate_loop");
  }
}

function callCrawlStill(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const velocity = states.testPlayerVelocity;
  const isRidingMinecart = player.getDynamicProperty("ks_pa:ride_minecart") && minecartAnimation;
  const health = player.getComponent("minecraft:health").currentValue;
  const isIdle = velocity < 0.01;
  const holdingSword = states.testholdSword && swordAnimations;
  const isGrounded = player.isOnGround && !player.isClimbing;
  const idle = isIdle && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const crawl = velocity >= 0.04 && states.canCrawl == true;
  if (isRidingMinecart && isIdle) {
    states.loopCrawlStill = false
    player.setDynamicProperty('ks_pa:loop_crawl_still', false);
    states.loopRideMinecart = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_idle", "animation.ks_pa.humanoid_fpp.minecart_idle", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (isRidingMinecart && !isIdle) {
    states.loopCrawlStill = false
    player.setDynamicProperty('ks_pa:loop_crawl_still', false);
    states.loopRideMinecartMove = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_move", "animation.ks_pa.humanoid_fpp.minecart_move", 0.28, "third_animate_loop", "first_animate_loop");
  }

  if (idle && !holdingSword && (health > 10 || !lowHealthAnimation) && !states.loopIdleEvent) {
    states.loopCrawlStill = false
    player.setDynamicProperty('ks_pa:loop_crawl_still', false);
    states.loopIdle = true
    player.setDynamicProperty('ks_pa:loop_idle', true);
    if(standAnimation && !player.isSneaking) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (idle && holdingSword && health > 10 && states.canCrawl == false) {
    states.loopCrawlStill = false
    player.setDynamicProperty('ks_pa:loop_crawl_still', false);
    states.loopIdleSword = true
    player.setDynamicProperty('ks_pa:loop_idle_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_sword", "animation.ks_pa.player.first.idle_sword", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (idle && health <= 10 && states.canCrawl == false && lowHealthAnimation) {
    states.loopCrawlStill = false;
    player.setDynamicProperty('ks_pa:loop_crawl_still', false);
    states.loopIdleLowHealth = true;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_low_health", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (crawl) {
    states.loopCrawlStill = false
    player.setDynamicProperty('ks_pa:loop_crawl_still', false);
    states.loopCrawl = true
    player.setDynamicProperty('ks_pa:loop_crawl', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl", "animation.ks_pa.player.first.crawl", 0.38, "third_animate_loop", "first_animate_loop");
  }
}

function callCrawl(player, first, third, swordAnimations, standAnimation, walkAnimation, sprintAnimation, lowHealthAnimation, minecartAnimation, jumpAnimation, landAnimation, flyAnimation, climbAnimation, fallAnimation, sneakAnimation, swimAnimation, crawlAnimation) {
  let states = getPlayerAnimationState(player);
  const velocity = states.testPlayerVelocity;
  const isRidingMinecart = player.getDynamicProperty("ks_pa:ride_minecart") && minecartAnimation;
  const health = player.getComponent("minecraft:health").currentValue;
  const canMove = velocity >= 0.12;
  const isIdle = velocity < 0.01;
  const holdingSword = states.testholdSword && swordAnimations;
  const isGrounded = player.isOnGround && !player.isClimbing;
  const idle = isIdle && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const walk = canMove && !player.isSneaking && !player.isJumping && isGrounded && !player.isInWater;
  const crawlStand = velocity < 0.04 && states.canCrawl == true;
  if (isRidingMinecart && isIdle) {
    states.loopCrawl = false
    player.setDynamicProperty('ks_pa:loop_crawl', false);
    states.loopRideMinecart = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_idle", "animation.ks_pa.humanoid_fpp.minecart_idle", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (isRidingMinecart && !isIdle) {
    states.loopCrawl = false
    player.setDynamicProperty('ks_pa:loop_crawl', false);
    states.loopRideMinecartMove = true
    player.setDynamicProperty('ks_pa:loop_ride_minecart_move', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.humanoid_tpp.minecart_move", "animation.ks_pa.humanoid_fpp.minecart_move", 0.28, "third_animate_loop", "first_animate_loop");
  }

  if (idle && !holdingSword && (health > 10 || !lowHealthAnimation) && !states.loopIdleEvent) {
    states.loopCrawl = false
    player.setDynamicProperty('ks_pa:loop_crawl', false);
    states.loopIdle = true
    player.setDynamicProperty('ks_pa:loop_idle', true);
    if(standAnimation && !player.isSneaking) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
    else if (!player.hasTag('ks_pa_attack_anim')){
      playPlayerAnimation(player, third, first, "animation.humanoid.base_pose", "animation.ks_pa.player.first_person.base_pose", 0.38, "third_animate_loop", "first_animate_loop");
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (idle && holdingSword && health > 10 && states.canCrawl == false) {
    states.loopCrawl = false
    player.setDynamicProperty('ks_pa:loop_crawl', false);
    states.loopIdleSword = true
    player.setDynamicProperty('ks_pa:loop_idle_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_sword", "animation.ks_pa.player.first.idle_sword", 0.28, "third_animate_loop", "first_animate_loop");
  } else if (idle && health <= 10 && states.canCrawl == false && lowHealthAnimation) {
    states.loopCrawl = false;
    player.setDynamicProperty('ks_pa:loop_crawl', false);
    states.loopIdleLowHealth = true;
    player.setDynamicProperty('ks_pa:loop_idle_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_low_health", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (walk && !player.isSprinting && !holdingSword && states.canCrawl == true) {
    states.loopCrawl = false
    player.setDynamicProperty('ks_pa:loop_crawl', false);
    states.loopWalk = true
    player.setDynamicProperty('ks_pa:loop_walk', true);
    if (walkAnimation){
      if (third) { player.playAnimation("animation.ks_pa.player.third.walk", { blendOutTime: 0.42, stopExpression: "query.log", controller: "third_animate_loop" }) }
      if (first) { player.playAnimation("animation.ks_pa.player.first.walk", { blendOutTime: 0.38, stopExpression: "query.log", controller: "first_animate_loop" }) }
    }
    else if (!player.hasTag('ks_pa_attack_anim')) {
      if (third) playPlayerAnimation(player, third, first, "animation.ks_pa.player.head.rotation", "animation.ks_pa.player.head.rotation", 0.38, "third_animate_loop", "first_animate_loop");
    }
  } else if (walk && !player.isSprinting && health > 10 && holdingSword && states.canCrawl == true) {
    states.loopCrawl = false
    player.setDynamicProperty('ks_pa:loop_crawl', false);
    states.loopWalkSword = true
    player.setDynamicProperty('ks_pa:loop_walk_sword', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.walk_sword", "animation.ks_pa.player.first.walk", 0.38, "third_animate_loop", "first_animate_loop");
  } else if (walk && !player.isSprinting && health <= 10 && states.canCrawl == true && lowHealthAnimation) {
    states.loopCrawl = false
    player.setDynamicProperty('ks_pa:loop_crawl', false);
    states.loopWalkLowHealth = true
    player.setDynamicProperty('ks_pa:loop_walk_low_health', true);
    playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.walk_low_health", "animation.ks_pa.player.first.walk_low_health", 0.38, "third_animate_loop", "first_animate_loop");
  }

  if (crawlStand) {
    states.loopCrawl = false
    player.setDynamicProperty('ks_pa:loop_crawl', false);
    states.loopCrawlStill = true
    player.setDynamicProperty('ks_pa:loop_crawl_still', true);
    if (crawlAnimation) playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.crawl_still", "animation.ks_pa.player.first.crawl_still", 0.38, "third_animate_loop", "first_animate_loop");
  }
}

let interactDelay = false

world.beforeEvents.playerInteractWithBlock.subscribe(data => {
  const block = data.block
  const blockInteract = block.typeId == "minecraft:crafting_table" || block.typeId == "minecraft:fletching_table" || block.typeId == "minecraft:crafter" || block.typeId == "minecraft:smithing_table" || block.typeId == "minecraft:furnace" || block.typeId == "minecraft:lectern" || block.typeId == "minecraft:chiseled_bookshelf" || block.typeId == "minecraft:enchanting_table" || block.typeId == "minecraft:brewing_stand" || block.typeId == "minecraft:jukebox" || block.typeId == "minecraft:blast_furnace" || block.typeId == "minecraft:anvil" || block.typeId == "minecraft:stonecutter_block" || block.typeId == "minecraft:smoker" || block.typeId == "minecraft:cauldron" || block.typeId == "minecraft:hopper" || block.typeId == "minecraft:dropper" || block.typeId == "minecraft:dispenser" || block.typeId == "minecraft:chest" || block.typeId == "minecraft:barrel" || block.typeId == "minecraft:composter" || block.typeId == "minecraft:bed" || block.typeId == "minecraft:undyed_shulker_box" || block.typeId == "minecraft:shulker_box" || block.typeId == "minecraft:trapped_chest" || block.typeId == "minecraft:loom" || block.typeId == "minecraft:cartography_table" || block.typeId == "minecraft:grindstone" || block.typeId == "minecraft:wooden_door" || block.typeId == "minecraft:spruce_door" || block.typeId == "minecraft:birch_door" || block.typeId == "minecraft:jungle_door" || block.typeId == "minecraft:acacia_door" || block.typeId == "minecraft:dark_oak_door" || block.typeId == "minecraft:iron_door" || block.typeId == "minecraft:trapdoor" || block.typeId == "minecraft:spruce_trapdoor" || block.typeId == "minecraft:birch_trapdoor" || block.typeId == "minecraft:jungle_trapdoor" || block.typeId == "minecraft:acacia_trapdoor" || block.typeId == "minecraft:dark_oak_trapdoor" || block.typeId == "minecraft:iron_trapdoor" || block.typeId == "minecraft:stone_button" || block.typeId == "minecraft:wooden_button" || block.typeId == "minecraft:spruce_button" || block.typeId == "minecraft:birch_button" || block.typeId == "minecraft:jungle_button" || block.typeId == "minecraft:acacia_button" || block.typeId == "minecraft:dark_oak_button" || block.typeId == "minecraft:mangrove_button" || block.typeId == "minecraft:crimson_button" || block.typeId == "minecraft:warped_button" || block.typeId == "minecraft:lever"

  system.run(() => {
    if (blockInteract) {
      if (interactDelay == false) {
        interactDelay = true
        let first = data.player.getDynamicProperty("ks_pa:first");
        let third = data.player.getDynamicProperty("ks_pa:third");

        if (third) { data.player.playAnimation("animation.ks_pa.player.third.interact_layer", { blendOutTime: 0.16, controller: "third_interact_layer" }) }
        if (third) { data.player.playAnimation("animation.ks_pa.player.third.attack_rotation_layer", { blendOutTime: 0.16, stopExpression: "variable.attack_time == 0", controller: "third_attack_rotation_layer" }) }
        if (first) { data.player.playAnimation("animation.ks_pa.player.first.interact_layer", { blendOutTime: 0.16, controller: "first_interact_layer" }) }
        system.runTimeout(() => {
          if (interactDelay == true) {
            interactDelay = false
          }
        }, time(0.28))
      }
    }
  })
})

world.afterEvents.entityHurt.subscribe(data => {
  const target = data.hurtEntity
  if (target.typeId == "minecraft:player") {
    let first = target.getDynamicProperty("ks_pa:first");
    let third = target.getDynamicProperty("ks_pa:third");
    if (third) { target.playAnimation("animation.ks_pa.player.third.get_hurt_layer", { blendOutTime: 0.48, controller: "third_hurt_layer" }) }
    if (first) { target.playAnimation("animation.ks_pa.player.first.get_hurt_layer", { blendOutTime: 0.48, controller: "first_hurt_layer" }) }
  }
})

let attackCount = 0
let attackDelay = false

world.afterEvents.entityHitEntity.subscribe(data => {
  const player = data.damagingEntity
  const target = data.hitEntity
  let first = player.getDynamicProperty("ks_pa:first");
  let third = player.getDynamicProperty("ks_pa:third");
  let swordAnimations = player.getDynamicProperty("ks_pa:idle_sword_animation");
  let attackAnimations = player.getDynamicProperty("ks_pa:attack_animations");
  let standAnimation = player.getDynamicProperty("ks_pa:idle_animation");
  let walkAnimation = player.getDynamicProperty("ks_pa:walk_animation");
  let sprintAnimation = player.getDynamicProperty("ks_pa:sprint_animation");
  if (target) {
    callAttack(player, first, third, swordAnimations, attackAnimations, standAnimation, walkAnimation, sprintAnimation)
  }
})

let initiateBreakBlock = false

world.afterEvents.entityHitBlock.subscribe(data => {
  const player = data.damagingEntity
  const holdItem = player.getComponent("minecraft:inventory").container.getItem(player.selectedSlotIndex)
  const testHoldPickaxe = holdItem?.typeId == "minecraft:wooden_pickaxe" || holdItem?.typeId == "minecraft:stone_pickaxe" || holdItem?.typeId == "minecraft:iron_pickaxe" || holdItem?.typeId == "minecraft:gold_pickaxe" || holdItem?.typeId == "minecraft:diamond_pickaxe" || holdItem?.typeId == "minecraft:netherite_pickaxe"
  let first = player.getDynamicProperty("ks_pa:first");
  let third = player.getDynamicProperty("ks_pa:third");
  let swordAnimations = player.getDynamicProperty("ks_pa:idle_sword_animation");
  let attackAnimations = player.getDynamicProperty("ks_pa:attack_animations");
  let standAnimation = player.getDynamicProperty("ks_pa:idle_animation");
  let walkAnimation = player.getDynamicProperty("ks_pa:walk_animation");
  let sprintAnimation = player.getDynamicProperty("ks_pa:sprint_animation");
  if (testHoldPickaxe == true && initiateBreakBlock == false && attackAnimations) {
    initiateBreakBlock = true
    if (third) { player.playAnimation("animation.ks_pa.player.third.mine", { blendOutTime: 0.28, stopExpression: "variable.attack_time == 0", controller: "third_mine_layer" }) }
    if (third) { player.playAnimation("animation.ks_pa.player.third.attack_rotation_layer", { blendOutTime: 0.16, stopExpression: "variable.attack_time == 0", controller: "third_attack_rotation_layer" }) }
  }
  callAttack(player, first, third, swordAnimations, attackAnimations, standAnimation, walkAnimation, sprintAnimation)
})

world.afterEvents.playerBreakBlock.subscribe(data => {
  if (initiateBreakBlock == true) {
    initiateBreakBlock = false
  }
})

function callAttack(player, first, third, swordAnimations, attackAnimations, standAnimation, walkAnimation, sprintAnimation) {
  let states = getPlayerAnimationState(player);
  if (!player.hasComponent("minecraft:inventory")) return;
  if (!attackAnimations) return;
  if (states.testholdSword && attackAnimations && player.getComponent("minecraft:inventory").container.getItem(player.selectedSlotIndex)?.typeId != "minecraft:shield") {
    player.addTag('ks_pa_attack_anim');
    if (attackCount == 0 && attackDelay == false) {
      if (states.testPlayerVelocity < 0.12) {
        if (third) { player.playAnimation("animation.ks_pa.player.third.sword_attack1", { blendOutTime: 0.22, controller: "third_animate_loop" }) }
        system.runTimeout(() => {
          if (attackCount == 1 && attackDelay == false) {
            if (states.loopIdleSword == true) {
              playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_sword", "animation.ks_pa.player.first.idle_sword", 0.28, "third_animate_loop", "first_animate_loop");
            } else if (states.loopIdleLowHealth) {
              playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_low_health", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
            }
            attackCount = 0
          }
        }, time(1.2))
      } else {
        if (third) { player.playAnimation("animation.ks_pa.player.third.sword_attack1_layer", { blendOutTime: 0.22, controller: "third_attack_layer" }) }
        system.runTimeout(() => {
          if (attackCount == 1 && attackDelay == false) {
            attackCount = 0
          }
        }, time(1.2))
      }
      attackCount = 1
      attackDelay = true

      system.runTimeout(() => {
        if (attackDelay == true) {
          attackDelay = false
        }
      }, time(0.42))
    } else if (attackCount == 1 && attackDelay == false) {
      if (states.testPlayerVelocity < 0.12) {
        if (third) { player.playAnimation("animation.ks_pa.player.third.sword_attack2", { blendOutTime: 0.22, controller: "third_animate_loop" }) }
        system.runTimeout(() => {
          if (attackCount == 2 && attackDelay == false) {
            if (states.loopIdleSword == true) {
              playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_sword", "animation.ks_pa.player.first.idle_sword", 0.28, "third_animate_loop", "first_animate_loop");
            } else if (states.loopIdleLowHealth) {
              playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_low_health", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
            }
            attackCount = 0
          }
        }, time(1.2))
      } else {
        if (third) { player.playAnimation("animation.ks_pa.player.third.sword_attack2_layer", { blendOutTime: 0.22, controller: "third_attack_layer" }) }
        system.runTimeout(() => {
          if (attackCount == 1 && attackDelay == false) {
            attackCount = 0
          }
        }, time(1.2))
      }
      attackCount = 2
      attackDelay = true

      system.runTimeout(() => {
        if (attackDelay == true) {
          attackDelay = false
        }
      }, time(0.42))
    } else if (attackCount == 2 && attackDelay == false) {
      if (states.testPlayerVelocity < 0.12) {
        if (third) { player.playAnimation("animation.ks_pa.player.third.sword_attack1", { blendOutTime: 0.22, controller: "third_animate_loop" }) }
        system.runTimeout(() => {
          if (attackCount == 3 && attackDelay == false) {
            if (states.loopIdleSword == true) {
              playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_sword", "animation.ks_pa.player.first.idle_sword", 0.28, "third_animate_loop", "first_animate_loop");
            } else if (states.loopIdleLowHealth) {
              playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_low_health", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
            }
            attackCount = 0
          }
        }, time(1.2))
      } else {
        if (third) { player.playAnimation("animation.ks_pa.player.third.sword_attack1_layer", { blendOutTime: 0.22, controller: "third_attack_layer" }) }
        system.runTimeout(() => {
          if (attackCount == 1 && attackDelay == false) {
            attackCount = 0
          }
        }, time(1.2))
      }
      attackCount = 3
      attackDelay = true

      system.runTimeout(() => {
        if (attackDelay == true) {
          attackDelay = false
        }
      }, time(0.42))
    } else if (attackCount == 3 && attackDelay == false) {
      if (states.testPlayerVelocity < 0.12) {
        if (third) { player.playAnimation("animation.ks_pa.player.third.sword_attack3", { blendOutTime: 0.22, controller: "third_animate_loop" }) }
        system.runTimeout(() => {
          if (attackCount == 0 && attackDelay == false) {
            if (states.loopIdleSword == true) {
              playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_sword", "animation.ks_pa.player.first.idle_sword", 0.28, "third_animate_loop", "first_animate_loop");
            } else if (states.loopIdleLowHealth) {
              playPlayerAnimation(player, third, first, "animation.ks_pa.player.third.idle_low_health", "animation.ks_pa.player.first.idle", 0.38, "third_animate_loop", "first_animate_loop");
            }
            attackCount = 0
          }
        }, time(1.2))
      } else {
        if (third) { player.playAnimation("animation.ks_pa.player.third.sword_attack3_layer", { blendOutTime: 0.22, controller: "third_attack_layer" }) }
        system.runTimeout(() => {
          if (attackCount == 0 && attackDelay == false) {
            attackCount = 0
          }
        }, time(1.2))
      }
      attackCount = 0
      attackDelay = true

      system.runTimeout(() => {
        if (attackDelay == true) {
          attackDelay = false
        }
      }, time(0.42))
    }
    if (first) player.removeTag('ks_pa_attack_anim');
    else if (attackCount == 0) system.runTimeout(() => { player.removeTag('ks_pa_attack_anim'); }, 34);
  }
}

world.afterEvents.entityHurt.subscribe(event => {
  let entity = event.hurtEntity
  let entityDamageCause = event.damageSource.cause
  let first = entity.getDynamicProperty("ks_pa:first");
  let third = entity.getDynamicProperty("ks_pa:third");

  if (entityDamageCause == "fire" && !entity.hasTag("on_fire")) {
    entity.addTag("on_fire")
    if (third) { entity.playAnimation("animation.ks_pa.player.third.on_fire_layer", { blendOutTime: 0.38, stopExpression: "!query.is_on_fire", controller: "third_on_fire_layer" }) }
    if (first) { entity.playAnimation("animation.ks_pa.player.first.on_fire_layer", { blendOutTime: 0.38, stopExpression: "!query.is_on_fire", controller: "first_on_fire_layer" }) }
  } else if (entityDamageCause == "fireTick" && entity.hasTag("on_fire")) {
    entity.addTag("fire_tick")
    system.runTimeout(() => {
      entity.removeTag("fire_tick")
    }, time(0.6))
    system.runTimeout(() => {
      if (!entity.hasTag("fire_tick")) {
        entity.removeTag("on_fire")
      }
    }, time(1.2))
  }
})