Skip to content

Commit 42903f0

Browse files
updated version 0.0.5
1 parent 9addb0d commit 42903f0

File tree

15 files changed

+149
-83
lines changed

15 files changed

+149
-83
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Maven:
1414
<dependency>
1515
<groupId>cn.xiaopangxie732</groupId>
1616
<artifactId>mojang-api</artifactId>
17-
<version>0.0.4</version>
17+
<version>0.0.5</version>
1818
</dependency>
1919
```
2020

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>cn.xiaopangxie732</groupId>
44
<artifactId>mojang-api</artifactId>
5-
<version>0.0.4</version>
5+
<version>0.0.5</version>
66
<name>Mojang Public API in Java</name>
77
<description>Mojang Public API Java implementation</description>
88
<url>https://github.com/XiaoPangxie732/MojangAPI-in-Java</url>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cn.xiaopangxie732.mojang_api;
2+
3+
import cn.xiaopangxie732.mojang_api.util.Net;
4+
5+
/**
6+
* The class to get all blocked servers.
7+
* @author XiaoPangxie732
8+
* @since 0.0.5
9+
*/
10+
public class Servers {
11+
/**
12+
* Get all blocked servers.
13+
* @return String array of all SHA1 hashes.
14+
* @since 0.0.5
15+
*/
16+
public String[] getBlockedServers() {
17+
return Net.getConnection("https://sessionserver.mojang.com/blockedservers")
18+
.split("\n");
19+
}
20+
}

src/main/java/cn/xiaopangxie732/mojang_api/Skin.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818
package cn.xiaopangxie732.mojang_api;
1919

20-
import java.awt.image.BufferedImage;
2120
import java.io.ByteArrayOutputStream;
2221
import java.io.File;
2322
import java.io.IOException;
@@ -35,26 +34,42 @@
3534

3635
import cn.xiaopangxie732.mojang_api.util.Auth;
3736

37+
/**
38+
* The class for operating the skin.
39+
* @author XiaoPangxie732
40+
* @since 0.0.5
41+
*/
3842
public class Skin {
3943
/**
4044
* To change skin.
41-
* @param access_token The access token of UUID's account.
45+
* @param access_token The access token of UUID's account. can be get by using {@link Auth#getAccessToken(String, String)}
4246
* @param isSlim Skin is slim or not.
43-
* @param uuid UUID of player. can be get be using {@link UserName#UUIDAtNow(String)}
47+
* @param uuid UUID of the player. can be get by using {@link UserName#UUIDAtNow(String)}
4448
* @param uri The skin image path. if it is a local file, it needs add a prefix "file:///" and replace "\\" to "/"(Windows).
49+
* @throws IllegalStateException Throw when change skin failed.
50+
* @since 0.0.5
4551
*/
4652
public static void changeSkin(String access_token, boolean isSlim, String uuid, URI uri) {
4753
try {
4854
String response = Auth.postConnection("https://api.mojang.com/user/profile/" + uuid
4955
+"/skin", "model=" + (isSlim ? "slim" : "") + "&url=" +
5056
URLEncoder.encode(uri.toURL().toString(), "UTF-8"), access_token);
5157
if(response != null)
52-
throw new IllegalArgumentException("Failed to change skin");
58+
throw new IllegalStateException("Failed to change skin");
5359
} catch (UnsupportedEncodingException | MalformedURLException e) {
5460
e.printStackTrace();
5561
}
5662
}
57-
public static void changeSkinAndUpload(String access_token, boolean isSlim, String uuid, URI uri) {
63+
/**
64+
* To change skin and upload.
65+
* @param access_token The access token of UUID's account. can be get by using {@link Auth#getAccessToken(String, String)}
66+
* @param isSlim Skin is slim or not.
67+
* @param uuid UUID of the player. can be get be using {@link UserName#UUIDAtNow(String)}
68+
* @param uri The skin image path. if it is a local file, it needs add a prefix "file:///" and replace "\\" to "/"(Windows).
69+
* @throws IllegalStateException Throw when change skin failed.
70+
* @since 0.0.5
71+
*/
72+
public static void changeSkinAndUpload(String access_token, boolean isSlim, String uuid, URI uri) throws IllegalStateException{
5873
StringBuffer response = new StringBuffer();
5974
HttpURLConnection connection = null;
6075
try {
@@ -88,6 +103,8 @@ public static void changeSkinAndUpload(String access_token, boolean isSlim, Stri
88103
while((i = in.read())!= -1) {
89104
response.append((char)i);
90105
}
106+
if(response != null)
107+
throw new IllegalStateException("Failed to change skin");
91108
} catch(IOException ioe) {
92109
InputStream err = connection.getErrorStream();
93110
int i;
@@ -105,6 +122,13 @@ public static void changeSkinAndUpload(String access_token, boolean isSlim, Stri
105122
connection.disconnect();
106123
}
107124
}
125+
/**
126+
* Reset the skin.
127+
* @param access_token The access token of UUID's account. can be get by using {@link Auth#getAccessToken(String, String)}
128+
* @param uuid UUID of the player. can be get be using {@link UserName#UUIDAtNow(String)}
129+
* @throws IllegalStateException Throw when change skin failed.
130+
* @since 0.0.5
131+
*/
108132
public static void resetSkin(String access_token, String uuid) {
109133
StringBuffer response = new StringBuffer();
110134
HttpURLConnection connection = null;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package cn.xiaopangxie732.mojang_api;
2+
3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonObject;
5+
import com.google.gson.JsonParser;
6+
7+
import cn.xiaopangxie732.mojang_api.util.Net;
8+
9+
/**
10+
* Get statistics on the sales.
11+
* @author XiaoPangxie732
12+
* @since 0.0.5
13+
*/
14+
public class Statistics {
15+
private String response;
16+
private JsonObject request = new JsonObject();
17+
/**
18+
* All valid items.
19+
* @author XiaoPangxie732
20+
* @since 0.0.5
21+
*/
22+
public enum Items {
23+
ITEM_SOLD_MINECRAFT,
24+
PREPAID_CARD_REDEEMED_MINECRAFT,
25+
ITEM_SOLD_COBALT,
26+
ITEM_SOLD_SCROLLS;
27+
@Override
28+
public String toString() {
29+
return name().toLowerCase();
30+
}
31+
}
32+
/**
33+
* Construct a {@code Statistics} class.
34+
* @param items All items need to get sale statistics.
35+
* @since 0.0.5
36+
*/
37+
public Statistics(Items... items) {
38+
JsonArray arr = new JsonArray();
39+
for(Items i : items) {
40+
arr.add(i.toString());
41+
}
42+
request.add("metricKeys", arr);
43+
}
44+
/**
45+
* Get sale statistics
46+
* @return this object that got sale statistics.
47+
* @since 0.0.5
48+
*/
49+
public Statistics get() {
50+
response = Net.postConnection("https://api.mojang.com/orders/statistics", "application/json", request.toString());
51+
return this;
52+
}
53+
/**
54+
* Get total sale number
55+
* @return The total sale number.
56+
* @since 0.0.5
57+
*/
58+
public int getTotalSales() {
59+
return new JsonParser().parse(response).getAsJsonObject().get("total").getAsInt();
60+
}
61+
/**
62+
* Get last 24h sale number
63+
* @return The last 24h sale number.
64+
* @since 0.0.5
65+
*/
66+
public int getLast24hSales() {
67+
return new JsonParser().parse(response).getAsJsonObject().get("last24h").getAsInt();
68+
}
69+
/**
70+
* Get sale velocity per seconds.
71+
* @return The sale velocity per seconds.
72+
* @since 0.0.5
73+
*/
74+
public float getSaleVelocityPerSeconds() {
75+
return new JsonParser().parse(response).getAsJsonObject().get("saleVelocityPerSeconds").getAsFloat();
76+
}
77+
}

src/main/java/cn/xiaopangxie732/mojang_api/Status.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import java.util.Properties;
2929

3030
/**
31-
* To check the status of servers.
31+
* Check the status of servers.
3232
* @author XiaoPangxie732
3333
* @since 0.0.1
3434
*/

src/main/java/cn/xiaopangxie732/mojang_api/UUIDName.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,17 @@
3030
import javax.imageio.ImageIO;
3131

3232
/**
33-
* Used for UUID operating.
33+
* The class for operating UUID.
3434
* @author XiaoPangxie732
35+
* @since 0.0.4
3536
*/
3637
public class UUIDName {
3738

3839
/**
39-
* To get UUID's name history.
40+
* Get name history.
4041
* @param uuid The player's UUID. can be get be using {@link UserName#UUIDAtNow(String)}
4142
* @return The name history of the UUID.
43+
* @since 0.0.4
4244
*/
4345
public static String nameHistory(String uuid) {
4446
String url = "https://api.mojang.com/user/profiles/" + uuid + "/names";
@@ -51,9 +53,10 @@ public static String nameHistory(String uuid) {
5153
return result.toString();
5254
}
5355
/**
54-
* To get this UUID's skin URL.
56+
* Get skin URL.
5557
* @param uuid The UUID of player. can be get be using {@link UserName#UUIDAtNow(String)}
5658
* @return The skin URL of given UUID.
59+
* @since 0.0.5
5760
*/
5861
public static String getSkinURL(String uuid) {
5962
return new JsonParser().parse(new String(Base64.getDecoder().decode(new JsonParser()
@@ -62,7 +65,7 @@ public static String getSkinURL(String uuid) {
6265
.get("textures").getAsJsonObject().get("SKIN").getAsJsonObject().get("url").getAsString();
6366
}
6467
/**
65-
* Store this UUID's skin png to desktop.
68+
* Store skin image to desktop.
6669
* @param uuid The UUID of player. can be get be using {@link UserName#UUIDAtNow(String)}
6770
*/
6871
public static void storeSkinImageToDesktop(String uuid) {

src/main/java/cn/xiaopangxie732/mojang_api/UserName.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
import com.google.gson.JsonParser;
3131

3232
/**
33-
* Used for username(playername) operating.
33+
* The class for operating username(playername).
3434
* @author XiaoPangxie732
35+
* @since 0.0.3
3536
*/
3637
public class UserName {
3738

@@ -66,7 +67,7 @@ public static String UUIDAtTime(String username, long timestamp) throws IllegalA
6667
* Get the UUID of this playername at now.
6768
* @param username The playername needs to get UUID.
6869
* @return The UUID of the name at now.
69-
* @throws UsernameOrTimestampInvalidException When the username is invalid.
70+
* @throws IllegalArgumentException When the username is invalid.
7071
* @since 0.0.3
7172
* @author XiaoPangxie732
7273
*/
@@ -86,7 +87,7 @@ public static String UUIDAtNow(String username) throws IllegalArgumentException
8687
* This requires playername change at least once.
8788
* @param username The playername needs to get UUID.
8889
* @return The UUID of the name at the original.
89-
* @throws UsernameOrTimestampInvalidException When the username is invalid or playername wasn't change at least once.
90+
* @throws IllegalArgumentException When the username is invalid or playername wasn't change at least once.
9091
* @since 0.0.3
9192
* @author XiaoPangxie732
9293
*/

src/main/java/cn/xiaopangxie732/mojang_api/util/Auth.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@
2727
import com.google.gson.JsonParser;
2828

2929
public class Auth {
30-
public static String getAccessToken(String email, String password) {
30+
/**
31+
* Get the access token of the account.
32+
* @param email email of the account.
33+
* @param password password of the account.
34+
* @throws IllegalStateException Throw when authentication error happened.
35+
* @return Access token of the account
36+
*/
37+
public static String getAccessToken(String email, String password) throws IllegalStateException{
3138
return new JsonParser().parse(authConnection(email, password)).getAsJsonObject().get("accessToken").getAsString();
3239
}
3340
public static String postConnection(String url, String RequestParameters, String accessToken) {

src/main/java/cn/xiaopangxie732/mojang_api/util/package-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
*/
1818
/**
1919
* The utilities package of MojangAPI-in-Java.
20+
* @since 0.0.2
2021
*/
2122
package cn.xiaopangxie732.mojang_api.util;

0 commit comments

Comments
 (0)