Tutorial Create a written book and give it to a player.

Discussion in 'Resources' started by LEEFFM, Jul 1, 2018.

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

    LEEFFM

    here i will show you how i made a written book containing a list of regions.

    first make a new class and name it Book.
    Code:
    package your.package.here;
    
    public class Book
    {
    
    }
    
    next we need to add a string to hold the title, a string to hold the author, a string to hold the contents of the page we are creating, an integer to track the amount of pages the book contains, and an integer to track the amount of lines added to the current page.
    Code:
    package your.package.here;
    
    public class Book
    {
        String title;
        String author;
        String currentPage = "";
        int numPages = 0;
        int numLines = 0;
    }
    
    next we want to add the actual item stack written book and the book meta.
    Code:
    package your.package.here;
    
    import org.bukkit.Material;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.BookMeta;
    
    public class Book
    {
        String title;
        String author;
        String currentPage = "";
        int numPages = 0;
        int numLines = 0;
    
        ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
        BookMeta bookMeta = (BookMeta) book.getItemMeta();
    }
    
    next we need to add methods that can get and set the title,author,and the number of pages.
    Code:
    package your.package.here;
    
    import org.bukkit.Material;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.BookMeta;
    
    public class Book
    {
        String title;
        String author;
        String currentPage = "";
        int numPages = 0;
        int numLines = 0;
    
        ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
        BookMeta bookMeta = (BookMeta) book.getItemMeta();
    
        public String getTitle()
        {
            return title;
        }
        public String getAuthor()
        {
            return author;
        }
        public int getNumPages()
        {
            return numPages;
        }
    
        public void setTitle(String title)
        {
            this.title = title;
            bookMeta.setTitle(title);
        }
        public void setAuthor(String author)
        {
            this.author = author;
            bookMeta.setAuthor(author);
        }
        public void setNumPages(int numPages)
        {
            this.numPages = numPages;
        }
    }
    
    next we need a method to add the current page to the book.
    Code:
    package your.package.here;
    
    import org.bukkit.Material;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.BookMeta;
    
    public class Book
    {
        String title;
        String author;
        String currentPage = "";
        int numPages = 0;
        int numLines = 0;
    
        ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
        BookMeta bookMeta = (BookMeta) book.getItemMeta();
    
        public String getTitle()
        {
            return title;
        }
        public String getAuthor()
        {
            return author;
        }
        public int getNumPages()
        {
            return numPages;
        }
    
        public void setTitle(String title)
        {
            this.title = title;
            bookMeta.setTitle(title);
        }
        public void setAuthor(String author)
        {
            this.author = author;
            bookMeta.setAuthor(author);
        }
        public void setNumPages(int numPages)
        {
            this.numPages = numPages;
        }
    
        public void addPage ()
        {
            bookMeta.addPage(currentPage);
            ++numPages;
        }
    }
    
    next we need a method to add the book title to the page then add whatever we want to the page. i made this so it can be used with loops, looping through a set of strings and using this method it will add strings to the page until the page is full then it will make a new page.
    Code:
    package your.package.here;
    
    import org.bukkit.Material;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.BookMeta;
    
    public class Book
    {
        String title;
        String author;
        String currentPage = "";
        int numPages = 0;
        int numLines = 0;
    
        ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
        BookMeta bookMeta = (BookMeta) book.getItemMeta();
    
        public String getTitle()
        {
            return title;
        }
        public String getAuthor()
        {
            return author;
        }
        public int getNumPages()
        {
            return numPages;
        }
    
        public void setTitle(String title)
        {
            this.title = title;
            bookMeta.setTitle(title);
        }
        public void setAuthor(String author)
        {
            this.author = author;
            bookMeta.setAuthor(author);
        }
        public void setNumPages(int numPages)
        {
            this.numPages = numPages;
        }
    
        public void addPage ()
        {
            bookMeta.addPage(currentPage);
            ++numPages;
        }
    
        public void addToPage (String line)
        {
            if (numLines == 13)
            {
                addPage();
                this.currentPage = "";
                this.numLines = 0;
                this.currentPage = this.currentPage + title + "\n";
                this.currentPage = this.currentPage + line + "\n";
            }
            else if (numLines == 0)
            {
                this.currentPage = this.currentPage + title + "\n";
                this.currentPage = this.currentPage + line + "\n";
            }
            else
            {
                this.currentPage = this.currentPage + line + "\n";
            }
        
            ++numLines;
        }
    }
    
    then we will need a method to add the book meta to the book item stack.
    Code:
    package your.package.here;
    
    import org.bukkit.Material;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.BookMeta;
    
    public class Book
    {
        String title;
        String author;
        String currentPage = "";
        int numPages = 0;
        int numLines = 0;
    
        ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
        BookMeta bookMeta = (BookMeta) book.getItemMeta();
    
        public String getTitle()
        {
            return title;
        }
        public String getAuthor()
        {
            return author;
        }
        public int getNumPages()
        {
            return numPages;
        }
    
        public void setTitle(String title)
        {
            this.title = title;
            bookMeta.setTitle(title);
        }
        public void setAuthor(String author)
        {
            this.author = author;
            bookMeta.setAuthor(author);
        }
        public void setNumPages(int numPages)
        {
            this.numPages = numPages;
        }
    
        public void addPage ()
        {
            bookMeta.addPage(currentPage);
            ++numPages;
        }
    
        public void addToPage (String line)
        {
            if (numLines == 13)
            {
                addPage();
                this.currentPage = "";
                this.numLines = 0;
                this.currentPage = this.currentPage + title + "\n";
                this.currentPage = this.currentPage + line + "\n";
            }
            else if (numLines == 0)
            {
                this.currentPage = this.currentPage + title + "\n";
                this.currentPage = this.currentPage + line + "\n";
            }
            else
            {
                this.currentPage = this.currentPage + line + "\n";
            }
        
            ++numLines;
        }
    
        public void addInfo()
        {
            book.setItemMeta(bookMeta);
        }
    }
    
    then we will need a way to give the book to a player.
    Code:
    package your.package.here;
    
    import org.bukkit.Material;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.BookMeta;
    
    public class Book
    {
        String title;
        String author;
        String currentPage = "";
        int numPages = 0;
        int numLines = 0;
    
        ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
        BookMeta bookMeta = (BookMeta) book.getItemMeta();
    
        public String getTitle()
        {
            return title;
        }
        public String getAuthor()
        {
            return author;
        }
        public int getNumPages()
        {
            return numPages;
        }
    
        public void setTitle(String title)
        {
            this.title = title;
            bookMeta.setTitle(title);
        }
        public void setAuthor(String author)
        {
            this.author = author;
            bookMeta.setAuthor(author);
        }
        public void setNumPages(int numPages)
        {
            this.numPages = numPages;
        }
    
        public void addPage ()
        {
            bookMeta.addPage(currentPage);
            ++numPages;
        }
    
        public void addToPage (String line)
        {
            if (numLines == 13)
            {
                addPage();
                this.currentPage = "";
                this.numLines = 0;
                this.currentPage = this.currentPage + title + "\n";
                this.currentPage = this.currentPage + line + "\n";
            }
            else if (numLines == 0)
            {
                this.currentPage = this.currentPage + title + "\n";
                this.currentPage = this.currentPage + line + "\n";
            }
            else
            {
                this.currentPage = this.currentPage + line + "\n";
            }
        
            ++numLines;
        }
    
        public void addInfo()
        {
            book.setItemMeta(bookMeta);
        }
    
        public void giveBook(Player p)
        {
            if (p.getInventory().firstEmpty() != -1)
            {
                p.getInventory().addItem(book);
            }
            else
            {
                p.sendMessage("cant give you " + title + " book, your inventory is full!");
            }
        }
    }
    
    that is a complete class to handle adding pages to a written book and giving it to a player.

    here are a couple examples on how to use this Book class:

    Ex 1. (this is how i use this class in the plugin im currently working on):
    Notes:
    - I have made a command that executes this method to give the player who executed the command a book with a list of regions in the world they are in.

    Code:
    public void reslist (CommandSender sender)
    {
        ProtectedRegion region;
        Set<String> regions;
        Player p = (Player)sender;
        World world = p.getWorld();
        RegionManager regionManager = wg.getRegionManager(world);
        
        // create the book
        Book book = new Book();
        
        // set the title and author of the book
        book.setTitle(ChatColor.GREEN + "Residence List");
        book.setAuthor(ChatColor.RED + "CityWorld Plugin");
        
        // create pages
        regions = regionManager.getRegions().keySet();
        
        for (Object regionName : regions.toArray())
        {
            region = regionManager.getRegion(regionName.toString());
            boolean bSpawnArea = region.getFlag(Flags.Spawn_Area);
            
            // ignore global region and spawn areas
            if (!region.getId().contains("__global__") && (bSpawnArea == false))
            {
                book.addToPage(ChatColor.BLUE + regionName.toString());
            }
        }
        
        // add page to the meta
        book.addPage();
        
        // add meta to book
        book.addInfo();
        
        //give player the book
        book.giveBook(p);
    }
    
    Ex 2. (Generic Example assuming your using a command to execute the method):
    Code:
    public void createBook (CommandSender sender)
    {
        Player p = (Player)sender;
        
        // create the book
        Book book = new Book();
        
        // set the title and author of the book
        book.setTitle(ChatColor.GREEN + "My Book");
        book.setAuthor(ChatColor.RED + "Me");
        
        // create pages
        book.addToPage(ChatColor.BLUE + "Welcome"); // this is the first line after the title on the page.
        book.addToPage(ChatColor.BLUE + "To");
        book.addToPage(ChatColor.BLUE + "My");
        book.addToPage(ChatColor.BLUE + "Book");
        
        // add page to the meta
        book.addPage();
        
        // add meta to book
        book.addInfo();
        
        //give player the book
        book.giveBook(p);
    }
    
    https://imgur.com/a/33aKrkO
    [​IMG]
     
    Minesuchtiiii likes this.
  2. Offline

    PhantomUnicorns

    This is pretty cool, I'll be adding a variation to my collection of classes.

    PS: I recommend putting the code in spoilers so it's easier to navigate the thread.

    The variation I made:
    Book class extending ItemStack (open)

    Code:
    import java.util.List;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.BookMeta;
    
    public class Book extends ItemStack {
    
        private BookMeta _bookMeta;
    
        private boolean _titleAtTop;
    
        public Book(int amount) {
            super(Material.WRITTEN_BOOK, amount);
            this._bookMeta = (BookMeta) getItemMeta();
            this._titleAtTop = false;
        }
    
        public Book(int amount, boolean titleAtTop) {
            super(Material.WRITTEN_BOOK, amount);
            this._bookMeta = (BookMeta) getItemMeta();
            this._titleAtTop = titleAtTop;
        }
    
        public BookMeta getBookMeta() {
            return this._bookMeta;
        }
    
        public void setAuthor(String author) {
            this._bookMeta.setAuthor(ChatColor.translateAlternateColorCodes('&', author + "&f"));
            setItemMeta(this._bookMeta);
        }
    
        public void setTitle(String title) {
            this._bookMeta.setTitle(ChatColor.translateAlternateColorCodes('&', title + "&f"));
            setItemMeta(this._bookMeta);
        }
    
        public void addPage(String... lines) {
            int offset = this._titleAtTop ? 1 : 0;
            String[] array = new String[lines.length + offset];
            for (int i = offset; i < lines.length; ++i) {
                array[i + 1] = ChatColor.translateAlternateColorCodes('&', lines[i] + "&f");
            }
            if (offset == 1) {
                array[0] = this._bookMeta.getTitle();
            }
            this._bookMeta.addPage(array);
            setItemMeta(this._bookMeta);
        }
    
        public String[] getPage(int page) {
            return this._bookMeta.getPage(page).split("\n");
        }
    
        public boolean removePage(int page) {
            List<String> pages = this._bookMeta.getPages();
            if (pages.size() >= page) {
                pages.remove(page);
                this._bookMeta.setPages(pages);
                setItemMeta(this._bookMeta);
                return true;
            }
            return false;
        }
    
        public void addLine(String line) {
            List<String> pages = this._bookMeta.getPages();
            if (pages.size() == 0) {
                addPage(line);
            } else {
                if (pages.get(pages.size() - 1).split("\n").length == 13) {
                    addPage(line);
                } else {
                    pages.add(line);
                    this._bookMeta.setPages(pages);
                    setItemMeta(this._bookMeta);
                }
            }
        }
    }
    
     
    Last edited: Jul 2, 2018
Thread Status:
Not open for further replies.

Share This Page