Merge pull request #38 from plexusorg/player-mm

More Player MiniMessage & prefix handling improvement
This commit is contained in:
Telesphoreo 2022-05-04 12:45:31 -05:00 committed by GitHub
commit 9d5794dcfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 154 additions and 42 deletions

View File

@ -7,10 +7,9 @@ import dev.plex.command.annotation.CommandPermissions;
import dev.plex.command.source.RequiredCommandSource;
import dev.plex.player.PlexPlayer;
import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexUtils;
import dev.plex.util.minimessage.SafeMiniMessage;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.standard.StandardTags;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.apache.commons.lang.StringUtils;
import org.bukkit.command.CommandSender;
@ -49,7 +48,7 @@ public class TagCMD extends PlexCommand
}
String prefix = StringUtils.join(args, " ", 1, args.length);
Component convertedComponent = removeEvents(PlexUtils.mmCustomDeserialize(prefix = prefix.replace("<newline>", "").replace("<br>", ""), StandardTags.color(), StandardTags.rainbow(), StandardTags.decorations(), StandardTags.gradient(), StandardTags.transition())); //noColorComponentFromString(prefix)
Component convertedComponent = SafeMiniMessage.mmDeserializeWithoutEvents(prefix);
if (PlainTextComponentSerializer.plainText().serialize(convertedComponent).length() > plugin.config.getInt("chat.max-tag-length", 16))
{
@ -90,19 +89,6 @@ public class TagCMD extends PlexCommand
}
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

@ -0,0 +1,30 @@
package dev.plex.listener.impl;
import dev.plex.listener.PlexListener;
import dev.plex.util.PlexUtils;
import dev.plex.util.minimessage.SafeMiniMessage;
import java.util.ArrayList;
import java.util.List;
import net.kyori.adventure.inventory.Book;
import net.kyori.adventure.text.Component;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerEditBookEvent;
import org.bukkit.inventory.meta.BookMeta;
public class BookListener extends PlexListener
{
@EventHandler(priority = EventPriority.HIGHEST)
public void onBookEdit(PlayerEditBookEvent event)
{
List<Component> pages = new ArrayList<>();
for (Component page : event.getNewBookMeta().pages())
{
pages.add(SafeMiniMessage.mmDeserialize(PlexUtils.getTextFromComponent(page)));
}
event.setNewBookMeta((BookMeta)event.getNewBookMeta().pages(pages));
}
}

View File

@ -5,6 +5,7 @@ import dev.plex.listener.PlexListener;
import dev.plex.listener.annotation.Toggleable;
import dev.plex.player.PlexPlayer;
import dev.plex.util.PlexUtils;
import dev.plex.util.minimessage.SafeMiniMessage;
import io.papermc.paper.chat.ChatRenderer;
import io.papermc.paper.event.player.AsyncChatEvent;
import net.kyori.adventure.audience.Audience;
@ -59,25 +60,23 @@ public class ChatListener extends PlexListener
@Override
public @NotNull Component render(@NotNull Player source, @NotNull Component sourceDisplayName, @NotNull Component message, @NotNull Audience viewer)
{
message = message.replaceText(URL_REPLACEMENT_CONFIG);
String text = PlexUtils.getTextFromComponent(message);
Component component = Component.empty();
if (hasPrefix)
{
return Component.empty()
.append(prefix)
component = component.append(prefix);
}
return component
.append(Component.space())
.append(PlexUtils.mmDeserialize(plugin.config.getString("chat.name-color", "<white>") + MiniMessage.builder().tags(TagResolver.resolver(StandardTags.color(), StandardTags.rainbow(), StandardTags.decorations(), StandardTags.gradient(), StandardTags.transition())).build().serialize(sourceDisplayName)))
.append(Component.space())
.append(Component.text("»").color(NamedTextColor.GRAY))
.append(Component.space())
.append(message);
}
return Component.empty()
.append(PlexUtils.mmDeserialize(plugin.config.getString("chat.name-color", "<white>") + MiniMessage.builder().tags(TagResolver.resolver(StandardTags.color(), StandardTags.rainbow(), StandardTags.decorations(), StandardTags.gradient(), StandardTags.transition())).build().serialize(sourceDisplayName)))
.append(Component.space())
.append(Component.text("»").color(NamedTextColor.GRAY))
.append(Component.space())
.append(message);
.append(SafeMiniMessage.mmDeserializeWithoutEvents(text))
.replaceText(URL_REPLACEMENT_CONFIG);
}
}
}

View File

@ -0,0 +1,20 @@
package dev.plex.listener.impl;
import dev.plex.listener.PlexListener;
import dev.plex.util.PlexUtils;
import dev.plex.util.minimessage.SafeMiniMessage;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.SignChangeEvent;
public class SignListener extends PlexListener
{
@EventHandler(priority = EventPriority.HIGHEST)
public void onSignEdit(SignChangeEvent event)
{
for (int i = 0; i < event.lines().size(); i++)
{
event.line(i, SafeMiniMessage.mmDeserialize(PlexUtils.getTextFromComponent(event.line(i))));
}
}
}

