private static double[] findBestSpawnLocation(WorldServer machineWorld, int coord) {
    int size = Reference.getBoxSize(CompactMachines.instance.machineHandler.getRoomSize(coord));

    int posX1 = coord * ConfigurationHandler.cubeDistance + 1;
    int posY1 = 40 + 1;
    int posZ1 = 1;

    int posX2 = coord * ConfigurationHandler.cubeDistance + size - 1;
    int posY2 = 40 + size - 1;
    int posZ2 = size - 1;

    int minX = Math.min(posX1, posX2);
    int minY = Math.min(posY1, posY2);
    int minZ = Math.min(posZ1, posZ2);

    int maxX = Math.max(posX1, posX2);
    int maxY = Math.max(posY1, posY2) - 1;
    int maxZ = Math.max(posZ1, posZ2);

    for (int x = minX; x <= maxX; x++) {
      for (int y = minY; y <= maxY; y++) {
        for (int z = minZ; z <= maxZ; z++) {
          if (machineWorld.isAirBlock(x, y, z) && machineWorld.isAirBlock(x, y + 1, z)) {
            return new double[] {x + 0.5, y + 0.5, z + 0.5};
          }
        }
      }
    }

    return null;
  }
  public static void teleportPlayerToCoords(EntityPlayerMP player, int coord, boolean isReturning) {
    // LogHelper.info("Teleporting player to: " + coord);
    NBTTagCompound playerNBT = player.getEntityData();

    // Grab the CompactMachines entry from the player NBT data
    NBTTagCompound cmNBT;
    if (playerNBT.hasKey(Reference.MOD_ID)) {
      cmNBT = playerNBT.getCompoundTag(Reference.MOD_ID);
    } else {
      cmNBT = new NBTTagCompound();
      playerNBT.setTag(Reference.MOD_ID, cmNBT);
    }

    if (player.dimension != ConfigurationHandler.dimensionId) {
      cmNBT.setInteger("oldDimension", player.dimension);
      cmNBT.setDouble("oldPosX", player.posX);
      cmNBT.setDouble("oldPosY", player.posY);
      cmNBT.setDouble("oldPosZ", player.posZ);

      int oldDimension = player.dimension;

      WorldServer machineWorld =
          MinecraftServer.getServer().worldServerForDimension(ConfigurationHandler.dimensionId);
      MinecraftServer.getServer()
          .getConfigurationManager()
          .transferPlayerToDimension(
              player, ConfigurationHandler.dimensionId, new TeleporterCM(machineWorld));

      // If this is not being called teleporting from The End ends up without
      // the client knowing about any blocks, i.e. blank screen, no blocks, but
      // server collisions etc.
      if (oldDimension == 1) {
        machineWorld.spawnEntityInWorld(player);
      }

      // Since the player is currently not in the machine dimension, we want to clear
      // his coord history - in case he exited the machine world not via a shrinking device
      // which automatically clears the last entry in the coord history.
      if (playerNBT.hasKey("coordHistory")) {
        playerNBT.removeTag("coordHistory");
      }
    }

    if (!isReturning) {
      NBTTagList coordHistory;
      if (playerNBT.hasKey("coordHistory")) {
        coordHistory = playerNBT.getTagList("coordHistory", 10);
      } else {
        coordHistory = new NBTTagList();
      }
      NBTTagCompound toAppend = new NBTTagCompound();
      toAppend.setInteger("coord", coord);

      coordHistory.appendTag(toAppend);
      playerNBT.setTag("coordHistory", coordHistory);
    }

    MachineSaveData mHandler = CompactMachines.instance.machineHandler;
    double[] destination = mHandler.getSpawnLocation(coord);

    // Check whether the spawn location is blocked
    WorldServer machineWorld =
        MinecraftServer.getServer().worldServerForDimension(ConfigurationHandler.dimensionId);
    int dstX = (int) Math.floor(destination[0]);
    int dstY = (int) Math.floor(destination[1]);
    int dstZ = (int) Math.floor(destination[2]);

    if (!machineWorld.isAirBlock(dstX, dstY, dstZ)
        || !machineWorld.isAirBlock(dstX, dstY + 1, dstZ)) {
      // If it is blocked, try to find a better position
      double[] bestSpot = findBestSpawnLocation(machineWorld, coord);
      if (bestSpot != null) {
        destination = bestSpot;
      }

      // otherwise teleport to the default location... player will probably die though.
    }

    player.setPositionAndUpdate(destination[0], destination[1], destination[2]);

    if (destination.length == 5) {
      MessagePlayerRotation packet =
          new MessagePlayerRotation((float) destination[3], (float) destination[4]);
      PacketHandler.INSTANCE.sendTo(packet, player);
    }
  }