How to read data from a book on player's hand?

Discussion in 'Plugin Development' started by Hohohooo, Apr 3, 2015.

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

    Hohohooo

    I want to read it and save it to a variable. Could you tell me how can I do that?
     
  2. Offline

    THEPADA

    Material mat = p.getItemInHand().getType(); // is what he has in hand

    but probbably you should specify what you mean
    :)
     
  3. @Hohohooo Get their item. Check that they are holding a book. Get the ItemMeta. Cast it to BookMeta.
     
  4. Offline

    Hohohooo

    I just want to read the text of the book= =
     
    Last edited: Apr 3, 2015
  5. Offline

    THEPADA

    sorry i missed Book ._.


    Code:
    String wholeBook = "";
    
    Player p = YOUR_PLAYER;
    BookMeta bookMeta = (BookMeta) p.getItemInHand().getItemMeta();
    List<String> bookContent = bookMeta.getPages();
             
    for(String s:bookContent)
    {
          wholeBook += s;
    }
    
    
    don't now if its working but it should...

    It's whatt DJ skepter said just in code :)
     
    Last edited: Apr 3, 2015
  6. Offline

    teej107

    @THEPADA Like every other spoonfed code, it is terrible and should not be used.
     
  7. Offline

    Code0

    @teej107 What exactly is terrible about it? I was gonna use this but now...
     
  8. Offline

    teej107

    @Code0 The item in the players hand may be null and may not even be a book. Also it is inefficient to concatenate Strings like that. You should use a StringBuilder.
     
  9. Code:
    //Checks
    String wholeBook = "";
    
    List<String> bookContent = bookMeta.getPages();
    StringBuilder sb = new StringBuilder();
    
    for(String string : bookContent) {
        sb.append(string); //You can append in so many ways
    }
    
    wholeBook = sb.toString();
    
    
     
  10. Offline

    Hohohooo

    I've imported these classes:
    Code:
    import org.bukkit.plugin.java.*;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.command.*;
    import org.bukkit.inventory.meta.BookMeta;
    
    import java.awt.List;
    import java.util.*;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    In "List bookContent = bookMeta.getPages();",the "bookMeta.getPages()" is null. What shall I do?
     
  11. Offline

    I Al Istannen

    @Hohohooo How did you obtain the BookMeta?
    I tried it out myself and and it worked just like the others said.

    BookMeta bookMeta = (BookMeta) player.getItemInHand().getItemMeta();

    The list just gave back an empty String if the book was empty.
     
    Last edited: Apr 11, 2015
  12. Offline

    nverdier

    After you check that the meta is in fact an instance of BookMeta.
     
Thread Status:
Not open for further replies.

Share This Page