Skip to content

Commit

Permalink
Fix weird bukkit thread stuff..
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauriichan committed Apr 27, 2021
1 parent 62da481 commit e6b54fd
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 47 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.sourcewriters.minecraft</groupId>
<artifactId>vcompat</artifactId>
<version>2.1.5</version>
<version>2.1.6</version>
<name>vCompat</name>
<distributionManagement>
<repository>
Expand Down
23 changes: 15 additions & 8 deletions src/main/java/net/sourcewriters/minecraft/vcompat/skin/Mojang.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.sourcewriters.minecraft.vcompat.reflection.VersionControl;
import net.sourcewriters.minecraft.vcompat.reflection.entity.NmsPlayer;
import net.sourcewriters.minecraft.vcompat.utils.java.net.EasyRequest;
import net.sourcewriters.minecraft.vcompat.utils.java.net.EasyResponse;
import net.sourcewriters.minecraft.vcompat.utils.java.net.content.EasyUrlEncodedContent;
import net.sourcewriters.minecraft.vcompat.utils.minecraft.MojangProfileServer;
import net.sourcewriters.minecraft.vcompat.utils.minecraft.Skin;
Expand Down Expand Up @@ -145,18 +146,21 @@ public Skin getSkinFrom(URL url, SkinModel model, int timeout) {
logger.log(LogTypeId.ERROR, "Url has to be http or https!");
return null;
}
connection.setConnectTimeout(timeout);
HttpURLConnection http = (HttpURLConnection) connection;
http.setConnectTimeout(timeout);
http.setReadTimeout(timeout / 2);
try {
connection.connect();
http.connect();
http.disconnect();
} catch (SocketTimeoutException exception) {
logger.log(LogTypeId.ERROR, "Can't connect to url!");
return null;
}
EasyRequest request = new EasyRequest(RequestType.POST);
request.header("Authorization", "Bearer " + profile.getAuthToken());
request.data("url", url.toString()).data("model", model.toString());
int code = request.run(String.format(URL_SKIN_UPLOAD, profile.getUniqueId())).getCode();
if (code != ResponseCode.NO_CONTENT) {
EasyResponse response = request.run(String.format(URL_SKIN_UPLOAD, profile.getUniqueId()));
if (response.getCode() != ResponseCode.NO_CONTENT) {
return null;
}
return apply(MojangProfileServer.getSkinShorten(profile.getUniqueId()));
Expand Down Expand Up @@ -199,18 +203,21 @@ public Skin getSkinFrom(String name, URL url, SkinModel model, int timeout) {
logger.log(LogTypeId.ERROR, "Url has to be http or https!");
return null;
}
connection.setConnectTimeout(timeout);
HttpURLConnection http = (HttpURLConnection) connection;
http.setConnectTimeout(timeout);
http.setReadTimeout(timeout / 2);
try {
connection.connect();
http.connect();
http.disconnect();
} catch (SocketTimeoutException exception) {
logger.log(LogTypeId.ERROR, "Can't connect to url!");
return null;
}
EasyRequest request = new EasyRequest(RequestType.POST);
request.header("Authorization", "Bearer " + profile.getAuthToken());
request.data("url", url.toString()).data("model", model.toString());
int code = request.run(String.format(URL_SKIN_UPLOAD, profile.getUniqueId()), EasyUrlEncodedContent.URL_ENCODED).getCode();
if (code != ResponseCode.OK && code != ResponseCode.NO_CONTENT) {
EasyResponse response = request.run(String.format(URL_SKIN_UPLOAD, profile.getUniqueId()), EasyUrlEncodedContent.URL_ENCODED);
if (response.getCode() != ResponseCode.NO_CONTENT) {
return null;
}
return apply(MojangProfileServer.getSkinShorten(name, profile.getUniqueId()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,100 +65,68 @@ public boolean validate() {
if (authToken == null) {
return false;
}

try {

EasyRequest request = new EasyRequest(RequestType.POST);

request.data("accessToken", authToken).data("clientToken", provider.getClientIdentifier().toString());

int code = request.run(String.format(AUTH_SERVER, "validate")).getCode();

return code == 204 || code == 200;
EasyRequest request = new EasyRequest(RequestType.POST).data("accessToken", authToken).data("clientToken",
provider.getClientIdentifier().toString());
return request.run(String.format(AUTH_SERVER, "validate")).getCode() == 204;
} catch (IOException ignore) {
return false;
}

}

public Profile refresh() {
if (authToken == null) {
return this;
}

try {

EasyRequest request = new EasyRequest(RequestType.POST);

request.data("accessToken", authToken).data("clientToken", provider.getClientIdentifier().toString());

EasyRequest request = new EasyRequest(RequestType.POST).data("accessToken", authToken).data("clientToken",
provider.getClientIdentifier().toString());
JsonValue<?> responseRaw = request.run(String.format(AUTH_SERVER, "refresh")).getDataAsJson();

if (!responseRaw.hasType(ValueType.OBJECT)) {
return this;
}
JsonObject response = (JsonObject) responseRaw;

authToken = null;

if (!response.has("accessToken")) {
return this;
}

authToken = response.get("accessToken").getValue().toString();

return this;

} catch (IOException ignore) {
return this;
}

}

public Profile authenticate() {

try {

EasyRequest request = new EasyRequest(RequestType.POST);

JsonObject object = new JsonObject();
JsonObject agent = new JsonObject();
agent.set("name", "Minecraft");
agent.set("version", 1);
object.set("agent", agent);

object.set("username", username);
object.set("password", password);
object.set("clientToken", provider.getClientIdentifier().toString());

request.data(object);

JsonValue<?> responseRaw = request.run(String.format(AUTH_SERVER, "authenticate")).getDataAsJson();

if (!responseRaw.hasType(ValueType.OBJECT)) {
return this;
}
JsonObject response = (JsonObject) responseRaw;

authToken = null;

if (!response.has("selectedProfile")) {
return this;
}

JsonObject profile = (JsonObject) response.get("selectedProfile");

uuid = profile.get("id").getValue().toString();
name = profile.get("name").getValue().toString();

authToken = response.get("accessToken").getValue().toString();

Thread.yield();
return this;

} catch (IOException ignore) {
return this;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class EasyRequest {
private int readTimeout = 20000;
private int connectTimeout = 30000;

private String agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36";

public EasyRequest(RequestType requestType) {
this.requestType = Objects.requireNonNull(requestType, "RequestType is needed to cast an Http request");
}
Expand Down Expand Up @@ -69,6 +71,15 @@ public EasyRequest setConnectTimeout(long connectTimeout, TimeUnit unit) {
return this;
}

public String getAgent() {
return agent;
}

public EasyRequest setAgent(String agent) {
this.agent = Objects.requireNonNull(agent, "UserAgent cant be null!");
return this;
}

public String[] header(String name) {
return headers.containsKey(name) ? headers.get(name).toArray(EMPTY) : EMPTY;
}
Expand Down Expand Up @@ -170,6 +181,7 @@ public EasyResponse run(URL url, IEasyContent content) throws IOException {
for (Entry<String, ArrayList<String>> header : headers.entrySet()) {
connection.setRequestProperty(header.getKey(), String.join("; ", header.getValue()));
}
connection.setRequestProperty("User-Agent", agent);
if (requestType.hasOutput()) {
connection.setRequestProperty("Content-Type", content.type());
connection.setFixedLengthStreamingMode(output ? data.length : 0);
Expand Down

0 comments on commit e6b54fd

Please sign in to comment.