From 151c942c60a31642a797c161ca3a5af8719cf1bb Mon Sep 17 00:00:00 2001 From: Rainnny7 Date: Sun, 7 Apr 2024 00:15:53 -0400 Subject: [PATCH] Cleanup --- .../mc/controller/PlayerController.java | 31 +------------ .../me/braydon/mc/service/MojangService.java | 45 +++++++++++++++++-- 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/src/main/java/me/braydon/mc/controller/PlayerController.java b/src/main/java/me/braydon/mc/controller/PlayerController.java index a02af13..b043acf 100644 --- a/src/main/java/me/braydon/mc/controller/PlayerController.java +++ b/src/main/java/me/braydon/mc/controller/PlayerController.java @@ -2,12 +2,9 @@ package me.braydon.mc.controller; import lombok.NonNull; import lombok.extern.log4j.Log4j2; -import me.braydon.mc.common.EnumUtils; -import me.braydon.mc.common.PlayerUtils; import me.braydon.mc.exception.impl.BadRequestException; import me.braydon.mc.exception.impl.ResourceNotFoundException; import me.braydon.mc.model.Player; -import me.braydon.mc.model.Skin; import me.braydon.mc.model.cache.CachedPlayer; import me.braydon.mc.service.MojangService; import org.springframework.beans.factory.annotation.Autowired; @@ -25,9 +22,6 @@ import org.springframework.web.bind.annotation.*; @RequestMapping(value = "/player", produces = MediaType.APPLICATION_JSON_VALUE) @Log4j2(topic = "Player Controller") public final class PlayerController { - private static final int DEFAULT_PART_TEXTURE_SIZE = 128; - private static final int MAX_PART_TEXTURE_SIZE = 512; - /** * The Mojang service to use for player information. */ @@ -73,31 +67,8 @@ public final class PlayerController { public ResponseEntity getPartTexture(@PathVariable @NonNull String partName, @PathVariable @NonNull String query, @PathVariable @NonNull String extension, @RequestParam(required = false) Integer size ) throws BadRequestException { - Skin.Part part = EnumUtils.getEnumConstant(Skin.Part.class, partName.toUpperCase()); // The skin part to get - if (part == null) { // Default to the head part - part = Skin.Part.HEAD; - } - if ((extension = extension.trim()).isBlank()) { // Invalid extension - throw new BadRequestException("Invalid extension"); - } - if (size == null || size <= 0) { // Invalid size - size = DEFAULT_PART_TEXTURE_SIZE; - } - size = Math.min(size, MAX_PART_TEXTURE_SIZE); // Limit the size to 512 - - Skin target = null; // The target skin to get the skin part of - try { - CachedPlayer player = mojangService.getPlayer(query, false); // Retrieve the player - target = player.getSkin(); // Use the player's skin - } catch (Exception ignored) { - // Simply ignore, and fallback to the default skin - } - if (target == null) { // Fallback to the default skin - target = Skin.DEFAULT_STEVE; - } - // Return the skin part texture return ResponseEntity.ok() .contentType(extension.equalsIgnoreCase("png") ? MediaType.IMAGE_PNG : MediaType.IMAGE_JPEG) - .body(PlayerUtils.getSkinPart(target, part, size)); + .body(mojangService.getSkinPartTexture(partName, query, extension, size)); } } \ No newline at end of file diff --git a/src/main/java/me/braydon/mc/service/MojangService.java b/src/main/java/me/braydon/mc/service/MojangService.java index b33b162..f5fe245 100644 --- a/src/main/java/me/braydon/mc/service/MojangService.java +++ b/src/main/java/me/braydon/mc/service/MojangService.java @@ -2,10 +2,7 @@ package me.braydon.mc.service; import lombok.NonNull; import lombok.extern.log4j.Log4j2; -import me.braydon.mc.common.DNSUtils; -import me.braydon.mc.common.EnumUtils; -import me.braydon.mc.common.Tuple; -import me.braydon.mc.common.UUIDUtils; +import me.braydon.mc.common.*; import me.braydon.mc.common.web.JsonWebException; import me.braydon.mc.common.web.JsonWebRequest; import me.braydon.mc.exception.impl.BadRequestException; @@ -41,6 +38,9 @@ public final class MojangService { private static final String UUID_TO_PROFILE = SESSION_SERVER_ENDPOINT + "/session/minecraft/profile/%s"; private static final String USERNAME_TO_UUID = API_ENDPOINT + "/users/profiles/minecraft/%s"; + private static final int DEFAULT_PART_TEXTURE_SIZE = 128; + private static final int MAX_PART_TEXTURE_SIZE = 512; + /** * The cache repository for {@link Player}'s by their username. */ @@ -140,6 +140,43 @@ public final class MojangService { } } + /** + * Get the part of a skin texture for + * a player by their username or UUID. + * + * @param partName the part of the player's skin texture to get + * @param query the query to search for the player by + * @param extension the skin part image extension + * @param size the size of the skin part image + * @return the skin part texture + * @throws BadRequestException if the extension is invalid + */ + public byte[] getSkinPartTexture(@NonNull String partName, @NonNull String query, @NonNull String extension, Integer size) { + Skin.Part part = EnumUtils.getEnumConstant(Skin.Part.class, partName.toUpperCase()); // The skin part to get + if (part == null) { // Default to the head part + part = Skin.Part.HEAD; + } + if (extension.isBlank()) { // Invalid extension + throw new BadRequestException("Invalid extension"); + } + if (size == null || size <= 0) { // Invalid size + size = DEFAULT_PART_TEXTURE_SIZE; + } + size = Math.min(size, MAX_PART_TEXTURE_SIZE); // Limit the size to 512 + + Skin target = null; // The target skin to get the skin part of + try { + CachedPlayer player = getPlayer(query, false); // Retrieve the player + target = player.getSkin(); // Use the player's skin + } catch (Exception ignored) { + // Simply ignore, and fallback to the default skin + } + if (target == null) { // Fallback to the default skin + target = Skin.DEFAULT_STEVE; + } + return PlayerUtils.getSkinPart(target, part, size); + } + /** * Resolve a Minecraft server on the given * platform with the given hostname.