Solved Reading a Book

Discussion in 'Plugin Development' started by BlueMond416, Dec 17, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    BlueMond416

    I am attempting to read the contents of a single page from a book. BookMeta.getPage(#) returns a string. I have been very unsuccessful in converting this String into separate lines. Does anyone have a way of doing this?
     
  2. Offline

    Darkpicasa

    One way is counting characters per line, and formatting based on that.
    I don't know much about books in Bukkit though.
     
  3. Offline

    BlueMond416

    @Darkpicasa that wouldn't work because the lines are only as long as you make them..
    Ex:
    line1\n
    line2 and some more stuff \n
    blah blah\n
     
  4. Offline

    Skionz

  5. Offline

    BlueMond416

    I've tried using split() to create an array with the regex for backslash and then removing the n with a for loop but I get an error on that line..
     
  6. Offline

    Darkpicasa

    Oh yeah, sorry... Haven't used books in a while :p
     
  7. Offline

    BlueMond416

    @Skionz Can you split by more than one character?
     
  8. Offline

    Webbeh

    ...why couldn't you ?
     
  9. Offline

    BlueMond416

    @Webbeh Oh.. for some reason I've always been under the impression you can't..
     
  10. Offline

    Webbeh

    You can, and even more than that, you actually can use a regex.
     
  11. Offline

    BlueMond416

    Not sure what's going on..

    String page = book.getPage(1);
    String[] pageLines = page.split("\n");
    System.out.println(pageLines[0]);

    this outputs: sometext\nothertext\ntesting
    it didn't split it
     
  12. Offline

    Webbeh

    Since split uses regex, you should use "\\n", with two slashes.

    But the best way to avoid the windows/unix carriage return flaws would be to split this way :

    split("[\\r\\n]+")
     
  13. Offline

    BlueMond416

    @Webbeh Already tried it..
    Same result.
     
  14. Offline

    Webbeh

    ^updated my post.

    Code:
      String test = "asdf\njklm\r\ntest\raaaa\n\raslk\ntest";
      String[] as = test.split("[\\r\\n]+");
       
      System.out.println(as[0]);
      System.out.println(as[1]);
      System.out.println(as[2]);
      System.out.println(as[3]);
      System.out.println(as[4]);
      System.out.println(as[5]);
    This compiles fine and outputs this :
    Code:
    asdf 
    jklm 
    test 
    aaaa 
    aslk 
    test 
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  15. Offline

    BlueMond416

    @Webbeh hmmmm.. I'll try it

    @Webbeh No dice..

    @Webbeh but it's outputting like this

    this\nis\na\npage
    shouldn't it be using those carriages to take it to the next line and not outputting it?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  16. Offline

    Webbeh

    Would you please copy-paste the code here so we can see where you're doing wrong ?
     
  17. Offline

    BlueMond416

    The code:
    Code:
    @EventHandler
       public void onBookEdit(PlayerEditBookEvent event){
         BookMeta book = event.getNewBookMeta();
         if(event.isSigning()){
           System.out.println("Signing");
           if(book.getTitle().equalsIgnoreCase("[BookWarp]")){
             System.out.println("Bookwarp");
             if(book.hasPages()){
               System.out.println("has pages");
               String page = book.getPage(1);
               String[] pageLines = page.split("[\\r\\n]+");
               if(pageLines[0]!=null){
                 System.out.println(page);
                 System.out.println(pageLines[0]);
               }
             }
           }
         }
       }
    
    Outputs
    Code:
    this\nis\na\npage
    this\nis\na\npage
    
    Actually one thing I should mention now that I notice it..
    Complete Output
    Code:
    Signing
    Bookwarp
    has pages
    "this\nis\na\npage"
    "this\nis\na\npage"
    
    There are random quotations around the page and the arrays index of 0..

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  18. Offline

    Webbeh

    I actually have no idea what causes this....
     
  19. Offline

    BlueMond416

    Well I couldn't figure it out but I found another way. Even though split() wouldn't recognize any of the things I've tried, for some reason contains() and indexOf() did. I just made a function utilizing those to split the page into its lines and that seemed to work perfectly fine.
     
Thread Status:
Not open for further replies.

Share This Page