diff --git a/build.gradle.kts b/build.gradle.kts index 4b07a1d..e3fff3a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,7 +6,7 @@ plugins { } group = "me.unurled" -version = "0.1.0" +version = "0.1.1" val mcVersion = "1.21.1-R0.1-SNAPSHOT" diff --git a/src/main/java/me/unurled/srcore/utils/Component.java b/src/main/java/me/unurled/srcore/utils/Component.java new file mode 100644 index 0000000..77efc8a --- /dev/null +++ b/src/main/java/me/unurled/srcore/utils/Component.java @@ -0,0 +1,78 @@ +package me.unurled.srcore.utils; + +import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.text.TextComponent; +import net.kyori.adventure.text.minimessage.MiniMessage; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +/** Utility class for working with components. */ +public class Component { + /** The message to display when the player is not a player. */ + public static final String NOT_PLAYER = "You must be a player to use this command."; + + /** The message to display when the player does not have permission to use a command. */ + public static final String NO_PERMISSION = "You do not have permission to use this command."; + + /** The message to display when an error occurs while executing a command. */ + public static final String ERROR = + "An error occurred while executing this command. Please contact an admin."; + + /** The message to display when a player is not found. */ + public static final String PLAYER_NOT_FOUND = "Player not found."; + + private static MiniMessage miniMessage; + + private Component() {} + + /** + * Converts a string to a MiniMessage component. + * + * @param msg the string to convert + * @return the MiniMessage component + */ + public static net.kyori.adventure.text.@NotNull Component comp(String msg) { + if (miniMessage == null) { + miniMessage = MiniMessage.miniMessage(); + } + return miniMessage.deserialize(msg); + } + + /** + * Converts a string to a TextComponent. + * + * @param msg the string to convert + * @return the TextComponent + */ + public static net.kyori.adventure.text.@NotNull TextComponent textComp(String msg) { + if (miniMessage == null) { + miniMessage = MiniMessage.miniMessage(); + } + return (TextComponent) miniMessage.deserialize(msg); + } + + /** + * Converts a list of strings to a list of MiniMessage components. + * + * @param list the list of strings to convert + * @return the list of MiniMessage components + */ + public static List fromStringList( + @NotNull List list) { + return list.stream().map(Component::comp).toList(); + } + + /** + * Gets an audience for a player. + * + * @param player the player + * @return the audience + */ + @Contract(pure = true) + public static @NotNull Audience audience(Player player) { + return Audience.audience(player); + } +}