Skip to content

Commit fe3370f

Browse files
Add files via upload
1 parent af0ddd1 commit fe3370f

File tree

9 files changed

+129
-23
lines changed

9 files changed

+129
-23
lines changed

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44

55
import com.google.gson.Gson;
66

7-
import cn.xiaopangxie732.mojang_api.exceptions.InvalidServerException;
87
import cn.xiaopangxie732.mojang_api.status.StatusServer;
98
import cn.xiaopangxie732.mojang_api.status.StatusType;
109
import cn.xiaopangxie732.mojang_api.util.Net;
1110

1211
/**
13-
* Use this class to check Mojang's server status
12+
* Used to check the Mojang's servers status.
1413
* @author XiaoPangxie732
1514
*/
1615
public class Status {
@@ -21,13 +20,14 @@ public Status() {
2120
response = Net.getConnection(url);
2221
}
2322
/**
24-
* To check server status
25-
* @throws InvalidServerException When the server is invalid or <code>null</code>.
23+
* To check server status.
24+
* @throws NullPointerException When the server is <code>null</code>.
2625
* @param server Which server needs to check the status.
2726
* @return The status of this server.
27+
* @author XiaoPangxie732
2828
*/
29-
public StatusType getStatus(StatusServer server) throws InvalidServerException {
30-
if(server == null) throw new InvalidServerException("null");
29+
public StatusType getStatus(StatusServer server) throws NullPointerException {
30+
if(server == null) throw new NullPointerException("The server is null");
3131
Properties[] result = json.fromJson(response, Properties[].class);
3232
String value = result[server.ordinal()].getProperty(server.toString());
3333
switch (value) {
@@ -41,9 +41,10 @@ public StatusType getStatus(StatusServer server) throws InvalidServerException {
4141
return StatusType.COULD_NOT_CONNECT;
4242
}
4343
/**
44-
* To flush Mojang's server status.
44+
* To fresh Mojang's servers status.
45+
* @author XiaoPangxie732
4546
*/
46-
public void flush() {
47+
public void fresh() {
4748
response = Net.getConnection(url);
4849
}
4950
}
Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,65 @@
11
package cn.xiaopangxie732.mojang_api;
22

3+
import java.util.Properties;
4+
5+
import com.google.gson.Gson;
6+
import com.google.gson.JsonSyntaxException;
7+
8+
import cn.xiaopangxie732.mojang_api.exceptions.UsernameOrTimestampInvalidException;
9+
import cn.xiaopangxie732.mojang_api.util.Net;
10+
311
/**
4-
* Incomplete
12+
* To get UUID of username.
513
* @author XiaoPangxie732
614
*/
715
public class UserName {
816

9-
// public
17+
private static String url = "https://api.mojang.com/users/profiles/minecraft/";
18+
private static Gson json = new Gson();
19+
20+
public static String UUIDAtTime(String username, long timestamp) {
21+
String furl = url + username + "?at=" + timestamp;
22+
String response = null;
23+
try {
24+
response = Net.getConnection(furl);
25+
} catch(IllegalArgumentException ex) {
26+
throw new UsernameOrTimestampInvalidException("Username \"" + username + "\" is invalid or on timestamp \"" + Long.toString(timestamp).replace("0", "Original") + "\" the username wasn't change.");
27+
}
28+
Properties result = json.fromJson(response, Properties.class);
29+
if(!result.getProperty("error", "noError").equals("noError"))
30+
throw new IllegalArgumentException(result.getProperty("errorMessage"));
31+
return result.getProperty("id");
32+
}
33+
34+
public static String UUIDAtNow(String username) {
35+
String furl = url + username;
36+
String response;
37+
try {
38+
response = Net.getConnection(furl);
39+
} catch (IllegalArgumentException e) {
40+
throw new UsernameOrTimestampInvalidException("Username \"" + username + "\" is invalid");
41+
}
42+
Properties result = json.fromJson(response, Properties.class);
43+
return result.getProperty("id");
44+
}
45+
46+
public static String UUIDAtOriginal(String username) {
47+
return UUIDAtTime(username, 0);
48+
}
49+
50+
public static String UUIDOfNames(String... usernames) {
51+
if(usernames.length > 100) throw new IllegalArgumentException("Too more names! (" + usernames.length + "/100)");
52+
String response = Net.postConnection("https://api.mojang.com/profiles/minecraft", "application/json", json.toJson(usernames));
53+
Properties[] names = null;
54+
try {
55+
names = json.fromJson(response, Properties[].class);
56+
} catch (JsonSyntaxException e) {
57+
throw new IllegalArgumentException(json.fromJson(response, Properties.class).getProperty("errorMessage"));
58+
}
59+
String result = "";
60+
for(Properties output : names) {
61+
result += output.getProperty("name") + "=" + output.getProperty("id") + "\n";
62+
}
63+
return result;
64+
}
1065
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.xiaopangxie732.mojang_api.exceptions;
2+
3+
/**
4+
* Throw when the user name on that timestamp has not changed, or the username is invalid.
5+
* @author XiaoPangxie732
6+
*/
7+
public class UsernameOrTimestampInvalidException extends RuntimeException {
8+
9+
private static final long serialVersionUID = -3145228876580727833L;
10+
11+
public UsernameOrTimestampInvalidException() {
12+
super();
13+
}
14+
15+
public UsernameOrTimestampInvalidException(String message) {
16+
super(message);
17+
}
18+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* This package stored all exception classes.
2+
* The exception package.
33
* @author XiaoPangxie732
44
*/
55
package cn.xiaopangxie732.mojang_api.exceptions;
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/**
2-
* The MojangAPI main package
3-
*
2+
* The MojangAPI-in-Java main package
43
* @author XiaoPangxie732
54
*/
65
package cn.xiaopangxie732.mojang_api;

src/main/java/cn/xiaopangxie732/mojang_api/status/StatusServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public enum StatusServer {
1515
MOJANG_COM;
1616

1717
/**
18-
* The toString() method of this class.<br>
18+
* Let enum to string.<br>
1919
* @return E.g MINECRAFT_NET toString() is minecraft.net
2020
* @author XiaoPangxie732
2121
*/
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cn.xiaopangxie732.mojang_api.status;
22

33
/**
4-
* This class lists all status type.
4+
* List all status type.
55
* @author XiaoPangxie732
66
*/
77
public enum StatusType {
@@ -11,12 +11,12 @@ public enum StatusType {
1111
COULD_NOT_CONNECT;
1212

1313
/**
14-
* The StatusType toString() method.
14+
* Let enum to string.
1515
* @return E.g GREEN toString() is green.
1616
* @author XiaoPangxie732
1717
*/
1818
@Override
1919
public String toString() {
20-
return name().toLowerCase();
20+
return name().toLowerCase().replace("_", " ");
2121
}
2222
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* The package about Status.
2+
* The <code>Status</code> package.
33
* @author XiaoPangxie732
44
*/
55
package cn.xiaopangxie732.mojang_api.status;
Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,54 @@
11
package cn.xiaopangxie732.mojang_api.util;
22

3+
import java.io.IOException;
4+
import java.net.MalformedURLException;
5+
import java.net.URISyntaxException;
36
import java.net.URL;
4-
7+
import java.nio.charset.Charset;
58
import org.apache.http.HttpResponse;
9+
import org.apache.http.client.ClientProtocolException;
610
import org.apache.http.client.methods.HttpGet;
11+
import org.apache.http.client.methods.HttpPost;
12+
import org.apache.http.entity.StringEntity;
713
import org.apache.http.impl.client.HttpClientBuilder;
814
import org.apache.http.util.EntityUtils;
915

10-
public class Net {
11-
public static String getConnection(String url) {
16+
public final class Net {
17+
private Net() {}
18+
public static String getConnection(String url) throws IllegalArgumentException {
1219
String response = null;
20+
HttpGet get = null;
1321
try {
14-
HttpGet get = new HttpGet(new URL(url).toURI());
22+
get = new HttpGet(new URL(url).toURI());
1523
HttpResponse re = HttpClientBuilder.create().build().execute(get);
1624
response = EntityUtils.toString(re.getEntity());
1725
get.releaseConnection();
18-
} catch (Exception e) {e.printStackTrace();}
26+
} catch(MalformedURLException e){e.printStackTrace();}catch(URISyntaxException e){e.printStackTrace();}catch(ClientProtocolException e){e.printStackTrace();}catch (IOException e) {e.printStackTrace();
27+
} finally {
28+
if(get != null){
29+
get.releaseConnection();
30+
}
31+
}
1932
return response;
2033
}
34+
public static String postConnection(String url, String ContentType, String RequestParameters) {
35+
HttpPost post = null;
36+
String retur = null;
37+
try {
38+
post = new HttpPost(new URL(url).toURI());
39+
post.setHeader("Content-Type", ContentType);
40+
StringEntity se = new StringEntity(RequestParameters,
41+
Charset.forName("UTF-8"));
42+
post.setEntity(se);
43+
HttpResponse re = HttpClientBuilder.create().build().execute(post);
44+
retur = EntityUtils.toString(re.getEntity());
45+
} catch (Exception e) {
46+
e.printStackTrace();
47+
} finally {
48+
if(post != null){
49+
post.releaseConnection();
50+
}
51+
}
52+
return retur;
53+
}
2154
}

0 commit comments

Comments
 (0)