This commit is contained in:
Lee 2024-04-12 18:56:25 +01:00
parent e788ae003f
commit 4e08955ab9
3 changed files with 11 additions and 11 deletions

@ -52,6 +52,8 @@ public class ImageUtils {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
ImageIO.write(image, "png", outputStream); ImageIO.write(image, "png", outputStream);
return outputStream.toByteArray(); return outputStream.toByteArray();
} catch (Exception e) {
throw new Exception("Failed to convert image to bytes", e);
} }
} }
} }

@ -38,7 +38,7 @@ public class Skin {
/** /**
* The legacy status of the skin * The legacy status of the skin
*/ */
private boolean isLegacy = false; private boolean legacy;
/** /**
* The skin image for the skin * The skin image for the skin
@ -60,9 +60,7 @@ public class Skin {
if (this.skinImage != null) { if (this.skinImage != null) {
try { try {
BufferedImage image = ImageIO.read(new ByteArrayInputStream(this.skinImage)); BufferedImage image = ImageIO.read(new ByteArrayInputStream(this.skinImage));
if (image.getWidth() == 64 && image.getHeight() == 32) { // Using the old skin format this.legacy = image.getWidth() == 64 && image.getHeight() == 32;
this.isLegacy = true;
}
} catch (Exception ignored) {} } catch (Exception ignored) {}
} }
} }

@ -137,26 +137,26 @@ public class PlayerService {
throw new BadRequestException("Invalid skin part: %s".formatted(partName)); throw new BadRequestException("Invalid skin part: %s".formatted(partName));
} }
log.info("Getting skin part {} for player: {}", part.name(), player.getUniqueId()); String name = part.name();
String key = "%s-%s-%s-%s".formatted(player.getUniqueId(), part.name(), size, renderOverlay); log.info("Getting skin part {} for player: {}", name, player.getUniqueId());
String key = "%s-%s-%s-%s".formatted(player.getUniqueId(), name, size, renderOverlay);
Optional<CachedPlayerSkinPart> cache = playerSkinPartCacheRepository.findById(key); Optional<CachedPlayerSkinPart> cache = playerSkinPartCacheRepository.findById(key);
// The skin part is cached // The skin part is cached
if (cache.isPresent() && Config.INSTANCE.isProduction()) { if (cache.isPresent() && Config.INSTANCE.isProduction()) {
log.info("Skin part {} for player {} is cached", part.name(), player.getUniqueId()); log.info("Skin part {} for player {} is cached", name, player.getUniqueId());
return cache.get(); return cache.get();
} }
long before = System.currentTimeMillis(); long before = System.currentTimeMillis();
BufferedImage renderedPart = part.render(player.getSkin(), renderOverlay, size); // Render the skin part BufferedImage renderedPart = part.render(player.getSkin(), renderOverlay, size); // Render the skin part
log.info("Took {}ms to render skin part {} for player: {}", System.currentTimeMillis() - before, part.name(), player.getUniqueId()); log.info("Took {}ms to render skin part {} for player: {}", System.currentTimeMillis() - before, name, player.getUniqueId());
byte[] skinPartBytes = ImageUtils.imageToBytes(renderedPart); // Convert the image to bytes
CachedPlayerSkinPart skinPart = new CachedPlayerSkinPart( CachedPlayerSkinPart skinPart = new CachedPlayerSkinPart(
key, key,
skinPartBytes ImageUtils.imageToBytes(renderedPart)
); );
log.info("Fetched skin part {} for player: {}", part.name(), player.getUniqueId()); log.info("Fetched skin part {} for player: {}", name, player.getUniqueId());
playerSkinPartCacheRepository.save(skinPart); playerSkinPartCacheRepository.save(skinPart);
return skinPart; return skinPart;