import { system, world, ItemStack } from "@minecraft/server"
import { getLocalPosition } from "./localPost.js"
import { ActionFormData } from "@minecraft/server-ui"
function sleep(tick) {
  return new Promise(resolve => {
    system.runTimeout(() => {
      resolve(tick)
    }, tick)
  })
}
world.afterEvents.entitySpawn.subscribe(async e => {
  const entity = e.entity
  if (!entity.typeId.includes("4ks_da")) return
  if (entity.typeId.includes("secret_door")) return
  if (entity.typeId.includes("beam")) return
  await sleep(2)
  entity.setDynamicProperty("4ks_da:open", true)
  if (entity.typeId.includes("garage_door_small")) {
    garageDoorSmallClose(entity)
    return
  } if (entity.typeId.includes("space_door_large")) {
    spaceDoorLargeClose(entity)
    return
  } else if (entity.typeId.includes("space_door_hangar")) {
    spaceDoorHangarClose(entity)
    return
  } else if (entity.typeId.includes("space_door")) {
    spaceDoorClose(entity)
    return
  } else if (entity.typeId.includes("shoji_door")) {
    shojiDoorClose(entity)
  } else if (entity.typeId.includes("double_barn_door")) {
    entity.runCommand("/event entity @s 4ks_da:enable_visual")
    doubleBranDoorClose(entity)
    return
  } else if (entity.typeId.includes("garage_door_medium")) {
    await sleep(1)
    entity.runCommand("/tp ^-0.5^^")
    entity.runCommand("/event entity @s 4ks_da:enable_visual")
    garageDoorMediumClose(entity)
    return
  } else if (entity.typeId.includes("garage_door_large")) {
    await sleep(4)
    entity.runCommand("/event entity @s 4ks_da:enable_visual")
    garageDoorLargeClose(entity)
    return
  } else if (entity.typeId.includes("4ks_da:glass_door")) {
    glassDoorClose(entity)
    entity.runCommand("/event entity @s 4ks_da:enable_visual")
    return
  } else {
    woodDoorClose(entity)
  }
  await sleep(1)
  if (!entity || !entity.isValid() || entity.hasTag("4ksdakill")) {
    return
  }
  entity.setDynamicProperty("4ks_da:location", entity.location)
  const right = getLocalPosition(entity.location, entity.getRotation(), { x: 1, y: 0, z: 0 })
  let pairLeft = false;
  let pairEntity = entity.dimension.getEntities({ location: right, maxDistance: 0.5, count: 1, type: entity.typeId, tags: entity.getTags() })


  if (pairEntity[0] == undefined || pairEntity[0].id == entity.id || pairEntity[0].getTags().length > 1) {
    const left = getLocalPosition(entity.location, entity.getRotation(), { x: -1, y: 0, z: 0 })
    pairEntity = entity.dimension.getEntities({ location: left, maxDistance: 0.5, count: 1, type: entity.typeId, tags: entity.getTags() })
    pairLeft = true
  }


  if (pairEntity[0] == undefined || pairEntity[0].id == entity.id || pairEntity[0].getTags().length > 1)
    return

  pairEntity[0].addTag("pl_" + entity.id)
  entity.addTag("pl_" + entity.id)
  await sleep(2)
  pairEntity[0].addTag("pl_" + pairEntity[0].id)
  entity.addTag("pl_" + pairEntity[0].id)

  if (pairLeft) {
    entity.triggerEvent("4ks_da:pair_left")
    entity.addTag("4ksdapl")
  } else {
    pairEntity[0].triggerEvent("4ks_da:pair_left")
    pairEntity[0].addTag("4ksdapl")
  }
  pairEntity[0].triggerEvent("4ks_da:close")
  entity.triggerEvent("4ks_da:close")
});

