Skip to content

Commit 429c5c7

Browse files
committed
add documents
1 parent 8fa5ac3 commit 429c5c7

File tree

5 files changed

+421
-0
lines changed

5 files changed

+421
-0
lines changed

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Mojang API
2+
3+
![Discord](https://img.shields.io/discord/795952027443527690?label=discord&logo=discord&style=for-the-badge)
4+
5+
.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)
6+
7+
- Asynchronous API
8+
- Getting Player Data
9+
- Changing Player Name or Skin
10+
- Mojang Authentication
11+
- Microsoft Authentication
12+
- Security Question-Answer
13+
- Statistics
14+
15+
Support:
16+
17+
- .NET Framework 4.6.2
18+
- .NET Core 3.1
19+
- .NET 5.0
20+
21+
## Install
22+
23+
Use Nuget package [MojangAPI](https://nuget.org) or download dll from [release](https://github.com).
24+
25+
## Dependencies
26+
27+
- Newtonsoft.Json
28+
- System.Net.Http
29+
30+
## Usage
31+
32+
Include these namespaces :
33+
34+
```csharp
35+
using MojangAPI;
36+
using MojangAPI.Model;
37+
```
38+
39+
Sample program: [MojangAPISample](./MojangAPISample)
40+
41+
### [MojangAPI](./docs/MojangAPI.md)
42+
43+
Getting player profile, Changing name or skin, Statistics, Blocked Server, Checking Game Ownership
44+
45+
### [Authentication](./docs/MojangAuth.md)
46+
47+
Mojang Yggdrasil authentication.
48+
49+
### [XboxAuthentication](./docs/XboxAuthentication.md)
50+
51+
Microsoft Xbox Authentication
52+
53+
### [SecurityQuestion](./docs/SecurityQuestion.md)
54+
55+
Security question-answer flow

docs/MojangAPI.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
## Mojang
2+
3+
Most methods return `MojangAPIResponse` or class inherited from `MojangAPIResponse`.
4+
You can check whether the request was successful or failed to check `IsSuccess` property in `MojangAPIResponse`.
5+
If `IsSuccess` is false, `Error` and `ErrorMessage` property tell you why the request failed.
6+
7+
Example:
8+
```csharp
9+
HttpClient httpClient = new HttpClient();
10+
Mojang mojang = new Mojang(httpClient);
11+
12+
MojangAPIResponse response = await mojang.something();
13+
if (response.isSuccess)
14+
{
15+
Console.WriteLine("Success!");
16+
}
17+
else
18+
{
19+
Console.WriteLine(response.Error);
20+
Console.WriteLine(response.ErrorMessage);
21+
}
22+
```
23+
24+
#### GetUUID
25+
26+
```csharp
27+
PlayerUUID uuid = await mojang.GetUUID("username");
28+
29+
// uuid.UUID
30+
// uuid.IsLegacy
31+
// uuid.IsDemo
32+
```
33+
34+
#### GetUUIDs
35+
```csharp
36+
PlayerUUID[] uuids = await mojang.GetUUIDs(new string[] { "user1", "user2" });
37+
38+
if (uuids == null)
39+
Console.WriteLine("Failed to request");
40+
else
41+
{
42+
foreach (PlayerUUID uuid in uuids)
43+
{
44+
Console.WriteLine(uuid.UUID);
45+
}
46+
}
47+
```
48+
49+
*note: this method return null if the request was failed*
50+
51+
#### GetNameHistories
52+
```csharp
53+
NameHistoryResponse response = await mojang.GetNameHistories("uuid");
54+
55+
if (response.IsSuccess)
56+
{
57+
foreach (NameHistory item in response.Histories)
58+
{
59+
// item.Name
60+
// item.ChangedToAt
61+
// item.ChangedTime
62+
}
63+
}
64+
else
65+
{
66+
Console.WriteLine(response.Error);
67+
Console.WriteLine(response.ErrorMessage);
68+
}
69+
```
70+
71+
#### GetProfileUsingUUID
72+
73+
```csharp
74+
PlayerProfile profile = await mojang.GetProfileUsingUUID("uuid");
75+
76+
// profile.UUID
77+
// profile.Name
78+
// profile.Skin
79+
// profile.IsLegacy
80+
```
81+
82+
#### GetProfileUsingAccessToken
83+
84+
```csharp
85+
PlayerProfile profile = await mojang.GetProfileUsingAccessToken("accessToken");
86+
```
87+
88+
#### ChangeName
89+
90+
```csharp
91+
PlayerProfile profile = await mojang.ChangeName("accessToken", "newName");
92+
```
93+
94+
#### ChangeSkin
95+
96+
```csharp
97+
MojangAPIResponse response = await mojang.ChangeSkin("uuid", "accessToken", SkinType.Steve, "skinUrl");
98+
```
99+
100+
#### UploadSkin
101+
102+
```csharp
103+
MojangAPIResponse response = await mojang.UploadSkin("accessToken", SkinType.Steve, "skin_png_file_path");
104+
```
105+
```csharp
106+
Stream stream; // create stream for uploading skin
107+
MojangAPIResponse response = await mojang.UploadSkin("accessToken", SkinType.Steve, stream, "file_name");
108+
```
109+
110+
#### ResetSkin
111+
112+
```csharp
113+
MojangAPIResponse response = await mojang.ResetSkin("uuid", "accessToken");
114+
```
115+
116+
#### GetBlockedServer
117+
118+
```csharp
119+
string[] servers = await mojang.GetBlockedServer();
120+
```
121+
122+
#### GetStatistics
123+
124+
```csharp
125+
Statistics stats = await mojang.GetStatistics(
126+
StatisticOption.ItemSoldMinecraft,
127+
StatisticOption.ItemSoldCobalt
128+
);
129+
130+
// stats.Total
131+
// stats.Last24h
132+
// stats.SaleVelocityPerSeconds
133+
```
134+
135+
#### CheckGameOwnership
136+
137+
```csharp
138+
bool result = await mojang.CheckGameOwnership("accessToken");
139+
140+
if (result)
141+
Console.WriteLine("You have Minecraft JE");
142+
else
143+
Console.WriteLine("You don't have Minecraft JE");
144+
```
145+
146+
*note: this method works only accessToken from Microsoft login*

docs/MojangAuth.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
## MojangAuth
2+
3+
Most methods return `MojangAuthResponse` or class inherited from `MojangAuthResponse`.
4+
You can check whether the request was successful or failed to check `IsSuccess` property in `MojangAuthResponse`.
5+
`Session` property is the result of authenticate. It contains `Username`, `AccessToken`, and `UUID`
6+
If `IsSuccess` is false, `Result`, `Error`, and `ErrorMessage` property tell you why the request failed.
7+
8+
`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.
9+
10+
Example:
11+
12+
```csharp
13+
HttpClient httpClient = new HttpClient();
14+
MojangAuth auth = new MojangAuth(httpClient);
15+
16+
MojangAuthResponse res;
17+
18+
res = await auth.TryAutoLogin(); // login using cached session
19+
if (!res.IsSuccess) // failed to login using cached session
20+
{
21+
Console.WriteLine(res.Result.ToString());
22+
23+
// it will save your login information into json file
24+
res = await auth.Authenticate("mojang email", "password");
25+
}
26+
27+
Console.WriteLine(res.ToString());
28+
29+
Session session = res.Session;
30+
// session.Username
31+
// session.UUID
32+
// session.AccessToken
33+
```
34+
35+
### Constructor
36+
37+
#### public MojangAuth(HttpClient client)
38+
39+
Same as `new MojangAuth(client, new SessionFileCacheManager("mojang_auth.json"))`
40+
41+
#### public MojangAuth(HttpClient httpClient, ICacheManager\<Session\> _cacheManager)
42+
43+
Initialize a new instance of the `MojangAuth` class with the specified cache manager.
44+
If `_cacheManager` is null, `MojangAuth` does not cache session.
45+
If you want to change cache file path, create a instance of `SessionFileCacheManager` with file path.
46+
Example:
47+
```csharp
48+
MojangAuth auth = new MojangAuth(client, new SessionFileCacheManager("session_file_path.json"));
49+
```
50+
51+
### Methods
52+
53+
#### Authenticate
54+
55+
```csharp
56+
// using cached client token or create new client token if it was null.
57+
MojangAuthResponse res = await auth.Authenticate("mojang email", "password");
58+
59+
// using the specified client token
60+
MojangAuthResponse res = await auth.Authenticate("mojang email", "password", "clientToken");
61+
62+
if (res.IsSuccess)
63+
{
64+
// res.Session
65+
}
66+
else
67+
{
68+
// res.Result
69+
// res.Error
70+
// res.ErrorMessage
71+
}
72+
```
73+
74+
#### TryAutoLogin
75+
76+
```csharp
77+
// call Validate() and Refersh() using cached session.
78+
MojangAuthResponse res = await auth.TryAutoLogin();
79+
```
80+
81+
#### Refresh
82+
83+
```csharp
84+
// using cached session
85+
MojangAuthResponse res = await auth.Refresh();
86+
```
87+
```csharp
88+
// using the specified session
89+
MojangAuthResponse res = await auth.Refresh(session);
90+
```
91+
92+
#### Validate
93+
94+
```csharp
95+
// using cached session
96+
MojangAuthResponse res = await auth.Validate();
97+
```
98+
```csharp
99+
// using the specified session
100+
MojangAuthResponse res = await auth.Validate(session);
101+
```
102+
103+
#### Signout
104+
105+
```csharp
106+
MojangAuthResponse res = await auth.Signout("mojang email", "password");
107+
```
108+
109+
#### Invalidate
110+
111+
```csharp
112+
// using cached session
113+
MojangAuthResponse res = await auth.Invalidate();
114+
```
115+
```csharp
116+
// using the specified session
117+
MojangAuthResponse res = await auth.Invalidate("accessToken", "clientToken");
118+
```
119+
*note: 'clientToken' can be null*

0 commit comments

Comments
 (0)