mirror of
https://github.com/plexusorg/Plex.git
synced 2025-06-28 22:46:40 +00:00
Make a safe mini message class with a custom tag resolver
This commit is contained in:
@ -30,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
|
||||
@ -114,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));
|
||||
@ -137,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)
|
||||
@ -150,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)
|
||||
@ -208,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)
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user