Skip to content

Commit

Permalink
[Adventure hook] Fix startup error when an older version of the libra…
Browse files Browse the repository at this point in the history
…ry is present
  • Loading branch information
NEZNAMY committed Feb 4, 2025
1 parent d5a0ae8 commit 434c021
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -99,7 +98,7 @@ public static TabComponent convert(@NotNull Component component) {
Map<TextDecoration, TextDecoration.State> 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)),
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
}

0 comments on commit 434c021

Please sign in to comment.