changeName function

Future<bool> changeName(
  1. String uuid,
  2. String newName,
  3. AccessToken accessToken,
  4. String password,
)

Changes the Mojang account name to newName.

Implementation

Future<bool> changeName(String uuid, String newName, AccessToken accessToken,
    String password) async {
  final body = <String, String>{'name': newName, 'password': password};
  final headers = <String, String>{
    'authorization': 'Bearer $accessToken',
    'content-type': 'application/json'
  };
  final response = await requestBody(
      http.post, _minecraftServicesApi, 'user/profile/$uuid/name', body,
      headers: headers);

  if (response.statusCode != 200) {
    /// https://wiki.vg/Mojang_API#Change_Name for details on the
    /// possibly errors.
    switch (response.statusCode) {
      case 400:
        throw ArgumentError(
            'Name is invalid, longer than 16 characters or contains characters other than (a-zA-Z0-9_)');
      case 401:
        throw AuthException(AuthException.invalidCredentialsMessage);
      case 403:
        throw Exception(
            'Name is unavailable (Either taken or has not become available).');
      case 429:
        throw TooManyRequestsException('Too many requests sent.');
      case 500:
        throw Exception('Timed out.');
      default:
        throw Exception('Unexpected error occurred.');
    }
  } else {
    return true;
  }
}