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

// Array of dinos that can jump
const dinoTypes = ["pb_dino:herrerasaurus", "pb_dino:prenocephale", "pb_dino:utahraptor", "pb_dino:velociraptor"];
// Define an array of air block types
const airBlocks = ["minecraft:air"];

system.runInterval(() => {
  const dimensions = [
    world.getDimension("overworld"),
    world.getDimension("nether"),
    world.getDimension("the_end"),
  ];

  let dinos = [];

  // Fetch entities for each type in each dimension
  for (const dimension of dimensions) {
    for (const type of dinoTypes) {
      dinos = dinos.concat(dimension.getEntities({ type }));
    }
  }

  for (const dino of dinos) {
    // Get the block below the dino
    const blockBelow = dino.dimension.getBlock({
      x: Math.floor(dino.location.x),
      y: Math.floor(dino.location.y - 0.5),
      z: Math.floor(dino.location.z),
    });

    // Check if the block below is in the airBlocks array
    const isInAir = blockBelow && airBlocks.includes(blockBelow.typeId) && !dino.isOnGround;

    // Update the "pb_dino:in_air" property
    try {
      dino.setProperty("pb_dino:in_air", isInAir);
    } catch (error) {}
  }
});
