-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Adventure hook] Fix startup error when an older version of the libra…
…ry is present
- Loading branch information
NEZNAMY
committed
Feb 4, 2025
1 parent
d5a0ae8
commit 434c021
Showing
2 changed files
with
46 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
shared/src/main/java/me/neznamy/tab/shared/hook/AdventureShadowHook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |