Skip to content

Commit 0d3948a

Browse files
Im-FranTheProgramSrc
authored andcommitted
* Added ConnectionBuilder
* Fixed MojangAPI spamming error 429 in console
1 parent da67b5e commit 0d3948a

File tree

5 files changed

+167
-20
lines changed

5 files changed

+167
-20
lines changed

dependency-reduced-pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>xyz.theprogramsrc</groupId>
55
<artifactId>SuperCoreAPI</artifactId>
66
<name>SuperCoreAPI</name>
7-
<version>4.6.0</version>
7+
<version>4.7.0</version>
88
<build>
99
<sourceDirectory>src/main/java</sourceDirectory>
1010
<defaultGoal>clean package</defaultGoal>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>xyz.theprogramsrc</groupId>
88
<artifactId>SuperCoreAPI</artifactId>
9-
<version>4.6.0</version>
9+
<version>4.7.0</version>
1010
<packaging>jar</packaging>
1111

1212
<name>SuperCoreAPI</name>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package xyz.theprogramsrc.supercoreapi.global.networking;
2+
3+
import java.io.IOException;
4+
import java.net.HttpURLConnection;
5+
import java.net.MalformedURLException;
6+
import java.net.URL;
7+
8+
public class ConnectionBuilder {
9+
10+
private final URL url;
11+
12+
/**
13+
* Create a new connection builder
14+
* @param url the url
15+
* @throws MalformedURLException if any error occurs
16+
*/
17+
public ConnectionBuilder(String url) throws MalformedURLException {
18+
this.url = new URL(url);
19+
}
20+
21+
/**
22+
* Create the Custom Connection
23+
* @return the {@link CustomConnection}
24+
* @throws IOException if any error occurs
25+
*/
26+
public CustomConnection connect() throws IOException {
27+
return new CustomConnection(url, ((HttpURLConnection) this.url.openConnection()));
28+
}
29+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package xyz.theprogramsrc.supercoreapi.global.networking;
2+
3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonObject;
5+
import com.google.gson.JsonParser;
6+
7+
import java.io.BufferedReader;
8+
import java.io.IOException;
9+
import java.io.InputStreamReader;
10+
import java.net.HttpURLConnection;
11+
import java.net.URISyntaxException;
12+
import java.net.URL;
13+
import java.util.LinkedList;
14+
import java.util.stream.Collectors;
15+
16+
public class CustomConnection {
17+
18+
private final URL url;
19+
private final HttpURLConnection connection;
20+
21+
/**
22+
* Init a new custom connection (Used for managing a connection)
23+
* @param url the url
24+
* @param connection the {@link HttpURLConnection connection}
25+
* @throws IOException if any error occurs
26+
*/
27+
public CustomConnection(URL url, HttpURLConnection connection) throws IOException {
28+
this.url = url;
29+
this.connection = connection;
30+
this.connection.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36");
31+
this.connection.connect();
32+
}
33+
34+
/**
35+
* Gets the url
36+
* @return the url
37+
*/
38+
public URL getURL() {
39+
return this.url;
40+
}
41+
42+
/**
43+
* Gets the url as string
44+
* @return the url as string
45+
* @throws URISyntaxException if any error occurs
46+
*/
47+
public String getURLString() throws URISyntaxException {
48+
return this.getURL().toURI().toString();
49+
}
50+
51+
/**
52+
* Gets the {@link HttpURLConnection connection}
53+
* @return the connection
54+
*/
55+
public HttpURLConnection getConnection() {
56+
return this.connection;
57+
}
58+
59+
/**
60+
* Gets the response code
61+
* @return the response code
62+
* @throws IOException if any error occurs
63+
*/
64+
public int getResponseCode() throws IOException {
65+
return this.getConnection().getResponseCode();
66+
}
67+
68+
/**
69+
* Gets the response message
70+
* @return the response message
71+
* @throws IOException if any error occurs
72+
*
73+
* @see HttpURLConnection#getResponseMessage()
74+
*/
75+
public String getResponseMessage() throws IOException {
76+
return this.getConnection().getResponseMessage();
77+
}
78+
79+
/**
80+
* Gets the response from the server
81+
* @return the response
82+
* @throws IOException if any error occurs
83+
*/
84+
public LinkedList<String> getResponse() throws IOException {
85+
return new BufferedReader(new InputStreamReader(this.getConnection().getInputStream())).lines().collect(Collectors.toCollection(LinkedList::new));
86+
}
87+
88+
/**
89+
* Gets the response from the server as a string
90+
* @return the response from the server as a string
91+
* @throws IOException if any error occurs
92+
*/
93+
public String getResponseString() throws IOException {
94+
return String.join("", this.getResponse());
95+
}
96+
97+
/**
98+
* Gets the response from the server as a json object
99+
* @return the response from the server as a json object
100+
* @throws IOException if any error occurs
101+
*/
102+
public JsonObject getResponseJson() throws IOException {
103+
String data = this.getResponseString();
104+
if(data == null) return null;
105+
JsonElement element = new JsonParser().parse(data);
106+
if(element.isJsonNull()) return null;
107+
return element.getAsJsonObject();
108+
}
109+
}

src/main/java/xyz/theprogramsrc/supercoreapi/spigot/utils/skintexture/SkinTexture.java

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
import com.mojang.authlib.properties.Property;
99
import org.apache.commons.codec.binary.Base64;
1010
import org.bukkit.entity.Player;
11+
import xyz.theprogramsrc.supercoreapi.global.networking.ConnectionBuilder;
12+
import xyz.theprogramsrc.supercoreapi.global.networking.CustomConnection;
1113
import xyz.theprogramsrc.supercoreapi.global.utils.Utils;
1214

15+
import java.io.IOException;
1316
import java.util.UUID;
1417

1518
@SuppressWarnings("ALL")
@@ -61,15 +64,18 @@ public static SkinTexture fromPlayer(Player player) {
6164
*/
6265
public static SkinTexture fromMojang(String playerName) {
6366
if(isMojangDown()) return null;
64-
String response = Utils.readWithInputStream("https://api.mojang.com/users/profiles/minecraft/" + playerName);
65-
if(response == null)
67+
try {
68+
CustomConnection connection = new ConnectionBuilder("https://api.mojang.com/users/profiles/minecraft/" + playerName).connect();
69+
if(connection.getResponseCode() == 429) return null;
70+
JsonObject response = connection.getResponseJson();
71+
if(response == null) return null;
72+
String uuid = response.get("id").getAsString();
73+
String fullUUID = Utils.uuidToFullUUID(uuid);
74+
return fromMojang(UUID.fromString(fullUUID));
75+
} catch (IOException e) {
76+
e.printStackTrace();
6677
return null;
67-
JsonElement el = (new JsonParser()).parse(response);
68-
if(el.isJsonNull())
69-
return null;
70-
String uuid = el.getAsJsonObject().get("id").getAsString();
71-
String fullUUID = Utils.uuidToFullUUID(uuid);
72-
return fromMojang(UUID.fromString(fullUUID));
78+
}
7379
}
7480

7581
/**
@@ -79,15 +85,18 @@ public static SkinTexture fromMojang(String playerName) {
7985
*/
8086
public static SkinTexture fromMojang(UUID uuid) {
8187
if(isMojangDown()) return null;
82-
String response = Utils.readWithInputStream("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid.toString().replace("-", "") + "?unsigned=false");
83-
if(response == null)
88+
try {
89+
CustomConnection connection = new ConnectionBuilder("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid.toString().replace("-", "") + "?unsigned=false").connect();
90+
if(connection.getResponseCode() == 429) return null;
91+
JsonObject response = connection.getResponseJson();
92+
if(response == null) return null;
93+
JsonObject properties = response.get("properties").getAsJsonArray().get(0).getAsJsonObject();
94+
String value = properties.get("value").getAsString();
95+
return new SkinTexture(base64ToUrl(value));
96+
} catch (IOException e) {
97+
e.printStackTrace();
8498
return null;
85-
JsonElement el = (new JsonParser()).parse(response);
86-
if(el.isJsonNull())
87-
return null;
88-
JsonObject properties = el.getAsJsonObject().get("properties").getAsJsonArray().get(0).getAsJsonObject();
89-
String value = properties.get("value").getAsString();
90-
return new SkinTexture(base64ToUrl(value));
99+
}
91100
}
92101

93102
/**
@@ -116,8 +125,8 @@ private static boolean isMojangDown(){
116125
if(current - lastCheck >= 25000L){
117126
lastCheck = current;
118127
try{
119-
String url = "http://status.mojang.com/check";
120-
String content = Utils.readWithInputStream(url);
128+
CustomConnection connection = new ConnectionBuilder("http://status.mojang.com/check").connect();
129+
String content = connection.getResponseString();
121130
if(content != null){
122131
JsonArray array = new JsonParser().parse(content).getAsJsonArray();
123132
for(JsonElement el : array){

0 commit comments

Comments
 (0)