From 6cac59d41ebecbaba0cc8e8983ab2fda345a604a Mon Sep 17 00:00:00 2001 From: NEZNAMY Date: Mon, 8 Feb 2021 18:50:55 +0100 Subject: [PATCH] small cleanup and improvement --- .../shared/packets/IChatBaseComponent.java | 32 ++++++++++++------- .../me/neznamy/tab/shared/rgb/TextColor.java | 30 ++++++++++++----- 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/src/main/java/me/neznamy/tab/shared/packets/IChatBaseComponent.java b/src/main/java/me/neznamy/tab/shared/packets/IChatBaseComponent.java index dbc5c0386..07c393d9d 100644 --- a/src/main/java/me/neznamy/tab/shared/packets/IChatBaseComponent.java +++ b/src/main/java/me/neznamy/tab/shared/packets/IChatBaseComponent.java @@ -165,6 +165,11 @@ public class IChatBaseComponent { */ public IChatBaseComponent setColor(TextColor color) { this.color = color; + if (color != null) { + jsonObject.put("color", color.toString(true)); + } else { + jsonObject.remove("color"); + } return this; } @@ -342,7 +347,7 @@ public class IChatBaseComponent { * @return self */ public IChatBaseComponent onHoverShowText(IChatBaseComponent text) { - return onHover(HoverAction.SHOW_TEXT, text.jsonObject); + return onHover(HoverAction.SHOW_TEXT, text); } /** @@ -494,16 +499,7 @@ public class IChatBaseComponent { } } //the core component, fixing all colors - if (color != null) { - jsonObject.put("color", color.toString(clientVersion.getMinorVersion() >= 16)); - } - if (extra != null) { - for (IChatBaseComponent extra : extra) { - if (extra.color != null) { - extra.jsonObject.put("color", extra.color.toString(clientVersion.getMinorVersion() >= 16)); - } - } - } + if (clientVersion.getMinorVersion() < 16) convertColorsToLegacy(); return toString(); } @@ -511,6 +507,20 @@ public class IChatBaseComponent { public String toString() { return jsonObject.toString(); } + + private void convertColorsToLegacy() { + if (color != null) { + jsonObject.put("color", color.getLegacyColor().toString().toLowerCase()); + } + if (extra != null) { + for (IChatBaseComponent extra : extra) { + extra.convertColorsToLegacy(); + } + } + if (hoverValue instanceof IChatBaseComponent) { + ((IChatBaseComponent)hoverValue).convertColorsToLegacy(); + } + } /** * Returns organized component from colored text diff --git a/src/main/java/me/neznamy/tab/shared/rgb/TextColor.java b/src/main/java/me/neznamy/tab/shared/rgb/TextColor.java index 93aec0663..f4680cd9c 100644 --- a/src/main/java/me/neznamy/tab/shared/rgb/TextColor.java +++ b/src/main/java/me/neznamy/tab/shared/rgb/TextColor.java @@ -21,9 +21,8 @@ public class TextColor { /** - * Returns a new instance based on hex code as string + * Creates a new instance based on hex code as string * @param hexCode - a 6-digit combination of hex numbers - * @return TextColor from hex color */ public TextColor(String hexCode) { int hexColor = Integer.parseInt(hexCode, 16); @@ -34,9 +33,8 @@ public class TextColor { } /** - * Returns a new instance based on legacy color + * Creates a new instance with given parameter * @param legacyColor - legacy color - * @return TextColor from legacy color */ public TextColor(EnumChatFormat legacyColor) { red = legacyColor.getRed(); @@ -46,11 +44,10 @@ public class TextColor { } /** - * Returns a new instance based on color bytes + * Creates a new instance with given parameters * @param red - red value * @param green - green value * @param blue - blue value - * @return TextColor from RGB combination */ public TextColor(int red, int green, int blue) { this.red = red; @@ -59,6 +56,23 @@ public class TextColor { loadClosestColor(); } + /** + * Creates a new instance with given parameters + * @param red - red value + * @param green - green value + * @param blue - blue value + * @param legacyColor + */ + public TextColor(int red, int green, int blue, EnumChatFormat legacyColor) { + this.red = red; + this.green = green; + this.blue = blue; + this.legacyColor = legacyColor; + } + + /** + * Loads closest legacy color based on current rgb values + */ private void loadClosestColor() { double minDist = 9999; double dist; @@ -100,8 +114,8 @@ public class TextColor { } /** - * Returns the closest legacy color - * @return closest legacy color + * Returns defined legacy color + * @return defined legacy color */ public EnumChatFormat getLegacyColor() { return legacyColor;