Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoPangxie732 authored Feb 20, 2019
1 parent 82ec96f commit 6a6873d
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.xiaopangxie732</groupId>
<artifactId>mojang-api</artifactId>
<version>0.0.1</version>
<name>Mojang Public API in Java</name>
<description>Mojang Public API Java implementation</description>
<url>https://github.com/XiaoPangxie732/MojangAPI-in-Java</url>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<encoding>GBK</encoding>
<docencoding>GBK</docencoding>
<show>private</show>
<locale>en_US</locale>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
49 changes: 49 additions & 0 deletions src/main/java/cn/xiaopangxie732/mojang_api/Status.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cn.xiaopangxie732.mojang_api;

import java.util.Properties;

import com.google.gson.Gson;

import cn.xiaopangxie732.mojang_api.exceptions.InvalidServerException;
import cn.xiaopangxie732.mojang_api.status.StatusServer;
import cn.xiaopangxie732.mojang_api.status.StatusType;
import cn.xiaopangxie732.mojang_api.util.Net;

/**
* Use this class to check Mojang's server status
* @author XiaoPangxie732
*/
public class Status {
private final String url = "https://status.mojang.com/check";
private String response;
private Gson json = new Gson();
public Status() {
response = Net.getConnection(url);
}
/**
* To check server status
* @throws InvalidServerException When the server is invalid or <code>null</code>.
* @param server Which server needs to check the status.
* @return The status of this server.
*/
public StatusType getStatus(StatusServer server) throws InvalidServerException {
if(server == null) throw new InvalidServerException("null");
Properties[] result = json.fromJson(response, Properties[].class);
String value = result[server.ordinal()].getProperty(server.toString());
switch (value) {
case "green":
return StatusType.GREEN;
case "yellow":
return StatusType.YELLOW;
case "red":
return StatusType.RED;
}
return StatusType.COULD_NOT_CONNECT;
}
/**
* To flush Mojang's server status.
*/
public void flush() {
response = Net.getConnection(url);
}
}
9 changes: 9 additions & 0 deletions src/main/java/cn/xiaopangxie732/mojang_api/UUIDName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package cn.xiaopangxie732.mojang_api;

/**
* Incomplete
* @author XiaoPangxie732
*/
public class UUIDName {

}
10 changes: 10 additions & 0 deletions src/main/java/cn/xiaopangxie732/mojang_api/UserName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cn.xiaopangxie732.mojang_api;

/**
* Incomplete
* @author XiaoPangxie732
*/
public class UserName {

// public
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cn.xiaopangxie732.mojang_api.exceptions;

/**
* Thrown when server is invalid or <code>null</code>.
* @author XiaoPangxie732
*/
public class InvalidServerException extends RuntimeException {

private static final long serialVersionUID = -6624733586870154037L;

public InvalidServerException(String cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* This package stored all exception classes.
* @author XiaoPangxie732
*/
package cn.xiaopangxie732.mojang_api.exceptions;
6 changes: 6 additions & 0 deletions src/main/java/cn/xiaopangxie732/mojang_api/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* The MojangAPI main package
*
* @author XiaoPangxie732
*/
package cn.xiaopangxie732.mojang_api;
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cn.xiaopangxie732.mojang_api.status;

/**
* This class lists all the servers that can check the status.
* @author XiaoPangxie732
*/
public enum StatusServer {
MINECRAFT_NET,
SESSION_MINECRAFT_NET,
ACCOUNT_MOJANG_COM,
AUTHSERVER_MOJANG_COM,
SESSIONSERVER_MOJANG_COM,
API_MOJANG_COM,
TEXTURES_MINECRAFT_NET,
MOJANG_COM;

/**
* The toString() method of this class.<br>
* @return E.g MINECRAFT_NET toString() is minecraft.net
* @author XiaoPangxie732
*/
@Override
public String toString() {
return name().toLowerCase().replace("_", ".");
}
}
22 changes: 22 additions & 0 deletions src/main/java/cn/xiaopangxie732/mojang_api/status/StatusType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cn.xiaopangxie732.mojang_api.status;

/**
* This class lists all status type.
* @author XiaoPangxie732
*/
public enum StatusType {
GREEN,
YELLOW,
RED,
COULD_NOT_CONNECT;

/**
* The StatusType toString() method.
* @return E.g GREEN toString() is green.
* @author XiaoPangxie732
*/
@Override
public String toString() {
return name().toLowerCase();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* The package about Status.
* @author XiaoPangxie732
*/
package cn.xiaopangxie732.mojang_api.status;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* The package about UserName.
* @author XiaoPangxie732
*/
package cn.xiaopangxie732.mojang_api.username;
21 changes: 21 additions & 0 deletions src/main/java/cn/xiaopangxie732/mojang_api/util/Net.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cn.xiaopangxie732.mojang_api.util;

import java.net.URL;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class Net {
public static String getConnection(String url) {
String response = null;
try {
HttpGet get = new HttpGet(new URL(url).toURI());
HttpResponse re = HttpClientBuilder.create().build().execute(get);
response = EntityUtils.toString(re.getEntity());
get.releaseConnection();
} catch (Exception e) {e.printStackTrace();}
return response;
}
}

0 comments on commit 6a6873d

Please sign in to comment.