SHARE
TWEET

Untitled

a guest Apr 1st, 2019 63 in 94 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class BookCreator
  2. {
  3.     private static boolean initialised = false;
  4.     private static Method getHandle;
  5.     private static Method openBook;
  6.  
  7.     static {
  8.         try {
  9.             getHandle = ReflectionUtils.getReflection().getCBClass("entity.CraftPlayer").getMethod("getHandle");
  10.             openBook = ReflectionUtils.getReflection().getNMSClass("EntityPlayer").
  11.                     getMethod("a", ReflectionUtils.getReflection().getNMSClass("ItemStack"),
  12.                             ReflectionUtils.getReflection().getNMSClass("EnumHand"));
  13.         } catch(Exception e) {
  14.             e.printStackTrace();
  15.             Bukkit.getServer().getLogger().warning("Não se pode forçar a abrir o livro!");
  16.             initialised = false;
  17.         }
  18.     }
  19.  
  20.     private ItemStack book;
  21.     private List<BaseComponent[]> pages;
  22.  
  23.     public BookCreator(String name, String author)
  24.     {
  25.         book = new ItemStack(Material.WRITTEN_BOOK);
  26.         BookMeta meta = (BookMeta) book.getItemMeta();
  27.         meta.setAuthor(author);
  28.         meta.setTitle(name);
  29.         meta.setGeneration(BookMeta.Generation.COPY_OF_ORIGINAL);
  30.         book.setItemMeta(meta);
  31.         pages = new ArrayList<>();
  32.     }
  33.  
  34.     public static boolean isInitialised(){
  35.         return initialised;
  36.     }
  37.  
  38.     public boolean openBook(Player p) {
  39.         if (!initialised) return false;
  40.         ItemStack held = p.getInventory().getItemInMainHand();
  41.         try {
  42.             p.getInventory().setItemInMainHand(this.create());
  43.             sendPacket(this.create(), p);
  44.         } catch (ReflectiveOperationException e) {
  45.             e.printStackTrace();
  46.             initialised = false;
  47.         }
  48.         p.getInventory().setItemInMainHand(held);
  49.         return initialised;
  50.     }
  51.  
  52.     private  void sendPacket(ItemStack i, Player p) throws ReflectiveOperationException {
  53.         Object entityplayer = getHandle.invoke(p);
  54.         Class<?> enumHand = ReflectionUtils.getReflection().getNMSClass("EnumHand");
  55.         Object[] enumArray = enumHand.getEnumConstants();
  56.         openBook.invoke(entityplayer, getItemStack(i), enumArray[0]);
  57.     }
  58.  
  59.     private Object getItemStack(ItemStack item) {
  60.         try {
  61.             Method asNMSCopy = ReflectionUtils.getReflection().getNMSClass("CraftItemStack").getMethod("asNMSCopy", ItemStack.class);
  62.             return asNMSCopy.invoke(null, item);
  63.         } catch (Exception e) {
  64.             e.printStackTrace();
  65.         }
  66.         return null;
  67.     }
  68.  
  69.     public void addPage(PageCreator page)
  70.     {
  71.         pages.add(page.create());
  72.     }
  73.  
  74.     public ItemStack create()
  75.     {
  76.         BookMeta meta = (BookMeta) book.getItemMeta();
  77.  
  78.         try {
  79.             List<Object> pagec = (List<Object>) ReflectionUtils.getReflection().getCBClass("inventory.CraftMetaBook").getDeclaredField("pages").get(meta);
  80.             //List<IChatBaseComponent> pagec = (List<IChatBaseComponent>) CraftMetaBook.class.getDeclaredField("pages").get(meta);
  81.             //IChatBaseComponent page = IChatBaseComponent.ChatSerializer.a(ComponentSerializer.toString(pages.get(0)));
  82.             Method m = ReflectionUtils.getReflection().getNMSClass("IChatBaseComponent$ChatSerializer").getMethod("a", String.class);
  83.             for(BaseComponent[] page: pages)
  84.             {
  85.                 pagec.add(m.invoke(null, ComponentSerializer.toString(page)));
  86.             }
  87.             book.setItemMeta(meta);
  88.         } catch (Exception e) {
  89.             e.printStackTrace();
  90.         }
  91.  
  92.         return book;
  93.     }
  94. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top