From bfbaf34b2414c5d148e55990c524e9c83372dd22 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 11 Apr 2024 07:07:49 +0100 Subject: [PATCH] fix legacy skins --- .../java/cc.fascinated/model/player/Skin.java | 58 ++++++++++++------- .../service/skin/SkinRenderer.java | 26 +++++---- .../service/skin/impl/BodyRenderer.java | 12 ++-- .../service/skin/impl/HeadRenderer.java | 2 +- .../skin/impl/IsometricHeadRenderer.java | 11 +--- 5 files changed, 62 insertions(+), 47 deletions(-) diff --git a/src/main/java/cc.fascinated/model/player/Skin.java b/src/main/java/cc.fascinated/model/player/Skin.java index 70ae2fa..96d2bf9 100644 --- a/src/main/java/cc.fascinated/model/player/Skin.java +++ b/src/main/java/cc.fascinated/model/player/Skin.java @@ -84,7 +84,7 @@ public class Skin { } String url = json.get("url").getAsString(); JsonObject metadata = json.getAsJsonObject("metadata"); - Model model = Model.fromName(metadata == null ? "slim" : // Fall back to slim if the model is not found + Model model = Model.fromName(metadata == null ? "default" : // Fall back to slim if the model is not found metadata.get("model").getAsString()); return new Skin(url, model); } @@ -148,37 +148,37 @@ public class Skin { /** * Skin postions */ - HEAD(8, 8, 8, 8, false), - HEAD_TOP(8, 0, 8, 8, false), - HEAD_FRONT(8, 8, 8, 8, false), - HEAD_RIGHT(0, 8, 8, 8, false), + HEAD(8, 8, 8, 8, null), + HEAD_TOP(8, 0, 8, 8, null), + HEAD_FRONT(8, 8, 8, 8, null), + HEAD_RIGHT(0, 8, 8, 8, null), - BODY(20, 20, 8, 12, false), - BODY_BACK(20, 36, 8, 12, false), - BODY_LEFT(32, 52, 8, 12, false), - BODY_RIGHT(44, 20, 8, 12, false), + BODY(20, 20, 8, 12, null), + BODY_BACK(20, 36, 8, 12, null), + BODY_LEFT(32, 52, 8, 12, null), + BODY_RIGHT(44, 20, 8, 12, null), - RIGHT_ARM(44, 20, 4, 12, false), - LEFT_ARM(36, 52, 4, 12, true), + RIGHT_ARM(44, 20, 4, 12, null), + LEFT_ARM(36, 52, 4, 12, new LegacySkinPosition(44, 20, true)), - RIGHT_LEG(4, 20, 4, 12, false), - LEFT_LEG(20, 52, 4, 12, true), + RIGHT_LEG(4, 20, 4, 12, null), + LEFT_LEG(20, 52, 4, 12, new LegacySkinPosition(4, 20, true)), /** * Skin overlay (layer) positions */ - HEAD_OVERLAY_TOP(40, 0, 8, 8, false), - HEAD_OVERLAY_FRONT(40, 8, 8, 8, false), - HEAD_OVERLAY_RIGHT(32, 8, 8, 8, false), - HEAD_OVERLAY_LEFT(48, 8, 8, 8, false), + HEAD_OVERLAY_TOP(40, 0, 8, 8, null), + HEAD_OVERLAY_FRONT(40, 8, 8, 8, null), + HEAD_OVERLAY_RIGHT(32, 8, 8, 8, null), + HEAD_OVERLAY_LEFT(48, 8, 8, 8, null), - BODY_OVERLAY_FRONT(20, 36, 8, 12, false), + BODY_OVERLAY_FRONT(20, 36, 8, 12, null), - RIGHT_ARM_OVERLAY(44, 36, 8, 12, false), - LEFT_ARM_OVERLAY(52, 52, 8, 12, false), + RIGHT_ARM_OVERLAY(44, 36, 8, 12, null), + LEFT_ARM_OVERLAY(52, 52, 8, 12, null), - RIGHT_LEG_OVERLAY(4, 36, 4, 12, false), - LEFT_LEG_OVERLAY(4, 52, 20, 12, false); + RIGHT_LEG_OVERLAY(4, 36, 4, 12, null), + LEFT_LEG_OVERLAY(4, 52, 20, 12, null); /** * The x, and y position of the part. @@ -190,6 +190,20 @@ public class Skin { */ private final int width, height; + /** + * The legacy skin position of the part. + */ + private final LegacySkinPosition legacySkinPosition; + } + + @AllArgsConstructor @Getter + public static class LegacySkinPosition { + + /** + * The x, and y position of the part. + */ + private final int x, y; + /* * Should the part be flipped horizontally? */ diff --git a/src/main/java/cc.fascinated/service/skin/SkinRenderer.java b/src/main/java/cc.fascinated/service/skin/SkinRenderer.java index a2fb900..160a416 100644 --- a/src/main/java/cc.fascinated/service/skin/SkinRenderer.java +++ b/src/main/java/cc.fascinated/service/skin/SkinRenderer.java @@ -45,12 +45,15 @@ public abstract class SkinRenderer { return null; } int width = skin.getModel() == Skin.Model.SLIM && position.name().contains("ARM") ? position.getWidth() - 1 : position.getWidth(); - BufferedImage part = skinImage.getSubimage(position.getX(), position.getY(), width, position.getHeight()); - if (part == null) { - return null; - } - if (position.isFlipped()) { // Flip the part horizontally - part = ImageUtils.flip(part); + BufferedImage part; + Skin.LegacySkinPosition legacySkinPosition = position.getLegacySkinPosition(); + if (legacySkinPosition != null) { + part = skinImage.getSubimage(legacySkinPosition.getX(), legacySkinPosition.getY(), width, position.getHeight()); + if (legacySkinPosition.isFlipped()) { + part = ImageUtils.flip(part); + } + } else { + part = skinImage.getSubimage(position.getX(), position.getY(), width, position.getHeight()); } return ImageUtils.resize(part, scale); } catch (Exception ex) { @@ -81,15 +84,18 @@ public abstract class SkinRenderer { /** * Applies an overlay (skin layer) to the head part. * - * @param graphics the graphics + * @param originalImage the original image * @param part the part */ - public void applyOverlay(Graphics2D graphics, BufferedImage part) { + public void applyOverlay(BufferedImage originalImage, BufferedImage part) { if (part == null) { return; } - graphics.drawImage(part, 0, 0, null); - graphics.dispose(); + try { + Graphics2D graphics = originalImage.createGraphics(); + graphics.drawImage(part, 0, 0, null); + graphics.dispose(); + } catch (Exception ignored) {} // We can safely ignore this } /** diff --git a/src/main/java/cc.fascinated/service/skin/impl/BodyRenderer.java b/src/main/java/cc.fascinated/service/skin/impl/BodyRenderer.java index fcbbd8d..305dfc4 100644 --- a/src/main/java/cc.fascinated/service/skin/impl/BodyRenderer.java +++ b/src/main/java/cc.fascinated/service/skin/impl/BodyRenderer.java @@ -32,12 +32,12 @@ public class BodyRenderer extends SkinRenderer { BufferedImage leftLeg = this.getSkinPart(skin, Skin.PartPosition.LEFT_LEG, 1); if (renderOverlay) { // Render the skin layers - applyOverlay(head.createGraphics(), this.getSkinPart(skin, Skin.PartPosition.HEAD_OVERLAY_FRONT, 1)); - applyOverlay(body.createGraphics(), this.getSkinPart(skin, Skin.PartPosition.BODY_OVERLAY_FRONT, 1)); - applyOverlay(rightArm.createGraphics(), this.getSkinPart(skin, Skin.PartPosition.RIGHT_ARM_OVERLAY, 1)); - applyOverlay(leftArm.createGraphics(), this.getSkinPart(skin, Skin.PartPosition.LEFT_ARM_OVERLAY, 1)); - applyOverlay(rightLeg.createGraphics(), this.getSkinPart(skin, Skin.PartPosition.RIGHT_LEG_OVERLAY, 1)); - applyOverlay(leftLeg.createGraphics(), this.getSkinPart(skin, Skin.PartPosition.LEFT_LEG_OVERLAY, 1)); + applyOverlay(head, this.getSkinPart(skin, Skin.PartPosition.HEAD_OVERLAY_FRONT, 1)); + applyOverlay(body, this.getSkinPart(skin, Skin.PartPosition.BODY_OVERLAY_FRONT, 1)); + applyOverlay(rightArm, this.getSkinPart(skin, Skin.PartPosition.RIGHT_ARM_OVERLAY, 1)); + applyOverlay(leftArm, this.getSkinPart(skin, Skin.PartPosition.LEFT_ARM_OVERLAY, 1)); + applyOverlay(rightLeg, this.getSkinPart(skin, Skin.PartPosition.RIGHT_LEG_OVERLAY, 1)); + applyOverlay(leftLeg, this.getSkinPart(skin, Skin.PartPosition.LEFT_LEG_OVERLAY, 1)); } // Draw the body diff --git a/src/main/java/cc.fascinated/service/skin/impl/HeadRenderer.java b/src/main/java/cc.fascinated/service/skin/impl/HeadRenderer.java index c32a3f9..61174d7 100644 --- a/src/main/java/cc.fascinated/service/skin/impl/HeadRenderer.java +++ b/src/main/java/cc.fascinated/service/skin/impl/HeadRenderer.java @@ -25,7 +25,7 @@ public class HeadRenderer extends SkinRenderer { graphics.drawImage(this.getSkinPart(skin, Skin.PartPosition.HEAD, 1), 0, 0, null); if (renderOverlay) { // Render the skin layers - applyOverlay(outputImage.createGraphics(), this.getSkinPart(skin, Skin.PartPosition.HEAD_OVERLAY_FRONT, 1)); + applyOverlay(outputImage, this.getSkinPart(skin, Skin.PartPosition.HEAD_OVERLAY_FRONT, 1)); } return super.getBytes(outputImage, skin, partName); diff --git a/src/main/java/cc.fascinated/service/skin/impl/IsometricHeadRenderer.java b/src/main/java/cc.fascinated/service/skin/impl/IsometricHeadRenderer.java index cead04f..5c6186c 100644 --- a/src/main/java/cc.fascinated/service/skin/impl/IsometricHeadRenderer.java +++ b/src/main/java/cc.fascinated/service/skin/impl/IsometricHeadRenderer.java @@ -41,14 +41,9 @@ public class IsometricHeadRenderer extends SkinRenderer { BufferedImage headRight = ImageUtils.resize(this.getSkinPart(skin, Skin.PartPosition.HEAD_RIGHT, 1), scale); if (renderOverlay) { // Render the skin layers - Graphics2D headGraphics = headTop.createGraphics(); - applyOverlay(headGraphics, this.getSkinPart(skin, Skin.PartPosition.HEAD_OVERLAY_TOP, 1)); - - headGraphics = headFront.createGraphics(); - applyOverlay(headGraphics, this.getSkinPart(skin, Skin.PartPosition.HEAD_OVERLAY_FRONT, 1)); - - headGraphics = headRight.createGraphics(); - applyOverlay(headGraphics, this.getSkinPart(skin, Skin.PartPosition.HEAD_OVERLAY_RIGHT, 1)); + applyOverlay(headTop, this.getSkinPart(skin, Skin.PartPosition.HEAD_OVERLAY_TOP, 1)); + applyOverlay(headFront, this.getSkinPart(skin, Skin.PartPosition.HEAD_OVERLAY_FRONT, 1)); + applyOverlay(headRight, this.getSkinPart(skin, Skin.PartPosition.HEAD_OVERLAY_RIGHT, 1)); } // Draw the head