Make a safe mini message class with a custom tag resolver

This commit is contained in:
Business Goose 2022-05-04 17:08:35 +01:00
parent 3604add18a
commit 45b338fd9b
No known key found for this signature in database
GPG Key ID: 77DCA801362E9645
5 changed files with 61 additions and 33 deletions

View File

@ -7,10 +7,9 @@ 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 dev.plex.util.minimessage.SafeMiniMessage;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage; 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 net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -49,7 +48,7 @@ public class TagCMD extends PlexCommand
} }
String prefix = StringUtils.join(args, " ", 1, args.length); 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)) 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(); 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

@ -5,11 +5,11 @@ import dev.plex.listener.PlexListener;
import dev.plex.listener.annotation.Toggleable; import dev.plex.listener.annotation.Toggleable;
import dev.plex.player.PlexPlayer; import dev.plex.player.PlexPlayer;
import dev.plex.util.PlexUtils; import dev.plex.util.PlexUtils;
import dev.plex.util.minimessage.SafeMiniMessage;
import io.papermc.paper.chat.ChatRenderer; import io.papermc.paper.chat.ChatRenderer;
import io.papermc.paper.event.player.AsyncChatEvent; 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.TextComponent;
import net.kyori.adventure.text.TextReplacementConfig; import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
@ -75,7 +75,7 @@ public class ChatListener extends PlexListener
.append(Component.space()) .append(Component.space())
.append(Component.text("»").color(NamedTextColor.GRAY)) .append(Component.text("»").color(NamedTextColor.GRAY))
.append(Component.space()) .append(Component.space())
.append(PlexUtils.mmDeserialize(text)) .append(SafeMiniMessage.mmDeserializeWithoutEvents(text))
.replaceText(URL_REPLACEMENT_CONFIG); .replaceText(URL_REPLACEMENT_CONFIG);
} }
} }

View File

@ -5,6 +5,7 @@ import dev.plex.player.PlexPlayer;
import dev.plex.rank.enums.Rank; import dev.plex.rank.enums.Rank;
import dev.plex.rank.enums.Title; import dev.plex.rank.enums.Title;
import dev.plex.util.PlexUtils; import dev.plex.util.PlexUtils;
import dev.plex.util.minimessage.SafeMiniMessage;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileWriter; import java.io.FileWriter;
@ -14,7 +15,6 @@ import java.util.stream.Collectors;
import lombok.SneakyThrows; import lombok.SneakyThrows;
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.tag.standard.StandardTags;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONTokener; import org.json.JSONTokener;
@ -95,7 +95,7 @@ public class RankManager
{ {
if (!player.getPrefix().equals("")) 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())) if (Plex.get().config.contains("titles.owners") && Plex.get().config.getStringList("titles.owners").contains(player.getName()))
{ {

View File

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