Skip to content

Commit

Permalink
fix: Changed SkinHandler fromProperties boolean to Property
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeV220 committed Jan 11, 2023
1 parent b8bb9d9 commit 6fc4532
Showing 1 changed file with 49 additions and 100 deletions.
149 changes: 49 additions & 100 deletions core/src/main/java/com/georgev22/skinoverlay/handler/SkinHandler.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package com.georgev22.skinoverlay.handler;

import com.georgev22.library.maps.ObjectMap;
import com.georgev22.library.yaml.file.FileConfiguration;
import com.georgev22.skinoverlay.SkinOverlay;
import com.georgev22.skinoverlay.utilities.Utilities.Request;
import com.georgev22.skinoverlay.utilities.player.PlayerObject;
import com.google.gson.*;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.net.ssl.HttpsURLConnection;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ProtocolException;
import java.net.URL;
import java.util.UUID;
import java.util.logging.Level;

public abstract class SkinHandler {
Expand All @@ -40,26 +37,35 @@ public GameProfile getGameProfile(@NotNull final PlayerObject playerObject) thro
return gameProfile;
}

public byte[] getProfileBytes(@NotNull final PlayerObject playerObject, final boolean fromProperties) throws IOException {
return playerObject.isBedrock() ? this.getBedrockProfileBytes(playerObject) : this.getJavaProfileBytes(playerObject, fromProperties);
public byte[] getProfileBytes(@NotNull final PlayerObject playerObject, @Nullable Property property) throws IOException {
return playerObject.isBedrock() ? this.getBedrockProfileBytes(playerObject, property) : this.getJavaProfileBytes(playerObject, property);
}

public byte[] getJavaProfileBytes(@NotNull final PlayerObject playerObject, final boolean fromProperties) throws IOException {
return fromProperties ? new ByteArrayInputStream(this.createJsonFromProperty(playerObject).getAsJsonObject().toString().getBytes()).readAllBytes() : new Request(String.format("https://sessionserver.mojang.com/session/minecraft/profile/%s?unsigned=false", playerObject.playerUUID().toString().replaceAll("-", ""))).getRequest().finalizeRequest().getBytes();
public byte[] getJavaProfileBytes(@NotNull final PlayerObject playerObject, @Nullable Property property) throws IOException {
return property != null ?
new ByteArrayInputStream(this.createJsonFromProperty(playerObject, property)
.getAsJsonObject().toString().getBytes()).readAllBytes() :
new Request()
.openConnection(
String.format(
"https://sessionserver.mojang.com/session/minecraft/profile/%s?unsigned=false",
(SkinOverlay.getInstance().isOnlineMode() ?
playerObject.playerUUID() :
getUUID(playerObject.playerName()))
.toString().replaceAll("-", "")))
.getRequest()
.finalizeRequest()
.getBytes();
}

public byte[] getBedrockProfileBytes(@NotNull final PlayerObject playerObject) throws IOException {
final JsonObject jsonObject = this.createJsonForBedrock(playerObject).getAsJsonObject();
final InputStream is = new ByteArrayInputStream(jsonObject.toString().getBytes());
try {
return is.readAllBytes();
} catch (Exception e) {
return null;
}
public byte[] getBedrockProfileBytes(@NotNull final PlayerObject playerObject, final Property property) throws IOException {
return property != null ?
new ByteArrayInputStream(this.createJsonFromProperty(playerObject, property).getAsJsonObject().toString().getBytes()).readAllBytes() :
new ByteArrayInputStream(this.createJsonForBedrock(playerObject).getAsJsonObject().toString().getBytes()).readAllBytes();
}

public JsonObject createJsonForBedrock(@NotNull final PlayerObject playerObject) throws IOException {
final byte[] profileBytes = new Request(String.format("https://api.geysermc.org/v2/skin/%s", this.getXUID(playerObject))).getRequest().finalizeRequest().getBytes();
final byte[] profileBytes = new Request().openConnection(String.format("https://api.geysermc.org/v2/skin/%s", this.getXUID(playerObject))).getRequest().finalizeRequest().getBytes();
final JsonElement json = JsonParser.parseString(new String(profileBytes));
final JsonElement value = json.getAsJsonObject().get("value");
final JsonElement signature = json.getAsJsonObject().get("signature");
Expand All @@ -74,8 +80,9 @@ public JsonObject createJsonForBedrock(@NotNull final PlayerObject playerObject)
return jsonObject;
}

public JsonObject createJsonFromProperty(@NotNull final PlayerObject playerObject) throws IOException {
final Property property = this.getGameProfile(playerObject).getProperties().get("textures").iterator().next();
public JsonObject createJsonFromProperty(@NotNull final PlayerObject playerObject, @Nullable Property property) throws IOException {
if (property == null)
property = this.getGameProfile(playerObject).getProperties().get("textures").iterator().next();
final JsonArray properties = new JsonArray();
final JsonObject innerProperties = new JsonObject();
innerProperties.add("name", new JsonPrimitive("textures"));
Expand All @@ -88,24 +95,40 @@ public JsonObject createJsonFromProperty(@NotNull final PlayerObject playerObjec
}

public String getXUID(@NotNull final PlayerObject playerObject) throws IOException {
Request request = new Request(String.format("https://api.geysermc.org/v2/xbox/xuid/%s", playerObject.playerName().replace(".", ""))).getRequest().finalizeRequest();
Request request = new Request().openConnection(String.format("https://api.geysermc.org/v2/xbox/xuid/%s", playerObject.playerName().replace(".", ""))).getRequest().finalizeRequest();
final int httpCode = request.getHttpCode();
if (httpCode != 200) {
request = new Request(String.format("https://api.geysermc.org/v2/xbox/xuid/%s", playerObject.playerName().replace(".", "").replace("_", "%20"))).getRequest().finalizeRequest();
request = new Request()
.openConnection(String.format("https://api.geysermc.org/v2/xbox/xuid/%s", playerObject.playerName().replace(".", "").replace("_", "%20")))
.getRequest()
.finalizeRequest();
}
final byte[] profileBytes = request.getBytes();
final JsonElement json = JsonParser.parseString(new String(profileBytes));
return json.getAsJsonObject().get("xuid").getAsString();
}

public UUID getUUID(final String playerName) throws IOException {
Request request = new Request().openConnection(String.format("https://api.minetools.eu/uuid/%s", playerName)).getRequest().finalizeRequest();
if (request.getHttpCode() != 200) {
request = new Request().openConnection(String.format("https://api.mojang.com/users/profiles/minecraft/%s", playerName)).getRequest().finalizeRequest();
}

final byte[] jsonBytes = request.getBytes();
final JsonElement json = JsonParser.parseString(new String(jsonBytes));
return UUID.fromString(json.getAsJsonObject().get("id").getAsString().replaceAll(
"(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})",
"$1-$2-$3-$4-$5"));
}

public Property getXUIDSkin(final String xuid) throws IOException {
final Request profileBytes = new Request(String.format("https://api.geysermc.org/v2/skin/%s", xuid)).getRequest().finalizeRequest();
final Request profileBytes = new Request().openConnection(String.format("https://api.geysermc.org/v2/skin/%s", xuid)).getRequest().finalizeRequest();
final JsonElement json = JsonParser.parseString(new String(profileBytes.getBytes()));
return new Property("textures", json.getAsJsonObject().get("value").getAsString(), json.getAsJsonObject().get("signature").getAsString());
}

public Property getJavaSkin(final PlayerObject playerObject) throws IOException {
final JsonElement json = JsonParser.parseString(new String(this.getProfileBytes(playerObject, false)));
final JsonElement json = JsonParser.parseString(new String(this.getProfileBytes(playerObject, null)));
final JsonArray properties = json.getAsJsonObject().get("properties").getAsJsonArray();
Property property = null;
for (final JsonElement object : properties) {
Expand All @@ -128,7 +151,7 @@ public void updateSkin(@NotNull FileConfiguration fileConfiguration, @NotNull Pl

@Override
public void updateSkin(@NotNull FileConfiguration fileConfiguration, @NotNull PlayerObject playerObject, boolean reset, @NotNull String skinName, Property property) {
SkinOverlay.getInstance().getLogger().log(Level.WARNING, "[SkinHandler]: updateSkin(); Unsupported Minecraft Version");
updateSkin(fileConfiguration, playerObject, reset, skinName);
}

@Override
Expand All @@ -140,78 +163,4 @@ protected <T> GameProfile getGameProfile0(@NotNull PlayerObject playerObject) th
return gameProfile;
}
}

public static class Request {
private final String address;
private byte[] bytes;
private int httpCode;
private final HttpsURLConnection httpsURLConnection;

public Request(final String address) throws IOException {
this.address = address;
final URL url = new URL(address);
this.httpsURLConnection = (HttpsURLConnection) url.openConnection();
}

public String getAddress() {
return this.address;
}

public Request getRequest() throws ProtocolException {
this.httpsURLConnection.setRequestMethod("GET");
this.httpsURLConnection.setRequestProperty("User-Agent", "SkinOverlay");
return this;
}

public Request postRequest() throws ProtocolException {
this.httpsURLConnection.setRequestMethod("POST");
this.httpsURLConnection.setRequestProperty("Connection", "Keep-Alive");
this.httpsURLConnection.setRequestProperty("Cache-Control", "no-cache");
this.httpsURLConnection.setRequestProperty("User-Agent", "SkinOverlay");
this.httpsURLConnection.setDoOutput(true);
return this;
}

@SafeVarargs
@Contract("_ -> this")
public final Request setProperty(final ObjectMap.Pair<String, String>... pairs) {
for (final ObjectMap.Pair<String, String> pair : pairs) {
this.httpsURLConnection.setRequestProperty(pair.key(), pair.value());
}
return this;
}

public Request writeToOutputStream(final String... data) throws IOException {
for (final String str : data) {
this.httpsURLConnection.getOutputStream().write(str.getBytes());
}
return this;
}

public Request writeToOutputStream(final byte[]... data) throws IOException {
for (final byte[] bytes : data) {
this.httpsURLConnection.getOutputStream().write(bytes);
}
return this;
}

public Request closeOutputStream() throws IOException {
this.httpsURLConnection.getOutputStream().close();
return this;
}

public Request finalizeRequest() throws IOException {
this.httpCode = this.httpsURLConnection.getResponseCode();
this.bytes = this.httpsURLConnection.getInputStream().readAllBytes();
return this;
}

public int getHttpCode() {
return this.httpCode;
}

public byte[] getBytes() {
return this.bytes;
}
}
}

0 comments on commit 6fc4532

Please sign in to comment.