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

// Array of dinos that can jump
const dinoTypes = ["pb_dino:pteranodon", "pb_dino:tupandactylus"];
// Define an array of air block types
const leafBlocks = [
  "minecraft:acacia_leaves",
  "minecraft:azalea_leaves",
  "minecraft:azalea_leaves_flowered",
  "minecraft:birch_leaves",
  "minecraft:cherry_leaves",
  "minecraft:dark_oak_leaves",
  "minecraft:jungle_leaves",
  "minecraft:mangrove_leaves",
  "minecraft:oak_leaves",
  "minecraft:spruce_leaves",
];

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 blockAbove = dino.dimension.getBlock({
      x: Math.floor(dino.location.x),
      y: Math.floor(dino.location.y + 3),
      z: Math.floor(dino.location.z),
    });

    // Check if the block above is in the leafBlocks array
    const foundLeaves =
      blockAbove && leafBlocks.includes(blockAbove.typeId) && !dino.isOnGround;

    if (foundLeaves) {
        dino.triggerEvent("pb_dino:move_walk");
    }
  }
});
