Advertisement
Tux2

Simple Bukkit Book API

Aug 15th, 2012
2,296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.85 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2012  Joshua Reetz
  3.  
  4.     This program is free software: you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation, either version 3 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16.  */
  17.  
  18. import net.minecraft.server.NBTTagCompound;
  19. import net.minecraft.server.NBTTagList;
  20. import net.minecraft.server.NBTTagString;
  21.  
  22. import org.bukkit.craftbukkit.inventory.CraftItemStack;
  23.  
  24. public class BookItem {
  25.    
  26.     private net.minecraft.server.ItemStack item = null;
  27.     private CraftItemStack stack = null;
  28.    
  29.     public BookItem(org.bukkit.inventory.ItemStack item) {
  30.         if(item instanceof CraftItemStack) {
  31.             stack = (CraftItemStack)item;
  32.             this.item = stack.getHandle();
  33.         }else if(item instanceof org.bukkit.inventory.ItemStack) {
  34.             stack = new CraftItemStack(item);
  35.             this.item = stack.getHandle();
  36.         }
  37.     }
  38.    
  39.     public String[] getPages() {
  40.         NBTTagCompound tags = item.getTag();
  41.         if(tags == null) {
  42.             return null;
  43.         }
  44.         NBTTagList pages = tags.getList("pages");
  45.         String[] pagestrings = new String[pages.size()];
  46.         for(int i = 0; i < pages.size(); i++) {
  47.             pagestrings[i] = pages.get(i).toString();
  48.         }
  49.         return pagestrings;
  50.     }
  51.    
  52.     public String getAuthor() {
  53.         NBTTagCompound tags = item.getTag();
  54.         if(tags == null) {
  55.             return null;
  56.         }
  57.         String author = tags.getString("author");
  58.         return author;
  59.     }
  60.    
  61.     public String getTitle() {
  62.         NBTTagCompound tags = item.getTag();
  63.         if(tags == null) {
  64.             return null;
  65.         }
  66.         String title = tags.getString("title");
  67.         return title;
  68.     }
  69.    
  70.     public void setPages(String[] newpages) {
  71.         NBTTagCompound tags = item.tag;
  72.         if (tags == null) {
  73.             tags = item.tag = new NBTTagCompound();
  74.         }
  75.         NBTTagList pages = new NBTTagList("pages");
  76.         //we don't want to throw any errors if the book is blank!
  77.         if(newpages.length == 0) {
  78.             pages.add(new NBTTagString("1", ""));
  79.         }else {
  80.             for(int i = 0; i < newpages.length; i++) {
  81.                 pages.add(new NBTTagString("" + i + "", newpages[i]));
  82.             }
  83.         }
  84.         tags.set("pages", pages);
  85.     }
  86.    
  87.     public void addPages(String[] newpages) {
  88.         NBTTagCompound tags = item.tag;
  89.         if (tags == null) {
  90.             tags = item.tag = new NBTTagCompound();
  91.         }
  92.         NBTTagList pages;
  93.         if(getPages() == null) {
  94.             pages = new NBTTagList("pages");
  95.         }else {
  96.             pages = tags.getList("pages");
  97.         }
  98.         //we don't want to throw any errors if the book is blank!
  99.         if(newpages.length == 0 && pages.size() == 0) {
  100.             pages.add(new NBTTagString("1", ""));
  101.         }else {
  102.             for(int i = 0; i < newpages.length; i++) {
  103.                 pages.add(new NBTTagString("" + pages.size() + "", newpages[i]));
  104.             }
  105.         }
  106.         tags.set("pages", pages);
  107.     }
  108.    
  109.     public void setAuthor(String author) {
  110.         NBTTagCompound tags = item.tag;
  111.         if (tags == null) {
  112.             tags = item.tag = new NBTTagCompound();
  113.         }
  114.         if(author != null && !author.equals("")) {
  115.             tags.setString("author", author);
  116.         }
  117.     }
  118.    
  119.     public void setTitle(String title) {
  120.         NBTTagCompound tags = item.tag;
  121.         if (tags == null) {
  122.             tags = item.tag = new NBTTagCompound();
  123.         }
  124.         if(title != null && !title.equals("")) {
  125.             tags.setString("title", title);
  126.         }
  127.     }
  128.    
  129.     public org.bukkit.inventory.ItemStack getItemStack() {
  130.         return stack;
  131.     }
  132.  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement