Simple Temp Book API

Discussion in 'Resources' started by Tux2, Aug 15, 2012.

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


    Hello Guys,
    I know there's already a Book API out there, but it seems there's a bit of a kludge with it as it needs two files in order to run. Below I have my one file API for book support:

    UPDATE: Since Bukkit is changing a few things, this static code won't work on future minecraft versions. I offer you the alternative, use the TuxTwoLib which will always have this file in it compiled for future minecraft versions! You will need to import Tux2.TuxTwoLib.BookItem and require TuxTwoLib in your plugin.yml.

    An example of using this:
    //Creating a book.
    BookItem bi = new BookItem(new ItemStack(387,1));
    //When making an ItemStack it isn't automatically the correct
    //format, so we need to make sure to get the one from
    //book item.
    ItemStack writtenbook = bi.getItemStack();
    //Reading a book.
    BookItem bi = new BookItem(book);
    //If any of these fields don't exist they return null.
    String[] pages = bi.getPages();
    String author = bi.getAuthor();
    String title = bi.getTitle();
    Please note that this was created independently of the other one out there and is a result of my research for the MultiInv plugin. This library was created for my InfiniteKits plugin and has been tested to work. You will need to be compiling against CraftBukkit and not the Bukkit library, otherwise you'll have import exceptions.
  2. Works as expected :} thanks !

    I made a small change tough (like I did when I tested the other book API), instead of 'String[]' for add/setPages() I used 'String...' so I can use setPages("", "", "", "", "", etc); which also supports setPages(new String[] { "", "", etc });

    EDIT: Also, you should add the character limit checks as well.... 16 for title and author and 255 for pages.
  3. Offline


    Thanks for the feedback Digi! I'll probably add the 255 character limit for pages, and the String... change, although the limit for authors/titles, I'm not quite sure. I'm quite fond of my 26 character title in my kits plugin that displays perfectly fine on my client :D
  4. Oh I see, good to know. I noticed that 16 character limit in the other API so I thought the game would freak if you set more... xD you should re-check the 255 limit as well :}
    I think I'll gonna test some values myself :-?
  5. Offline


  6. Max 50 pages ? I got to 192 when I tested this API xD
    Yes, I guess those limits are enforced by the client when writing books manually, not by using plugins, it's good that there's no hard limit xD

    Quickly tested with 1000 pages, works just fine :}, I didn't browse all the way to page 1000 tough xD

    EDIT #2:
    Hmm, I found a limit... the title, the author, contents and number of pages all add up to some size that can't be exeeded, if it is it will give invalid packet or end of stream errors to the client.

    EDIT #3: Also, you can't use "\n\n" or more to skip more lines, you *need* to place a character between them... to place an invisible character just use any ChatColor value :}
  7. Offline


    lol, 1000 pages?!?! That's the size of a novel! Now just watch as a server opens up as a book club where players can "check out" books from a library and read them in game using project gutenberg and a plugin to put the contents of the book into a minecraft book... ;)
  8. Yes well, 1000 pages with a few characters per page works ok, but if you want 256 characters per page, I think like 100 or 200 pages work.
    The limit is for the packet sent to the player with the pages, title and author.
  9. Offline


    Currently, I have this:
    if(cmd.getName().equalsIgnoreCase("book")) {
            BookItem bi = new BookItem(new ItemStack(387,1));
            String[] pages = null;
            String author = (getConfig().getString("author"));
            String title = "BookTitle";
            ItemStack writtenbook = bi.getItemStack();
    How should I spawn the book, and how do I write words in books?
  10. The "pages" variable is where you write stuff... do you even understand that code you have ?

    And you can either give the itemstack to a player or drop it as a pickup in a world at a location.
  11. Offline


    How do I check if the item a player has in his hands is a written and signed book?
    I've got
    if (!e.getPlayer().getItemInHand().equals(Material.WRITTEN_BOOK))
    now but that doesn't seem to work...
  12. And how doesn't that work ?
    Also, you can just compare them with == because Material is an enum.

    To check if the book is signed I guess you can check for the author... use new BookItem(player's item) and then check book's title against null.
  13. Offline


    if (e.getPlayer().getItemInHand().getTypeID() == 387)
  14. Offline


    Ty tux, found that one already XD
  15. Offline


    You can use /n and other formats when using the String.format method.
  16. Offline


    I got a Eclipse's error in the imports, what should I do to fix this?
  17. Offline


    I have an error on bookitem, it wont allow me to use it or import it
  18. Offline


    You need to copy BookItem from the original link. Add it to your package.
  19. Offline


    Lovely lib, made my day.
  20. Offline


    I'd like to know when the bukkit devs plan to add the official book api. I've been holding off adding books to my plugin, but if it wont be for weeks or months, I may go with Tux2's api. advice?
  21. lorenzo_p
    You'll gonna wait a while, I recommend you just use this.
  22. Offline


    Great lib. I make it static so I can use it like that:
    2. Books.setTitle(someBook);
  23. That would be very wrong tough because it can't know what book you're refering to.
    You should either add an argument to specify the book or make a Books.editBook() to asign the currently edited book... but isn't reliable.

    You'd be better off using the instanced way, it's better.
  24. Offline


    I know.I've done class and there was no error, but then i tested it and it wasn't working.And now I realized that i posted wrong code becouse after someBook i also pass String.
  25. Offline


    As he said, making it static wouldn't be a good idea as you would have to have a whole bunch of code duplication and it could get unreliable. (It would never set the title on a new org.bukkit.ItemStack properly)
  26. Offline


    So, I've never done this before, but I imagine I'm supposed to copy/paste the pastebin text into .java file, save it in my API's folder with bukkit.jar (or wherever really), and then import it in a similar way as I did the bukkit.jar?

    Or do you just create class in every project separately?
  27. That.

    Making it a separate jar would require it to be released along with it.
  28. Offline


    Quick question for anyone who might happen to know: is it normal behaviour for written books to go blank when dropped?

    Love the temporary API by the way - works a treat :)
  29. No, I don't think it is normal, people must be able to fully share them, dropped, stored in chests, etc, so they shouldn't get suddenly blank.

    Did that happen to you when you wrote them with this API ? It might not be because of this API but you should test it with a book written the normal way.... or maybe you struck some kind of silent limit.
  30. Offline


    This is with books written normally - I'm using the API to check the contents later. All works fine until a book is dropped (including on death) at which point the book goes blank. Putting a book in a chest doesn't have the problem, and they survive a server restart. Guess I'll need test this out in vanilla...
Thread Status:
Not open for further replies.

Share This Page