Skip to content

Commit 6a6873d

Browse files
Add files via upload
1 parent 82ec96f commit 6a6873d

File tree

12 files changed

+213
-0
lines changed

12 files changed

+213
-0
lines changed

pom.xml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>cn.xiaopangxie732</groupId>
4+
<artifactId>mojang-api</artifactId>
5+
<version>0.0.1</version>
6+
<name>Mojang Public API in Java</name>
7+
<description>Mojang Public API Java implementation</description>
8+
<url>https://github.com/XiaoPangxie732/MojangAPI-in-Java</url>
9+
<packaging>jar</packaging>
10+
<dependencies>
11+
<dependency>
12+
<groupId>com.google.code.gson</groupId>
13+
<artifactId>gson</artifactId>
14+
<version>2.8.5</version>
15+
</dependency>
16+
<dependency>
17+
<groupId>org.apache.httpcomponents</groupId>
18+
<artifactId>httpclient</artifactId>
19+
<version>4.5.7</version>
20+
</dependency>
21+
</dependencies>
22+
<build>
23+
<plugins>
24+
<plugin>
25+
<groupId>org.apache.maven.plugins</groupId>
26+
<artifactId>maven-javadoc-plugin</artifactId>
27+
<version>3.0.1</version>
28+
<configuration>
29+
<encoding>GBK</encoding>
30+
<docencoding>GBK</docencoding>
31+
<show>private</show>
32+
<locale>en_US</locale>
33+
</configuration>
34+
</plugin>
35+
</plugins>
36+
</build>
37+
<properties>
38+
<maven.compiler.source>1.8</maven.compiler.source>
39+
<maven.compiler.target>1.8</maven.compiler.target>
40+
</properties>
41+
</project>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cn.xiaopangxie732.mojang_api;
2+
3+
import java.util.Properties;
4+
5+
import com.google.gson.Gson;
6+
7+
import cn.xiaopangxie732.mojang_api.exceptions.InvalidServerException;
8+
import cn.xiaopangxie732.mojang_api.status.StatusServer;
9+
import cn.xiaopangxie732.mojang_api.status.StatusType;
10+
import cn.xiaopangxie732.mojang_api.util.Net;
11+
12+
/**
13+
* Use this class to check Mojang's server status
14+
* @author XiaoPangxie732
15+
*/
16+
public class Status {
17+
private final String url = "https://status.mojang.com/check";
18+
private String response;
19+
private Gson json = new Gson();
20+
public Status() {
21+
response = Net.getConnection(url);
22+
}
23+
/**
24+
* To check server status
25+
* @throws InvalidServerException When the server is invalid or <code>null</code>.
26+
* @param server Which server needs to check the status.
27+
* @return The status of this server.
28+
*/
29+
public StatusType getStatus(StatusServer server) throws InvalidServerException {
30+
if(server == null) throw new InvalidServerException("null");
31+
Properties[] result = json.fromJson(response, Properties[].class);
32+
String value = result[server.ordinal()].getProperty(server.toString());
33+
switch (value) {
34+
case "green":
35+
return StatusType.GREEN;
36+
case "yellow":
37+
return StatusType.YELLOW;
38+
case "red":
39+
return StatusType.RED;
40+
}
41+
return StatusType.COULD_NOT_CONNECT;
42+
}
43+
/**
44+
* To flush Mojang's server status.
45+
*/
46+
public void flush() {
47+
response = Net.getConnection(url);
48+
}
49+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package cn.xiaopangxie732.mojang_api;
2+
3+
/**
4+
* Incomplete
5+
* @author XiaoPangxie732
6+
*/
7+
public class UUIDName {
8+
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package cn.xiaopangxie732.mojang_api;
2+
3+
/**
4+
* Incomplete
5+
* @author XiaoPangxie732
6+
*/
7+
public class UserName {
8+
9+
// public
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cn.xiaopangxie732.mojang_api.exceptions;
2+
3+
/**
4+
* Thrown when server is invalid or <code>null</code>.
5+
* @author XiaoPangxie732
6+
*/
7+
public class InvalidServerException extends RuntimeException {
8+
9+
private static final long serialVersionUID = -6624733586870154037L;
10+
11+
public InvalidServerException(String cause) {
12+
super(cause);
13+
}
14+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* This package stored all exception classes.
3+
* @author XiaoPangxie732
4+
*/
5+
package cn.xiaopangxie732.mojang_api.exceptions;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* The MojangAPI main package
3+
*
4+
* @author XiaoPangxie732
5+
*/
6+
package cn.xiaopangxie732.mojang_api;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cn.xiaopangxie732.mojang_api.status;
2+
3+
/**
4+
* This class lists all the servers that can check the status.
5+
* @author XiaoPangxie732
6+
*/
7+
public enum StatusServer {
8+
MINECRAFT_NET,
9+
SESSION_MINECRAFT_NET,
10+
ACCOUNT_MOJANG_COM,
11+
AUTHSERVER_MOJANG_COM,
12+
SESSIONSERVER_MOJANG_COM,
13+
API_MOJANG_COM,
14+
TEXTURES_MINECRAFT_NET,
15+
MOJANG_COM;
16+
17+
/**
18+
* The toString() method of this class.<br>
19+
* @return E.g MINECRAFT_NET toString() is minecraft.net
20+
* @author XiaoPangxie732
21+
*/
22+
@Override
23+
public String toString() {
24+
return name().toLowerCase().replace("_", ".");
25+
}
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cn.xiaopangxie732.mojang_api.status;
2+
3+
/**
4+
* This class lists all status type.
5+
* @author XiaoPangxie732
6+
*/
7+
public enum StatusType {
8+
GREEN,
9+
YELLOW,
10+
RED,
11+
COULD_NOT_CONNECT;
12+
13+
/**
14+
* The StatusType toString() method.
15+
* @return E.g GREEN toString() is green.
16+
* @author XiaoPangxie732
17+
*/
18+
@Override
19+
public String toString() {
20+
return name().toLowerCase();
21+
}
22+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* The package about Status.
3+
* @author XiaoPangxie732
4+
*/
5+
package cn.xiaopangxie732.mojang_api.status;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* The package about UserName.
3+
* @author XiaoPangxie732
4+
*/
5+
package cn.xiaopangxie732.mojang_api.username;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cn.xiaopangxie732.mojang_api.util;
2+
3+
import java.net.URL;
4+
5+
import org.apache.http.HttpResponse;
6+
import org.apache.http.client.methods.HttpGet;
7+
import org.apache.http.impl.client.HttpClientBuilder;
8+
import org.apache.http.util.EntityUtils;
9+
10+
public class Net {
11+
public static String getConnection(String url) {
12+
String response = null;
13+
try {
14+
HttpGet get = new HttpGet(new URL(url).toURI());
15+
HttpResponse re = HttpClientBuilder.create().build().execute(get);
16+
response = EntityUtils.toString(re.getEntity());
17+
get.releaseConnection();
18+
} catch (Exception e) {e.printStackTrace();}
19+
return response;
20+
}
21+
}

0 commit comments

Comments
 (0)