Solved Splitting onCommand arguments using new line character

Discussion in 'Spigot Plugin Development' started by asenthd, Mar 21, 2020.

Thread Status:
Not open for further replies.
  1. I've been working on a private chat formatting commission and I keep getting stuck with this stupid separator. I'm trying to split the argument by newline characters so that I can center each line individually, regex is definitely not my strongsuit.

    This code is inside of an onCommand
    Code (Java):
                String message = ChatUtils.translate(StringUtils.join(args, ' ', 0, args.length));
                String[] messageSeperated = message.split("\\r?\\n");
                for(String s : messageSeperated) {
                    sender.sendMessage(s);
                }
    For some reason instead of splitting the string by newline characters it's keeping them and returning the whole string.

    [​IMG]

    Any help or advice would be appreciated :)
     
  2. you have \\r? in there... i dont think \\n would register as regex, so if you're trying to split for \n, just put "\\n"? am confused.

    /e if it does register as regex, declare a Pattern#quote to use as the splitter for \\n
     
  3. I've attempted to do so using just \\n as well with no luck, the \\r retrieves the raw string with no parsing but I had no luck
     
  4. I personally use this to send multiple lines;
    Code (Java):
    public static void sendMessage(CommandSender sender, String... message) {
            for(String s : message) {
                sender.sendMessage(Utils.colour(s));
            }
        }

        private void sendHelpMessage(Player player) {
            sendMessage(player, "Line!", "Line2", "Line3!");
        }
    You can shorten the for loop if you wish with Java 8 stuff. I was lazy writing this out for now.

    While I see this being kinda niche for certain stuff. It's one solution.

    Here's a solution to yours.
    Code (Java):
    public static void sendTest(CommandSender sender) {
            String message = "Hey\nPlayer!";
            String[] messageSeperated = message.split("\n");
            for(String s : messageSeperated) {
                sender.sendMessage(s);
            }
        }
     
    • Like Like x 1
  5. That solution doesn't work because Im retrieving the message from the arguments. So even using the seperator as just "\n" java isnt taking it in correctly to split it.
     
  6. Oh my bad.

    Here you go.
    Code (Text):

            String message = StringUtils.join(args, ' ', 0, args.length).replace("\\n", "\n");
            sender.sendMessage(message);
     
     
    • Like Like x 1
  7. Thanks a ton, I used what you gave me and mixed it up (I needed to split it so I could center each split string).
    This ended up being the final code I used.

    Code (Java):
                String message = ChatUtils.translate(StringUtils.join(args, ' ', 0, args.length).replace("\\n", "\n"));
                String[] messageSeperated = message.split("\n");
                for(String s : messageSeperated) {
                    sender.sendMessage(ChatUtils.centerMessage(s));
                }
     
    • Like Like x 1
Thread Status:
Not open for further replies.