This commit is contained in:
Braydon 2024-04-07 00:15:53 -04:00
parent add07cb84c
commit 151c942c60
2 changed files with 42 additions and 34 deletions

@ -2,12 +2,9 @@ package me.braydon.mc.controller;
import lombok.NonNull; import lombok.NonNull;
import lombok.extern.log4j.Log4j2; 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.BadRequestException;
import me.braydon.mc.exception.impl.ResourceNotFoundException; import me.braydon.mc.exception.impl.ResourceNotFoundException;
import me.braydon.mc.model.Player; import me.braydon.mc.model.Player;
import me.braydon.mc.model.Skin;
import me.braydon.mc.model.cache.CachedPlayer; import me.braydon.mc.model.cache.CachedPlayer;
import me.braydon.mc.service.MojangService; import me.braydon.mc.service.MojangService;
import org.springframework.beans.factory.annotation.Autowired; 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) @RequestMapping(value = "/player", produces = MediaType.APPLICATION_JSON_VALUE)
@Log4j2(topic = "Player Controller") @Log4j2(topic = "Player Controller")
public final class PlayerController { 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. * The Mojang service to use for player information.
*/ */
@ -73,31 +67,8 @@ public final class PlayerController {
public ResponseEntity<byte[]> getPartTexture(@PathVariable @NonNull String partName, @PathVariable @NonNull String query, public ResponseEntity<byte[]> getPartTexture(@PathVariable @NonNull String partName, @PathVariable @NonNull String query,
@PathVariable @NonNull String extension, @RequestParam(required = false) Integer size @PathVariable @NonNull String extension, @RequestParam(required = false) Integer size
) throws BadRequestException { ) 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() return ResponseEntity.ok()
.contentType(extension.equalsIgnoreCase("png") ? MediaType.IMAGE_PNG : MediaType.IMAGE_JPEG) .contentType(extension.equalsIgnoreCase("png") ? MediaType.IMAGE_PNG : MediaType.IMAGE_JPEG)
.body(PlayerUtils.getSkinPart(target, part, size)); .body(mojangService.getSkinPartTexture(partName, query, extension, size));
} }
} }

@ -2,10 +2,7 @@ package me.braydon.mc.service;
import lombok.NonNull; import lombok.NonNull;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import me.braydon.mc.common.DNSUtils; import me.braydon.mc.common.*;
import me.braydon.mc.common.EnumUtils;
import me.braydon.mc.common.Tuple;
import me.braydon.mc.common.UUIDUtils;
import me.braydon.mc.common.web.JsonWebException; import me.braydon.mc.common.web.JsonWebException;
import me.braydon.mc.common.web.JsonWebRequest; import me.braydon.mc.common.web.JsonWebRequest;
import me.braydon.mc.exception.impl.BadRequestException; 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 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 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. * 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 * Resolve a Minecraft server on the given
* platform with the given hostname. * platform with the given hostname.