Skip to content
This repository was archived by the owner on Jul 8, 2020. It is now read-only.

Commit 87c2c8f

Browse files
committed
Rewrite callables to use unrest
Using maven is now essential - Also removed jcabi - Redid the async stuff - Simpler way to send queries to API - Shutdown hook to terminate executors and unirest
1 parent 0438817 commit 87c2c8f

File tree

9 files changed

+115
-98
lines changed

9 files changed

+115
-98
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ A Java implementation of the [mc-api.net](https://mc-api.net) API.
55

66
## Custom Handlers
77

8-
A handler decides how to get a uuid or username. The default handler will just query the API and then cache the result. However you can create your own, custom handler.
8+
A handler decides how a uuid or username is obtained. The default handler will just query the API and then cache the result. However you can create your own, custom handler.
99

1010
There are two premade for [Bukkit](https://bukkit.org)/[Spigot](http://spigotmc.org) and [BungeeCord](https://github.com/spigotmc/bungeecord):
11-
- [Bukkit Handler](https://gist.github.com/njb-said/cec2f56907e8eaa54021)
12-
- [Bungee Handler](https://gist.github.com/njb-said/39e1b0fdebc18b02fc14)
11+
- [Bukkit Handler](https://gist.github.com/njb-said/cec2f56907e8eaa54021): Checks if player is online or has played before, before querying API
12+
- [Bungee Handler](https://gist.github.com/njb-said/39e1b0fdebc18b02fc14): Checks if player is online before querying API
1313

1414
Just implement these somewhere within your project and then set it as the active handler using this example (example is for the BungeeHandler.
1515
```java

pom.xml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>net.mcapi.uuid</groupId>
44
<artifactId>uuid-java</artifactId>
5-
<version>1.0.4</version>
5+
<version>1.0.5</version>
66
<name>uuid-java</name>
77

88
<scm>
@@ -19,18 +19,18 @@
1919
</repositories>
2020

2121
<dependencies>
22-
<dependency>
23-
<groupId>com.jcabi</groupId>
24-
<artifactId>jcabi-aspects</artifactId>
25-
<version>1.0-SNAPSHOT</version>
26-
</dependency>
27-
2822
<dependency>
2923
<groupId>com.google.guava</groupId>
3024
<artifactId>guava</artifactId>
3125
<version>18.0</version>
3226
</dependency>
3327

28+
<dependency>
29+
<groupId>com.mashape.unirest</groupId>
30+
<artifactId>unirest-java</artifactId>
31+
<version>1.3.27</version>
32+
</dependency>
33+
3434
<dependency>
3535
<groupId>com.googlecode.json-simple</groupId>
3636
<artifactId>json-simple</artifactId>

src/main/java/net/mcapi/uuid/UUIDAPI.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package net.mcapi.uuid;
22

3+
import java.io.IOException;
34
import java.util.UUID;
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
47

58
import net.mcapi.uuid.handlers.JavaHandler;
69

710
import com.google.common.base.Preconditions;
11+
import com.mashape.unirest.http.Unirest;
812

913
/**
1014
* A UUID lookup API for Java and
@@ -23,6 +27,7 @@
2327
*/
2428
public class UUIDAPI {
2529

30+
private static ExecutorService service;
2631
private static UUIDHandler handler;
2732
private static ServerRegion region;
2833

@@ -64,6 +69,22 @@ public static UUIDHandler getHandler() {
6469
static {
6570
handler = new JavaHandler();
6671
region = ServerRegion.US;
72+
Unirest.setTimeouts(3500, 15000);
73+
service = Executors.newFixedThreadPool(5);
74+
75+
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
76+
public void run() {
77+
if(!service.isShutdown()) {
78+
service.shutdown();
79+
}
80+
try {
81+
Unirest.shutdown();
82+
} catch (IOException ex) {
83+
System.err.println("Error while shutting down MC-API unirest library: " + ex.getLocalizedMessage());
84+
}
85+
System.out.println("[MC-API] Shutdown");
86+
}
87+
}));
6788
}
6889

6990
/**
@@ -116,4 +137,8 @@ public static String getUsername(String uuid) {
116137
return handler.getUsername(uuid);
117138
}
118139

140+
public static ExecutorService getExecutor() {
141+
return service;
142+
}
143+
119144
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package net.mcapi.uuid.calls.base;
2+
3+
import java.util.concurrent.Future;
4+
5+
import net.mcapi.uuid.UUIDAPI;
6+
7+
/**
8+
* An async callable
9+
*
10+
* @param <T> type to call and return
11+
*/
12+
public abstract class Callable<T> implements java.util.concurrent.Callable<T> {
13+
14+
private Future<T> run() {
15+
return UUIDAPI.getExecutor().submit(this);
16+
}
17+
18+
public T execute() throws Exception {
19+
return run().get();
20+
}
21+
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package net.mcapi.uuid.calls.base;
2+
3+
import net.mcapi.uuid.utils.UUIDUtils;
4+
5+
import org.json.simple.JSONObject;
6+
7+
import com.mashape.unirest.http.*;
8+
9+
public abstract class JSONCallable extends Callable<JSONObject> {
10+
11+
private final String url;
12+
13+
public JSONCallable(String url) {
14+
this.url = url;
15+
}
16+
17+
@Override
18+
public JSONObject call() throws Exception {
19+
HttpResponse<JsonNode> response = Unirest.get(url).asJson();
20+
if(response == null || response.getBody() == null || response.getBody().getObject() == null) {
21+
return null;
22+
}
23+
24+
return (JSONObject)UUIDUtils.PARSER.parse(response.getBody().getObject().toString());
25+
}
26+
27+
}

src/main/java/net/mcapi/uuid/handlers/JavaHandler.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
import net.mcapi.uuid.UUIDAPI;
77
import net.mcapi.uuid.UUIDHandler;
8-
import net.mcapi.uuid.queries.NameQuery;
9-
import net.mcapi.uuid.queries.UUIDQuery;
8+
import net.mcapi.uuid.queries.APIQuery;
109
import net.mcapi.uuid.utils.ExpireHashMap;
1110
import net.mcapi.uuid.utils.UUIDUtils;
1211

@@ -30,10 +29,10 @@ public UUID getUUID(String username) {
3029
return uuid_cache.get(username);
3130
}
3231

33-
UUIDQuery query = new UUIDQuery(username);
32+
APIQuery query = new APIQuery(username, "full_uuid");
3433

3534
try {
36-
UUID uuid = query.call();
35+
UUID uuid = UUID.fromString(query.request());
3736
uuid_cache.put(username, uuid, 1, TimeUnit.HOURS);
3837
return uuid;
3938
} catch (Exception ex) {
@@ -57,10 +56,9 @@ public String getUsername(UUID uuid) {
5756
return name_cache.get(uuid);
5857
}
5958

60-
NameQuery query = new NameQuery(uuid.toString().replace("-", ""));
61-
59+
APIQuery query = new APIQuery(uuid.toString().replace("-", ""), "name");
6260
try {
63-
String username = query.call();
61+
String username = query.request();
6462
name_cache.put(uuid, username, 1, TimeUnit.HOURS);
6563
return username;
6664
} catch (Exception ex) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package net.mcapi.uuid.queries;
2+
3+
import net.mcapi.uuid.UUIDAPI;
4+
import net.mcapi.uuid.calls.base.JSONCallable;
5+
6+
import org.json.simple.JSONObject;
7+
8+
public class APIQuery extends JSONCallable {
9+
10+
private final String key;
11+
12+
public APIQuery(String query, String key) {
13+
super(UUIDAPI.getRegion().buildURL() + "/v3/" + key + "/" + query);
14+
this.key = key;
15+
}
16+
17+
public String request() throws Exception {
18+
JSONObject jsonReturn = call();
19+
20+
if(jsonReturn.containsKey(key)) {
21+
return jsonReturn.get(key).toString();
22+
}
23+
return null;
24+
}
25+
26+
}

src/main/java/net/mcapi/uuid/queries/NameQuery.java

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

src/main/java/net/mcapi/uuid/queries/UUIDQuery.java

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

0 commit comments

Comments
 (0)