Solved Break line after a period

Discussion in 'Spigot Plugin Development' started by Zain20, May 11, 2017.

  1. So, I was just playing around with some methods and I thought something, "I wonder if I could break a line after someone puts a period?". I want to be able to break the line of the lore after a player puts a period. I do not think this is possible, but if it is, I would love to know!

    Code (Text):

      public static void giveBook(Player player, String bookName, String lore){
         ItemStack book = new ItemStack(Material.BOOK);
         ItemMeta bookmeta = book.getItemMeta();
         ArrayList<String> loreS = new ArrayList<>();
         loreS.add(lore); // Line break somehow?
    Say if a player were to put 'getBook(player, "SuperBook", "On 1st line. On 2nd line")' could I make it so after the '.' goes on a new line?

    Note: Sorry if I am being very nooby right now!

    - Regards,
  2. Yeah use String[] lines = lore.split(".") which will split the lore at the periods.

    Then you can iterate over the array and add the lore in one line at a time and add the period to the end.
    for loop from 0 to lines.length
    LoreS.add(lines [ i] + ".");

    Sent from my iPhone using Tapatalk[/i]
    #2 dNiym, May 11, 2017
    Last edited: May 11, 2017
    • Like Like x 1
  3. String#split returns an array.

    Code (Java):

    String example = "hello bob. today is my birthday";

    String[] exampleParts = example.split(".");

    System.out.println(exampleParts[0]); //hello bob
    System.out.println(exampleParts[1]); // today is my birthday
  4. Thank you for the help with this!

    Edit: Wow, I did not know there was a .split() method!
  5. You could also replace the periods with the line break.
    Code (Text):
    (\\n) in this case i believe.
    • Agree Agree x 1
  6. Okay so when I try this it gives me this:

    My giveBook() method is here:
    Code (Text):
        public static void giveBook(Player player, String bookName, String lore) {
            ItemStack book = new ItemStack(Material.BED);
            ItemMeta bookmeta = book.getItemMeta();
            ArrayList<String> loreS = new ArrayList<>();
            String[] lines = lore.split(".");
            loreS.add(lines + ".");
    And, to call it I am using,
    Code (Text):
    ChatUtils.giveBook(p, "Name of book", "Lore line 1. Lore Line 2");
    Any help would be greatly appreciated, thank you!
  7. Inkzzz

    Resource Staff

    Use Pattern objects for regex splitting - compile your pattern.
    You're adding the arrays object to the lore - you want to add each element.
  8. Yes you are adding the whole lines[] array each time. What you're seeing is a memory address.
    Code (Text):

    For (i = 0; i < lines.length;i++)
        loreS.add(lines[i] + ".");

    You should also add a check before that
    If lines.length == null or lines.length = 0 to just add the string as is. In case the player does not add any periods to their lore.

    Sent from my iPhone using Tapatalk[/i]
  9. Okay, everything works for me now. Thank you guys for your help, this will probably be useful in the future! :)