SHARE
TWEET

FancyBook.java

a guest Feb 15th, 2018 192 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import YOUR.PACKAGE.HERE.FancyHoverEvent;
  2. import io.netty.buffer.ByteBuf;
  3. import io.netty.buffer.Unpooled;
  4. import org.bukkit.Bukkit;
  5. import org.bukkit.Material;
  6. import org.bukkit.entity.Player;
  7. import org.bukkit.inventory.ItemStack;
  8.  
  9. import java.lang.reflect.Constructor;
  10. import java.util.ArrayList;
  11. import java.util.Arrays;
  12.  
  13. public class FancyBook {
  14.  
  15.     private final String title;
  16.     private final String author;
  17.  
  18.     private ArrayList<FancyBookPage> pages = new ArrayList<>();
  19.  
  20.     public FancyBook(String title, String author) {
  21.         this.title = title;
  22.         this.author = author;
  23.     }
  24.  
  25.     public void addPage(FancyBookPage page) {
  26.         pages.add(page);
  27.     }
  28.  
  29.     public void addPage(FancyBookPage... pages) {
  30.         this.pages.addAll(Arrays.asList(pages));
  31.     }
  32.  
  33.     @SuppressWarnings("deprecation")
  34.     public ItemStack getBook() {
  35.  
  36.         ItemStack stack = new ItemStack(Material.WRITTEN_BOOK);
  37.  
  38.         try {
  39.             return Bukkit.getUnsafe().modifyItemStack(stack, translateBook());
  40.         } catch (Throwable localThrowable) {
  41.             return stack;
  42.         }
  43.     }
  44.  
  45.     @SuppressWarnings("All")
  46.     public void openBook(ItemStack book, Player player) {
  47.  
  48.         int slot = player.getInventory().getHeldItemSlot();
  49.         ItemStack old = player.getInventory().getItem(slot);
  50.         player.getInventory().setItem(slot, book);
  51.  
  52.         ByteBuf buf = Unpooled.buffer(256);
  53.         buf.setByte(0, (byte) 0);
  54.         buf.writerIndex(1);
  55.  
  56.         try {
  57.             //get minecraft server version
  58.             String version = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
  59.             //get player handle
  60.             Object nmsPlayer = player.getClass().getMethod("getHandle").invoke(player);
  61.             //get player connection
  62.             Object connection = nmsPlayer.getClass().getField("playerConnection").get(nmsPlayer);
  63.             Class<?> packetDataSerializer = Class.forName("net.minecraft.server." + version + ".PacketDataSerializer");
  64.             Constructor<?> packetDataSerializerConstructor = packetDataSerializer.getConstructor(ByteBuf.class);
  65.  
  66.             Class<?> packetPlayOutCustomPayload = Class.forName("net.minecraft.server." + version + ".PacketPlayOutCustomPayload");
  67.             Constructor packetPlayOutCustomPayloadConstructor = packetPlayOutCustomPayload.getConstructor(String.class,
  68.                     Class.forName("net.minecraft.server." + version + ".PacketDataSerializer"));
  69.  
  70.             connection.getClass().getMethod("sendPacket", Class.forName("net.minecraft.server." + version + ".Packet"))
  71.                     .invoke(connection, packetPlayOutCustomPayloadConstructor.newInstance("MC|BOpen", packetDataSerializerConstructor.newInstance(buf)));
  72.  
  73.         } catch (Exception ex) {
  74.             player.getInventory().addItem(book);
  75.         }
  76.         player.getInventory().setItem(slot, old);
  77.     }
  78.  
  79.     public void openBook(Player player) {
  80.         openBook(getBook(), player);
  81.     }
  82.  
  83.     public String translateBook() {
  84.         return new Translate(title, author, pages).translate.toString();
  85.     }
  86.  
  87.     private class Translate {
  88.  
  89.         private StringBuilder translate;
  90.  
  91.         private Translate(String title, String author, ArrayList<FancyBookPage> pages) {
  92.  
  93.             translate = new StringBuilder("{pages:[");
  94.            
  95.             if (pages != null && !pages.isEmpty()) {
  96.  
  97.                 int pageNumber = 1;
  98.                 for (FancyBookPage page : pages) {
  99.                     translate.append("\"[\\\"\\\",");
  100.                     translate.append(translatePage(page));
  101.                     translate.append("]\"").append(pageNumber < pages.size() ? "," : "");
  102.                     pageNumber++;
  103.                 }
  104.             }
  105.  
  106.             translate.append(String.format("],title:%s,author:%s}", title, author));
  107.         }
  108.  
  109.         private String translatePage(FancyBookPage page) {
  110.  
  111.             StringBuilder str = new StringBuilder();
  112.             int textNumber = 1;
  113.  
  114.             for (FancyTextComponent textComponent : page.getText()) {
  115.  
  116.                 if (textNumber == 1) {
  117.                     str.append("\\\"\\\",");
  118.                 }
  119.  
  120.                 str.append(String.format("{\\\"text\\\":\\\"%s\\\",\\\"color\\\":\\\"%s\\\"%s%s%s}",
  121.                         textComponent.getText(),
  122.                         textComponent.getColor(),
  123.  
  124.                         translateAttributes(textComponent),
  125.  
  126.                         (textComponent.hasClickEvent() ? String.format(",\\\"clickEvent\\\":{\\\"action\\\":\\\"%s\\\",\\\"value\\\":\\\"%s\\\"}",
  127.                                 textComponent.getClickEvent().getClickEvent(), textComponent.getClickEvent().getValue()) : ""),
  128.  
  129.                         (textComponent.hasHoverEvent() ? String.format("%s", translateHoverEvent(textComponent.getHoverEvent())) : "")))
  130.  
  131.  
  132.                         .append(textNumber < page.getText().size() ? "," : "");
  133.                 textNumber++;
  134.             }
  135.             return str.toString();
  136.         }
  137.  
  138.         private String translateAttributes(FancyTextComponent textComponent) {
  139.             StringBuilder str = new StringBuilder();
  140.             for (String attribute : textComponent.getAttributes()) {
  141.                 str.append(String.format(",\\\"%s\\\":\\\"true\\\"", attribute));
  142.             }
  143.             return str.toString();
  144.         }
  145.  
  146.         private String translateHoverEvent(FancyHoverEvent hoverEvent) {
  147.             StringBuilder str = new StringBuilder(",\\\"hoverEvent\\\":{\\\"action\\\":\\\"show_text\\\",\\\"value\\\":{\\\"text\\\":\\\"\\\",\\\"extra\\\":[");
  148.             int textNumber = 1;
  149.  
  150.             for (FancyTextComponent textComponent : hoverEvent.getText()) {
  151.                 str.append(String.format("{\\\"text\\\":\\\"%s\\\",\\\"color\\\":\\\"%s\\\"}", textComponent.getText(), textComponent.getColor()))
  152.  
  153.                         .append(textNumber < hoverEvent.getText().size() ? "," : "");
  154.                 textNumber++;
  155.             }
  156.  
  157.             str.append("]}}");
  158.  
  159.             return str.toString();
  160.         }
  161.     }
  162. }
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