From 3f7f1864a6795b411838b0a1a7892282918693e4 Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 8 Apr 2024 05:22:54 +0100 Subject: [PATCH] more cleanup --- .../controller/PlayerController.java | 8 +- .../types/MojangSessionServerProfile.java | 42 -------- .../MojangSessionServerProfileProperties.java | 12 --- .../cc/fascinated/player/impl/Player.java | 65 ----------- .../mojang/MojangAPIService.java | 20 ++-- .../service/mojang/types/MojangProfile.java | 101 ++++++++++++++++++ .../mojang/types/MojangUsernameToUuid.java} | 17 +-- .../{ => service}/player/PlayerService.java | 25 +++-- .../{ => service}/player/impl/Cape.java | 2 +- .../service/player/impl/Player.java | 50 +++++++++ .../{ => service}/player/impl/Skin.java | 2 +- .../{ => service}/player/impl/SkinPart.java | 2 +- src/main/java/cc/fascinated/util/Tuple.java | 18 ++++ .../java/cc/fascinated/util/UUIDUtils.java | 3 + .../java/cc/fascinated/util/WebRequest.java | 2 + target/classes/templates/index.html | 3 +- 16 files changed, 218 insertions(+), 154 deletions(-) delete mode 100644 src/main/java/cc/fascinated/mojang/types/MojangSessionServerProfile.java delete mode 100644 src/main/java/cc/fascinated/mojang/types/MojangSessionServerProfileProperties.java delete mode 100644 src/main/java/cc/fascinated/player/impl/Player.java rename src/main/java/cc/fascinated/{ => service}/mojang/MojangAPIService.java (57%) create mode 100644 src/main/java/cc/fascinated/service/mojang/types/MojangProfile.java rename src/main/java/cc/fascinated/{mojang/types/MojangApiProfile.java => service/mojang/types/MojangUsernameToUuid.java} (52%) rename src/main/java/cc/fascinated/{ => service}/player/PlayerService.java (68%) rename src/main/java/cc/fascinated/{ => service}/player/impl/Cape.java (80%) create mode 100644 src/main/java/cc/fascinated/service/player/impl/Player.java rename src/main/java/cc/fascinated/{ => service}/player/impl/Skin.java (98%) rename src/main/java/cc/fascinated/{ => service}/player/impl/SkinPart.java (97%) create mode 100644 src/main/java/cc/fascinated/util/Tuple.java diff --git a/src/main/java/cc/fascinated/controller/PlayerController.java b/src/main/java/cc/fascinated/controller/PlayerController.java index 0cb831e..9d38114 100644 --- a/src/main/java/cc/fascinated/controller/PlayerController.java +++ b/src/main/java/cc/fascinated/controller/PlayerController.java @@ -1,9 +1,9 @@ package cc.fascinated.controller; -import cc.fascinated.player.PlayerService; -import cc.fascinated.player.impl.Player; -import cc.fascinated.player.impl.Skin; -import cc.fascinated.player.impl.SkinPart; +import cc.fascinated.service.player.PlayerService; +import cc.fascinated.service.player.impl.Player; +import cc.fascinated.service.player.impl.Skin; +import cc.fascinated.service.player.impl.SkinPart; import lombok.NonNull; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.CacheControl; diff --git a/src/main/java/cc/fascinated/mojang/types/MojangSessionServerProfile.java b/src/main/java/cc/fascinated/mojang/types/MojangSessionServerProfile.java deleted file mode 100644 index a153813..0000000 --- a/src/main/java/cc/fascinated/mojang/types/MojangSessionServerProfile.java +++ /dev/null @@ -1,42 +0,0 @@ -package cc.fascinated.mojang.types; - -import lombok.Getter; -import lombok.ToString; - -import java.util.ArrayList; -import java.util.List; - -@Getter @ToString -public class MojangSessionServerProfile { - - /** - * The UUID of the player. - */ - private String id; - - /** - * The name of the player. - */ - private String name; - - /** - * The properties for the player. - */ - private final List properties = new ArrayList<>(); - - public MojangSessionServerProfile() {} - - /** - * Get the texture property for the player. - * - * @return the texture property - */ - public MojangSessionServerProfileProperties getTextureProperty() { - for (MojangSessionServerProfileProperties property : properties) { - if (property.getName().equals("textures")) { - return property; - } - } - return null; - } -} diff --git a/src/main/java/cc/fascinated/mojang/types/MojangSessionServerProfileProperties.java b/src/main/java/cc/fascinated/mojang/types/MojangSessionServerProfileProperties.java deleted file mode 100644 index 6df0197..0000000 --- a/src/main/java/cc/fascinated/mojang/types/MojangSessionServerProfileProperties.java +++ /dev/null @@ -1,12 +0,0 @@ -package cc.fascinated.mojang.types; - -import lombok.Getter; -import lombok.ToString; - -@Getter @ToString -public class MojangSessionServerProfileProperties { - private String name; - private String value; - - public MojangSessionServerProfileProperties() {} -} diff --git a/src/main/java/cc/fascinated/player/impl/Player.java b/src/main/java/cc/fascinated/player/impl/Player.java deleted file mode 100644 index 153d5df..0000000 --- a/src/main/java/cc/fascinated/player/impl/Player.java +++ /dev/null @@ -1,65 +0,0 @@ -package cc.fascinated.player.impl; - -import cc.fascinated.Main; -import cc.fascinated.config.Config; -import cc.fascinated.mojang.types.MojangSessionServerProfile; -import cc.fascinated.mojang.types.MojangSessionServerProfileProperties; -import cc.fascinated.util.UUIDUtils; -import com.google.gson.JsonObject; -import lombok.Getter; - -import java.util.UUID; - -@Getter -public class Player { - - /** - * The UUID of the player - */ - private final UUID uuid; - - /** - * The name of the player - */ - private final String name; - - /** - * The skin of the player - *

- * This will be null if the player does not have a skin. - *

- */ - 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(); - - MojangSessionServerProfileProperties textureProperty = profile.getTextureProperty(); - if (textureProperty == null) { - return; - } - - // Decode the texture property - String decoded = new String(java.util.Base64.getDecoder().decode(textureProperty.getValue())); - - // Parse the decoded JSON - JsonObject json = Main.getGSON().fromJson(decoded, JsonObject.class); - JsonObject texturesJson = json.getAsJsonObject("textures"); - JsonObject skinJson = texturesJson.getAsJsonObject("SKIN"); - JsonObject capeJson = texturesJson.getAsJsonObject("CAPE"); - JsonObject metadataJson = skinJson.get("metadata").getAsJsonObject(); - - this.skin = new Skin(this.uuid.toString(), skinJson.get("url").getAsString(), - Skin.SkinType.valueOf(metadataJson.get("model").getAsString().toUpperCase())); - this.cape = new Cape(capeJson.get("url").getAsString()); - } -} diff --git a/src/main/java/cc/fascinated/mojang/MojangAPIService.java b/src/main/java/cc/fascinated/service/mojang/MojangAPIService.java similarity index 57% rename from src/main/java/cc/fascinated/mojang/MojangAPIService.java rename to src/main/java/cc/fascinated/service/mojang/MojangAPIService.java index 50da6b7..cc27717 100644 --- a/src/main/java/cc/fascinated/mojang/MojangAPIService.java +++ b/src/main/java/cc/fascinated/service/mojang/MojangAPIService.java @@ -1,7 +1,7 @@ -package cc.fascinated.mojang; +package cc.fascinated.service.mojang; -import cc.fascinated.mojang.types.MojangApiProfile; -import cc.fascinated.mojang.types.MojangSessionServerProfile; +import cc.fascinated.service.mojang.types.MojangProfile; +import cc.fascinated.service.mojang.types.MojangUsernameToUuid; import cc.fascinated.util.WebRequest; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Value; @@ -17,22 +17,24 @@ public class MojangAPIService { private String mojangApiUrl; /** - * Gets the Session Server profile of the player with the given UUID. + * Gets the Session Server profile of the + * player with the given UUID. * * @param id the uuid or name of the player * @return the profile */ - public MojangSessionServerProfile getSessionServerProfile(String id) { - return WebRequest.get(mojangSessionServerUrl + "/session/minecraft/profile/" + id, MojangSessionServerProfile.class); + public MojangProfile getProfile(String id) { + return WebRequest.get(mojangSessionServerUrl + "/session/minecraft/profile/" + id, MojangProfile.class); } /** - * Gets the Mojang API profile of the player with the given UUID. + * Gets the UUID of the player using + * the name of the player. * * @param id the name of the player * @return the profile */ - public MojangApiProfile getApiProfile(String id) { - return WebRequest.get(mojangApiUrl + "/users/profiles/minecraft/" + id, MojangApiProfile.class); + public MojangUsernameToUuid getUuidFromUsername(String id) { + return WebRequest.get(mojangApiUrl + "/users/profiles/minecraft/" + id, MojangUsernameToUuid.class); } } diff --git a/src/main/java/cc/fascinated/service/mojang/types/MojangProfile.java b/src/main/java/cc/fascinated/service/mojang/types/MojangProfile.java new file mode 100644 index 0000000..646b55a --- /dev/null +++ b/src/main/java/cc/fascinated/service/mojang/types/MojangProfile.java @@ -0,0 +1,101 @@ +package cc.fascinated.service.mojang.types; + +import cc.fascinated.Main; +import cc.fascinated.service.player.impl.Cape; +import cc.fascinated.service.player.impl.Skin; +import cc.fascinated.util.Tuple; +import com.google.gson.JsonObject; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.ArrayList; +import java.util.List; + +@Getter @NoArgsConstructor +public class MojangProfile { + + /** + * The UUID of the player. + */ + private String id; + + /** + * The name of the player. + */ + private String name; + + /** + * The properties of the player. + */ + private final List properties = new ArrayList<>(); + + /** + * Get the skin and cape of the player. + * + * @return the skin and cape of the player + */ + public Tuple getSkinAndCape() { + ProfileProperty textureProperty = getTextureProperty(); + if (textureProperty == null) { + return null; + } + + // Decode the texture property + String decoded = new String(java.util.Base64.getDecoder().decode(textureProperty.getValue())); + + // Parse the decoded JSON + JsonObject json = Main.getGSON().fromJson(decoded, JsonObject.class); + JsonObject texturesJson = json.getAsJsonObject("textures"); + JsonObject skinJson = texturesJson.getAsJsonObject("SKIN"); + JsonObject capeJson = texturesJson.getAsJsonObject("CAPE"); + JsonObject metadataJson = skinJson.get("metadata").getAsJsonObject(); + + Skin skin = new Skin(id, skinJson.get("url").getAsString(), + Skin.SkinType.valueOf(metadataJson.get("model").getAsString().toUpperCase())); + Cape cape = new Cape(capeJson.get("url").getAsString()); + + return new Tuple<>(skin, cape); + } + + /** + * Get the texture property of the player. + * + * @return the texture property + */ + public ProfileProperty getTextureProperty() { + for (ProfileProperty property : properties) { + if (property.getName().equals("textures")) { + return property; + } + } + return null; + } + + @Getter @AllArgsConstructor + public static class ProfileProperty { + /** + * The name of the property. + */ + private String name; + + /** + * The base64 value of the property. + */ + private String value; + + /** + * The signature of the property. + */ + private String signature; + + /** + * Check if the property is signed. + * + * @return true if the property is signed, false otherwise + */ + public boolean isSigned() { + return signature != null; + } + } +} diff --git a/src/main/java/cc/fascinated/mojang/types/MojangApiProfile.java b/src/main/java/cc/fascinated/service/mojang/types/MojangUsernameToUuid.java similarity index 52% rename from src/main/java/cc/fascinated/mojang/types/MojangApiProfile.java rename to src/main/java/cc/fascinated/service/mojang/types/MojangUsernameToUuid.java index 89656f0..8496e37 100644 --- a/src/main/java/cc/fascinated/mojang/types/MojangApiProfile.java +++ b/src/main/java/cc/fascinated/service/mojang/types/MojangUsernameToUuid.java @@ -1,15 +1,20 @@ -package cc.fascinated.mojang.types; +package cc.fascinated.service.mojang.types; import lombok.Getter; -import lombok.ToString; +import lombok.NoArgsConstructor; -@Getter @ToString -public class MojangApiProfile { +@Getter @NoArgsConstructor +public class MojangUsernameToUuid { + /** + * The UUID of the player. + */ private String id; - private String name; - public MojangApiProfile() {} + /** + * The name of the player. + */ + private String name; /** * Check if the profile is valid. diff --git a/src/main/java/cc/fascinated/player/PlayerService.java b/src/main/java/cc/fascinated/service/player/PlayerService.java similarity index 68% rename from src/main/java/cc/fascinated/player/PlayerService.java rename to src/main/java/cc/fascinated/service/player/PlayerService.java index 7f3c312..fb099e2 100644 --- a/src/main/java/cc/fascinated/player/PlayerService.java +++ b/src/main/java/cc/fascinated/service/player/PlayerService.java @@ -1,9 +1,9 @@ -package cc.fascinated.player; +package cc.fascinated.service.player; -import cc.fascinated.mojang.MojangAPIService; -import cc.fascinated.mojang.types.MojangApiProfile; -import cc.fascinated.mojang.types.MojangSessionServerProfile; -import cc.fascinated.player.impl.Player; +import cc.fascinated.service.mojang.MojangAPIService; +import cc.fascinated.service.mojang.types.MojangProfile; +import cc.fascinated.service.mojang.types.MojangUsernameToUuid; +import cc.fascinated.service.player.impl.Player; import cc.fascinated.util.UUIDUtils; import lombok.extern.log4j.Log4j2; import net.jodah.expiringmap.ExpirationPolicy; @@ -47,25 +47,28 @@ public class PlayerService { */ public Player getPlayer(String id) { UUID uuid = null; - if (id.length() == 32 || id.length() == 36) { + if (id.length() == 32 || id.length() == 36) { // Check if the id is a UUID try { uuid = UUID.fromString(id.length() == 32 ? UUIDUtils.addUUIDDashes(id) : id); } catch (Exception ignored) {} - } else { + } else { // Check if the id is a name uuid = playerNameToUUIDCache.get(id.toUpperCase()); } + // Check if the player is cached if (uuid != null && players.containsKey(uuid)) { return players.get(uuid); } - MojangSessionServerProfile profile = uuid == null ? null : mojangAPIService.getSessionServerProfile(uuid.toString()); - if (profile == null) { - MojangApiProfile apiProfile = mojangAPIService.getApiProfile(id); + MojangProfile profile = uuid == null ? null : mojangAPIService.getProfile(uuid.toString()); + if (profile == null) { // The player cannot be found using their UUID + MojangUsernameToUuid apiProfile = mojangAPIService.getUuidFromUsername(id); // Get the UUID of the player using their name if (apiProfile == null || !apiProfile.isValid()) { return null; } - profile = mojangAPIService.getSessionServerProfile(apiProfile.getId().length() == 32 ? UUIDUtils.addUUIDDashes(apiProfile.getId()) : apiProfile.getId()); + // Get the profile of the player using their UUID + profile = mojangAPIService.getProfile(apiProfile.getId().length() == 32 ? + UUIDUtils.addUUIDDashes(apiProfile.getId()) : apiProfile.getId()); } if (profile == null) { // The player cannot be found using their name or UUID log.info("Player with id {} could not be found", id); diff --git a/src/main/java/cc/fascinated/player/impl/Cape.java b/src/main/java/cc/fascinated/service/player/impl/Cape.java similarity index 80% rename from src/main/java/cc/fascinated/player/impl/Cape.java rename to src/main/java/cc/fascinated/service/player/impl/Cape.java index 92eed7c..9262eed 100644 --- a/src/main/java/cc/fascinated/player/impl/Cape.java +++ b/src/main/java/cc/fascinated/service/player/impl/Cape.java @@ -1,4 +1,4 @@ -package cc.fascinated.player.impl; +package cc.fascinated.service.player.impl; import lombok.AllArgsConstructor; import lombok.Getter; diff --git a/src/main/java/cc/fascinated/service/player/impl/Player.java b/src/main/java/cc/fascinated/service/player/impl/Player.java new file mode 100644 index 0000000..5b88820 --- /dev/null +++ b/src/main/java/cc/fascinated/service/player/impl/Player.java @@ -0,0 +1,50 @@ +package cc.fascinated.service.player.impl; + +import cc.fascinated.service.mojang.types.MojangProfile; +import cc.fascinated.util.Tuple; +import cc.fascinated.util.UUIDUtils; +import lombok.Getter; + +import java.util.UUID; + +@Getter +public class Player { + + /** + * The UUID of the player + */ + private final UUID uuid; + + /** + * The name of the player + */ + private final String name; + + /** + * The skin of the player + *

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

+ */ + 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(MojangProfile profile) { + this.uuid = UUID.fromString(UUIDUtils.addUUIDDashes(profile.getId())); + this.name = profile.getName(); + + // Get the skin and cape + Tuple skinAndCape = profile.getSkinAndCape(); + if (skinAndCape != null) { + this.skin = skinAndCape.getLeft(); + this.cape = skinAndCape.getRight(); + } + } +} diff --git a/src/main/java/cc/fascinated/player/impl/Skin.java b/src/main/java/cc/fascinated/service/player/impl/Skin.java similarity index 98% rename from src/main/java/cc/fascinated/player/impl/Skin.java rename to src/main/java/cc/fascinated/service/player/impl/Skin.java index 58eb331..8d4a306 100644 --- a/src/main/java/cc/fascinated/player/impl/Skin.java +++ b/src/main/java/cc/fascinated/service/player/impl/Skin.java @@ -1,4 +1,4 @@ -package cc.fascinated.player.impl; +package cc.fascinated.service.player.impl; import cc.fascinated.Main; import cc.fascinated.config.Config; diff --git a/src/main/java/cc/fascinated/player/impl/SkinPart.java b/src/main/java/cc/fascinated/service/player/impl/SkinPart.java similarity index 97% rename from src/main/java/cc/fascinated/player/impl/SkinPart.java rename to src/main/java/cc/fascinated/service/player/impl/SkinPart.java index 78e6a92..e882dc1 100644 --- a/src/main/java/cc/fascinated/player/impl/SkinPart.java +++ b/src/main/java/cc/fascinated/service/player/impl/SkinPart.java @@ -1,4 +1,4 @@ -package cc.fascinated.player.impl; +package cc.fascinated.service.player.impl; import lombok.Getter; import lombok.extern.log4j.Log4j2; diff --git a/src/main/java/cc/fascinated/util/Tuple.java b/src/main/java/cc/fascinated/util/Tuple.java new file mode 100644 index 0000000..cfdd669 --- /dev/null +++ b/src/main/java/cc/fascinated/util/Tuple.java @@ -0,0 +1,18 @@ +package cc.fascinated.util; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter @AllArgsConstructor +public class Tuple { + + /** + * The left value of the tuple. + */ + private final L left; + + /** + * The right value of the tuple. + */ + private final R right; +} diff --git a/src/main/java/cc/fascinated/util/UUIDUtils.java b/src/main/java/cc/fascinated/util/UUIDUtils.java index d8aed39..5d42eac 100644 --- a/src/main/java/cc/fascinated/util/UUIDUtils.java +++ b/src/main/java/cc/fascinated/util/UUIDUtils.java @@ -1,5 +1,8 @@ package cc.fascinated.util; +import lombok.experimental.UtilityClass; + +@UtilityClass public class UUIDUtils { /** diff --git a/src/main/java/cc/fascinated/util/WebRequest.java b/src/main/java/cc/fascinated/util/WebRequest.java index fb98de4..5d3151c 100644 --- a/src/main/java/cc/fascinated/util/WebRequest.java +++ b/src/main/java/cc/fascinated/util/WebRequest.java @@ -1,10 +1,12 @@ package cc.fascinated.util; +import lombok.experimental.UtilityClass; import org.springframework.http.ResponseEntity; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.RestClient; +@UtilityClass public class WebRequest { /** diff --git a/target/classes/templates/index.html b/target/classes/templates/index.html index c338ca2..0779cab 100644 --- a/target/classes/templates/index.html +++ b/target/classes/templates/index.html @@ -19,8 +19,7 @@

Wrapper for the Minecraft APIs to make them easier to use.

-

Player Data: ???

-

Avatar Url: ???

+

Player Data: ???

\ No newline at end of file