From b1ef89dae983054ad449962b9d2c42a15d04736e Mon Sep 17 00:00:00 2001 From: ayunami2000 Date: Sat, 9 Apr 2022 21:53:27 -0400 Subject: [PATCH 1/3] fix bug --- .../java/dev/plex/command/PlexCommand.java | 4 +-- .../java/dev/plex/command/impl/TagCMD.java | 3 +- .../java/dev/plex/command/impl/UnbanCMD.java | 2 +- .../plex/listener/impl/CommandListener.java | 3 +- .../plex/listener/impl/PlayerListener.java | 2 +- src/main/java/dev/plex/rank/RankManager.java | 2 +- src/main/java/dev/plex/rank/enums/Rank.java | 3 +- src/main/java/dev/plex/rank/enums/Title.java | 3 +- src/main/java/dev/plex/util/PlexUtils.java | 33 ++++++++++++++++++- .../java/dev/plex/util/UpdateChecker.java | 2 +- 10 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/main/java/dev/plex/command/PlexCommand.java b/src/main/java/dev/plex/command/PlexCommand.java index b40e1fe..1a1e1a1 100644 --- a/src/main/java/dev/plex/command/PlexCommand.java +++ b/src/main/java/dev/plex/command/PlexCommand.java @@ -160,7 +160,7 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC } catch (PlayerNotFoundException | CommandFailException | ConsoleOnlyException | ConsoleMustDefinePlayerException | PlayerNotBannedException ex) { - send(sender, MiniMessage.miniMessage().deserialize(ex.getMessage())); + send(sender, PlexUtils.mmDeserialize(ex.getMessage())); } return true; } @@ -544,7 +544,7 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC */ protected Component mmString(String s) { - return MiniMessage.miniMessage().deserialize(s); + return PlexUtils.mmDeserialize(s); } public Rank getLevel() diff --git a/src/main/java/dev/plex/command/impl/TagCMD.java b/src/main/java/dev/plex/command/impl/TagCMD.java index deff701..0f05322 100644 --- a/src/main/java/dev/plex/command/impl/TagCMD.java +++ b/src/main/java/dev/plex/command/impl/TagCMD.java @@ -7,6 +7,7 @@ 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 net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; @@ -48,7 +49,7 @@ public class TagCMD extends PlexCommand } String prefix = StringUtils.join(args, " ", 1, args.length); Component convertedComponent = removeEvents(noColorComponentFromString(prefix)); - convertedComponent = removeEvents(MiniMessage.miniMessage().deserialize(LegacyComponentSerializer.legacySection().serialize(convertedComponent))); + convertedComponent = removeEvents(PlexUtils.mmDeserialize(LegacyComponentSerializer.legacySection().serialize(convertedComponent))); if (PlainTextComponentSerializer.plainText().serialize(convertedComponent).length() > plugin.config.getInt("chat.max-tag-length", 16)) { diff --git a/src/main/java/dev/plex/command/impl/UnbanCMD.java b/src/main/java/dev/plex/command/impl/UnbanCMD.java index dc1d0d1..8022fc5 100644 --- a/src/main/java/dev/plex/command/impl/UnbanCMD.java +++ b/src/main/java/dev/plex/command/impl/UnbanCMD.java @@ -47,7 +47,7 @@ public class UnbanCMD extends PlexCommand PlexPlayer plexPlayer = getOfflinePlexPlayer(targetUUID); if (!aBoolean) { - send(sender, MiniMessage.miniMessage().deserialize(new PlayerNotBannedException().getMessage())); + send(sender, PlexUtils.mmDeserialize(new PlayerNotBannedException().getMessage())); return; } plugin.getPunishmentManager().unban(targetUUID); diff --git a/src/main/java/dev/plex/listener/impl/CommandListener.java b/src/main/java/dev/plex/listener/impl/CommandListener.java index 745eb46..68bc3d7 100644 --- a/src/main/java/dev/plex/listener/impl/CommandListener.java +++ b/src/main/java/dev/plex/listener/impl/CommandListener.java @@ -8,7 +8,6 @@ import dev.plex.command.blocker.RegexCommand; import dev.plex.listener.PlexListener; import dev.plex.player.PlexPlayer; import dev.plex.util.PlexUtils; -import net.kyori.adventure.text.minimessage.MiniMessage; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.entity.Player; @@ -65,7 +64,7 @@ public class CommandListener extends PlexListener { event.setCancelled(true); //PlexLog.debug("Command blocked."); - player.sendMessage(MiniMessage.miniMessage().deserialize(PlexUtils.messageString("blockedCommandColor") + blockedCommand.getMessage())); + player.sendMessage(PlexUtils.mmDeserialize(PlexUtils.messageString("blockedCommandColor") + blockedCommand.getMessage())); return; } } diff --git a/src/main/java/dev/plex/listener/impl/PlayerListener.java b/src/main/java/dev/plex/listener/impl/PlayerListener.java index 7ab342c..b0848a1 100644 --- a/src/main/java/dev/plex/listener/impl/PlayerListener.java +++ b/src/main/java/dev/plex/listener/impl/PlayerListener.java @@ -77,7 +77,7 @@ public class PlayerListener extends PlexListener String loginMessage = plugin.getRankManager().getLoginMessage(plexPlayer); if (!loginMessage.isEmpty()) { - PlexUtils.broadcast(MiniMessage.miniMessage().deserialize("" + player.getName() + " is " + loginMessage)); + PlexUtils.broadcast(PlexUtils.mmDeserialize("" + player.getName() + " is " + loginMessage)); } if (plugin.getStorageType() != StorageType.MONGODB) diff --git a/src/main/java/dev/plex/rank/RankManager.java b/src/main/java/dev/plex/rank/RankManager.java index a71f81f..194e6dd 100644 --- a/src/main/java/dev/plex/rank/RankManager.java +++ b/src/main/java/dev/plex/rank/RankManager.java @@ -95,7 +95,7 @@ public class RankManager { if (!player.getPrefix().equals("")) { - return MiniMessage.miniMessage().deserialize(player.getPrefix()); + return PlexUtils.mmDeserialize(player.getPrefix()); } if (Plex.get().config.contains("titles.owners") && Plex.get().config.getStringList("titles.owners").contains(player.getName())) { diff --git a/src/main/java/dev/plex/rank/enums/Rank.java b/src/main/java/dev/plex/rank/enums/Rank.java index 177b9a0..b114085 100644 --- a/src/main/java/dev/plex/rank/enums/Rank.java +++ b/src/main/java/dev/plex/rank/enums/Rank.java @@ -1,5 +1,6 @@ package dev.plex.rank.enums; +import dev.plex.util.PlexUtils; import lombok.Getter; import lombok.Setter; import net.kyori.adventure.text.Component; @@ -47,7 +48,7 @@ public enum Rank public Component getPrefix() { - return MiniMessage.miniMessage().deserialize(this.prefix); + return PlexUtils.mmDeserialize(this.prefix); } public JSONObject toJSON() diff --git a/src/main/java/dev/plex/rank/enums/Title.java b/src/main/java/dev/plex/rank/enums/Title.java index ac52169..641b2cc 100644 --- a/src/main/java/dev/plex/rank/enums/Title.java +++ b/src/main/java/dev/plex/rank/enums/Title.java @@ -1,5 +1,6 @@ package dev.plex.rank.enums; +import dev.plex.util.PlexUtils; import lombok.Getter; import lombok.Setter; import net.kyori.adventure.text.Component; @@ -39,7 +40,7 @@ public enum Title public Component getPrefix() { - return MiniMessage.miniMessage().deserialize(this.prefix); + return PlexUtils.mmDeserialize(this.prefix); } public JSONObject toJSON() diff --git a/src/main/java/dev/plex/util/PlexUtils.java b/src/main/java/dev/plex/util/PlexUtils.java index 37653a4..06aeace 100644 --- a/src/main/java/dev/plex/util/PlexUtils.java +++ b/src/main/java/dev/plex/util/PlexUtils.java @@ -7,7 +7,13 @@ import dev.plex.PlexBase; import dev.plex.config.Config; import dev.plex.storage.StorageType; 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 net.kyori.adventure.text.minimessage.tag.standard.*; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.apache.commons.lang.math.NumberUtils; import org.bukkit.*; @@ -15,6 +21,8 @@ import org.bukkit.command.Command; import org.bukkit.command.PluginCommandYamlParser; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; @@ -145,9 +153,32 @@ public class PlexUtils extends PlexBase return ChatColor.translateAlternateColorCodes('&', string); } + private static 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); + } + public static Component messageComponent(String entry, Object... objects) { - return MiniMessage.miniMessage().deserialize(messageString(entry, objects)); + return mmDeserialize(messageString(entry, objects)); } public static String messageString(String entry, Object... objects) diff --git a/src/main/java/dev/plex/util/UpdateChecker.java b/src/main/java/dev/plex/util/UpdateChecker.java index 4a03dd1..7fadc07 100644 --- a/src/main/java/dev/plex/util/UpdateChecker.java +++ b/src/main/java/dev/plex/util/UpdateChecker.java @@ -149,7 +149,7 @@ public class UpdateChecker extends PlexBase private void sendMini(CommandSender sender, String message) { - sender.sendMessage(MiniMessage.miniMessage().deserialize(message)); + sender.sendMessage(PlexUtils.mmDeserialize(message)); } public void updateJar(CommandSender sender) From f180eec44ac90edc745e08c633e107590ef6d0b3 Mon Sep 17 00:00:00 2001 From: ayunami2000 Date: Sat, 9 Apr 2022 21:54:34 -0400 Subject: [PATCH 2/3] "OOPS!!" --- src/main/java/dev/plex/util/PlexUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/dev/plex/util/PlexUtils.java b/src/main/java/dev/plex/util/PlexUtils.java index 06aeace..4015414 100644 --- a/src/main/java/dev/plex/util/PlexUtils.java +++ b/src/main/java/dev/plex/util/PlexUtils.java @@ -153,7 +153,7 @@ public class PlexUtils extends PlexBase return ChatColor.translateAlternateColorCodes('&', string); } - private static MiniMessage eggMessage = MiniMessage.builder().tags(new TagResolver() + 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 From f274e20f84de5fe4fd8ee81b04acaa8f78c53756 Mon Sep 17 00:00:00 2001 From: ayunami2000 Date: Sat, 9 Apr 2022 23:24:44 -0400 Subject: [PATCH 3/3] sanitize minimessage + strip legacy colors --- src/main/java/dev/plex/util/PlexUtils.java | 39 ++++++++++++------- .../java/dev/plex/util/UpdateChecker.java | 9 +---- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/main/java/dev/plex/util/PlexUtils.java b/src/main/java/dev/plex/util/PlexUtils.java index 4015414..4ac0db1 100644 --- a/src/main/java/dev/plex/util/PlexUtils.java +++ b/src/main/java/dev/plex/util/PlexUtils.java @@ -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) diff --git a/src/main/java/dev/plex/util/UpdateChecker.java b/src/main/java/dev/plex/util/UpdateChecker.java index 7fadc07..15b1065 100644 --- a/src/main/java/dev/plex/util/UpdateChecker.java +++ b/src/main/java/dev/plex/util/UpdateChecker.java @@ -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, "Downloading latest Plex jar file: " + name); + sender.sendMessage(PlexUtils.mmDeserialize("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, "Saved new jar. Please restart your server."); + sender.sendMessage(PlexUtils.mmDeserialize("Saved new jar. Please restart your server.")); } catch (IOException e) {