Add minimessage support to prefixes

This commit is contained in:
Taah 2022-02-25 00:37:43 -08:00
parent 27daa3a858
commit 210b02ff78
2 changed files with 22 additions and 6 deletions

View File

@ -7,10 +7,11 @@ import dev.plex.command.annotation.CommandPermissions;
import dev.plex.command.source.RequiredCommandSource; import dev.plex.command.source.RequiredCommandSource;
import dev.plex.player.PlexPlayer; import dev.plex.player.PlexPlayer;
import dev.plex.rank.enums.Rank; import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexUtils;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender; import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -46,12 +47,17 @@ public class TagCMD extends PlexCommand
return usage("/tag set <prefix>"); return usage("/tag set <prefix>");
} }
String prefix = StringUtils.join(args, " ", 1, args.length); String prefix = StringUtils.join(args, " ", 1, args.length);
if (ChatColor.stripColor(prefix).length() > plugin.config.getInt("chat.max-tag-length", 16))
Component convertedComponent = removeEvents(componentFromString(prefix));
convertedComponent = removeEvents(MiniMessage.miniMessage().deserialize(LegacyComponentSerializer.legacySection().serialize(convertedComponent)));
if (PlainTextComponentSerializer.plainText().serialize(convertedComponent).length() > plugin.config.getInt("chat.max-tag-length", 16))
{ {
return messageComponent("maximumPrefixLength", plugin.config.getInt("chat.max-tag-length", 16)); return messageComponent("maximumPrefixLength", plugin.config.getInt("chat.max-tag-length", 16));
} }
player.setPrefix(prefix);
return messageComponent("prefixSetTo", PlexUtils.colorize(prefix)); player.setPrefix(MiniMessage.miniMessage().serialize(convertedComponent));
return messageComponent("prefixSetTo", MiniMessage.miniMessage().serialize(convertedComponent));
} }
if (args[0].equalsIgnoreCase("clear")) if (args[0].equalsIgnoreCase("clear"))
@ -76,4 +82,13 @@ public class TagCMD extends PlexCommand
} }
return usage(); return usage();
} }
private Component removeEvents(Component component)
{
if (component.clickEvent() != null) component = component.clickEvent(null);
if (component.hoverEvent() != null) component = component.hoverEvent(null);
return component;
}
} }

View File

@ -10,6 +10,7 @@ import io.papermc.paper.event.player.AsyncChatEvent;
import net.kyori.adventure.audience.Audience; import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -71,7 +72,7 @@ public class ChatListener extends PlexListener
{ {
if (hasPrefix) if (hasPrefix)
{ {
return Component.empty().append(LegacyComponentSerializer.legacyAmpersand().deserialize(prefix)) return Component.empty().append(MiniMessage.miniMessage().deserialize(prefix))
.append(Component.space()) .append(Component.space())
.append(LegacyComponentSerializer.legacyAmpersand().deserialize("&" + plugin.config.getString("chat.name-color") + LegacyComponentSerializer.legacyAmpersand().serialize(sourceDisplayName))) .append(LegacyComponentSerializer.legacyAmpersand().deserialize("&" + plugin.config.getString("chat.name-color") + LegacyComponentSerializer.legacyAmpersand().serialize(sourceDisplayName)))
.append(Component.space()) .append(Component.space())