Solved Getting the contents of a book

Discussion in 'Plugin Help/Development/Requests' started by metmad22, Mar 17, 2015.

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

    metmad22

    Hello all. I've been trying to add some code into my plugin to get the contents of a book. Let's say I create a book. I want to check for all the pages available and get the message inside the book without replacing, modifying or resetting the book.

    How exactly would I do this? (Test codes are appreciated :)). I tried a couple of things, but had no luck, nor errors.

    Thanks, metmad22.
     
  2. Offline

    pie_flavor

    @metmad22
    Code:java
    1. //ItemStack book
    2. BookMeta meta = (BookMeta) book.getItemMeta();
    3. List<String> pages = book.getPages();
     
  3. Offline

    metmad22

    @pie_flavor Hey pie, thank you for responding. I tried that code before. The thing this, I'm using some code to get information from my server and send it over to an online website as a log. I'm using java's URLConnection to achieve this.

    I tried doing this, but it returns nothing. Just an empty [].
    Code:
    ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
            BookMeta meta = (BookMeta) book.getItemMeta();
            List<String> pages = meta.getPages();
    
    
    
    String data = URLEncoder.encode(String.valueOf(pages), "UTF-8");
    Any idea how I can fix this? All I get is this:
    "-BOOK-: []" An empty bracket. (No errors)
     
  4. Offline

    pie_flavor

    @metmad22 Try Joiner.on(" ").join(pages.toArray(new String[]{}) to get the unbroken string. Make sure you're importing com.google.common.core instead of net.minecraft.server.util.com.google.common.core.
     
  5. Offline

    metmad22

    @pie_flavor Also, on the custom logs I created if I use
    Code:
    String data = URLEncoder.encode(String.valueOf(pages.toArray()), "UTF-8");
    It gives me this in the custom log:
    [​IMG]

    @pie_flavor That doesn't seem to be working at all. No errors either.

    <Edit by mrCookieSlime: Merged posts.>
     
    Last edited by a moderator: Mar 19, 2015
  6. Offline

    nverdier

    @metmad22 Use Arrays.toString(pages.toArray()).
     
  7. Offline

    metmad22

    @nverdier

    This is what I get.
    metmad22-BOOK-: []
    [​IMG]
     
  8. Offline

    nverdier

    @metmad22 And you're sure there is stuff in the book?
     
  9. Offline

    metmad22

    @nverdier Yes. I just created a book. What I do is open a connection to the url and those strings in the pictures are the variables of the link. The log is just a basic file sheet online which gets the information.
     
  10. Offline

    nverdier

    @metmad22 Try using PlayerEditBookEvent. Just tested with
    Code:
    @EventHandler
        public void onBookEdit(PlayerEditBookEvent e)
        {
          System.out.println(Arrays.toString(e.getNewBookMeta().getPages().toArray()));
        }
     
  11. Offline

    metmad22

    metmad22-BOOK-: [This§0
    §0is§0
    §0
    §0an§0
    §0exampe, test to, check if the plugin§0
    §0
    §0
    §0is §0
    §0
    §0working!]

    It works now :) Are those signs generated by PHP? I'm using a Url encoder to encode the message before saving it though. Also, is there a way to possibly write this all in one line like a phrase? @nverdier

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

    nverdier

    @metmad22 Just replace all of the "\n" with nothing.
     
  13. Offline

    metmad22

    @nverdier I get this now.
    metmad22-BOOK-: [This§0§0is§0§0§0an§0§0exampe, test to, check if the plugin§0§0§0§0is §0§0§0working!]

    How can I remove those signs? Here is what I did:

    Arrays.toString(event.getNewBookMeta().getPages().toArray()).replaceAll("\n", "")

    Do I just filter those using regex too?
     
  14. Offline

    nverdier

    @metmad22 What are you putting into the book?
     
  15. Offline

    metmad22

    @nverdier "This is an example text to check if the plugin is working" on random lines and pages
     
  16. Offline

    nverdier

    @metmad22 And what server version are you using?
     
  17. Offline

    metmad22

  18. Offline

    nverdier

    @metmad22 Hmm, same here... I didn't get any of those symbols. Try using the code I posted.
     
  19. Offline

    metmad22

    @nverdier I did. It's because i'm sending over to a PHP code and displaying it through a link. Hmmm, if you're interested, I'll post a fix if I find one now. But thank you very much for helping me out!
     
  20. Offline

    nverdier

    @metmad22 No problem! Glad to be of help :D
     
  21. Offline

    metmad22

    @nverdier There you go. I fixed it all up now :D Here is what I used:
    Code:
    Arrays.toString(event.getNewBookMeta().getPages().toArray()).replaceAll("(Â?§0\\n?|\\s+)+", " ")
    I used that regex code to replace those weird symbols. It filters through it all.
    Result:
    Code:
    metmad22-BOOK-: [This is an exampe, test to, check if the plugin is working!]
    Thanks again!
     
  22. Offline

    nverdier

    @metmad22 Sweet! But I'm still not sure why they were there in the first place...
     
  23. Offline

    metmad22

    @nverdier Those were just the way the PHP code handled the raw text and stuff, since I'm communicating between online web servers. Anyway, that regex code saves the day :)
     
  24. Offline

    nverdier

Thread Status:
Not open for further replies.

Share This Page