diff --git a/shared/src/main/java/me/neznamy/tab/shared/hook/AdventureHook.java b/shared/src/main/java/me/neznamy/tab/shared/hook/AdventureHook.java index 93bc0d7bc..0b318ff79 100644 --- a/shared/src/main/java/me/neznamy/tab/shared/hook/AdventureHook.java +++ b/shared/src/main/java/me/neznamy/tab/shared/hook/AdventureHook.java @@ -8,7 +8,6 @@ import me.neznamy.tab.shared.util.ReflectionUtils; import net.kyori.adventure.key.Key; import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.format.ShadowColor; import net.kyori.adventure.text.format.Style; import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.format.TextDecoration; @@ -45,8 +44,8 @@ public static Component convert(@NotNull TabComponent component) { .decoration(TextDecoration.STRIKETHROUGH, getDecoration(component.getModifier().getStrikethrough())) .decoration(TextDecoration.OBFUSCATED, getDecoration(component.getModifier().getObfuscated())) .font(component.getModifier().getFont() == null ? null : Key.key(component.getModifier().getFont())); - if (SHADOW_COLOR_AVAILABLE && component.getModifier().getShadowColor() != null) { - style.shadowColor(ShadowColor.shadowColor(component.getModifier().getShadowColor())); + if (SHADOW_COLOR_AVAILABLE) { + AdventureShadowHook.setShadowColor(style, component.getModifier().getShadowColor()); } // Extra @@ -99,7 +98,7 @@ public static TabComponent convert(@NotNull Component component) { Map decorations = component.style().decorations(); tabComponent.setModifier(new ChatModifier( component.color() == null ? null : new me.neznamy.tab.shared.chat.TextColor(component.color().value()), - !SHADOW_COLOR_AVAILABLE || component.shadowColor() == null ? null : component.shadowColor().value(), + SHADOW_COLOR_AVAILABLE ? AdventureShadowHook.getShadowColor(component) : null, getDecoration(decorations.get(TextDecoration.BOLD)), getDecoration(decorations.get(TextDecoration.ITALIC)), getDecoration(decorations.get(TextDecoration.UNDERLINED)), diff --git a/shared/src/main/java/me/neznamy/tab/shared/hook/AdventureShadowHook.java b/shared/src/main/java/me/neznamy/tab/shared/hook/AdventureShadowHook.java new file mode 100644 index 000000000..10864580c --- /dev/null +++ b/shared/src/main/java/me/neznamy/tab/shared/hook/AdventureShadowHook.java @@ -0,0 +1,43 @@ +package me.neznamy.tab.shared.hook; + +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.ShadowColor; +import net.kyori.adventure.text.format.Style; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * A separate class for handling shadow color in adventure components. + * It is required to be separate. Using static functions on interfaces causes + * it to load on class initialization even if the code never runs, resulting in an error + * when using an older version of the library before shadow color class was added. + */ +public class AdventureShadowHook { + + /** + * Returns shadow color of the component in ARGB format or {@code null} if not defined. + * + * @param component + * Component to get shadow color of + * @return Shadow color of the component in ARGB format or {@code null} if not defined + */ + @Nullable + public static Integer getShadowColor(@NotNull Component component) { + ShadowColor color = component.shadowColor(); + return color == null ? null : color.value(); + } + + /** + * Sets shadow color for the style in AGB format if it is not {@code null}. + * + * @param style + * Style to set shadow color of + * @param shadowColor + * Shadow color in ARGB format or {@code null} for default + */ + public static void setShadowColor(@NotNull Style.Builder style, @Nullable Integer shadowColor) { + if (shadowColor != null) { + style.shadowColor(ShadowColor.shadowColor(shadowColor)); + } + } +} \ No newline at end of file