From 69833bf560b67aa596c6deb3f63e6ac633cdd4b6 Mon Sep 17 00:00:00 2001 From: Liam Date: Sat, 20 Apr 2024 21:14:00 +0100 Subject: [PATCH] add italics to the server preview renderer --- .../mcutils/backend/common/ColorUtils.java | 3 +-- .../xyz/mcutils/backend/common/Fonts.java | 2 ++ .../impl/server/ServerPreviewRenderer.java | 21 ++++++++++++------- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/main/java/xyz/mcutils/backend/common/ColorUtils.java b/src/main/java/xyz/mcutils/backend/common/ColorUtils.java index 38a1715..8789669 100644 --- a/src/main/java/xyz/mcutils/backend/common/ColorUtils.java +++ b/src/main/java/xyz/mcutils/backend/common/ColorUtils.java @@ -104,8 +104,7 @@ public final class ColorUtils { public static Color getMinecraftColor(char colorCode) { String color = COLOR_MAP.getOrDefault(colorCode, null); if (color == null) { - System.out.println("Unknown color code: " + colorCode); - return Color.WHITE; + throw new IllegalArgumentException("Invalid color code: " + colorCode); } return Color.decode(color); } diff --git a/src/main/java/xyz/mcutils/backend/common/Fonts.java b/src/main/java/xyz/mcutils/backend/common/Fonts.java index f5b7715..6ef318a 100644 --- a/src/main/java/xyz/mcutils/backend/common/Fonts.java +++ b/src/main/java/xyz/mcutils/backend/common/Fonts.java @@ -12,12 +12,14 @@ public class Fonts { public static final Font MINECRAFT; public static final Font MINECRAFT_BOLD; + public static final Font MINECRAFT_ITALIC; static { InputStream stream = Main.class.getResourceAsStream("/fonts/minecraft-font.ttf"); try { MINECRAFT = Font.createFont(Font.TRUETYPE_FONT, stream).deriveFont(18f); MINECRAFT_BOLD = MINECRAFT.deriveFont(Font.BOLD); + MINECRAFT_ITALIC = MINECRAFT.deriveFont(Font.ITALIC); } catch (FontFormatException | IOException e) { log.error("Failed to load Minecraft font", e); throw new RuntimeException("Failed to load Minecraft font", e); diff --git a/src/main/java/xyz/mcutils/backend/common/renderer/impl/server/ServerPreviewRenderer.java b/src/main/java/xyz/mcutils/backend/common/renderer/impl/server/ServerPreviewRenderer.java index b0ad17e..8468985 100644 --- a/src/main/java/xyz/mcutils/backend/common/renderer/impl/server/ServerPreviewRenderer.java +++ b/src/main/java/xyz/mcutils/backend/common/renderer/impl/server/ServerPreviewRenderer.java @@ -79,14 +79,21 @@ public class ServerPreviewRenderer extends Renderer { // Move x position to after the drawn text x += textWidth; // Set color based on color code - char colorCode = line.charAt(colorIndex + 1); + char colorCode = Character.toLowerCase(line.charAt(colorIndex + 1)); - if (colorCode == 'l') { - graphics.setFont(Fonts.MINECRAFT_BOLD); - } else { - Color color = ColorUtils.getMinecraftColor(colorCode); - graphics.setColor(color); - graphics.setFont(Fonts.MINECRAFT); + // Set the color and font style + switch (colorCode) { + case 'l': graphics.setFont(Fonts.MINECRAFT_BOLD); + case 'o': graphics.setFont(Fonts.MINECRAFT_ITALIC); + default: { + try { + Color color = ColorUtils.getMinecraftColor(colorCode); + graphics.setColor(color); + graphics.setFont(Fonts.MINECRAFT); + } catch (Exception ignored) { + // Unknown color, can ignore the error + } + } } // Move index to after the color code