sanitize minimessage + strip legacy colors

This commit is contained in:
ayunami2000 2022-04-09 23:24:44 -04:00
parent f180eec44a
commit f274e20f84
2 changed files with 26 additions and 22 deletions

View File

@ -15,6 +15,7 @@ import net.kyori.adventure.text.minimessage.tag.resolver.ArgumentQueue;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.minimessage.tag.standard.*; import net.kyori.adventure.text.minimessage.tag.standard.*;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.apache.commons.lang.math.NumberUtils; import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.*; import org.bukkit.*;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -153,6 +154,14 @@ public class PlexUtils extends PlexBase
return ChatColor.translateAlternateColorCodes('&', string); return ChatColor.translateAlternateColorCodes('&', string);
} }
private static final MiniMessage safeMessage = MiniMessage.builder().tags(TagResolver.builder().resolvers(
StandardTags.color(),
StandardTags.decorations(),
StandardTags.gradient(),
StandardTags.rainbow(),
StandardTags.reset()
).build()).build();
private static final MiniMessage eggMessage = MiniMessage.builder().tags(new TagResolver() private static final MiniMessage eggMessage = MiniMessage.builder().tags(new TagResolver()
{ {
@Override @Override
@ -172,8 +181,8 @@ public class PlexUtils extends PlexBase
public static Component mmDeserialize(String input) public static Component mmDeserialize(String input)
{ {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
MiniMessage mm = (calendar.get(Calendar.MONTH) == Calendar.APRIL && calendar.get(Calendar.DAY_OF_MONTH) == 1 && (!plugin.config.contains("april_fools") || plugin.config.getBoolean("april_fools"))) ? eggMessage : MiniMessage.miniMessage(); MiniMessage mm = (calendar.get(Calendar.MONTH) == Calendar.APRIL && calendar.get(Calendar.DAY_OF_MONTH) == 1 && (!plugin.config.contains("april_fools") || plugin.config.getBoolean("april_fools"))) ? eggMessage : safeMessage;
return mm.deserialize(input); return mm.deserialize(PlainTextComponentSerializer.plainText().serialize(LegacyComponentSerializer.legacySection().deserialize(input)));
} }
public static Component messageComponent(String entry, Object... objects) public static Component messageComponent(String entry, Object... objects)

View File

@ -147,11 +147,6 @@ public class UpdateChecker extends PlexBase
} }
} }
private void sendMini(CommandSender sender, String message)
{
sender.sendMessage(PlexUtils.mmDeserialize(message));
}
public void updateJar(CommandSender sender) public void updateJar(CommandSender sender)
{ {
CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpClient client = HttpClients.createDefault();
@ -162,7 +157,7 @@ public class UpdateChecker extends PlexBase
JSONObject object = new JSONObject(EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8)); JSONObject object = new JSONObject(EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8));
JSONObject artifact = object.getJSONArray("artifacts").getJSONObject(0); JSONObject artifact = object.getJSONArray("artifacts").getJSONObject(0);
String name = artifact.getString("fileName"); String name = artifact.getString("fileName");
sendMini(sender, "<green>Downloading latest Plex jar file: " + name); sender.sendMessage(PlexUtils.mmDeserialize("<green>Downloading latest Plex jar file: " + name));
CompletableFuture.runAsync(() -> CompletableFuture.runAsync(() ->
{ {
try try
@ -171,7 +166,7 @@ public class UpdateChecker extends PlexBase
new URL(DOWNLOAD_PAGE + "job/" + branch + "/lastSuccessfulBuild/artifact/build/libs/" + name), new URL(DOWNLOAD_PAGE + "job/" + branch + "/lastSuccessfulBuild/artifact/build/libs/" + name),
new File(Bukkit.getUpdateFolderFile(), name) new File(Bukkit.getUpdateFolderFile(), name)
); );
sendMini(sender, "<green>Saved new jar. Please restart your server."); sender.sendMessage(PlexUtils.mmDeserialize("<green>Saved new jar. Please restart your server."));
} }
catch (IOException e) catch (IOException e)
{ {