diff --git a/src/main/java/cc/fascinated/Main.java b/src/main/java/cc/fascinated/Main.java index 47d2997..5116594 100644 --- a/src/main/java/cc/fascinated/Main.java +++ b/src/main/java/cc/fascinated/Main.java @@ -3,11 +3,13 @@ package cc.fascinated; import com.google.gson.Gson; import lombok.Getter; import lombok.SneakyThrows; +import lombok.experimental.Helper; import lombok.extern.log4j.Log4j2; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import java.io.File; +import java.net.http.HttpClient; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.Objects; @@ -18,6 +20,9 @@ public class Main { @Getter private static final Gson GSON = new Gson(); + @Getter + private static final HttpClient CLIENT = HttpClient.newHttpClient(); + @SneakyThrows public static void main(String[] args) { File config = new File("application.yml"); diff --git a/src/main/java/cc/fascinated/mojang/MojangAPIService.java b/src/main/java/cc/fascinated/mojang/MojangAPIService.java index ec42b49..e20cd02 100644 --- a/src/main/java/cc/fascinated/mojang/MojangAPIService.java +++ b/src/main/java/cc/fascinated/mojang/MojangAPIService.java @@ -15,8 +15,6 @@ import java.net.http.HttpResponse; @Service public class MojangAPIService { - private static final HttpClient CLIENT = HttpClient.newHttpClient(); - @Value("${mojang.session-server}") private String mojangSessionServerUrl; @@ -36,7 +34,7 @@ public class MojangAPIService { .GET() .build(); - HttpResponse response = CLIENT.send(request, HttpResponse.BodyHandlers.ofString()); + HttpResponse response = Main.getCLIENT().send(request, HttpResponse.BodyHandlers.ofString()); return Main.getGSON().fromJson(response.body(), MojangSessionServerProfile.class); } @@ -53,7 +51,7 @@ public class MojangAPIService { .GET() .build(); - HttpResponse response = CLIENT.send(request, HttpResponse.BodyHandlers.ofString()); + HttpResponse response = Main.getCLIENT().send(request, HttpResponse.BodyHandlers.ofString()); return Main.getGSON().fromJson(response.body(), MojangApiProfile.class); } } diff --git a/src/main/java/cc/fascinated/mojang/types/MojangApiProfile.java b/src/main/java/cc/fascinated/mojang/types/MojangApiProfile.java index 7705dcb..7dceb79 100644 --- a/src/main/java/cc/fascinated/mojang/types/MojangApiProfile.java +++ b/src/main/java/cc/fascinated/mojang/types/MojangApiProfile.java @@ -1,8 +1,9 @@ package cc.fascinated.mojang.types; import lombok.Getter; +import lombok.ToString; -@Getter +@Getter @ToString public class MojangApiProfile { private String id; diff --git a/src/main/java/cc/fascinated/player/PlayerManagerService.java b/src/main/java/cc/fascinated/player/PlayerManagerService.java index 2251b4d..b92eb0f 100644 --- a/src/main/java/cc/fascinated/player/PlayerManagerService.java +++ b/src/main/java/cc/fascinated/player/PlayerManagerService.java @@ -64,6 +64,9 @@ public class PlayerManagerService { } profile = mojangAPIService.getSessionServerProfile(apiProfile.getId().length() == 32 ? UUIDUtils.addUUIDDashes(apiProfile.getId()) : apiProfile.getId()); } + if (profile == null) { // The player cannot be found using their name or UUID + return null; + } Player player = new Player(profile); players.put(player.getUuid(), player); playerNameToUUIDCache.put(player.getName().toUpperCase(), player.getUuid()); diff --git a/src/main/java/cc/fascinated/player/impl/Cape.java b/src/main/java/cc/fascinated/player/impl/Cape.java new file mode 100644 index 0000000..92eed7c --- /dev/null +++ b/src/main/java/cc/fascinated/player/impl/Cape.java @@ -0,0 +1,13 @@ +package cc.fascinated.player.impl; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter @AllArgsConstructor +public class Cape { + + /** + * The URL of the cape + */ + private final String url; +} diff --git a/src/main/java/cc/fascinated/player/impl/Player.java b/src/main/java/cc/fascinated/player/impl/Player.java index f653d59..3dd924c 100644 --- a/src/main/java/cc/fascinated/player/impl/Player.java +++ b/src/main/java/cc/fascinated/player/impl/Player.java @@ -30,6 +30,14 @@ public class Player { */ private Skin skin; + /** + * The cape of the player + *

+ * This will be null if the player does not have a cape. + *

+ */ + private Cape cape; + public Player(MojangSessionServerProfile profile) { this.uuid = UUID.fromString(UUIDUtils.addUUIDDashes(profile.getId())); this.name = profile.getName(); @@ -44,14 +52,13 @@ public class Player { // Parse the decoded JSON JsonObject json = Main.getGSON().fromJson(decoded, JsonObject.class); - JsonObject textures = json.getAsJsonObject("textures"); - JsonObject skin = textures.getAsJsonObject("SKIN"); - JsonObject metadata = skin.get("metadata").getAsJsonObject(); + JsonObject texturesJson = json.getAsJsonObject("textures"); + JsonObject skinJson = texturesJson.getAsJsonObject("SKIN"); + JsonObject capeJson = texturesJson.getAsJsonObject("CAPE"); + JsonObject metadataJson = skinJson.get("metadata").getAsJsonObject(); - String url = skin.get("url").getAsString(); - SkinType model = SkinType.fromString(metadata.get("model").getAsString()); - - this.skin = new Skin(url, model); + this.skin = new Skin(skinJson.get("url").getAsString(), SkinType.fromString(metadataJson.get("model").getAsString())); + this.cape = new Cape(capeJson.get("url").getAsString()); } }