fix legacy skins

This commit is contained in:
Lee 2024-04-11 07:07:49 +01:00
parent c8b6f2aad8
commit bfbaf34b24
5 changed files with 62 additions and 47 deletions

@ -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?
*/

@ -45,13 +45,16 @@ 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
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) {
return null;
@ -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;
}
try {
Graphics2D graphics = originalImage.createGraphics();
graphics.drawImage(part, 0, 0, null);
graphics.dispose();
} catch (Exception ignored) {} // We can safely ignore this
}
/**

@ -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

@ -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);

@ -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