adding of Component util
All checks were successful
publish / build (push) Successful in 2m57s

This commit is contained in:
unurled 2024-10-01 22:27:01 +02:00
parent 8e9efb97f3
commit 2f7de9a271
Signed by: unurled
GPG key ID: EFC5F5E709B47DDD
2 changed files with 79 additions and 1 deletions

View file

@ -6,7 +6,7 @@ plugins {
}
group = "me.unurled"
version = "0.1.0"
version = "0.1.1"
val mcVersion = "1.21.1-R0.1-SNAPSHOT"

View file

@ -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 = "<red>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 = "<red>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 =
"<red>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 = "<red>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<net.kyori.adventure.text.Component> fromStringList(
@NotNull List<String> 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);
}
}