View File

@ -5,6 +5,7 @@ import dev.plex.player.PlexPlayer;
import dev.plex.rank.enums.Rank;
import dev.plex.rank.enums.Title;
import dev.plex.util.PlexUtils;
import dev.plex.util.minimessage.SafeMiniMessage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
@ -14,7 +15,6 @@ import java.util.stream.Collectors;
import lombok.SneakyThrows;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.tag.standard.StandardTags;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
@ -95,7 +95,7 @@ public class RankManager
{
if (!player.getPrefix().equals(""))
{
return PlexUtils.mmCustomDeserialize(player.getPrefix(), StandardTags.color(), StandardTags.rainbow(), StandardTags.decorations(), StandardTags.gradient(), StandardTags.transition());
return SafeMiniMessage.mmDeserializeWithoutEvents(player.getPrefix());
}
if (Plex.get().config.contains("titles.owners") && Plex.get().config.getStringList("titles.owners").contains(player.getName()))
{

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;
@ -29,6 +30,8 @@ import org.bukkit.plugin.Plugin;
public class PlexUtils implements PlexBase
{
private static final MiniMessage MINI_MESSAGE = MiniMessage.miniMessage();
public static List<String> DEVELOPERS =
Arrays.asList("78408086-1991-4c33-a571-d8fa325465b2", // Telesphoreo
"f5cd54c4-3a24-4213-9a56-c06c49594dff", // Taahh
@ -113,14 +116,6 @@ public class PlexUtils implements PlexBase
return false;
}
private static final MiniMessage safeMessage = MiniMessage.builder().tags(TagResolver.builder().resolvers(
StandardTags.color(),
StandardTags.decorations(),
StandardTags.gradient(),
StandardTags.rainbow(),
StandardTags.reset()
).build()).build();
public static String mmStripColor(String input)
{
return PlainTextComponentSerializer.plainText().serialize(mmDeserialize(input));
@ -136,10 +131,10 @@ public class PlexUtils implements PlexBase
ZonedDateTime date = ZonedDateTime.now(ZoneId.of(TimeUtils.TIMEZONE));
if (aprilFools && date.getMonth() == Month.APRIL && date.getDayOfMonth() == 1)
{
Component component = MiniMessage.miniMessage().deserialize(input); // removes existing tags
return MiniMessage.miniMessage().deserialize("<rainbow>" + PlainTextComponentSerializer.plainText().serialize(component));
Component component = MINI_MESSAGE.deserialize(input); // removes existing tags
return MINI_MESSAGE.deserialize("<rainbow>" + PlainTextComponentSerializer.plainText().serialize(component));
}
return MiniMessage.miniMessage().deserialize(input);
return MINI_MESSAGE.deserialize(input);
}
public static Component mmCustomDeserialize(String input, TagResolver... resolvers)
@ -149,7 +144,7 @@ public class PlexUtils implements PlexBase
public static Component messageComponent(String entry, Object... objects)
{
return MiniMessage.miniMessage().deserialize(messageString(entry, objects));
return MINI_MESSAGE.deserialize(messageString(entry, objects));
}
public static String messageString(String entry, Object... objects)
@ -166,6 +161,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());
@ -173,7 +202,7 @@ public class PlexUtils implements PlexBase
public static void broadcast(String s)
{
Bukkit.broadcast(MiniMessage.miniMessage().deserialize(s));
Bukkit.broadcast(MINI_MESSAGE.deserialize(s));
}
public static void broadcast(Component component)

View File

@ -0,0 +1,48 @@
package dev.plex.util.minimessage;
import com.google.common.collect.ImmutableList;
import java.util.List;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.Context;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.ParsingException;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.ArgumentQueue;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class SafeMiniMessage
{
public static final MiniMessage MINI_MESSAGE = MiniMessage.builder().tags(new SafeMiniMessageTagResolver()).build();
public static Component mmDeserialize(String text)
{
return MINI_MESSAGE.deserialize(text);
}
public static Component mmDeserializeWithoutEvents(String text)
{
return mmDeserialize(text)
.clickEvent(null)
.hoverEvent(null);
}
public static class SafeMiniMessageTagResolver implements TagResolver
{
private static final TagResolver STANDARD_RESOLVER = TagResolver.standard();
private static final List<String> IGNORED_TAGS = ImmutableList.of("obfuscated", "obf", "br", "newline");
@Override
public @Nullable Tag resolve(@NotNull String name, @NotNull ArgumentQueue arguments, @NotNull Context ctx) throws ParsingException
{
return IGNORED_TAGS.contains(name.toLowerCase()) ? null : STANDARD_RESOLVER.resolve(name, arguments, ctx);
}
@Override
public boolean has(@NotNull String name)
{
return STANDARD_RESOLVER.has(name);
}
}
}