Skip to content

Commit 474417a

Browse files
Update Files
1 parent 46d3c3f commit 474417a

File tree

9 files changed

+106
-78
lines changed

9 files changed

+106
-78
lines changed

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.3</version>
5+
<version>0.0.4</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>

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

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,92 @@
11
package cn.xiaopangxie732.mojang_api;
22

3+
import java.util.HashMap;
34
import java.util.Properties;
4-
55
import com.google.gson.Gson;
66

7-
import cn.xiaopangxie732.mojang_api.status.StatusServer;
8-
import cn.xiaopangxie732.mojang_api.status.StatusType;
97
import cn.xiaopangxie732.mojang_api.util.Net;
108

119
/**
1210
* Used to check the Mojang's servers status.
1311
* @author XiaoPangxie732
1412
*/
1513
public class Status {
14+
15+
/**
16+
* Lists all the servers that can check the status.
17+
* @since 0.0.1
18+
* @author XiaoPangxie732
19+
*/
20+
public enum StatusServer {
21+
MINECRAFT_NET,
22+
SESSION_MINECRAFT_NET,
23+
ACCOUNT_MOJANG_COM,
24+
AUTHSERVER_MOJANG_COM,
25+
SESSIONSERVER_MOJANG_COM,
26+
API_MOJANG_COM,
27+
TEXTURES_MINECRAFT_NET,
28+
MOJANG_COM;
29+
30+
/**
31+
* Let enum to string.<br>
32+
* @return The lower case of enum name.
33+
* @author XiaoPangxie732
34+
*/
35+
@Override
36+
public String toString() {
37+
return name().toLowerCase().replace("_", ".");
38+
}
39+
}
40+
41+
/**
42+
* List all status type.
43+
* @since 0.0.1
44+
* @author XiaoPangxie732
45+
*/
46+
public enum StatusType {
47+
GREEN,
48+
YELLOW,
49+
RED,
50+
ERROR_TO_CONNECT;
51+
52+
/**
53+
* Let enum to string.
54+
* @return The lower case of enum name.
55+
* @author XiaoPangxie732
56+
*/
57+
@Override
58+
public String toString() {
59+
return name().toLowerCase().replace("_", " ");
60+
}
61+
}
62+
1663
private final String url = "https://status.mojang.com/check";
1764
private String response;
1865
private Gson json = new Gson();
66+
67+
/**
68+
* Construct a <code>Status</code> class.
69+
*/
1970
public Status() {
2071
response = Net.getConnection(url);
2172
}
73+
2274
/**
2375
* To check server status.
2476
* @throws NullPointerException When the server is <code>null</code>.
25-
* @param server Which server needs to check the status.
77+
* @param server The server needs to check the status.
2678
* @return The status of this server.
2779
* @since 0.0.1
2880
* @author XiaoPangxie732
2981
*/
3082
public StatusType getStatus(StatusServer server) throws NullPointerException {
3183
if(server == null) throw new NullPointerException("The server is null");
32-
Properties[] result = json.fromJson(response, Properties[].class);
33-
String value = result[server.ordinal()].getProperty(server.toString());
84+
Properties[] parr = json.fromJson(response, Properties[].class);
85+
HashMap<String, String> result = new HashMap<>();
86+
for(Properties p : parr) {
87+
result.put((String)p.keySet().toArray()[0], p.getProperty((String)p.keySet().toArray()[0]));
88+
}
89+
String value = result.get(server.toString());
3490
switch (value) {
3591
case "green":
3692
return StatusType.GREEN;
@@ -39,10 +95,11 @@ public StatusType getStatus(StatusServer server) throws NullPointerException {
3995
case "red":
4096
return StatusType.RED;
4197
}
42-
return StatusType.COULD_NOT_CONNECT;
98+
return StatusType.ERROR_TO_CONNECT;
4399
}
100+
44101
/**
45-
* To fresh Mojang's servers status.
102+
* To fresh Mojang servers status.
46103
* @since 0.0.2
47104
* @author XiaoPangxie732
48105
*/
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
package cn.xiaopangxie732.mojang_api;
22

3+
import cn.xiaopangxie732.mojang_api.util.Net;
4+
import com.google.gson.Gson;
5+
6+
import java.text.DateFormat;
7+
import java.util.Date;
8+
import java.util.Properties;
9+
310
/**
411
* Incomplete
512
* @author XiaoPangxie732
613
*/
714
public class UUIDName {
815

16+
private static Gson json = new Gson();
17+
18+
public static String NameHistory(String uuid) {
19+
String url = "https://api.mojang.com/user/profiles/" + uuid + "/names";
20+
Properties[] response = json.fromJson(Net.getConnection(url), Properties[].class);
21+
StringBuilder result = new StringBuilder("Original: " + response[0].getProperty("name"));
22+
for(int var = 1; var < response.length; ++var) {
23+
result.append("\n" + DateFormat.getDateInstance().format(new Date(Long.parseLong(response[var].getProperty("changedToAt")))) + ": " + response[var].getProperty("name"));
24+
}
25+
return result.toString();
26+
}
927
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class UserName {
1818
private static Gson json = new Gson();
1919

2020
/**
21-
* Get the UUID of this playername at a time.
21+
* Get the UUID of this playername at a timestamp(requires playername change at least once).
2222
* @param username The playername needs to get UUID.
2323
* @param timestamp A UNIX timestamp (without milliseconds).
2424
* @return The UUID of the name at the timestamp provided.
@@ -62,7 +62,8 @@ public static String UUIDAtNow(String username) throws UsernameOrTimestampInvali
6262
}
6363

6464
/**
65-
* Get the UUID of this playername at original time (timestamp=0).
65+
* Get the UUID of this playername at original time (timestamp=0).<br>
66+
* This requires playername change at least once.
6667
* @param username The playername needs to get UUID.
6768
* @return The UUID of the name at the original.
6869
* @throws UsernameOrTimestampInvalidException When the username is invalid or playername wasn't change at least once.
@@ -89,10 +90,10 @@ public static String UUIDOfNames(String... usernames) throws IllegalArgumentExce
8990
} catch (JsonSyntaxException e) {
9091
throw new IllegalArgumentException(json.fromJson(response, Properties.class).getProperty("errorMessage"));
9192
}
92-
String result = "";
93+
StringBuilder result = new StringBuilder();
9394
for(Properties output : names) {
94-
result += output.getProperty("name") + "=" + output.getProperty("id") + "\n";
95+
result.append(output.getProperty("name") + "=" + output.getProperty("id") + "\n");
9596
}
96-
return result;
97+
return result.toString();
9798
}
9899
}

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

Lines changed: 0 additions & 27 deletions
This file was deleted.

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

Lines changed: 0 additions & 23 deletions
This file was deleted.

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

Lines changed: 0 additions & 6 deletions
This file was deleted.

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package cn.xiaopangxie732.mojang_api.util;
22

3-
import java.io.IOException;
4-
import java.net.MalformedURLException;
5-
import java.net.URISyntaxException;
63
import java.net.URL;
74
import java.nio.charset.Charset;
85
import org.apache.http.HttpResponse;
9-
import org.apache.http.client.ClientProtocolException;
106
import org.apache.http.client.methods.HttpGet;
117
import org.apache.http.client.methods.HttpPost;
128
import org.apache.http.entity.StringEntity;
@@ -22,8 +18,10 @@ public static String getConnection(String url) throws IllegalArgumentException {
2218
get = new HttpGet(new URL(url).toURI());
2319
HttpResponse re = HttpClientBuilder.create().build().execute(get);
2420
response = EntityUtils.toString(re.getEntity());
25-
get.releaseConnection();
26-
} catch(MalformedURLException e){e.printStackTrace();}catch(URISyntaxException e){e.printStackTrace();}catch(ClientProtocolException e){e.printStackTrace();}catch (IOException e) {e.printStackTrace();
21+
} catch(IllegalArgumentException ex) {
22+
throw new IllegalArgumentException();
23+
} catch(Exception e) {
24+
e.printStackTrace();
2725
} finally {
2826
if(get != null){
2927
get.releaseConnection();
@@ -37,8 +35,7 @@ public static String postConnection(String url, String ContentType, String Reque
3735
try {
3836
post = new HttpPost(new URL(url).toURI());
3937
post.setHeader("Content-Type", ContentType);
40-
StringEntity se = new StringEntity(RequestParameters,
41-
Charset.forName("UTF-8"));
38+
StringEntity se = new StringEntity(RequestParameters, Charset.forName("UTF-8"));
4239
post.setEntity(se);
4340
HttpResponse re = HttpClientBuilder.create().build().execute(post);
4441
retur = EntityUtils.toString(re.getEntity());

src/main/main.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/java" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

0 commit comments

Comments
 (0)