From 65e03a58aef997743d694291575d0b72d93b5ed1 Mon Sep 17 00:00:00 2001 From: ceze88 Date: Tue, 2 Jul 2024 16:56:02 +0200 Subject: [PATCH] Fix chat messages become blank for players --- .../craftaro/core/chat/AdventureUtils.java | 59 +++++ .../com/craftaro/core/chat/ChatMessage.java | 244 ------------------ .../craftaro/core/commands/MainCommand.java | 28 +- .../com/craftaro/core/locale/Message.java | 75 ++---- 4 files changed, 105 insertions(+), 301 deletions(-) delete mode 100644 Core/src/main/java/com/craftaro/core/chat/ChatMessage.java diff --git a/Core/src/main/java/com/craftaro/core/chat/AdventureUtils.java b/Core/src/main/java/com/craftaro/core/chat/AdventureUtils.java index 902c3077..84512c18 100644 --- a/Core/src/main/java/com/craftaro/core/chat/AdventureUtils.java +++ b/Core/src/main/java/com/craftaro/core/chat/AdventureUtils.java @@ -10,17 +10,23 @@ import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; +import net.kyori.adventure.title.Title; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.java.JavaPlugin; import java.lang.reflect.Method; +import java.time.Duration; +import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalUnit; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.List; +import java.util.concurrent.TimeUnit; public class AdventureUtils { private static Method displayNameMethod = null; @@ -399,4 +405,57 @@ public class AdventureUtils { public static String clear(Component component) { return PlainTextComponentSerializer.plainText().serialize(component); } + + public static List splitComponent(Component message, char c) { + List components = new ArrayList<>(); + StringBuilder builder = new StringBuilder(); + for (char character : toLegacy(message).toCharArray()) { + if (character == c) { + components.add(formatComponent(builder.toString())); + builder = new StringBuilder(); + } else { + builder.append(character); + } + } + components.add(formatComponent(builder.toString())); + return components; + } + + public static Component formatPlaceholder(Component message, MiniMessagePlaceholder... placeholder) { + return AdventureUtils.formatComponent(AdventureUtils.replaceLegacy(AdventureUtils.toLegacy(message)), placeholder); + } + + + //Bukkit defaults for time + public static Title createTitle (Component title, Component subtitle) { + return Title.title(title, subtitle, Title.Times.times( + Duration.of(10 * 50L, ChronoUnit.MILLIS), + Duration.of(70 * 50L, ChronoUnit.MILLIS), + Duration.of(20 * 50L, ChronoUnit.MILLIS) + )); + } + // times in ticks + public static Title createTitle (Component title, Component subtitle, int fadeIn, int stay, int fadeOut) { + return Title.title(title, subtitle, Title.Times.times( + Duration.of(fadeIn * 50L, ChronoUnit.MILLIS), + Duration.of(stay * 50L, ChronoUnit.MILLIS), + Duration.of(fadeOut * 50L, ChronoUnit.MILLIS) + )); + } + + public static Title createTitle(Component title, Component subtitle, Title.Times times) { + return Title.title(title, subtitle, times); + } + + public static void sendTitle(JavaPlugin hijackedPlugin, Title title, CommandSender sender) { + try (BukkitAudiences bukkitAudiences = BukkitAudiences.create(hijackedPlugin)) { + bukkitAudiences.sender(sender).showTitle(title); + } + } + + public static void sendActionBar(JavaPlugin hijackedPlugin, Component message, CommandSender sender) { + try (BukkitAudiences bukkitAudiences = BukkitAudiences.create(hijackedPlugin)) { + bukkitAudiences.sender(sender).sendActionBar(message); + } + } } diff --git a/Core/src/main/java/com/craftaro/core/chat/ChatMessage.java b/Core/src/main/java/com/craftaro/core/chat/ChatMessage.java deleted file mode 100644 index 64060ee2..00000000 --- a/Core/src/main/java/com/craftaro/core/chat/ChatMessage.java +++ /dev/null @@ -1,244 +0,0 @@ -package com.craftaro.core.chat; - -import com.craftaro.core.SongodaCore; -import com.craftaro.core.SongodaPlugin; -import com.craftaro.core.compatibility.ClassMapping; -import com.craftaro.core.compatibility.ServerProject; -import com.craftaro.core.compatibility.ServerVersion; -import com.craftaro.core.nms.Nms; -import com.craftaro.core.utils.TextUtils; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonObject; -import net.minecraft.core.HolderLookup; -import net.minecraft.core.IRegistry; -import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.UUID; -import java.util.logging.Level; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class ChatMessage { - private static final Gson GSON = new GsonBuilder().create(); - private final List textList = new ArrayList<>(); - - public void clear() { - this.textList.clear(); - } - - public ChatMessage fromText(String text) { - return fromText(text, false); - } - - public ChatMessage fromText(String text, boolean noHex) { - Pattern pattern = Pattern.compile( - "(.*?)(?!&([omnlk]))(?=(&([123456789abcdefr#])|$)|#([a-f]|[A-F]|[0-9]){6})", - Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(text); - - while (matcher.find()) { - ColorContainer color = null; - String match1 = matcher.group(1); - - if (matcher.groupCount() == 0 || match1.isEmpty()) { - continue; - } - - char colorChar = '-'; - - if (matcher.start() != 0) { - colorChar = text.substring(matcher.start() - 1, matcher.start()).charAt(0); - } - - if (colorChar != '-') { - if (colorChar == '#') { - color = new ColorContainer(match1.substring(0, 6), noHex); - match1 = match1.substring(5); - } else if (colorChar == '&') { - color = new ColorContainer(ColorCode.getByChar(Character.toLowerCase(match1.charAt(0)))); - } - } - - Pattern subPattern = Pattern.compile("(.*?)(?=&([omnlk])|$)"); - Matcher subMatcher = subPattern.matcher(match1); - - List stackedCodes = new ArrayList<>(); - while (subMatcher.find()) { - String match2 = subMatcher.group(1); - if (match2.isEmpty()) { - continue; - } - - ColorCode code = ColorCode.getByChar(Character.toLowerCase(match2.charAt(0))); - - if (code != null && code != ColorCode.RESET) { - stackedCodes.add(code); - } - - if (color != null) { - match2 = match2.substring(1); - } - - if (!match2.isEmpty()) { - addMessage(match2, color, stackedCodes); - } - } - } - - return this; - } - - public String toText() { - return toText(false); - } - - public String toText(boolean noHex) { - StringBuilder text = new StringBuilder(); - - for (JsonObject object : this.textList) { - if (object.has("color")) { - String color = object.get("color").getAsString(); - text.append("&"); - - if (color.length() == 7) { - text.append(new ColorContainer(color, noHex).getColor().getCode()); - } else { - text.append(ColorCode.valueOf(color.toUpperCase()).getCode()); - } - } - - for (ColorCode code : ColorCode.values()) { - if (code.isColor()) { - continue; - } - - String c = code.name().toLowerCase(); - if (object.has(c) && object.get(c).getAsBoolean()) { - text.append("&").append(code.getCode()); - } - } - - text.append(object.get("text").getAsString()); - } - - return text.toString(); - } - - public ChatMessage addMessage(String s) { - JsonObject txt = new JsonObject(); - txt.addProperty("text", s); - - this.textList.add(txt); - - return this; - } - - public ChatMessage addMessage(String text, ColorContainer color) { - return addMessage(text, color, Collections.emptyList()); - } - - public ChatMessage addMessage(String text, ColorContainer color, List colorCodes) { - JsonObject txt = new JsonObject(); - txt.addProperty("text", text); - - if (color != null) { - txt.addProperty("color", color.getHexCode() != null ? "#" + color.getHexCode() : color.getColorCode().name().toLowerCase()); - } - - for (ColorCode code : ColorCode.values()) { - if (!code.isColor()) { - txt.addProperty(code.name().toLowerCase(), colorCodes.contains(code)); - } - } - - this.textList.add(txt); - return this; - } - - public ChatMessage addRunCommand(String text, String hoverText, String cmd) { - JsonObject txt = new JsonObject(); - txt.addProperty("text", text); - - JsonObject hover = new JsonObject(); - hover.addProperty("action", "show_text"); - hover.addProperty("value", hoverText); - txt.add("hoverEvent", hover); - - JsonObject click = new JsonObject(); - click.addProperty("action", "run_command"); - click.addProperty("value", cmd); - txt.add("clickEvent", click); - - this.textList.add(txt); - return this; - } - - public ChatMessage addPromptCommand(String text, String hoverText, String cmd) { - JsonObject txt = new JsonObject(); - txt.addProperty("text", text); - - JsonObject hover = new JsonObject(); - hover.addProperty("action", "show_text"); - hover.addProperty("value", hoverText); - txt.add("hoverEvent", hover); - - JsonObject click = new JsonObject(); - click.addProperty("action", "suggest_command"); - click.addProperty("value", cmd); - txt.add("clickEvent", click); - - this.textList.add(txt); - return this; - } - - public ChatMessage addURL(String text, String hoverText, String url) { - JsonObject txt = new JsonObject(); - txt.addProperty("text", text); - - JsonObject hover = new JsonObject(); - hover.addProperty("action", "show_text"); - hover.addProperty("value", hoverText); - txt.add("hoverEvent", hover); - - JsonObject click = new JsonObject(); - click.addProperty("action", "open_url"); - click.addProperty("value", url); - txt.add("clickEvent", hover); - - this.textList.add(txt); - return this; - } - - @Override - public String toString() { - return GSON.toJson(this.textList); - } - - public void sendTo(CommandSender sender) { - sendTo(null, sender); - } - - public void sendTo(ChatMessage prefix, CommandSender sender) { - AdventureUtils.sendMessage(SongodaCore.getHijackedPlugin(), AdventureUtils.formatComponent(prefix == null ? "" : prefix.toText() + toText()), sender); - } - - public ChatMessage replaceAll(String toReplace, String replaceWith) { - for (JsonObject object : this.textList) { - String text = object.get("text").getAsString().replaceAll(toReplace, replaceWith); - - object.remove("text"); - object.addProperty("text", text); - } - - return this; - } -} diff --git a/Core/src/main/java/com/craftaro/core/commands/MainCommand.java b/Core/src/main/java/com/craftaro/core/commands/MainCommand.java index 082e4226..3f95e2cc 100644 --- a/Core/src/main/java/com/craftaro/core/commands/MainCommand.java +++ b/Core/src/main/java/com/craftaro/core/commands/MainCommand.java @@ -1,6 +1,8 @@ package com.craftaro.core.commands; -import com.craftaro.core.chat.ChatMessage; +import com.craftaro.core.SongodaCore; +import com.craftaro.core.chat.AdventureUtils; +import net.kyori.adventure.text.Component; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; @@ -60,9 +62,11 @@ public class MainCommand extends AbstractCommand { if (this.header != null) { sender.sendMessage(this.header); } else { - new ChatMessage().fromText(String.format("#ff8080&l%s &8» &7Version %s Created with <3 by #ec4e74&l&oS#fa5b65&l&oo#ff6c55&l&on#ff7f44&l&og#ff9432&l&oo#ffaa1e&l&od#f4c009&l&oa", - this.plugin.getDescription().getName(), this.plugin.getDescription().getVersion()), sender instanceof ConsoleCommandSender) - .sendTo(sender); + AdventureUtils.sendMessage(SongodaCore.getHijackedPlugin(), AdventureUtils.formatComponent( + String.format("&l%s &8» &7Version %s Created with <3 by Songoda", + this.plugin.getDescription().getName(), + this.plugin.getDescription().getVersion() + )), sender); } sender.sendMessage(ChatColor.DARK_GRAY + "- " + ChatColor.YELLOW + "/songoda" + ChatColor.GRAY + " - Opens the Songoda plugin GUI"); @@ -85,12 +89,16 @@ public class MainCommand extends AbstractCommand { if (!isPlayer) { sender.sendMessage(ChatColor.DARK_GRAY + "- " + ChatColor.YELLOW + cmd.getSyntax() + ChatColor.GRAY + " - " + cmd.getDescription()); } else if (cmd.getPermissionNode() == null || sender.hasPermission(cmd.getPermissionNode())) { - ChatMessage chatMessage = new ChatMessage(); - final String command = "/" + this.command + " "; - chatMessage.addMessage(ChatColor.DARK_GRAY + "- ") - .addPromptCommand(ChatColor.YELLOW + command + cmd.getSyntax(), ChatColor.YELLOW + command + cmdStr, command + cmdStr) - .addMessage(ChatColor.GRAY + " - " + cmd.getDescription()); - chatMessage.sendTo(sender); + String command = "/" + this.command + " "; + Component component = AdventureUtils.formatComponent( + String.format("- %s%s - %s %s", + command, + cmd.getSyntax(), + cmd.getDescription(), + cmd.getPermissionNode() == null ? "" : cmd.getPermissionNode() + ) + ); + AdventureUtils.sendMessage(SongodaCore.getHijackedPlugin(), component, sender); } } } diff --git a/Core/src/main/java/com/craftaro/core/locale/Message.java b/Core/src/main/java/com/craftaro/core/locale/Message.java index 018dc142..4784490f 100644 --- a/Core/src/main/java/com/craftaro/core/locale/Message.java +++ b/Core/src/main/java/com/craftaro/core/locale/Message.java @@ -1,8 +1,12 @@ package com.craftaro.core.locale; -import com.craftaro.core.chat.ChatMessage; +import com.craftaro.core.SongodaCore; +import com.craftaro.core.chat.AdventureUtils; +import com.craftaro.core.chat.MiniMessagePlaceholder; import com.craftaro.core.compatibility.ServerVersion; import com.craftaro.core.utils.TextUtils; +import net.kyori.adventure.Adventure; +import net.kyori.adventure.text.Component; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -30,8 +34,8 @@ public class Message { } } - private ChatMessage prefix = null; - private ChatMessage message; + private Component prefix = null; + private Component message; /** * create a new message @@ -39,19 +43,7 @@ public class Message { * @param message the message text */ public Message(String message) { - ChatMessage chatMessage = new ChatMessage(); - chatMessage.fromText(message); - - this.message = chatMessage; - } - - /** - * create a new message - * - * @param message the message text - */ - public Message(ChatMessage message) { - this.message = message; + this.message = AdventureUtils.formatComponent(message); } /** @@ -60,7 +52,7 @@ public class Message { * @param player player to send the message to */ public void sendMessage(Player player) { - player.sendMessage(this.getMessage()); + sendMessage((CommandSender) player); } /** @@ -69,7 +61,7 @@ public class Message { * @param sender command sender to send the message to */ public void sendMessage(CommandSender sender) { - this.message.sendTo(sender); + AdventureUtils.sendMessage(SongodaCore.getHijackedPlugin(), this.message, sender); } /** @@ -80,16 +72,16 @@ public class Message { public void sendTitle(CommandSender sender) { if (sender instanceof Player) { if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11)) { - ((Player) sender).sendTitle("", getMessage(), 10, 30, 10); + AdventureUtils.sendTitle(SongodaCore.getHijackedPlugin(), AdventureUtils.createTitle(Component.empty(), getMessage(), 10, 30, 10), sender); return; } - ((Player) sender).sendTitle("", getMessage()); + AdventureUtils.sendTitle(SongodaCore.getHijackedPlugin(), AdventureUtils.createTitle(Component.empty(), getMessage()), sender); return; } - sender.sendMessage(this.getMessage()); + AdventureUtils.sendMessage(SongodaCore.getHijackedPlugin(), this.message, sender); } /** @@ -99,7 +91,7 @@ public class Message { */ public void sendActionBar(CommandSender sender) { if (!(sender instanceof Player)) { - sender.sendMessage(this.getMessage()); + AdventureUtils.sendMessage(SongodaCore.getHijackedPlugin(), this.message, sender); return; } @@ -108,8 +100,7 @@ public class Message { return; } - ((Player) sender).spigot().sendMessage(net.md_5.bungee.api.ChatMessageType.ACTION_BAR, - new net.md_5.bungee.api.chat.TextComponent(getMessage())); + AdventureUtils.sendActionBar(SongodaCore.getHijackedPlugin(), getMessage(), sender); } /** @@ -119,7 +110,7 @@ public class Message { * @param sender command sender to send the message to */ public void sendPrefixedMessage(CommandSender sender) { - this.message.sendTo(this.prefix, sender); + AdventureUtils.sendMessage(SongodaCore.getHijackedPlugin(), this.prefix.append(this.message), sender); } /** @@ -128,8 +119,8 @@ public class Message { * * @return the prefixed message */ - public String getPrefixedMessage() { - return TextUtils.formatText((this.prefix == null ? "" : this.prefix.toText()) + " " + this.message.toText()); + public Component getPrefixedMessage() { + return this.prefix.append(this.message); } /** @@ -137,8 +128,8 @@ public class Message { * * @return the message */ - public String getMessage() { - return TextUtils.formatText(this.message.toText()); + public Component getMessage() { + return this.message; } /** @@ -146,17 +137,9 @@ public class Message { * * @return the message */ - public List getMessageLines() { - return Arrays.asList(ChatColor.translateAlternateColorCodes('&', this.message.toText()).split("[\n|]")); - } - - /** - * Get the held message - * - * @return the message - */ - public String getUnformattedMessage() { - return this.message.toText(); + public List getMessageLines() { + //return Arrays.asList(ChatColor.translateAlternateColorCodes('&', this.message.toText()).split("[\n|]")); + return AdventureUtils.splitComponent(this.message, '\n'); } /** @@ -169,25 +152,23 @@ public class Message { * @return the modified Message */ public Message processPlaceholder(String placeholder, Object replacement) { - final String place = Matcher.quoteReplacement(placeholder); - this.message = this.message.replaceAll("%" + place + "%|\\{" + place + "\\}", replacement == null ? "" : Matcher.quoteReplacement(replacement.toString())); + MiniMessagePlaceholder placeholderProcessor = new MiniMessagePlaceholder(placeholder, replacement == null ? "" : replacement.toString()); + this.message = AdventureUtils.formatPlaceholder(this.message, placeholderProcessor); return this; } Message setPrefix(String prefix) { - this.prefix = new ChatMessage(); - this.prefix.fromText(prefix + " "); - + this.prefix = AdventureUtils.formatComponent(prefix + " "); return this; } @Override public String toString() { - return this.message.toString(); + return AdventureUtils.toLegacy(this.message); } public String toText() { - return this.message.toText(); + return AdventureUtils.toLegacy(this.message); } }