Skip to content

Commit

Permalink
add documents
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaBs committed Feb 11, 2021
1 parent 8fa5ac3 commit 429c5c7
Show file tree
Hide file tree
Showing 5 changed files with 421 additions and 0 deletions.
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Mojang API

![Discord](https://img.shields.io/discord/795952027443527690?label=discord&logo=discord&style=for-the-badge)

.NET Library for [Mojang API](https://wiki.vg/Mojang_API), [Mojang Authentication](https://wiki.vg/Authentication) and [Microsoft Xbox Authentication](https://wiki.vg/Microsoft_Authentication_Scheme)

- Asynchronous API
- Getting Player Data
- Changing Player Name or Skin
- Mojang Authentication
- Microsoft Authentication
- Security Question-Answer
- Statistics

Support:

- .NET Framework 4.6.2
- .NET Core 3.1
- .NET 5.0

## Install

Use Nuget package [MojangAPI](https://nuget.org) or download dll from [release](https://github.com).

## Dependencies

- Newtonsoft.Json
- System.Net.Http

## Usage

Include these namespaces :

```csharp
using MojangAPI;
using MojangAPI.Model;
```

Sample program: [MojangAPISample](./MojangAPISample)

### [MojangAPI](./docs/MojangAPI.md)

Getting player profile, Changing name or skin, Statistics, Blocked Server, Checking Game Ownership

### [Authentication](./docs/MojangAuth.md)

Mojang Yggdrasil authentication.

### [XboxAuthentication](./docs/XboxAuthentication.md)

Microsoft Xbox Authentication

### [SecurityQuestion](./docs/SecurityQuestion.md)

Security question-answer flow
146 changes: 146 additions & 0 deletions docs/MojangAPI.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
## Mojang

Most methods return `MojangAPIResponse` or class inherited from `MojangAPIResponse`.
You can check whether the request was successful or failed to check `IsSuccess` property in `MojangAPIResponse`.
If `IsSuccess` is false, `Error` and `ErrorMessage` property tell you why the request failed.

Example:
```csharp
HttpClient httpClient = new HttpClient();
Mojang mojang = new Mojang(httpClient);

MojangAPIResponse response = await mojang.something();
if (response.isSuccess)
{
Console.WriteLine("Success!");
}
else
{
Console.WriteLine(response.Error);
Console.WriteLine(response.ErrorMessage);
}
```

#### GetUUID

```csharp
PlayerUUID uuid = await mojang.GetUUID("username");

// uuid.UUID
// uuid.IsLegacy
// uuid.IsDemo
```

#### GetUUIDs
```csharp
PlayerUUID[] uuids = await mojang.GetUUIDs(new string[] { "user1", "user2" });

if (uuids == null)
Console.WriteLine("Failed to request");
else
{
foreach (PlayerUUID uuid in uuids)
{
Console.WriteLine(uuid.UUID);
}
}
```

*note: this method return null if the request was failed*

#### GetNameHistories
```csharp
NameHistoryResponse response = await mojang.GetNameHistories("uuid");

if (response.IsSuccess)
{
foreach (NameHistory item in response.Histories)
{
// item.Name
// item.ChangedToAt
// item.ChangedTime
}
}
else
{
Console.WriteLine(response.Error);
Console.WriteLine(response.ErrorMessage);
}
```

#### GetProfileUsingUUID

```csharp
PlayerProfile profile = await mojang.GetProfileUsingUUID("uuid");

// profile.UUID
// profile.Name
// profile.Skin
// profile.IsLegacy
```

#### GetProfileUsingAccessToken

```csharp
PlayerProfile profile = await mojang.GetProfileUsingAccessToken("accessToken");
```

#### ChangeName

```csharp
PlayerProfile profile = await mojang.ChangeName("accessToken", "newName");
```

#### ChangeSkin

```csharp
MojangAPIResponse response = await mojang.ChangeSkin("uuid", "accessToken", SkinType.Steve, "skinUrl");
```

#### UploadSkin

```csharp
MojangAPIResponse response = await mojang.UploadSkin("accessToken", SkinType.Steve, "skin_png_file_path");
```
```csharp
Stream stream; // create stream for uploading skin
MojangAPIResponse response = await mojang.UploadSkin("accessToken", SkinType.Steve, stream, "file_name");
```

#### ResetSkin

```csharp
MojangAPIResponse response = await mojang.ResetSkin("uuid", "accessToken");
```

#### GetBlockedServer

```csharp
string[] servers = await mojang.GetBlockedServer();
```

#### GetStatistics

```csharp
Statistics stats = await mojang.GetStatistics(
StatisticOption.ItemSoldMinecraft,
StatisticOption.ItemSoldCobalt
);

// stats.Total
// stats.Last24h
// stats.SaleVelocityPerSeconds
```

#### CheckGameOwnership

```csharp
bool result = await mojang.CheckGameOwnership("accessToken");

if (result)
Console.WriteLine("You have Minecraft JE");
else
Console.WriteLine("You don't have Minecraft JE");
```

*note: this method works only accessToken from Microsoft login*
119 changes: 119 additions & 0 deletions docs/MojangAuth.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
## MojangAuth

Most methods return `MojangAuthResponse` or class inherited from `MojangAuthResponse`.
You can check whether the request was successful or failed to check `IsSuccess` property in `MojangAuthResponse`.
`Session` property is the result of authenticate. It contains `Username`, `AccessToken`, and `UUID`
If `IsSuccess` is false, `Result`, `Error`, and `ErrorMessage` property tell you why the request failed.

`MojangAuth` has its own session caching manager. You don't have to type mojang email and password everytime when you login. Login once using your mojang email and password, and `MojangAuth` will save your session information into json file(not contain your raw password, it contains only tokens). Next time when you login just call `TryAutoLogin`. it authenticate you using cached session without typing mojang email and password.

Example:

```csharp
HttpClient httpClient = new HttpClient();
MojangAuth auth = new MojangAuth(httpClient);

MojangAuthResponse res;

res = await auth.TryAutoLogin(); // login using cached session
if (!res.IsSuccess) // failed to login using cached session
{
Console.WriteLine(res.Result.ToString());

// it will save your login information into json file
res = await auth.Authenticate("mojang email", "password");
}

Console.WriteLine(res.ToString());

Session session = res.Session;
// session.Username
// session.UUID
// session.AccessToken
```

### Constructor

#### public MojangAuth(HttpClient client)

Same as `new MojangAuth(client, new SessionFileCacheManager("mojang_auth.json"))`

#### public MojangAuth(HttpClient httpClient, ICacheManager\<Session\> _cacheManager)

Initialize a new instance of the `MojangAuth` class with the specified cache manager.
If `_cacheManager` is null, `MojangAuth` does not cache session.
If you want to change cache file path, create a instance of `SessionFileCacheManager` with file path.
Example:
```csharp
MojangAuth auth = new MojangAuth(client, new SessionFileCacheManager("session_file_path.json"));
```

### Methods

#### Authenticate

```csharp
// using cached client token or create new client token if it was null.
MojangAuthResponse res = await auth.Authenticate("mojang email", "password");

// using the specified client token
MojangAuthResponse res = await auth.Authenticate("mojang email", "password", "clientToken");

if (res.IsSuccess)
{
// res.Session
}
else
{
// res.Result
// res.Error
// res.ErrorMessage
}
```

#### TryAutoLogin

```csharp
// call Validate() and Refersh() using cached session.
MojangAuthResponse res = await auth.TryAutoLogin();
```

#### Refresh

```csharp
// using cached session
MojangAuthResponse res = await auth.Refresh();
```
```csharp
// using the specified session
MojangAuthResponse res = await auth.Refresh(session);
```

#### Validate

```csharp
// using cached session
MojangAuthResponse res = await auth.Validate();
```
```csharp
// using the specified session
MojangAuthResponse res = await auth.Validate(session);
```

#### Signout

```csharp
MojangAuthResponse res = await auth.Signout("mojang email", "password");
```

#### Invalidate

```csharp
// using cached session
MojangAuthResponse res = await auth.Invalidate();
```
```csharp
// using the specified session
MojangAuthResponse res = await auth.Invalidate("accessToken", "clientToken");
```
*note: 'clientToken' can be null*
Loading

0 comments on commit 429c5c7

Please sign in to comment.