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.standard.*;
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.bukkit.*;
import org.bukkit.command.Command;
@ -153,27 +154,35 @@ public class PlexUtils extends PlexBase
return ChatColor.translateAlternateColorCodes('&', string);
}
private static final MiniMessage eggMessage = MiniMessage.builder().tags(new TagResolver()
{
@Override
public @Nullable Tag resolve(@NotNull String name, @NotNull ArgumentQueue arguments, @NotNull Context ctx) throws ParsingException
{
return StandardTags.rainbow().resolve("rainbow", arguments, ctx);
}
private static final MiniMessage safeMessage = MiniMessage.builder().tags(TagResolver.builder().resolvers(
StandardTags.color(),
StandardTags.decorations(),
StandardTags.gradient(),
StandardTags.rainbow(),
StandardTags.reset()
).build()).build();
@Override
public boolean has(@NotNull String name)
{
return true;
}
}
private static final MiniMessage eggMessage = MiniMessage.builder().tags(new TagResolver()
{
@Override
public @Nullable Tag resolve(@NotNull String name, @NotNull ArgumentQueue arguments, @NotNull Context ctx) throws ParsingException
{
return StandardTags.rainbow().resolve("rainbow", arguments, ctx);
}
@Override
public boolean has(@NotNull String name)
{
return true;
}
}
).build();
public static Component mmDeserialize(String input)
{
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();
return mm.deserialize(input);
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(PlainTextComponentSerializer.plainText().serialize(LegacyComponentSerializer.legacySection().deserialize(input)));
}
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)
{
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 artifact = object.getJSONArray("artifacts").getJSONObject(0);
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(() ->
{
try
@ -171,7 +166,7 @@ public class UpdateChecker extends PlexBase
new URL(DOWNLOAD_PAGE + "job/" + branch + "/lastSuccessfulBuild/artifact/build/libs/" + 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)
{