Add getting text from component to PlexUtils

This commit is contained in:
Business Goose 2022-05-04 16:29:25 +01:00
parent d716f77ac0
commit 3604add18a
No known key found for this signature in database
GPG Key ID: 77DCA801362E9645
2 changed files with 37 additions and 1 deletions

View File

@ -60,7 +60,8 @@ public class ChatListener extends PlexListener
@Override
public @NotNull Component render(@NotNull Player source, @NotNull Component sourceDisplayName, @NotNull Component message, @NotNull Audience viewer)
{
String text = ((TextComponent)message).content();
String text = PlexUtils.getTextFromComponent(message);
Component component = Component.empty();
if (hasPrefix)

View File

@ -13,6 +13,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.minimessage.tag.standard.StandardTags;
@ -166,6 +167,40 @@ public class PlexUtils implements PlexBase
return f;
}
public static String getTextFromComponent(Component component)
{
try
{
return ((TextComponent)component).content();
}
catch (Exception e)
{
PlexLog.warn("Unable to get text of component", e.getLocalizedMessage());
return "";
}
}
public static String getTextFromComponents(Component... components)
{
try
{
StringBuilder builder = new StringBuilder();
for (Component component : components)
{
builder.append(getTextFromComponent(component));
}
return builder.toString();
}
catch (Exception e)
{
PlexLog.warn("Unable to get text of components", e.getLocalizedMessage());
return "";
}
}
public static List<String> getPlayerNameList()
{
return Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList());