import { system, world } from "@minecraft/server";
import FSLocalizedString from "./fs/FSLocalizedString";
export default class RoomManager {
    constructor() {
    }
    static sharedInstance() {
        if (!RoomManager.instance) {
            RoomManager.instance = new RoomManager();
        }
        return RoomManager.instance;
    }
    init() {
        system.afterEvents.scriptEventReceive.subscribe(this.handleScriptEventReceive.bind(this));
    }
    handleScriptEventReceive(eventData) {
        let block;
        let facing_direction = -2;
        if (eventData.sourceBlock) {
            block = eventData.sourceBlock;
            if (block.permutation.matches("minecraft:command_block")) {
                facing_direction = -1;
                if (block.permutation.matches("minecraft:command_block", { facing_direction: 0 })) {
                    facing_direction = 0;
                }
                else if (block.permutation.matches("minecraft:command_block", { facing_direction: 1 })) {
                    facing_direction = 1;
                }
                else if (block.permutation.matches("minecraft:command_block", { facing_direction: 2 })) {
                    facing_direction = 2;
                }
                else if (block.permutation.matches("minecraft:command_block", { facing_direction: 3 })) {
                    facing_direction = 3;
                }
                else if (block.permutation.matches("minecraft:command_block", { facing_direction: 4 })) {
                    facing_direction = 4;
                }
                else if (block.permutation.matches("minecraft:command_block", { facing_direction: 5 })) {
                    facing_direction = 5;
                }
            }
        }
        if (block && eventData.id === "57d_sb:auto_farm" && eventData.message.indexOf("plant_crops") > -1) {
            let location0 = block.location;
            let location1 = block.location;
            let location_offset = { x: 0, y: 0, z: 0 };
            let size = { x: 0, z: 0 };
            switch (facing_direction) {
                case 0: {
                    break;
                }
                case 1: {
                    break;
                }
                case 2: {
                    location_offset = { x: +1, y: 1, z: -1 };
                    size = { x: 5, z: -5 };
                    break;
                }
                case 3: {
                    location_offset = { x: -1, y: 1, z: 1 };
                    size = { x: -5, z: 5 };
                    break;
                }
                case 4: {
                    location_offset = { x: -1, y: 1, z: -1 };
                    size = { x: -5, z: -5 };
                    break;
                }
                case 5: {
                    location_offset = { x: 1, y: 1, z: 1 };
                    size = { x: 5, z: 5 };
                    break;
                }
            }
            if (size.x != 0 || size.z != 0) {
                location0 = { x: location0.x + location_offset.x, y: location0.y + location_offset.y, z: location0.z + location_offset.z };
                location1 = { x: location0.x + size.x, y: location0.y, z: location0.z + size.z };
                let segments = eventData.message.split(":");
                let crop = "carrots";
                let growth = `"growth":7`;
                if (segments.length > 1) {
                    crop = segments[1];
                }
                if (crop === "torchflower") {
                    growth = "";
                }
                let command = `fill ${location0.x} ${location0.y} ${location0.z} ${location1.x} ${location1.y} ${location1.z} ${crop} [${growth}] replace air []`;
                world.getDimension("overworld").runCommand(command);
            }
        }
        if (block && eventData.id === "57d_sb:auto_farm" && eventData.message === "add_water") {
            let location0 = block.location;
            let location1 = block.location;
            let location_offset = { x: 0, y: 0, z: 0 };
            let size = { x: 0, z: 0 };
            switch (facing_direction) {
                case 0: {
                    break;
                }
                case 1: {
                    break;
                }
                case 2: {
                    location_offset = { x: -6, y: 2, z: -7 };
                    size = { x: 5, z: 0 };
                    break;
                }
                case 3: {
                    location_offset = { x: 6, y: 2, z: 7 };
                    size = { x: -5, z: 0 };
                    break;
                }
                case 4: {
                    location_offset = { x: -7, y: 2, z: 6 };
                    size = { x: 0, z: -5 };
                    break;
                }
                case 5: {
                    location_offset = { x: 7, y: 2, z: -6 };
                    size = { x: 0, z: 5 };
                    break;
                }
            }
            if (size.x != 0 || size.z != 0) {
                location0 = { x: location0.x + location_offset.x, y: location0.y + location_offset.y, z: location0.z + location_offset.z };
                location1 = { x: location0.x + size.x, y: location0.y, z: location0.z + size.z };
                let command = "fill " + location0.x + " " + location0.y + " " + location0.z + " " + location1.x + " " + location1.y + " " + location1.z + " water [] replace air []";
                world.getDimension("overworld").runCommand(command);
                system.runTimeout(() => {
                    let command = "fill " + location0.x + " " + location0.y + " " + location0.z + " " + location1.x + " " + location1.y + " " + location1.z + " air [] replace water []";
                    world.getDimension("overworld").runCommand(command);
                }, 20);
            }
        }
        if (block && eventData.id === "57d_sbunk:spawn_hoverboard") {
            let eqo = {
                closest: 1,
                maxDistance: 10,
                location: block.location
            };
            let players = block.dimension.getPlayers(eqo);
            if (players.length > 0) {
                let player = players[0];
                try {
                    let hoverboard = player.dimension.spawnEntity("57d_sbunk:vehicle_hoverboard", player.location);
                    if (hoverboard) {
                        hoverboard.triggerEvent(`fs:${eventData.message}`);
                    }
                    player.sendMessage({ translate: FSLocalizedString.localize("chat.hoverboard.appear", "You will find your new hoverboard by your feet.", "Hoverboard summon message") });
                    player.playSound("fs.sbunk.chat_ping");
                }
                catch (error) {
                    player.sendMessage({ translate: FSLocalizedString.localize("chat.hoverboard.error.1", "Error: Unable to summon hoverboard (error code: 1).", "Hoverboard summon error message") });
                }
            }
            else {
                world.sendMessage({ translate: FSLocalizedString.localize("chat.hoverboard.error.2", "Error: Unable to summon hoverboard (error code: 2).", "Hoverboard summon error message") });
            }
        }
    }
}

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