// Import necessary modules from Minecraft server API
import { world, BlockVolume } from "@minecraft/server";

const testBlocks = ["hb_cb:backpack_1_block", "hb_cb:backpack_2_block"];

export function checkForBpBlocks(player, curTick) {
  if (curTick % 60 != 0) return;
  const volume = new BlockVolume({ x: player.location.x + 3, y: player.location.y + 2, z: player.location.z + 3 }, { x: player.location.x - 3, y: player.location.y - 2, z: player.location.z - 3 });

  const locations = volume.getBlockLocationIterator();

  for (const location of locations) {
    const block = player.dimension.getBlock(location);
    if (!block) continue;
    const blockType = block.typeId;
    if (!testBlocks.includes(blockType)) continue;
    let checkLoc = { x: block.center().x, y: block.center().y - 0.4, z: block.center().z };
    if (blockType.includes("backpack_2_block")) checkLoc = { x: block.center().x, y: block.below().center().y - 0.4, z: block.center().z };
    let nearbyEntities = world.getDimension(player.dimension.id).getEntities({
      families: ["hb_cb_backpack"],
      location: checkLoc,
      minDistance: 0,
      maxDistance: 0.2,
    });
    if (nearbyEntities.length === 0 || !nearbyEntities) {
      block.setType("minecraft:air");
      continue;
    }
  }
}
