import { EntityInitializationCause, system, world } from "@minecraft/server";
import SecurityDoor from "./entity/SecurityDoor";
import { FSCompassDirection } from "./fs/FSCompassDirection";
import EntranceManager from "./EntranceManager";
import LaserBeam from "./entity/LaserBeam";
import FSEntityPerms from "./fs/libs/perms/FSEntityPerms";
import { FSPlayerAccessLevel } from "./fs/libs/perms/FSPlayerPerms";
import CameraManager from "./CameraManager";
import FSDynamicProperty from "./fs/libs/FSDynamicProperty";
import FSUtil from "./fs/FSUtil";
import FSLocalizedString from "./fs/FSLocalizedString";
import { CURRENT_VERSION_NUMBER_INT, DEBUG_ENTITY_MANAGEMENT_SYSTEM, cuteChickenNames, cuteCowNames, cuteHorseNames, cutePigNames, cuteSheepNames } from "./fs/FSConstants";
import FSEntityHelper from "./fs/FSEntityHelper";
import SecretEntrance from "./entity/SecretEntrance";
import LockedBlock from "./entity/LockedBlock";
import ComputerMonitor from "./entity/ComputerMonitor";
import FSWorldHelper from "./fs/FSWorldHelper";
const ENTITY_SAVE_UNLOAD_DISTANCE = 31;
const ENTITY_LOAD_DISTANCE = 30;
const MAX_INT = 20000000;
export default class EntityManager {
    constructor() {
        this.entity_structures = [];
    }
    static sharedInstance() {
        if (!EntityManager.instance) {
            EntityManager.instance = new EntityManager();
        }
        return EntityManager.instance;
    }
    init() {
        world.afterEvents.entitySpawn.subscribe(this.handleEntitySpawn.bind(this));
        world.beforeEvents.entityRemove.subscribe(this.handleBeforeEntityRemove.bind(this));
    }
    static playAnimation(entity, animation) {
        entity.runCommand(`/playanimation @s ${animation}`);
    }
    static entityPermissionDidChange(entity) {
        let accessLevel = FSEntityPerms.getEntityPermission(entity);
        if (entity.typeId === "57d_sbunk:camera") {
            CameraManager.sharedInstance().updateNameAndAccessLevel(entity.id, entity.nameTag, accessLevel);
        }
        if (entity.typeId === "57d_sbunk:security_dog" || entity.typeId === "57d_sbunk:security_turret") {
            if (entity.hasTag("fs_sbunk:attack_mobs") || entity.hasTag("fs_sbunk:attack_players")) {
                entity.triggerEvent("fs:become_angry");
            }
            else {
                entity.triggerEvent("fs:become_calm");
            }
        }
        if (entity.typeId === "57d_sbunk:locked_chest") {
            LockedBlock.updateLinkedLockPermissions(entity);
        }
        EntityManager.triggerAccessLevelEvent(entity, accessLevel);
    }
    static triggerAccessLevelEvent(entity, accessLevel) {
        try {
            switch (accessLevel) {
                default:
                case FSPlayerAccessLevel.Everyone: {
                    entity.triggerEvent("fs:set_access_level_everyone");
                    break;
                }
                case FSPlayerAccessLevel.Red: {
                    entity.triggerEvent("fs:set_access_level_red");
                    break;
                }
                case FSPlayerAccessLevel.Orange: {
                    entity.triggerEvent("fs:set_access_level_orange");
                    break;
                }
                case FSPlayerAccessLevel.Yellow: {
                    entity.triggerEvent("fs:set_access_level_yellow");
                    break;
                }
                case FSPlayerAccessLevel.Green: {
                    entity.triggerEvent("fs:set_access_level_green");
                    break;
                }
                case FSPlayerAccessLevel.Blue: {
                    entity.triggerEvent("fs:set_access_level_blue");
                    break;
                }
                case FSPlayerAccessLevel.Pink: {
                    entity.triggerEvent("fs:set_access_level_pink");
                    break;
                }
            }
        }
        catch (error) {
            console.error(error);
        }
    }
    static getCompassDirection(entity) {
        const rotation = entity.getRotation();
        if (rotation.y > -45 && rotation.y < 45) {
            return FSCompassDirection.South;
        }
        else if (rotation.y > 45 && rotation.y < 135) {
            return FSCompassDirection.West;
        }
        else if (rotation.y >= -180 && rotation.y < -135) {
            return FSCompassDirection.North;
        }
        else if (rotation.y > 135 && rotation.y <= 180) {
            return FSCompassDirection.North;
        }
        else if (rotation.y > -135 && rotation.y < -45) {
            return FSCompassDirection.East;
        }
        return FSCompassDirection.Unknown;
    }
    static getLocationOffsetByRotation(entity, dx, dy, dz) {
        let location = entity.location;
        location.y += dy;
        const direction = EntityManager.getCompassDirection(entity);
        switch (direction) {
            case FSCompassDirection.South:
                location.z += dz;
                location.x += dx;
                break;
            case FSCompassDirection.West:
                location.x -= dz;
                location.z += dx;
                break;
            case FSCompassDirection.North:
                location.z -= dz;
                location.x -= dx;
                break;
            case FSCompassDirection.East:
                location.x += dz;
                location.z -= dx;
                break;
            default:
                break;
        }
        return location;
    }
    shouldEntityHaveCuteName(entity) {
        if (!entity.isValid()) {
            return false;
        }
        if (!entity.hasTag("57d_sbunk")) {
            return false;
        }
        if (entity.typeId === "minecraft:pig") {
            return true;
        }
        else if (entity.typeId === "minecraft:cow") {
            return true;
        }
        else if (entity.typeId === "minecraft:sheep") {
            return true;
        }
        else if (entity.typeId === "minecraft:chicken") {
            return true;
        }
        else if (entity.typeId === "minecraft:horse") {
            return true;
        }
        return false;
    }
    giveEntityCuteName(entity) {
        let names;
        if (entity.typeId === "minecraft:pig") {
            names = cutePigNames;
        }
        else if (entity.typeId === "minecraft:cow") {
            names = cuteCowNames;
        }
        else if (entity.typeId === "minecraft:sheep") {
            names = cuteSheepNames;
        }
        else if (entity.typeId === "minecraft:chicken") {
            names = cuteChickenNames;
        }
        else if (entity.typeId === "minecraft:horse") {
            names = cuteHorseNames;
        }
        if (names && names.length == 10) {
            let randomIndex = Math.floor(Math.random() * names.length);
            let name = names[randomIndex];
            entity.nameTag = name;
            entity.addTag(FSUtil.namespacedString("has_cute_name"));
        }
    }
    handleEntitySpawn(eventData) {
        const entity = eventData.entity;
        const entityTypeId = entity.typeId;
        const cause = eventData.cause;
        if (!entity || !entity.isValid()) {
            return;
        }
        if (entityTypeId === "57d_sbunk:laser_floor" && cause === EntityInitializationCause.Event) {
            LaserBeam.onClose(entity);
            return;
        }
        if (entityTypeId === "57d_sbunk:laser_wall" && cause === EntityInitializationCause.Event) {
            LaserBeam.onClose(entity);
            return;
        }
        if (entityTypeId === "57d_sbunk:security_door") {
            const split_components = entity.nameTag.split("\n");
            if (split_components.length > 1) {
                eventData.entity.nameTag = split_components[0];
                const undo_id = split_components[1].replace("Room ID: ", "");
                FSDynamicProperty.setString(entity, "undo_id", undo_id);
            }
            SecurityDoor.init(entity, cause);
            FSEntityPerms.setEntityCustomName(entity, entity.nameTag);
            FSEntityPerms.setEntityPermission(entity, FSPlayerAccessLevel.Everyone);
            entity.nameTag = FSEntityPerms.getEntityCustomName(entity);
        }
        if (entityTypeId.indexOf("57d_sbunk:secret_") !== -1 && entityTypeId.indexOf("_guide") === -1) {
            SecretEntrance.init(entity, cause);
        }
        if (entityTypeId.indexOf("57d_sbunk:locked_") !== -1) {
            LockedBlock.init(entity, cause);
        }
        if (entityTypeId === "57d_sbunk:security_dog") {
            entity.nameTag = "Guard Dog";
            entity.setDynamicProperty(FSUtil.namespacedString("attack_mobs"), true);
            entity.setDynamicProperty(FSUtil.namespacedString("attack_players"), true);
            entity.addTag(FSUtil.namespacedString("attack_mobs"));
            entity.addTag(FSUtil.namespacedString("attack_players"));
            entity.triggerEvent("fs:become_angry");
        }
        if (entityTypeId === "57d_sbunk:laser_wall_floor") {
            LaserBeam.init(entity);
        }
        if (entityTypeId === "57d_sbunk:elevator") {
            EntranceManager.sharedInstance().elevatorClosed(entity, true);
        }
        if (entityTypeId === "57d_sbunk:elevator_placeable") {
            EntranceManager.sharedInstance().initEntranceEntity(entity, cause);
        }
        if (entityTypeId === "57d_sbunk:laser_floor") {
            LaserBeam.calculateHeight(entity);
            FSEntityHelper.snapRotation(entity);
            system.runTimeout(() => {
                if (entity.isValid()) {
                    LaserBeam.onClose(entity);
                }
            }, 20);
        }
        if (entityTypeId === "57d_sbunk:laser_wall") {
            system.runTimeout(() => {
                if (entity.isValid()) {
                    LaserBeam.calculateWidth(entity);
                    FSEntityHelper.snapRotation(entity);
                    system.runTimeout(() => {
                        if (entity.isValid()) {
                            LaserBeam.onClose(entity);
                        }
                    }, 20);
                }
            }, 1);
        }
        if (entityTypeId === "57d_sbunk:laser_console") {
            FSEntityHelper.snapRotation(entity);
        }
        if (entityTypeId === "57d_sbunk:secret_door") {
            FSEntityHelper.snapRotation(entity);
        }
        if (entityTypeId === "57d_sbunk:computer_monitor") {
            ComputerMonitor.init(entity, cause);
        }
        if (EntranceManager.isEntranceEntity(entity)) {
            EntranceManager.sharedInstance().initEntranceEntity(entity, eventData.cause);
            FSEntityHelper.snapRotation(entity);
        }
        if (entityTypeId === "57d_sbunk:security_turret") {
            entity.setDynamicProperty(FSUtil.namespacedString("attack_mobs"), true);
            entity.setDynamicProperty(FSUtil.namespacedString("attack_players"), true);
            entity.addTag(FSUtil.namespacedString("attack_mobs"));
            entity.addTag(FSUtil.namespacedString("attack_players"));
            entity.triggerEvent("fs:become_angry");
        }
    }
    getEntityStructures() {
        this.entity_structures = JSON.parse(world.getDynamicProperty("entity_structures") || "[]");
        return this.entity_structures;
    }
    setEntityStructures(entity_structure) {
        world.setDynamicProperty("entity_structures", JSON.stringify(entity_structure));
    }
    addEntityStructure(structure_id, structure_loc, name_tag, entity_type) {
        const entity_structure = {
            id: structure_id,
            location: structure_loc,
            name_tag: name_tag,
            entity_type: entity_type
        };
        this.entity_structures.push(entity_structure);
        this.setEntityStructures(this.entity_structures);
    }
    getEntityStructuresForLocation(loc) {
        let found_structures = [];
        const structures = this.getEntityStructures();
        for (let i = 0; i < structures.length; i++) {
            const structure = structures[i];
            const distance = Math.hypot(structure.location.x - loc.x, structure.location.y - loc.y);
            if (distance <= ENTITY_LOAD_DISTANCE) {
                found_structures.push(structure);
            }
        }
        return found_structures;
    }
    loadEntityStructuresForPlayer(player) {
        return;
        const structures = this.getEntityStructuresForLocation(player.location);
        for (let i = 0; i < structures.length; i++) {
            const structure = structures[i];
            const command = `/structure load ${structure.id} ${structure.location.x} ${structure.location.y} ${structure.location.z} 0_degrees none true false false 1`;
            player.dimension.runCommand(command);
            if (DEBUG_ENTITY_MANAGEMENT_SYSTEM) {
                world.sendMessage(`§a[!] Loading ${structure.name_tag} [${structure.entity_type}]`);
            }
            const command_delete = `/structure delete ${structure.id}`;
            player.dimension.runCommand(command_delete);
            this.entity_structures.splice(this.entity_structures.indexOf(structure), 1);
            this.setEntityStructures(this.entity_structures);
        }
    }
    saveAndUnloadEntity(entity) {
        return;
        const loc = entity.location;
        const rot = entity.getRotation();
        const dimenion = entity.dimension;
        const name_tag = entity.nameTag;
        const self_tag = FSUtil.namespacedString("self_entity");
        const has_cute_name_tag = FSUtil.namespacedString("has_cute_name");
        entity.addTag(self_tag);
        let teleported_entities = [];
        const eqo_teleport = {
            location: loc,
            maxDistance: 1,
            excludeTags: [self_tag],
            tags: ["57d_sbunk", has_cute_name_tag],
            type: entity.typeId
        };
        const entities_teleport = dimenion.getEntities(eqo_teleport);
        entities_teleport.forEach(nearby_entity => {
            if (nearby_entity && nearby_entity.isValid()) {
                nearby_entity.teleport(loc);
                teleported_entities.push(nearby_entity);
            }
        });
        const uuid = `e_${FSUtil.uuid().substring(0, 8)}_${entity.id.replace(/-/g, "")}`;
        const structure_id = `57d_sbunk:${uuid}`;
        const command = `/structure save ${structure_id} ${loc.x} ${loc.y} ${loc.z} ${loc.x} ${loc.y} ${loc.z} true disk true`;
        dimenion.runCommand(command);
        if (DEBUG_ENTITY_MANAGEMENT_SYSTEM) {
            world.sendMessage(`§6[!] Saved ${name_tag} [${entity.typeId}]`);
        }
        teleported_entities.forEach(nearby_entity => {
            if (nearby_entity && nearby_entity.isValid()) {
                if (DEBUG_ENTITY_MANAGEMENT_SYSTEM) {
                    world.sendMessage(`§5[!] Also saved ${nearby_entity.nameTag} [${nearby_entity.typeId}]`);
                }
                nearby_entity.remove();
            }
        });
        entity.removeTag(self_tag);
        this.addEntityStructure(structure_id, loc, name_tag, entity.typeId);
        entity.addTag(FSUtil.namespacedString("manually_saved"));
        entity.remove();
    }
    handleBeforeEntityRemove(eventData) {
        const entity = eventData.removedEntity;
        const entityTypeId = entity.typeId;
        if (!entity) {
            return;
        }
        if (entityTypeId === "57d_sbunk:laser_floor" || entityTypeId === "57d_sbunk:laser_wall") {
            LaserBeam.onKilled(entity);
        }
        if (entityTypeId === "57d_sbunk:security_door") {
            SecurityDoor.onKilled(entity);
        }
        if (entityTypeId.indexOf("57d_sbunk:secret_") !== -1 && entityTypeId.indexOf("_guide") === -1) {
            SecretEntrance.onKilled(entity);
        }
        if (entityTypeId.indexOf("57d_sbunk:elevator") !== -1) {
            EntranceManager.sharedInstance().elevatorKilled(entity);
        }
        if (entityTypeId.indexOf("57d_sbunk:locked_") !== -1) {
            LockedBlock.onKilled(entity);
        }
        if (EntranceManager.isEntranceEntity(entity)) {
            EntranceManager.sharedInstance().entranceRemoved(entity);
        }
    }
    static entityTypeForCuteName(cuteName) {
        if (cutePigNames.indexOf(cuteName) > -1) {
            return "minecraft:pig";
        }
        else if (cuteCowNames.indexOf(cuteName) > -1) {
            return "minecraft:cow";
        }
        else if (cuteSheepNames.indexOf(cuteName) > -1) {
            return "minecraft:sheep";
        }
        else if (cuteChickenNames.indexOf(cuteName) > -1) {
            return "minecraft:chicken";
        }
        else if (cuteHorseNames.indexOf(cuteName) > -1) {
            return "minecraft:horse";
        }
        return "";
    }
    makeEntityReplacementArmorStandsInvisible() {
        return;
        const tag = FSUtil.namespacedString("entity_replacement");
        const eqo = {
            type: "minecraft:armor_stand",
            tags: [tag, "57d_sbunk"]
        };
        const entities = world.getDimension("overworld").getEntities(eqo);
        entities.forEach(entity => {
            if (entity && entity.isValid()) {
                let eeo = {
                    amplifier: 1,
                    showParticles: false
                };
                entity.addEffect("invisibility", MAX_INT, eeo);
            }
        });
    }
    static replaceAnimalArmorStandsForPlayer(player) {
        return;
        const tag = FSUtil.namespacedString("entity_replacement");
        const eqo = {
            type: "minecraft:armor_stand",
            maxDistance: ENTITY_LOAD_DISTANCE,
            location: player.location,
            tags: [tag, "57d_sbunk"]
        };
        const entities = player.dimension.getEntities(eqo);
        entities.forEach(entity => {
            if (entity && entity.isValid()) {
                const entity_uuid = FSDynamicProperty.getString(entity, "entity_uuid");
                const entity_type = FSDynamicProperty.getString(entity, "entity_type");
                const loc = entity.location;
                const rot = entity.getRotation();
                const dimenion = entity.dimension;
                const name_tag = entity.nameTag;
                if (entity_uuid.length != 0) {
                    try {
                        system.run(() => {
                            if (DEBUG_ENTITY_MANAGEMENT_SYSTEM) {
                                world.sendMessage(`§a[!] Loading ${name_tag} [${entity_type}]`);
                            }
                            const command_load = `/structure load 57d_sbunk:${entity_uuid} ${loc.x} ${loc.y} ${loc.z} 0_degrees none true false false 1`;
                            dimenion.runCommand(command_load);
                            const command_delete = `/structure delete 57d_sbunk:${entity_uuid}`;
                            dimenion.runCommand(command_delete);
                        });
                    }
                    catch (error) {
                    }
                    finally {
                        entity.remove();
                    }
                }
                else if (entity_type.length != 0) {
                    try {
                        system.run(() => {
                            let command = `/summon ${entity_type} ${loc.x} ${loc.y} ${loc.z} ${rot.x} ${rot.y} minecraft:entity_spawned "${name_tag}"`;
                            dimenion.runCommand(command);
                            let new_entity = dimenion.spawnEntity(entity_type, loc);
                            new_entity.nameTag = name_tag;
                            new_entity.setRotation(rot);
                            new_entity.teleport(loc);
                            new_entity.addTag("57d_sbunk");
                        });
                    }
                    catch (error) {
                    }
                    finally {
                        entity.remove();
                    }
                }
                else {
                    entity.remove();
                }
            }
        });
    }
    static tickMigration() {
        const version_tag = `${FSUtil.namespacedString("version")}_${CURRENT_VERSION_NUMBER_INT}`;
        const eqo = {
            families: ["57d_sbunk"],
            excludeTags: [version_tag]
        };
        const dimensions = FSWorldHelper.getActiveDimensions();
        dimensions.forEach(dimension => {
            const entities = dimension.getEntities(eqo);
            entities.forEach(entity => {
                EntityManager.runMigration(CURRENT_VERSION_NUMBER_INT, entity, version_tag);
            });
        });
    }
    static runMigration(version, entity, version_tag) {
        try {
            if (!entity.isValid) {
                return;
            }
            switch (version) {
                case 2: {
                    try {
                        entity.triggerEvent("fs:add_knockback_resistance");
                    }
                    catch (error) { }
                    try {
                        entity.triggerEvent("fs:interaction");
                    }
                    catch (error) { }
                    entity.addTag(version_tag);
                    let entityName = entity.nameTag;
                    if (entityName.length === 0) {
                        entityName = entity.typeId.split(":")[1];
                        entityName = entityName.replace(/_/g, " ");
                    }
                }
            }
        }
        catch (error) { }
    }
    tickCuteEntities() {
        return;
        const eqo = {
            tags: [FSUtil.namespacedString("has_cute_name")]
        };
        const entities = world.getDimension("overworld").getEntities(eqo);
        const player_nearby_tag = FSUtil.namespacedString("player_nearby");
        entities.forEach(entity => {
            entity.removeTag(player_nearby_tag);
            if (entity && entity.isValid()) {
                const eqo_player = {
                    closest: 1,
                    location: entity.location,
                    maxDistance: ENTITY_SAVE_UNLOAD_DISTANCE
                };
                let players = entity.dimension.getPlayers(eqo_player);
                if (players.length > 0) {
                    entity.addTag(player_nearby_tag);
                }
            }
            if (!entity.hasTag(player_nearby_tag)) {
                this.saveAndUnloadEntity(entity);
            }
        });
    }
    tickSecurityDoors() {
        const eqo = {
            type: "57d_sbunk:security_door"
        };
        const entities = world.getDimension("overworld").getEntities(eqo);
        entities.forEach(entity => {
            if (entity && entity.isValid()) {
                SecurityDoor.tick(entity);
            }
        });
    }
    tickProjecticleLasers() {
        const eqo = {
            type: "57d_sbunk:projectile_laser",
            excludeTags: ["fs_sbunk:init"]
        };
        const entities = world.getDimension("overworld").getEntities(eqo);
        entities.forEach(entity => {
            if (entity && entity.isValid()) {
                const eqo_turret = {
                    closest: 1,
                    type: "57d_sbunk:security_turret",
                    location: entity.location,
                    maxDistance: 10
                };
                entity.dimension.getEntities(eqo_turret).forEach(turret => {
                    if (turret && turret.isValid()) {
                        EntityManager.playAnimation(turret, "fire");
                    }
                });
                const canPlayTurretSound = FSDynamicProperty.has(world, "can_play_turret_sound") ? FSDynamicProperty.getBoolean(world, "can_play_turret_sound") : true;
                if (canPlayTurretSound) {
                    world.playSound("fs.sbunk.projectile_laser_fire", entity.location);
                }
                entity.addTag("fs_sbunk:init");
            }
        });
    }
    static pingSecretEntrances(player) {
        const eqo = {
            families: ["57d_sbunk_secret_entrance"],
            location: player.location,
            maxDistance: 16
        };
        const eqo_closest = {
            families: ["57d_sbunk_secret_entrance"],
            location: player.location,
            maxDistance: 16,
            closest: 1
        };
        const entities = player.dimension.getEntities(eqo);
        const closest = player.dimension.getEntities(eqo_closest);
        entities.forEach(entity => {
            if (entity && entity.isValid()) {
                entity.dimension.spawnParticle("fs_sbunk:sbunk_secret_entrance", entity.location);
            }
        });
        if (entities.length > 0 && closest.length > 0) {
            const message = {
                rawtext: [
                    { text: "§a[!] " },
                    {
                        translate: FSLocalizedString.localize("message.secret_entrances.ping_found_some", "Ping found %s Secret Entrances nearby. You are now facing the closest one to your location.", "Tip/instructions for player when they ping secret entrances. %s is replaced with the number of secret entrances found."),
                        with: [entities.length.toString()]
                    }
                ]
            };
            const closestEntity = closest[0];
            const to = {
                facingLocation: closestEntity.location,
                checkForBlocks: true
            };
            player.teleport(player.location, to);
            player.sendMessage(message);
            player.playSound("fs.sbunk.chat_ping");
        }
        else {
            const message = {
                rawtext: [
                    { text: "§c[!] " },
                    {
                        translate: FSLocalizedString.localize("message.secret_entrances.ping_found_none", "Ping found no Secret Entrances nearby.", "Tip/instructions for player when they ping secret entrances.")
                    }
                ]
            };
            player.sendMessage(message);
            player.playSound("fs.sbunk.chat_ping");
        }
    }
    tickSecurityTurrets() {
        const eqo = {
            type: "57d_sbunk:security_turret"
        };
        const entities = world.getDimension("overworld").getEntities(eqo);
        entities.forEach(entity => {
            if (entity && entity.isValid()) {
                entity.teleport(entity.location);
            }
        });
    }
    tickLaserWalls() {
        const entities = [];
        entities.push(...FSWorldHelper.getEntitiesInActiveDimensions({ type: "57d_sbunk:laser_wall_floor" }));
        entities.push(...FSWorldHelper.getEntitiesInActiveDimensions({ type: "57d_sbunk:laser_wall" }));
        entities.push(...FSWorldHelper.getEntitiesInActiveDimensions({ type: "57d_sbunk:laser_floor" }));
        entities.forEach(entity => {
            if (entity && entity.isValid()) {
                LaserBeam.tick(entity);
            }
        });
    }
    static getClosestPlayer(entity) {
        const eqo = {
            closest: 1,
            location: entity.location
        };
        let players = entity.dimension.getPlayers(eqo);
        if (players.length > 0) {
            return players[0];
        }
        return undefined;
    }
    tickBunkerEntrances() {
        const eqo = {
            type: "57d_sbunk:entrance_bunker"
        };
        const entities = world.getDimension("overworld").getEntities(eqo);
        entities.forEach((entity) => {
            if (entity && entity.isValid()) {
                if (!FSDynamicProperty.getBoolean(entity, "has_been_opened")) {
                    entity.dimension.spawnParticle("fs_sbunk:sbunk_look_here", entity.location);
                }
            }
        });
    }
    tickSecurityBots() {
        const eqo = {
            type: "57d_sbunk:security_bot"
        };
        const entities = world.getDimension("overworld").getEntities(eqo);
        entities.forEach((entity) => {
            if (entity && entity.isValid()) {
                let player = EntityManager.getClosestPlayer(entity);
                if (player) {
                    const tpo = {
                        facingLocation: player.location
                    };
                    entity.teleport(entity.location, tpo);
                }
            }
        });
    }
    tickTrainingBots() {
        const eqo = {
            type: "57d_sbunk:training_bot"
        };
        const entities = world.getDimension("overworld").getEntities(eqo);
        entities.forEach((entity) => {
            if (entity && entity.isValid()) {
                let player = EntityManager.getClosestPlayer(entity);
                if (player) {
                    const tpo = {
                        facingLocation: player.location
                    };
                    entity.teleport(entity.location, tpo);
                }
            }
        });
    }
    tickElevator() {
        const eqo = {
            type: "57d_sbunk:elevator"
        };
        const entities = world.getDimension("overworld").getEntities(eqo);
        entities.forEach((entity) => {
            if (entity && entity.isValid()) {
                const eqo_player = {
                    closest: 1,
                    location: entity.location,
                    maxDistance: 5
                };
                let players = entity.dimension.getPlayers(eqo_player);
                if (players.length > 0) {
                    const didSendElevatorToolip = FSDynamicProperty.getBoolean(world, FSUtil.namespacedString("did_send_elevator_tooltip")) || false;
                    if (!didSendElevatorToolip) {
                        FSDynamicProperty.setBoolean(world, FSUtil.namespacedString("did_send_elevator_tooltip"), true);
                        players.forEach(player => {
                            const message = {
                                rawtext: [
                                    { text: "§a[!] " },
                                    {
                                        translate: FSLocalizedString.localize("elevator.tip.1", "Elevator Doors can be opened by interacting with them (without a Security Tablet). Once inside you can sneak/jump to navigate between floors.", "Tip/instructions for player about elevator doors")
                                    }
                                ]
                            };
                            player.sendMessage(message);
                            world.playSound("fs.sbunk.chat_ping", entity.location);
                            system.runTimeout(() => {
                                const message2 = {
                                    rawtext: [
                                        { text: "§a[!] " },
                                        {
                                            translate: FSLocalizedString.localize("elevator.tip.2", "You can also assign a floor name and welcome message to an elevator by using your Security Tablet.", "Tip/instructions for player about renaming floor names via the elevator.")
                                        }
                                    ]
                                };
                                player.sendMessage(message2);
                                world.playSound("fs.sbunk.chat_ping", entity.location);
                            }, 40);
                        });
                    }
                }
            }
        });
    }
}

//# sourceMappingURL=../../../_SBUNK_BPDebug/fs_sbunk/EntityManager.js.map
