Open book for player?

Discussion in 'Plugin Development' started by Baba43, Jan 5, 2013.

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

    Baba43

    Hello,

    I want to open a specifc book for a player without giving it to the player.

    Is that possible?
     
  2. Offline

    hockeygoalie5

    Yes. You can create a custom Inventory and pass the book as the inventory holder, and save the inventory to the book using a HashMap. When they player runs the command (you can also set up the inventory opening and right-click), you can get the inventory from the HashMap and open it for the player.
     
  3. Offline

    Baba43

    So I handle a book like an inventory? :eek:
     
  4. Offline

    hockeygoalie5

    You can create custom inventories for anything. Here's an example, with each book having its own inventory:
    Code:
    HashMap<ItemStack, Inventory> bookInvs = new HashMap<ItemStack, Inventory>();
    // The second argument is how many slots the inventory has, and must be divisible by 9
    // The last argument is the title of the inventory, and is displayed at the top of the inventory GUI
    Inventory bookInv = server.createInventory(null, 9, "Book");
    // Assuming book is an existing ItemStack
    bookInvs.put(book, bookInv);
    
    Then, you can retrieve the Inventory like this:
    Code:
    // Assuming book is an existing ItemStack that has a registered Inventory
    bookInvs.get(book);
    
     
  5. Offline

    Baba43

    Okay but this will not open a book right?
     
  6. Offline

    RealDope

    He doesn't want it to have an inventory, he wants to open a book on a players screen.
     
  7. Offline

    hockeygoalie5

    Have I misinterpreted your question? I'm sorry, after rereading it seems you want to open the text of a written book?

    EDIT: Sorry. I can look into it for you, but - from what I know - you'll probably need to use a net.minecraft.server class and figure out some obfuscated method to do this.
     
    Baba43 likes this.
  8. Offline

    bobacadodl

    Sadly, opening books is client-sided :(
    Pretty sure this isn't possible. If it is, let me know! I have wanted to do this too.
    It would be extremely useful for displaying plugin help :)
     
    Baba43 likes this.
  9. Offline

    Baba43

    Thanks for your answers...
     
  10. Offline

    Intangir

    so i am thinking of doing something like this too

    can i even get notified when they open the book? or close the book? i want to make it so they cant keep the book perminently, but i want them to be able to read other peoples books
     
  11. Offline

    caseif

    You could probably find an event or two in the JavaDocs. Still not quite sure about modifying the opened book, though.
     
  12. Offline

    Comphenix

    The server is definitely notified when the user opens a book:
    Code:java
    1. @EventHandler
    2. public void onOpenBook(PlayerInteractEvent event) {
    3. ItemStack stack = event.getItem();
    4.  
    5. // Make sure this is a book
    6. if (stack != null && stack.getType() == Material.WRITTEN_BOOK) {
    7. getLogger().info("The player has opened " + stack);
    8. }
    9. }

    However, I don't think it's possible to determine if a player has closed the book. The client doesn't send any packet at all when this happens. But you could assume the book has been closed when you perform any other action (like moving).
     
  13. Offline

    Baba43

    Updates?

    It would be really cool if we could open books for players.
     
Thread Status:
Not open for further replies.

Share This Page