function garageDoorMediumOpen(entity) {
  if (entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", true)
  entity.runCommand("/playsound open.wooden_door @a[r=8]")

  //north is correct
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~2~ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^2^ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^2^2^ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-1^2^ 4ks_da:garage_door_block_top")

  //west is correct
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-2^2^ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^2^ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^2^ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^^^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^^2^ 4ks_da:garage_door_block_top")

  //east is correct
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~2~ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^2^ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^2^2^ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-1^2^ 4ks_da:garage_door_block_top")

  //south is correct
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-2^2^ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^2^ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^2^ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^^^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^^2^ 4ks_da:garage_door_block_top")

}
async function garageDoorMediumClose(entity) {

  if (!entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", false)
  entity.runCommand("/playsound close.wooden_door @a[r=8]")

  //north is correct
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ~~~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ~~1~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ~~2~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ^1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ^1^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ^1^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ^2^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ^2^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ^2^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ^-1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ^-1^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ^-1^2^ air run tag @s add 4ksdakill")

  //west is incorrect
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^-2^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^-2^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^-2^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^1^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^1^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^-1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^-1^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^-1^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^^2^ air run tag @s add 4ksdakill")

  //east is correct
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ~~~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ~~1~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ~~2~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ^1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ^1^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ^1^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ^2^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ^2^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ^2^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ^-1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ^-1^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ^-1^2^ air run tag @s add 4ksdakill")

  //south is incorrect
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^-2^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^-2^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^-2^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^1^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^1^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^-1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^-1^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^-1^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^^2^ air run tag @s add 4ksdakill")

  if (entity.hasTag("4ksdakill") && !entity.hasTag("4ksdainit")) {
    entity.dimension.spawnItem(new ItemStack(`${entity.typeId}_spawn_egg`), entity.location)
    entity.triggerEvent("4ks_da:despawn")
    return
  }
  entity.addTag("4ksdainit")

  //north is correct
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~2~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^1^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^2^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^2^^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^2^1^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^2^2^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-1^^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-1^1^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-1^2^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")

  //west is incorrect
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-2^^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-2^1^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-2^2^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^1^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^2^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^1^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^2^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^^^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^^1^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^^2^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")

  //east is correct
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~2~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^1^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^2^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^2^^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^2^1^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^2^2^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-1^^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-1^1^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-1^2^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")

  //south is incorrect
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-2^^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-2^1^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-2^2^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^1^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^2^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^1^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^2^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^^^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^^1^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^^2^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
}
function doubleBranDoorOpen(entity) {
  if (entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", true)
  entity.runCommand("/playsound open.wooden_door @a[r=8]")

  //north is correct
  entity.runCommand("/execute as @s run setblock ~~~ air")
  entity.runCommand("/execute as @s run setblock ~~1~ air")
  entity.runCommand("/execute as @s run setblock ^1^^ air")
  entity.runCommand("/execute as @s run setblock ^1^1^ air")

}
function doubleBranDoorClose(entity) {

  if (!entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", false)
  entity.runCommand("/playsound close.wooden_door @a[r=8]")

  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ~~~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ~~1~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ^1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ^1^1^ air run tag @s add 4ksdakill")

  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ~~~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ~~1~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^1^1^ air run tag @s add 4ksdakill")

  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ~~~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ~~1~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ^1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ^1^1^ air run tag @s add 4ksdakill")

  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ~~~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ~~1~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^1^1^ air run tag @s add 4ksdakill")

  if (entity.hasTag("4ksdakill") && !entity.hasTag("4ksdainit")) {
    entity.dimension.spawnItem(new ItemStack(`${entity.typeId}_spawn_egg`), entity.location)
    entity.triggerEvent("4ks_da:despawn")
    return
  }
  entity.addTag("4ksdainit")

  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^1^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")

  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^1^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")

  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^1^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")

  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^1^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")


}
function woodDoorOpen(entity) {
  if (entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", true)
  entity.runCommand("/playsound open.wooden_door @a[r=8]")
  const result = entity.runCommand(`/event entity @e[rm=0.1, c=1, tag=pl_${entity.id}] 4ks_da:open`)
  if (result.successCount == 0) {
    entity.triggerEvent("4ks_da:remove_pair_left")
    entity.removeTag("4ksdapl")
    for (const tag of entity.getTags()) {
      if (tag.startsWith("pl_")) entity.removeTag(tag)
    }
  }
  entity.runCommand("/execute as @s[tag=4ksdapl,tag=4ksdanorth] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdapl,tag=4ksdanorth] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdapl,tag=4ksdasouth] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdapl,tag=4ksdasouth] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdapl,tag=4ksdawest] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdapl,tag=4ksdawest] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdapl,tag=4ksdaeast] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdapl,tag=4ksdaeast] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")

  entity.runCommand("/execute as @s[tag=!4ksdapl,tag=4ksdanorth] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=!4ksdapl,tag=4ksdanorth] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=!4ksdapl,tag=4ksdasouth] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=!4ksdapl,tag=4ksdasouth] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=!4ksdapl,tag=4ksdawest] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=!4ksdapl,tag=4ksdawest] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=!4ksdapl,tag=4ksdaeast] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=!4ksdapl,tag=4ksdaeast] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")


}
function woodDoorClose(entity) {
  if (!entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", false)
  entity.runCommand("/playsound close.wooden_door @a[r=8]")
  const result = entity.runCommand(`/event entity @e[rm=0.1, c=1, tag=pl_${entity.id}] 4ks_da:close`)
  if (result.successCount == 0) {
    entity.triggerEvent("4ks_da:remove_pair_left")
    entity.removeTag("4ksdapl")
    for (const tag of entity.getTags()) {
      if (tag.startsWith("pl_")) entity.removeTag(tag)
    }
  }
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
}

function spaceDoorOpen(entity) {
  if (entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", true)
  entity.runCommand("/playsound open.wooden_door @a[r=8]")
  entity.runCommand("/execute as @s run setblock ~~~ air")
  entity.runCommand("/execute as @s run setblock ~~1~ air")



}
function spaceDoorClose(entity) {
  if (!entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", false)

  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ~~~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ~~1~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ~~~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ~~1~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ~~~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ~~1~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ~~~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ~~1~ air run tag @s add 4ksdakill")
  if (entity.hasTag("4ksdakill") && !entity.hasTag("4ksdainit")) {
    entity.dimension.spawnItem(new ItemStack(`${entity.typeId}_spawn_egg`), entity.location)
    entity.triggerEvent("4ks_da:despawn")
    return
  }
  entity.addTag("4ksdainit")

  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/playsound close.wooden_door @a[r=8]")

}

async function spaceDoorLargeOpen(entity) {
  if (entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", true)
  await sleep(8)
  entity.runCommand("/playsound open.wooden_door @a[r=8]")
  entity.runCommand("/execute as @s run fill ^-1^^ ^1^2^ air")
}
async function spaceDoorLargeClose(entity) {
  if (!entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", false)
  entity.runCommand("/execute positioned as @s rotated as @s unless block ^-1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute positioned as @s rotated as @s unless block ^^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute positioned as @s rotated as @s unless block ^1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute positioned as @s rotated as @s unless block ^-1^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute positioned as @s rotated as @s unless block ^^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute positioned as @s rotated as @s unless block ^1^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute positioned as @s rotated as @s unless block ^-1^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute positioned as @s rotated as @s unless block ^^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute positioned as @s rotated as @s unless block ^1^2^ air run tag @s add 4ksdakill")

  if (entity.hasTag("4ksdakill") && !entity.hasTag("4ksdainit")) {
    entity.dimension.spawnItem(new ItemStack(`${entity.typeId}_spawn_egg`), entity.location)
    entity.triggerEvent("4ks_da:despawn")
    return
  }
  entity.addTag("4ksdainit")
  await sleep(8)
  entity.runCommand("/execute as @s[tag=4ksdanorth] run fill ^-1^^ ^1^2^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run fill ^-1^^ ^1^2^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run fill ^-1^^ ^1^2^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run fill ^-1^^ ^1^2^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/playsound close.wooden_door @a[r=8]")
}

async function spaceDoorHangarOpen(entity) {
  if (entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", true)
  await sleep(30)
  entity.runCommand("/playsound open.wooden_door @a[r=8]")
  entity.runCommand("/execute as @s run fill ^-1^^ ^1^2^ air")



}
async function spaceDoorHangarClose(entity) {
  if (!entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", false)
  entity.runCommand("/execute positioned as @s rotated as @s unless block ^-2^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute positioned as @s rotated as @s unless block ^-1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute positioned as @s rotated as @s unless block ^^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute positioned as @s rotated as @s unless block ^1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute positioned as @s rotated as @s unless block ^2^^ air run tag @s add 4ksdakill")

  entity.runCommand("/execute positioned as @s rotated as @s unless block ^-2^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute positioned as @s rotated as @s unless block ^-1^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute positioned as @s rotated as @s unless block ^^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute positioned as @s rotated as @s unless block ^1^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute positioned as @s rotated as @s unless block ^2^1^ air run tag @s add 4ksdakill")

  entity.runCommand("/execute positioned as @s rotated as @s unless block ^-2^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute positioned as @s rotated as @s unless block ^-1^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute positioned as @s rotated as @s unless block ^^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute positioned as @s rotated as @s unless block ^1^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute positioned as @s rotated as @s unless block ^2^2^ air run tag @s add 4ksdakill")
  if (entity.hasTag("4ksdakill") && !entity.hasTag("4ksdainit")) {
    entity.dimension.spawnItem(new ItemStack(`${entity.typeId}_spawn_egg`), entity.location)
    entity.triggerEvent("4ks_da:despawn")
    return
  }
  entity.addTag("4ksdainit")
  await sleep(3)
  entity.runCommand("/execute as @s run fill ^-2^^ ^2^2^ barrier")
  entity.runCommand("/playsound close.wooden_door @a[r=8]")

}

function glassDoorOpen(entity) {
  if (entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", true)
  entity.runCommand("/playsound open.wooden_door @a[r=8]")

  entity.runCommand("/execute as @s run setblock ~~~ air")
  entity.runCommand("/execute as @s run setblock ~~1~ air")
  entity.runCommand("/execute as @s run setblock ^1^^ air")
  entity.runCommand("/execute as @s run setblock ^1^1^ air")

}
function glassDoorClose(entity) {

  if (!entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", false)
  entity.runCommand("/playsound close.wooden_door @a[r=8]")
  //north is correct

  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^1^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")

  //west is incorrect

  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^1^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")

  //east is correct

  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^1^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")

  //south is incorrect

  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^1^ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")


}

function shojiDoorOpen(entity) {
  if (entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", true)
  entity.runCommand("/playsound open.wooden_door @a[r=8]")
  const result = entity.runCommand(`/event entity @e[rm=0.1, c=1, tag=pl_${entity.id}] 4ks_da:open`)
  if (result.successCount == 0) {
    entity.triggerEvent("4ks_da:remove_pair_left")
    entity.removeTag("4ksdapl")
    for (const tag of entity.getTags()) {
      if (tag.startsWith("pl_")) entity.removeTag(tag)
    }
  }

  entity.runCommand("/execute as @s run setblock ~~~ air")
  entity.runCommand("/execute as @s run setblock ~~1~ air")

}
function shojiDoorClose(entity) {
  if (!entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", false)
  entity.runCommand("/playsound close.wooden_door @a[r=8]")
  const result = entity.runCommand(`/event entity @e[rm=0.1, c=1, tag=pl_${entity.id}] 4ks_da:close`)
  if (result.successCount == 0) {
    entity.triggerEvent("4ks_da:remove_pair_left")
    entity.removeTag("4ksdapl")
    for (const tag of entity.getTags()) {
      if (tag.startsWith("pl_")) entity.removeTag(tag)
    }
  }
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~1~ 4ks_da:door_barrier [\"minecraft:cardinal_direction\"=\"east\"]")
}

function garageDoorLargeOpen(entity) {
  if (entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", true)
  entity.runCommand("/playsound open.wooden_door @a[r=8]")


  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~2~ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^2^ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-1^2^ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^2^2^ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-2^2^ 4ks_da:garage_door_block_top")

  //west is correct
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~2~ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^2^ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^2^ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-2^2^ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^2^2^ 4ks_da:garage_door_block_top")

  //east is correct
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~2~ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^2^ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-1^2^ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^2^2^ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-2^2^ 4ks_da:garage_door_block_top")

  //south is correct
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~2~ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^2^ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^2^ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-2^2^ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^2^2^ 4ks_da:garage_door_block_top")

}
function garageDoorLargeClose(entity) {

  if (!entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", false)
  entity.runCommand("/playsound close.wooden_door @a[r=8]")

  //north is correct
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ~~~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ~~1~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ~~2~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ^1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ^1^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ^1^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ^-1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ^-1^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ^-1^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ^2^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ^2^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ^2^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ^-2^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ^-2^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdanorth] unless block ^-2^2^ air run tag @s add 4ksdakill")

  //west is correct
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ~~~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ~~1~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ~~2~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^-1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^-1^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^-1^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^1^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^1^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^-2^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^-2^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^-2^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^2^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^2^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdawest] unless block ^2^2^ air run tag @s add 4ksdakill")

  //east is correct
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ~~~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ~~1~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ~~2~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ^1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ^1^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ^1^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ^-1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ^-1^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ^-1^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ^2^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ^2^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ^2^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ^-2^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ^-2^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdaeast] unless block ^-2^2^ air run tag @s add 4ksdakill")

  //south is correct
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ~~~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ~~1~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ~~2~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^-1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^-1^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^-1^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^1^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^1^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^-2^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^-2^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^-2^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^2^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^2^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s[tag=4ksdasouth] unless block ^2^2^ air run tag @s add 4ksdakill")

  if (entity.hasTag("4ksdakill") && !entity.hasTag("4ksdainit")) {
    entity.dimension.spawnItem(new ItemStack(`${entity.typeId}_spawn_egg`), entity.location)
    entity.triggerEvent("4ks_da:despawn")
    return
  }
  entity.addTag("4ksdainit")


  //north is correct
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~~ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~1~ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~2~ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^1^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^2^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-1^^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-1^1^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-1^2^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^2^^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^2^1^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^2^2^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-2^^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-2^1^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-2^2^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"north\"]")

  //west is correct
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~~ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~1~ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~2~ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^1^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^2^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^1^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^2^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-2^^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-2^1^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-2^2^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^2^^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^2^1^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^2^2^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"west\"]")

  //east is correct
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~~ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~1~ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~2~ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^1^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^2^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-1^^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-1^1^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-1^2^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^2^^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^2^1^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^2^2^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-2^^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-2^1^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-2^2^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"east\"]")

  //south is correct
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~~ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~1~ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~2~ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^1^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^2^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^1^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^2^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-2^^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-2^1^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-2^2^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^2^^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^2^1^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^2^2^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"south\"]")


}
function garageDoorSmallOpen(entity) {
  if (entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", true)
  entity.runCommand("/playsound open.wooden_door @a[r=8]")

  //north is correct
  entity.runCommand("/execute as @s run setblock ~~~ air")
  entity.runCommand("/execute as @s run setblock ~~1~ air")
  entity.runCommand("/execute as @s run setblock ~~2~ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s run setblock ^1^^ air")
  entity.runCommand("/execute as @s run setblock ^1^1^ air")
  entity.runCommand("/execute as @s run setblock ^1^2^ 4ks_da:garage_door_block_top")
  entity.runCommand("/execute as @s run setblock ^-1^^ air")
  entity.runCommand("/execute as @s run setblock ^-1^1^ air")
  entity.runCommand("/execute as @s run setblock ^-1^2^ 4ks_da:garage_door_block_top")

}
function garageDoorSmallClose(entity) {

  if (!entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", false)
  entity.runCommand("/playsound close.wooden_door @a[r=8]")

  //north is correct
  entity.runCommand("/execute as @s unless block ~~~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s unless block ~~1~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s unless block ~~2~ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s unless block ^1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s unless block ^1^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s unless block ^1^2^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s unless block ^-1^^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s unless block ^-1^1^ air run tag @s add 4ksdakill")
  entity.runCommand("/execute as @s unless block ^-1^2^ air run tag @s add 4ksdakill")

  if (entity.hasTag("4ksdakill") && !entity.hasTag("4ksdainit")) {
    entity.dimension.spawnItem(new ItemStack(`${entity.typeId}_spawn_egg`), entity.location)
    entity.triggerEvent("4ks_da:despawn")
    return
  }
  entity.addTag("4ksdainit")

  //north is correct
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~~ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~1~ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~2~ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^1^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^2^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-1^^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-1^1^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"north\"]")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-1^2^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"north\"]")

  //west is correct
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~~ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~1~ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~2~ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^1^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^2^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^1^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"west\"]")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^2^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"west\"]")

  //east is correct
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~~ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~1~ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~2~ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^1^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^2^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-1^^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-1^1^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"east\"]")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-1^2^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"east\"]")

  //south is correct
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~~ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~1~ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~2~ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^1^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^2^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^1^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"south\"]")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^2^ 4ks_da:garage_door_block [\"minecraft:cardinal_direction\"=\"south\"]")


}

function garageDoorMediumRemove(entity) {
  //north is correct
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~2~ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^2^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-1^2^ air")

  //west is incorrect
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-2^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^^^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^^2^ air")

  //east is correct
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~2~ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^2^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-1^2^ air")

  //south is incorrect
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-2^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^^^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^^2^ air")
}
function doubleBranDoorRemove(entity) {
  //north is correct
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^2^1^ air")

  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^1^ air")

  //west is incorrect
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^1^ air")

  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^1^ air")

  //east is correct
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^2^1^ air")

  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^1^ air")

  //south is incorrect
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^1^ air")

  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^1^ air")


}
function spaceDoorRemove(entity) {
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~1~ air")
}
async function spaceDoorLargeRemove(entity) {
  entity.runCommand("/execute as @s[tag=4ksdanorth] run fill ^-1^^ ^1^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run fill ^-1^^ ^1^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run fill ^-1^^ ^1^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run fill ^-1^^ ^1^2^ air")
}
async function spaceDoorHangarRemove(entity) {
  entity.runCommand("/execute as @s run fill ^-2^^ ^2^2^ air")
}
function glassDoorRemove(entity) {

  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^1^ air")

  //west is incorrect

  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^1^ air")

  //east is correct

  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^1^ air")

  //south is incorrect

  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^1^ air")


}
function shojiDoorRemove(entity) {
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~1~ air")
}
function garageDoorLargeRemove(entity) {

  //north is correct
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~2~ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-1^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^2^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^-2^2^ air")

  //west is correct
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~2~ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-1^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^-2^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^2^2^ air")

  //east is correct
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~2~ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-1^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^2^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^-2^2^ air")

  //south is correct
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~2~ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-1^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^-2^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^2^2^ air")


}
function garageDoorSmallRemove(entity) {

  //north is correct
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ~~2~ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^1^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdanorth] run setblock ^2^2^ air")

  //west is correct
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ~~2~ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^2^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdawest] run setblock ^1^2^ air")

  //east is correct
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ~~2~  air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^1^2^  air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdaeast] run setblock ^2^2^  air")

  //south is correct
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~~ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~1~ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ~~2~ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^2^^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^2^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^2^2^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^1^ air")
  entity.runCommand("/execute as @s[tag=4ksdasouth] run setblock ^1^2^ air")


}

function doorRemoved(entity) {
  const pairs = entity.dimension.getEntities({ count: 2, type: entity.typeId, tags: [`pl_${entity.id}`] })
  for (const pairDoor of pairs) {
    pairDoor.triggerEvent("4ks_da:remove_pair_left")
    pairDoor.removeTag("4ksdapl")
    for (const tag of pairDoor.getTags()) {
      if (tag.startsWith("pl_")) pairDoor.removeTag(tag)
    }
  }
  if (entity.typeId.includes("double_barn_door")) {
    doubleBranDoorRemove(entity)
    return
  } else if (entity.typeId.includes("garage_door_small")) {
    garageDoorSmallRemove(entity)
    return
  } else if (entity.typeId.includes("garage_door_large")) {
    garageDoorLargeRemove(entity)
    return
  } else if (entity.typeId.includes("garage_door_medium")) {
    garageDoorMediumRemove(entity)
    return
  } else if (entity.typeId.includes("shoji_door") || entity.typeId.includes("modern_door_1") || entity.typeId.includes("barn_door_single")) {
    shojiDoorRemove(entity)
    return
  } else if (entity.typeId.includes("4ks_da:glass_door")) {
    glassDoorRemove(entity)
    return
  } else if (entity.typeId.includes("space_door_hangar")) {
    spaceDoorHangarRemove(entity)
    return
  } else if (entity.typeId.includes("space_door_large")) {
    spaceDoorLargeRemove(entity)
    return
  } else if (entity.typeId.includes("space_door")) {
    spaceDoorRemove(entity)
    return
  }
}

function manualMode(entity) {
  if (!entity.hasTag("4ksdapl"))
    entity.runCommand("/execute unless entity @s[tag=] run tellraw @p[r=6] {\"rawtext\":[{\"text\":\"Mode: Manual\"}]}")
  const pairs = entity.dimension.getEntities({ count: 2, type: entity.typeId, tags: [`pl_${entity.id}`] })
  for (const pairDoor of pairs) {
    pairDoor.setProperty('4ks_da:auto_mode', false)
  }
}

function autoMode(entity) {
  if (!entity.hasTag("4ksdapl"))
    entity.runCommand("/execute unless entity @s[tag=] run tellraw @p[r=6] {\"rawtext\":[{\"text\":\"Mode: Auto\"}]}")
  const pairs = entity.dimension.getEntities({ count: 2, type: entity.typeId, tags: [`pl_${entity.id}`] })
  for (const pairDoor of pairs) {
    pairDoor.setProperty('4ks_da:auto_mode', true)
  }
}

function openMenu(entity, player) {
  const actionForm = new ActionFormData()
  actionForm.title("Menu")
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_da.delete_button.text"
      },
    ],
  })
  actionForm.show(player).then(async (e) => {
    if (e.canceled) return
    if (e.selection == 0) {
      entity.setProperty('4ks_da:auto_mode', !autoMode)
    } else {
      entity.runCommand("/effect @s invisibility 2 2 true")
      entity.runCommand("/setblock ~~~ air")
      entity.runCommand("/setblock ~~1~ air")
      entity.runCommand("/scriptevent 4ks_da:door_removed")
      await sleep(4)
      entity.runCommand("/kill @s")
    }

  })
}

