import { Entity, Player, system, world } from "@minecraft/server";
import openDoorFunction from "./Door/openDoorFunction";

class PlayerUtils {
  /**
   * @param {Player} player
   */
  constructor(player) {
    this.player = player;
    this.scanner = player.getEntitiesFromViewDirection({
      maxDistance: 4,
      type: "scb:scanner",
    })[0]?.entity;
    this.scannerLoc = this.scanner?.location;
    this.doors = this.scanner?.dimension.getEntities({
      maxDistance: 3,
      type: "scb:scanner_door",
      location: this.scannerLoc,
    });
  }

  scannerOpen() {
    if (!this.scanner) return;
    this.doors?.forEach((door) => {
      if (!door.hasTag("scb:open")) {
        door.addTag("scb:open");
        openDoorFunction(door);
        system.runTimeout(() => {
          door.removeTag("scb:open");
          openDoorFunction(door);
        }, 40);
      }
    });
  }
}

export default PlayerUtils;
