diff --git a/plugin/src/main/java/me/libraryaddict/disguise/commands/libsdisguises/LDJson.java b/plugin/src/main/java/me/libraryaddict/disguise/commands/libsdisguises/LDJson.java index 82696d00..c302e062 100644 --- a/plugin/src/main/java/me/libraryaddict/disguise/commands/libsdisguises/LDJson.java +++ b/plugin/src/main/java/me/libraryaddict/disguise/commands/libsdisguises/LDJson.java @@ -3,16 +3,17 @@ package me.libraryaddict.disguise.commands.libsdisguises; import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.params.ParamInfoManager; import me.libraryaddict.disguise.utilities.reflection.ItemStackSerializer; +import me.libraryaddict.disguise.utilities.reflection.NmsVersion; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; import me.libraryaddict.disguise.utilities.translations.LibsMsg; -import net.md_5.bungee.api.chat.ClickEvent; -import net.md_5.bungee.api.chat.ComponentBuilder; -import net.md_5.bungee.api.chat.HoverEvent; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; import org.apache.commons.lang.StringUtils; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -30,6 +31,22 @@ public class LDJson implements LDCommand { return "libsdisguises.json"; } + private Component createComponent(String text, int section, int max) { + Component component = LibsMsg.CLICK_COPY.getAdv(section); + + if (NmsVersion.v1_15.isSupported()) { + component = component.clickEvent(net.kyori.adventure.text.event.ClickEvent.copyToClipboard(text)); + } else { + component = component.clickEvent(net.kyori.adventure.text.event.ClickEvent.suggestCommand(text)); + } + + LibsMsg hover = NmsVersion.v1_15.isSupported() ? LibsMsg.CLICK_TO_COPY_HOVER_CLIPBOARD : LibsMsg.CLICK_TO_COPY_HOVER; + Component hoverText = hover.getAdv(section, max, DisguiseUtilities.getMiniMessage().escapeTags(text)); + component = component.hoverEvent(net.kyori.adventure.text.event.HoverEvent.showText(hoverText)); + + return component; + } + @Override public void onCommand(CommandSender sender, String[] args) { if (!(sender instanceof Player)) { @@ -51,54 +68,81 @@ public class LDJson implements LDCommand { String ldItem = StringUtils.join(mcArray, "-"); String mcItem = StringUtils.join(mcArray, " "); - sendMessage(sender, LibsMsg.ITEM_SERIALIZED, LibsMsg.ITEM_SERIALIZED_NO_COPY, gson); + sendMessage(sender, LibsMsg.ITEM_SERIALIZED, LibsMsg.ITEM_SERIALIZED_NO_COPY, gson, false); if (!gson.equals(simple) && !ldItem.equals(simple) && !mcItem.equals(simple)) { - sendMessage(sender, LibsMsg.ITEM_SIMPLE_STRING, LibsMsg.ITEM_SIMPLE_STRING_NO_COPY, simple); + sendMessage(sender, LibsMsg.ITEM_SIMPLE_STRING, LibsMsg.ITEM_SIMPLE_STRING_NO_COPY, simple, false); } - sendMessage(sender, LibsMsg.ITEM_SERIALIZED_MC, LibsMsg.ITEM_SERIALIZED_MC_NO_COPY, mcItem); + sendMessage(sender, LibsMsg.ITEM_SERIALIZED_MC, LibsMsg.ITEM_SERIALIZED_MC_NO_COPY, mcItem, false); if (mcArray.size() > 1) { - sendMessage(sender, LibsMsg.ITEM_SERIALIZED_MC, LibsMsg.ITEM_SERIALIZED_MC_NO_COPY, ldItem); + sendMessage(sender, LibsMsg.ITEM_SERIALIZED_MC, LibsMsg.ITEM_SERIALIZED_MC_NO_COPY, ldItem, false); } } - private void sendMessage(CommandSender sender, LibsMsg prefix, LibsMsg oldVer, String string) { - /* if (!NmsVersion.v1_13.isSupported()) { - oldVer.send(sender, string); - return; - }*/ + private void sendMessage(CommandSender sender, LibsMsg msg, LibsMsg oldVer, String string, boolean forceAbbrev) { + TextComponent.Builder builder = Component.text().append(msg.getAdv()).appendSpace(); - int start = 0; - int msg = 1; + if (string.length() > 256 || forceAbbrev) { + String[] split = DisguiseUtilities.split(string); - ComponentBuilder builder = new ComponentBuilder("").append(prefix.getBase()); - - while (start < string.length()) { - int end = Math.min(256, string.length() - start); - - String sub = string.substring(start, start + end); - - builder.append(" "); - - if (string.length() <= 256) { - builder.append(LibsMsg.CLICK_TO_COPY_DATA.getBase()); - } else { - builder.reset(); - builder.append(LibsMsg.CLICK_COPY.getBase(msg)); + // Because the splitter removes the quotes.. + for (int i = 0; i < split.length; i++) { + split[i] = DisguiseUtilities.quote(split[i]); } - start += end; + for (int i = 0; i < split.length; i++) { + if (split[i].length() <= 256) { + continue; + } - builder.event(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, sub)); - builder.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, - new ComponentBuilder("").append(LibsMsg.CLICK_TO_COPY_HOVER.getBase()).append((string.length() <= 256 ? "" : " " + msg)) - .create())); - msg += 1; + split = Arrays.copyOf(split, split.length + 1); + + for (int a = split.length - 1; a > i; a--) { + split[a] = split[a - 1]; + } + + split[i + 1] = split[i].substring(256); + split[i] = split[i].substring(0, 256); + } + + List sections = new ArrayList<>(); + StringBuilder current = new StringBuilder(); + + for (int i = 0; i < split.length; i++) { + if (current.length() > 0) { + current.append(" "); + } + + current.append(split[i]); + + // If the next split would fit + if (split.length > i + 1 && split[i + 1].length() + current.length() + 1 <= 256) { + continue; + } + + // Have non-final end with a space + if (i + 1 < split.length) { + current.append(" "); + } + + sections.add(current.toString()); + current = new StringBuilder(); + } + + for (int i = 0; i < sections.size(); i++) { + if (i > 0) { + builder.appendSpace(); + } + + builder.append(createComponent(sections.get(i), i + 1, sections.size())); + } + } else { + builder.append(createComponent(string, 1, 1)); } - sender.spigot().sendMessage(builder.create()); + DisguiseUtilities.sendMessage(sender, builder.build()); } @Override diff --git a/plugin/src/main/java/me/libraryaddict/disguise/commands/utils/GrabSkinCommand.java b/plugin/src/main/java/me/libraryaddict/disguise/commands/utils/GrabSkinCommand.java index d7153656..02a96a4e 100644 --- a/plugin/src/main/java/me/libraryaddict/disguise/commands/utils/GrabSkinCommand.java +++ b/plugin/src/main/java/me/libraryaddict/disguise/commands/utils/GrabSkinCommand.java @@ -6,11 +6,11 @@ import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.LibsPremium; import me.libraryaddict.disguise.utilities.SkinUtils; +import me.libraryaddict.disguise.utilities.reflection.NmsVersion; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; import me.libraryaddict.disguise.utilities.translations.LibsMsg; -import net.md_5.bungee.api.chat.ClickEvent; -import net.md_5.bungee.api.chat.ComponentBuilder; -import net.md_5.bungee.api.chat.HoverEvent; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; import org.apache.commons.lang.StringUtils; import org.bukkit.ChatColor; import org.bukkit.command.Command; @@ -20,6 +20,9 @@ import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitTask; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import java.util.Locale; /** @@ -126,38 +129,8 @@ public class GrabSkinCommand implements CommandExecutor { LibsMsg.GRABBED_SKIN.send(sender, nName); String string = DisguiseUtilities.getGson().toJson(profile); - int start = 0; - int msg = 1; - //if (NmsVersion.v1_13.isSupported()) { - ComponentBuilder builder = new ComponentBuilder("").append(LibsMsg.CLICK_TO_COPY.getBase()); - - while (start < string.length()) { - int end = Math.min(256, string.length() - start); - - String sub = string.substring(start, start + end); - - builder.append(" "); - - if (string.length() <= 256) { - builder.append(LibsMsg.CLICK_TO_COPY_DATA.getBase()); - } else { - builder.reset(); - builder.append(LibsMsg.CLICK_COPY.getBase(msg)); - } - - start += end; - - builder.event(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, sub)); - builder.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, - new ComponentBuilder("").append(LibsMsg.CLICK_TO_COPY_HOVER.getBase()).append(" " + msg).create())); - msg += 1; - } - - sender.spigot().sendMessage(builder.create()); - /*} else { - LibsMsg.SKIN_DATA.send(sender, string); - }*/ + sendMessage(sender, LibsMsg.CLICK_TO_COPY, LibsMsg.CLICK_TO_COPY, string, false); DisguiseUtilities.setGrabSkinCommandUsed(); } @@ -176,4 +149,84 @@ public class GrabSkinCommand implements CommandExecutor { LibsMsg.GRAB_DISG_HELP_5.send(sender); LibsMsg.GRAB_DISG_HELP_6.send(sender); } + + private Component createComponent(String text, int section, int max) { + Component component = LibsMsg.CLICK_COPY.getAdv(section); + + if (NmsVersion.v1_15.isSupported()) { + component = component.clickEvent(net.kyori.adventure.text.event.ClickEvent.copyToClipboard(text)); + } else { + component = component.clickEvent(net.kyori.adventure.text.event.ClickEvent.suggestCommand(text)); + } + + LibsMsg hover = NmsVersion.v1_15.isSupported() ? LibsMsg.CLICK_TO_COPY_HOVER_CLIPBOARD : LibsMsg.CLICK_TO_COPY_HOVER; + Component hoverText = hover.getAdv(section, max, DisguiseUtilities.getMiniMessage().escapeTags(text)); + component = component.hoverEvent(net.kyori.adventure.text.event.HoverEvent.showText(hoverText)); + + return component; + } + + private void sendMessage(CommandSender sender, LibsMsg msg, LibsMsg oldVer, String string, boolean forceAbbrev) { + TextComponent.Builder builder = Component.text().append(msg.getAdv()).appendSpace(); + + if (string.length() > 256 || forceAbbrev) { + String[] split = DisguiseUtilities.split(string); + + // Because the splitter removes the quotes.. + for (int i = 0; i < split.length; i++) { + split[i] = DisguiseUtilities.quote(split[i]); + } + + for (int i = 0; i < split.length; i++) { + if (split[i].length() <= 256) { + continue; + } + + split = Arrays.copyOf(split, split.length + 1); + + for (int a = split.length - 1; a > i; a--) { + split[a] = split[a - 1]; + } + + split[i + 1] = split[i].substring(256); + split[i] = split[i].substring(0, 256); + } + + List sections = new ArrayList<>(); + StringBuilder current = new StringBuilder(); + + for (int i = 0; i < split.length; i++) { + if (current.length() > 0) { + current.append(" "); + } + + current.append(split[i]); + + // If the next split would fit + if (split.length > i + 1 && split[i + 1].length() + current.length() + 1 <= 256) { + continue; + } + + // Have non-final end with a space + if (i + 1 < split.length) { + current.append(" "); + } + + sections.add(current.toString()); + current = new StringBuilder(); + } + + for (int i = 0; i < sections.size(); i++) { + if (i > 0) { + builder.appendSpace(); + } + + builder.append(createComponent(sections.get(i), i + 1, sections.size())); + } + } else { + builder.append(createComponent(string, 1, 1)); + } + + DisguiseUtilities.sendMessage(sender, builder.build()); + } }