function openMenuSecretDoor(entity, player) {
  const actionForm = new ActionFormData()
  actionForm.title("Menu")
  const autoMode = entity.getProperty('4ks_da:auto_mode')
  if (autoMode) {
    actionForm.button({
      rawtext: [
        {
          translate: "4ks_da.toggle_mode_manual.text"
        },
      ],
    })
  } else {
    actionForm.button({
      rawtext: [
        {
          translate: "4ks_da.toggle_mode_auto.text"
        },
      ],
    })
  }
  actionForm.button({
    rawtext: [
      {
        translate: "4ks_da.delete_button.text"
      },
    ],
  })
  actionForm.show(player).then(async (e) => {
    if (e.canceled) return
    if (e.selection == 0) {
      entity.setProperty('4ks_da:auto_mode', !autoMode)
      if (autoMode) {
        entity.runCommand("/tellraw @p[r=6] {\"rawtext\":[{\"text\":\"Mode: Manual\"}]}")
      } else {
        entity.runCommand("/tellraw @p[r=6] {\"rawtext\":[{\"text\":\"Mode: Auto\"}]}")
      }
    } else {
      if (entity.typeId == "4ks_da:secret_door_garage") {
        entity.runCommand(`/structure save a_${entity.id} ^-2^^ ^2^2^ false disk`)
      } else if (entity.typeId == "4ks_da:secret_door_normal") {
        entity.runCommand(`/execute as @s[tag=4ksdasouth] run structure load a_${entity.id} ^-1^^`)
        entity.runCommand(`/execute as @s[tag=4ksdawest] run structure load a_${entity.id} ^-1^^`)
        entity.runCommand(`/execute as @s[tag=4ksdanorth] run structure load a_${entity.id} ^1^^`)
        entity.runCommand(`/execute as @s[tag=4ksdaeast] run structure load a_${entity.id} ^1^^`)
      } else if (entity.typeId == "4ks_da:secret_door_floor") {
        entity.runCommand(`/structure load a_${entity.id} ~-1~~-1`)
      } else if (entity.typeId == "4ks_da:secret_door_door") {
        entity.runCommand(`/structure load a_${entity.id} ~~~`)
      }

      entity.runCommand(`/structure delete a_${entity.id}`)
      entity.runCommand("/effect @s invisibility 2 2 true")
      entity.dimension.spawnItem(new ItemStack(`${entity.typeId}`), entity.location)
      await sleep(4)
      entity.runCommand("/kill @s")
    }



  })
}

async function secretDoorFloor(entity) {
  for (const tag of entity.getTags()) {
    if (tag.includes("4ksda")) continue
    entity.removeTag(tag)
  }

  entity.runCommand("/playsound random.anvil_use @p")
  entity.runCommand(`/structure save a_${entity.id} ~-1~~-1 ~1~~1 false disk`)
  entity.runCommand(`/event entity @s 4ks_da:placed`)
  entity.runCommand("/tellraw @p[r=6] {\"rawtext\":[{\"text\":\"[!] Secret Door placed.\"}]}")
}

function secretDoorFloorOpen(entity) {

  if (entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", true)
  entity.runCommand("/playsound tile.piston.out @p")
  entity.runCommand(`/structure save a_${entity.id} ~-1~~-1 ~1~~1 false disk`)
  entity.runCommand("/fill ~-1~~-1 ~1~~1 air")
}
function secretDoorFloorClose(entity) {

  if (!entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", false)
  entity.runCommand("/playsound tile.piston.in @p")
  entity.runCommand(`/structure load a_${entity.id} ~-1~~-1`)
  entity.runCommand(`/structure delete a_${entity.id}`)
}

async function secretDoorDoor(entity) {
  for (const tag of entity.getTags()) {
    if (tag.includes("4ksda")) continue
    entity.removeTag(tag)
  }

  entity.runCommand("/playsound random.anvil_use @p")
  entity.runCommand(`/structure save a_${entity.id} ~~~ ~~1~ false disk`)
  entity.runCommand(`/event entity @s 4ks_da:placed`)
  entity.runCommand("/tellraw @p[r=6] {\"rawtext\":[{\"text\":\"[!] Secret Door placed.\"}]}")
}
function secretDoorDoorOpen(entity) {
  if (entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", true)
  entity.runCommand("/playsound tile.piston.out @p")
  entity.runCommand(`/structure save a_${entity.id} ~~~ ~~1~ false disk`)
  entity.runCommand("/fill ~~~ ~~1~ air")
}
function secretDoorDoorClose(entity) {
  if (!entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", false)

  entity.runCommand("/playsound tile.piston.in @p")

  entity.runCommand(`/execute as @s run structure load a_${entity.id} ~~~`)
  entity.runCommand(`/structure delete a_${entity.id}`)

}

async function secretDoorNormal(entity) {
  for (const tag of entity.getTags()) {
    if (tag.includes("4ksda")) continue
    entity.removeTag(tag)
  }

  entity.runCommand("/playsound random.anvil_use @p")
  entity.runCommand(`/structure save a_${entity.id} ^-1^^ ^1^2^ false disk`)
  entity.runCommand(`/event entity @s 4ks_da:placed`)
  entity.runCommand("/tellraw @p[r=6] {\"rawtext\":[{\"text\":\"[!] Secret Door placed.\"}]}")
}
function secretDoorNormalOpen(entity) {
  if (entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", true)
  entity.runCommand("/playsound tile.piston.out @p")
  entity.runCommand(`/structure save a_${entity.id} ^-1^^ ^1^2^ false disk`)
  entity.runCommand("/fill ^-1^^ ^1^2^ air")
}
function secretDoorNormalClose(entity) {

  if (!entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", false)
  entity.runCommand("/playsound tile.piston.in @p")
  entity.runCommand(`/execute as @s[tag=4ksdasouth] run structure load a_${entity.id} ^-1^^`)
  entity.runCommand(`/execute as @s[tag=4ksdawest] run structure load a_${entity.id} ^-1^^`)
  entity.runCommand(`/execute as @s[tag=4ksdanorth] run structure load a_${entity.id} ^1^^`)
  entity.runCommand(`/execute as @s[tag=4ksdaeast] run structure load a_${entity.id} ^1^^`)

  entity.runCommand(`/structure delete a_${entity.id}`)

}

async function secretDoorGarage(entity) {
  for (const tag of entity.getTags()) {
    if (tag.includes("4ksda")) continue
    entity.removeTag(tag)
  }

  entity.runCommand("/playsound random.anvil_use @p")
  entity.runCommand(`/structure save a_${entity.id} ^-2^^ ^2^2^ false disk`)
  entity.runCommand(`/event entity @s 4ks_da:placed`)
  entity.runCommand("/tellraw @p[r=6] {\"rawtext\":[{\"text\":\"[!] Secret Door placed.\"}]}")
}
function secretDoorGarageOpen(entity) {
  if (entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", true)
  entity.runCommand("/playsound tile.piston.out @p")
  entity.runCommand(`/structure save a_${entity.id} ^-2^^ ^2^2^ false disk`)
  entity.runCommand("/fill ^-2^^ ^2^2^ air")
}
function secretDoorGarageClose(entity) {

  if (!entity.getDynamicProperty("4ks_da:open")) return
  entity.setDynamicProperty("4ks_da:open", false)
  entity.runCommand("/playsound tile.piston.in @p")
  entity.runCommand(`/execute as @s[tag=4ksdasouth] run structure load a_${entity.id} ^-2^^`)
  entity.runCommand(`/execute as @s[tag=4ksdawest] run structure load a_${entity.id} ^-2^^`)
  entity.runCommand(`/execute as @s[tag=4ksdanorth] run structure load a_${entity.id} ^2^^`)
  entity.runCommand(`/execute as @s[tag=4ksdaeast] run structure load a_${entity.id} ^2^^`)

  entity.runCommand(`/structure delete a_${entity.id}`)

}
system.afterEvents.scriptEventReceive.subscribe(async e => {
  const eventID = e.id
  if (!e.sourceEntity || !e.sourceEntity.isValid()) return
  if (eventID == "4ks_da:door_open") {
    if (e.sourceEntity.typeId.includes("double_barn_door")) {
      doubleBranDoorOpen(e.sourceEntity)
      return
    } else if (e.sourceEntity.typeId.includes("garage_door_small")) {
      garageDoorSmallOpen(e.sourceEntity)
      return
    } else if (e.sourceEntity.typeId.includes("garage_door_large")) {
      garageDoorLargeOpen(e.sourceEntity)
      return
    } else if (e.sourceEntity.typeId.includes("garage_door_medium")) {
      garageDoorMediumOpen(e.sourceEntity)
      return
    } else if (e.sourceEntity.typeId.includes("shoji_door") || e.sourceEntity.typeId.includes("modern_door_1") || e.sourceEntity.typeId.includes("barn_door_single")) {
      shojiDoorOpen(e.sourceEntity)
      return
    } else if (e.sourceEntity.typeId.includes("4ks_da:glass_door")) {
      glassDoorOpen(e.sourceEntity)
      return
    } else if (e.sourceEntity.typeId.includes("space_door_hangar")) {
      spaceDoorHangarOpen(e.sourceEntity)
      return
    } else if (e.sourceEntity.typeId.includes("space_door_large")) {
      spaceDoorLargeOpen(e.sourceEntity)
      return
    } else if (e.sourceEntity.typeId.includes("space_door")) {
      spaceDoorOpen(e.sourceEntity)
      return
    }
    woodDoorOpen(e.sourceEntity)
  } else if (eventID == "4ks_da:door_close") {
    if (e.sourceEntity.typeId.includes("double_barn_door")) {
      doubleBranDoorClose(e.sourceEntity)
      return
    } else if (e.sourceEntity.typeId.includes("garage_door_small")) {
      garageDoorSmallClose(e.sourceEntity)
      return
    } else if (e.sourceEntity.typeId.includes("garage_door_large")) {
      garageDoorLargeClose(e.sourceEntity)
      return
    } else if (e.sourceEntity.typeId.includes("garage_door_medium")) {
      garageDoorMediumClose(e.sourceEntity)
      return
    } else if (e.sourceEntity.typeId.includes("shoji_door") || e.sourceEntity.typeId.includes("modern_door_1") || e.sourceEntity.typeId.includes("barn_door_single")) {
      shojiDoorClose(e.sourceEntity)
      return
    } else if (e.sourceEntity.typeId.includes("4ks_da:glass_door")) {
      glassDoorClose(e.sourceEntity)
      return
    } else if (e.sourceEntity.typeId.includes("space_door_hangar")) {
      spaceDoorHangarClose(e.sourceEntity)
      return
    } else if (e.sourceEntity.typeId.includes("space_door_large")) {
      spaceDoorLargeClose(e.sourceEntity)
      return
    } else if (e.sourceEntity.typeId.includes("space_door")) {
      spaceDoorClose(e.sourceEntity)
      return
    }
    woodDoorClose(e.sourceEntity)
  } else if (eventID == "4ks_da:door_removed") {
    doorRemoved(e.sourceEntity)
  } else if (eventID == "4ks_da:manual_mode") {
    manualMode(e.sourceEntity)
  } else if (eventID == "4ks_da:auto_mode") {
    autoMode(e.sourceEntity)
  } else if (eventID == "4ks_da:open_menu") {

    const player = e.sourceEntity.dimension.getPlayers({ count: 1 })[0]
    if (e.sourceEntity.typeId.includes("4ks_da:secret_door_")) {
      openMenuSecretDoor(e.sourceEntity, player)
      return
    }
    openMenu(e.sourceEntity, player)
  }
  else if (eventID == "4ks_da:create_secret_door") {
    if (e.sourceEntity.typeId == "4ks_da:secret_door_normal") {
      secretDoorNormal(e.sourceEntity)
    } else if (e.sourceEntity.typeId == "4ks_da:secret_door_floor") {
      secretDoorFloor(e.sourceEntity)
    } else if (e.sourceEntity.typeId == "4ks_da:secret_door_door") {
      secretDoorDoor(e.sourceEntity)
    } else if (e.sourceEntity.typeId == "4ks_da:secret_door_garage") {
      secretDoorGarage(e.sourceEntity)
    }
  } else if (eventID == "4ks_da:secret_door_normal_open") {
    secretDoorNormalOpen(e.sourceEntity)
  } else if (eventID == "4ks_da:secret_door_normal_close") {
    secretDoorNormalClose(e.sourceEntity)
  } else if (eventID == "4ks_da:secret_door_floor_open") {
    secretDoorFloorOpen(e.sourceEntity)
  } else if (eventID == "4ks_da:secret_door_floor_close") {
    secretDoorFloorClose(e.sourceEntity)
  } else if (eventID == "4ks_da:secret_door_door_open") {
    secretDoorDoorOpen(e.sourceEntity)
  } else if (eventID == "4ks_da:secret_door_door_close") {
    secretDoorDoorClose(e.sourceEntity)
  } else if (eventID == "4ks_da:secret_door_garage_open") {
    secretDoorGarageOpen(e.sourceEntity)
  } else if (eventID == "4ks_da:secret_door_garage_close") {
    secretDoorGarageClose(e.sourceEntity)
  }

})


world.beforeEvents.itemUseOn.subscribe(async e => {
  if (e.block.typeId == "4ks_da:door_barrier" || e.block.typeId.includes("4ks_da:garage_door") || e.block.typeId.includes("barrier")) {
    e.cancel = true
  }
});

world.afterEvents.entityHitEntity.subscribe(async e => {
  const doors = e.hitEntity.dimension.getEntities({ location: e.hitEntity.location, maxDistance: 8 })
  for (const entity of doors) {
    if (!entity.typeId.includes("4ks_da")) continue
    entity.clearVelocity()
    const location = entity.getDynamicProperty("4ks_da:location")
    if (!location) continue
    entity.runCommand(`/tp ${location.x} ${location.y} ${location.z}`)
  }
})

async function checkPlayerAround(door) {
  let distance = 3;
  if (door.typeId.includes("space_door_hangar") || door.typeId.includes("garage_door_large") || door.typeId.includes("secret_door_garage")) distance = 5
  if (door.typeId.includes("garage_door_medium") || door.typeId.includes("secret_door_normal") || door.typeId.includes("garage_door_small")) distance = 3
  const players = door.dimension.getPlayers({ location: door.location, maxDistance: distance })
  if (players.length > 0) {
    if (door.getDynamicProperty("4ks_da:open")) return
    door.triggerEvent("4ks_da:open")
  } else {
    if (!door.getDynamicProperty("4ks_da:open")) return
    door.triggerEvent("4ks_da:close")
  }
}

const autoOpenDoor = async () => {
  const doors = world.getDimension("overworld").getEntities({ excludeTypes: ["minecraft:player"] });
  for (const door of doors) {
    if (!door.typeId.includes("4ks_da") || !door.typeId.includes("door")) continue
    if (!door.getProperty('4ks_da:auto_mode')) continue
    if (door.hasTag("4ksdapl")) continue
    checkPlayerAround(door)
  }
};

world.afterEvents.itemStopUse.subscribe(e => {
  const player = e.source
  const item = e.itemStack

  if (player.typeId != "minecraft:player") return
  if (!player.getDynamicProperty("4ks_da:selected")) return

  if (!item?.typeId.includes("4ks_da:secret_door_")) return
  player.runCommand(`/execute as @e[tag=a_${player.id}] positioned as @s run scriptevent 4ks_da:create_secret_door`)
});

async function beamBlockNormal(player) {
  const block = player.getBlockFromViewDirection({ maxDistance: 10 })
  if (!block) {
    player.runCommand(`/event entity @e[tag=a_${player.id}] 4ks_da:despawn`)
    player.setDynamicProperty("4ks_da:selected", false)
    player.setDynamicProperty("4ks_da:last_beam", undefined)
    return
  }
  if (block.block.location.y < player.location.y) {
    player.runCommand(`/event entity @e[tag=a_${player.id}] 4ks_da:despawn`)
    player.setDynamicProperty("4ks_da:selected", false)
    player.setDynamicProperty("4ks_da:last_beam", undefined)
    return
  }
  const lastLocation = player.getDynamicProperty("4ks_da:last_beam")
  if (block.block.location.x == lastLocation?.x && block.block.location.y == lastLocation?.y && block.block.location.z == lastLocation?.z)
    return

  const newLocation = {
    x: block.block.location.x + 0.5,
    y: block.block.location.y - 1,
    z: block.block.location.z + 0.5
  }
  player.setDynamicProperty("4ks_da:last_beam", block.block.location)
  player.runCommand(`/event entity @e[tag=a_${player.id}] 4ks_da:despawn`)
  const beamEntity = player.dimension.spawnEntity("4ks_da:secret_door_normal", newLocation)
  beamEntity.addTag(`a_${player.id}`)
  beamEntity.runCommand(`/tp ~~~ facing \"${player.name}\"`)
  beamEntity.runCommand(`/event entity @s 4ks_da:fix_face`)
  player.setDynamicProperty("4ks_da:selected", true)
}

async function beamBlockGarage(player) {
  const block = player.getBlockFromViewDirection({ maxDistance: 12 })
  if (!block) {
    player.runCommand(`/event entity @e[tag=a_${player.id}] 4ks_da:despawn`)
    player.setDynamicProperty("4ks_da:selected", false)
    player.setDynamicProperty("4ks_da:last_beam", undefined)
    return
  }
  if (block.block.location.y < player.location.y) {
    player.runCommand(`/event entity @e[tag=a_${player.id}] 4ks_da:despawn`)
    player.setDynamicProperty("4ks_da:selected", false)
    player.setDynamicProperty("4ks_da:last_beam", undefined)
    return
  }
  const lastLocation = player.getDynamicProperty("4ks_da:last_beam")
  if (block.block.location.x == lastLocation?.x && block.block.location.y == lastLocation?.y && block.block.location.z == lastLocation?.z)
    return

  const newLocation = {
    x: block.block.location.x + 0.5,
    y: block.block.location.y - 1,
    z: block.block.location.z + 0.5
  }
  player.setDynamicProperty("4ks_da:last_beam", block.block.location)
  player.runCommand(`/event entity @e[tag=a_${player.id}] 4ks_da:despawn`)
  const beamEntity = player.dimension.spawnEntity("4ks_da:secret_door_garage", newLocation)
  beamEntity.addTag(`a_${player.id}`)
  beamEntity.runCommand(`/tp ~~~ facing \"${player.name}\"`)
  beamEntity.runCommand(`/event entity @s 4ks_da:fix_face`)
  player.setDynamicProperty("4ks_da:selected", true)
}

async function beamBlockHangar(player) {
  const block = player.getBlockFromViewDirection({ maxDistance: 7 })
  if (!block || block?.block?.permutation.matches("4ks_da:door_barrier") || block?.block?.permutation.matches("4ks_da:garage_door_block_top") || block?.block?.permutation.matches("4ks_da:garage_door_block")) {
    player.runCommand(`/event entity @e[tag=a_${player.id}] 4ks_da:despawn`)
    player.setDynamicProperty("4ks_da:selected", false)
    player.setDynamicProperty("4ks_da:last_beam", undefined)
    return
  }

  const lastLocation = player.getDynamicProperty("4ks_da:last_beam")
  if (block.block.location.x == lastLocation?.x && block.block.location.y == lastLocation?.y && block.block.location.z == lastLocation?.z)
    return

  const newLocation = {
    x: block.block.location.x + 0.5,
    y: block.block.location.y + 1,
    z: block.block.location.z + 0.5
  }
  player.setDynamicProperty("4ks_da:last_beam", block.block.location)
  player.runCommand(`/event entity @e[tag=a_${player.id}] 4ks_da:despawn`)
  const beamEntity = player.dimension.spawnEntity("4ks_da:beam_5x3", newLocation)
  beamEntity.addTag(`a_${player.id}`)
  beamEntity.runCommand(`/tp ~~~ facing \"${player.name}\"`)
  beamEntity.runCommand(`/event entity @s 4ks_da:fix_face`)
  player.setDynamicProperty("4ks_da:selected", true)
}

async function beamBlock3x3(player) {
  const block = player.getBlockFromViewDirection({ maxDistance: 7 })
  if (!block || block?.block?.permutation.matches("4ks_da:door_barrier") || block?.block?.permutation.matches("4ks_da:garage_door_block_top") || block?.block?.permutation.matches("4ks_da:garage_door_block")) {
    player.runCommand(`/event entity @e[tag=a_${player.id}] 4ks_da:despawn`)
    player.setDynamicProperty("4ks_da:selected", false)
    player.setDynamicProperty("4ks_da:last_beam", undefined)
    return
  }

  const lastLocation = player.getDynamicProperty("4ks_da:last_beam")
  if (block.block.location.x == lastLocation?.x && block.block.location.y == lastLocation?.y && block.block.location.z == lastLocation?.z)
    return

  const newLocation = {
    x: block.block.location.x + 0.5,
    y: block.block.location.y + 1,
    z: block.block.location.z + 0.5
  }
  player.setDynamicProperty("4ks_da:last_beam", block.block.location)
  player.runCommand(`/event entity @e[tag=a_${player.id}] 4ks_da:despawn`)
  const beamEntity = player.dimension.spawnEntity("4ks_da:beam_3x3", newLocation)
  beamEntity.addTag(`a_${player.id}`)
  beamEntity.runCommand(`/tp ~~~ facing \"${player.name}\"`)
  beamEntity.runCommand(`/event entity @s 4ks_da:fix_face`)
  player.setDynamicProperty("4ks_da:selected", true)
}

async function beamBlock3x4(player) {
  const block = player.getBlockFromViewDirection({ maxDistance: 7 })
  if (!block || block?.block?.permutation.matches("4ks_da:door_barrier") || block?.block?.permutation.matches("4ks_da:garage_door_block_top") || block?.block?.permutation.matches("4ks_da:garage_door_block")) {
    player.runCommand(`/event entity @e[tag=a_${player.id}] 4ks_da:despawn`)
    player.setDynamicProperty("4ks_da:selected", false)
    player.setDynamicProperty("4ks_da:last_beam", undefined)
    return
  }

  const lastLocation = player.getDynamicProperty("4ks_da:last_beam")
  if (block.block.location.x == lastLocation?.x && block.block.location.y == lastLocation?.y && block.block.location.z == lastLocation?.z)
    return

  const newLocation = {
    x: block.block.location.x + 0.5,
    y: block.block.location.y + 1,
    z: block.block.location.z + 0.5
  }
  player.setDynamicProperty("4ks_da:last_beam", block.block.location)
  player.runCommand(`/event entity @e[tag=a_${player.id}] 4ks_da:despawn`)
  const beamEntity = player.dimension.spawnEntity("4ks_da:beam_3x4", newLocation)
  beamEntity.addTag(`a_${player.id}`)
  beamEntity.runCommand(`/tp ~~~ facing \"${player.name}\"`)
  beamEntity.runCommand(`/event entity @s 4ks_da:fix_face`)
  player.setDynamicProperty("4ks_da:selected", true)
}

async function beamBlockDoor(player) {
  const block = player.getBlockFromViewDirection({ maxDistance: 10 })
  if (!block) {
    player.runCommand(`/event entity @e[tag=a_${player.id}] 4ks_da:despawn`)
    player.setDynamicProperty("4ks_da:selected", false)
    player.setDynamicProperty("4ks_da:last_beam", undefined)
    return
  }
  if (block.block.location.y < player.location.y) {
    player.runCommand(`/event entity @e[tag=a_${player.id}] 4ks_da:despawn`)
    player.setDynamicProperty("4ks_da:selected", false)
    player.setDynamicProperty("4ks_da:last_beam", undefined)
    return
  }
  const lastLocation = player.getDynamicProperty("4ks_da:last_beam")
  if (block.block.location.x == lastLocation?.x && block.block.location.y == lastLocation?.y && block.block.location.z == lastLocation?.z)
    return

  const newLocation = {
    x: block.block.location.x + 0.5,
    y: block.block.location.y,
    z: block.block.location.z + 0.5
  }
  player.setDynamicProperty("4ks_da:last_beam", block.block.location)
  player.runCommand(`/event entity @e[tag=a_${player.id}] 4ks_da:despawn`)
  const beamEntity = player.dimension.spawnEntity("4ks_da:secret_door_door", newLocation)
  beamEntity.addTag(`a_${player.id}`)
  beamEntity.runCommand(`/tp ~~~ facing \"${player.name}\"`)
  beamEntity.runCommand(`/event entity @s 4ks_da:fix_face`)
  player.setDynamicProperty("4ks_da:selected", true)
}

async function beamBlockFloor(player) {
  const block = player.getBlockFromViewDirection({ maxDistance: 10 })
  if (!block) {
    player.runCommand(`/event entity @e[tag=a_${player.id}] 4ks_da:despawn`)
    player.setDynamicProperty("4ks_da:selected", false)
    player.setDynamicProperty("4ks_da:last_beam", undefined)
    return
  }
  const lastLocation = player.getDynamicProperty("4ks_da:last_beam")
  if (block.block.location.x == lastLocation?.x && block.block.location.y == lastLocation?.y && block.block.location.z == lastLocation?.z)
    return

  const newLocation = {
    x: block.block.location.x + 0.5,
    y: block.block.location.y,
    z: block.block.location.z + 0.5
  }
  player.setDynamicProperty("4ks_da:last_beam", block.block.location)
  player.runCommand(`/event entity @e[tag=a_${player.id}] 4ks_da:despawn`)
  const beamEntity = player.dimension.spawnEntity("4ks_da:secret_door_floor", newLocation)
  beamEntity.addTag(`a_${player.id}`)
  beamEntity.runCommand(`/tp ~~~ facing \"${player.name}\"`)
  beamEntity.runCommand(`/event entity @s 4ks_da:fix_face`)
  player.setDynamicProperty("4ks_da:selected", true)
}


const checkGuideBook = async () => {
  const players = world.getAllPlayers()
  for (const player of players) {
    const equipment = player.getComponent("minecraft:equippable")
    const mainhand = equipment?.getEquipment("Mainhand")

    if (mainhand?.typeId == "4ks_da:guidebook") {
      player.runCommand("/clear @s 4ks_da:guidebook 0 1")
      player.runCommand("/structure load fks_da:guidebook ~~~")
    }

    if (mainhand?.typeId == "4ks_da:secret_door_normal") {
      beamBlockNormal(player)
    } else if (mainhand?.typeId == "4ks_da:secret_door_floor") {
      beamBlockFloor(player)
    } else if (mainhand?.typeId == "4ks_da:secret_door_door") {
      beamBlockDoor(player)
    } else if (mainhand?.typeId == "4ks_da:secret_door_garage") {
      beamBlockGarage(player)
    } else if (mainhand?.typeId == "4ks_da:space_door_hangar_spawn_egg"
      || mainhand?.typeId == "4ks_da:garage_door_large_dark_oak_spawn_egg"
      || mainhand?.typeId == "4ks_da:garage_door_large_oak_spawn_egg"
      || mainhand?.typeId == "4ks_da:garage_door_large_white_spawn_egg"
    ) {
      beamBlockHangar(player)
    } else if (
      mainhand?.typeId == "4ks_da:garage_door_medium_dark_oak_spawn_egg"
      || mainhand?.typeId == "4ks_da:garage_door_medium_oak_spawn_egg"
      || mainhand?.typeId == "4ks_da:garage_door_medium_white_spawn_egg"
    ) {
      beamBlock3x4(player)
    }  else if (
      mainhand?.typeId == "4ks_da:space_door_large_spawn_egg"
      || mainhand?.typeId == "4ks_da:garage_door_small_dark_oak_spawn_egg"
      || mainhand?.typeId == "4ks_da:garage_door_small_oak_spawn_egg"
      || mainhand?.typeId == "4ks_da:garage_door_small_white_spawn_egg"
    ) {
      beamBlock3x3(player)
    } else {
      player.runCommand(`/event entity @e[tag=a_${player.id}] 4ks_da:despawn`)
    }
  }
}

world.afterEvents.playerSpawn.subscribe(async (event) => {
  event.player.runCommand("/give @s[tag=!4ksdagb] 4ks_da:guidebook")
  event.player.addTag("4ksdagb")
});

system.runInterval(autoOpenDoor, 0)
system.runInterval(checkGuideBook, 2)