Search in sources :

Example 1 with GlowAnvilInventory

use of net.glowstone.inventory.GlowAnvilInventory in project Glowstone by GlowstoneMC.

the class PluginMessageHandler method handleInternal.

private void handleInternal(GlowSession session, String channel, byte... data) {
    /*
        MC|Brand
            entire data: string of client's brand (e.g. "vanilla")
        MC|BEdit
            item stack: new book item (should be verified)
        MC|BSign
            item stack: new book item (should be verified)
        MC|TrSel
            int: villager trade to select
        MC|AdvCdm
            byte: mode
            if 0:
                int x, int y, int z (command block in world)
            if 1:
                int entity (command block minecart)
            string: command to set
        MC|Beacon
            two ints, presumably the selected enchants
        MC|ItemName
            entire data: name to apply to item in anvil
         */
    ByteBuf buf = Unpooled.wrappedBuffer(data);
    switch(channel) {
        case "MC|Brand":
            // vanilla server doesn't handle this, for now just log it
            String brand = null;
            try {
                brand = ByteBufUtils.readUTF8(buf);
            } catch (IOException e) {
                GlowServer.logger.log(Level.WARNING, "Error reading client brand of " + session, e);
            }
            if (brand != null && !brand.equals("vanilla")) {
                GlowServer.logger.info("Client brand of " + session.getPlayer().getName() + " is: " + brand);
            }
            break;
        case "MC|BEdit":
            {
                // read and verify stack
                ItemStack item = GlowBufUtils.readSlot(buf);
                //GlowServer.logger.info("BookEdit [" + session.getPlayer().getName() + "]: " + item);
                if (item == null || item.getType() != Material.BOOK_AND_QUILL) {
                    return;
                }
                ItemMeta meta = item.getItemMeta();
                if (!(meta instanceof BookMeta)) {
                    return;
                }
                BookMeta book = (BookMeta) meta;
                if (!book.hasPages()) {
                    return;
                }
                // verify item in hand
                ItemStack inHand = session.getPlayer().getItemInHand();
                if (inHand == null || inHand.getType() != Material.BOOK_AND_QUILL) {
                    return;
                }
                ItemMeta handMeta = inHand.getItemMeta();
                if (!(handMeta instanceof BookMeta)) {
                    return;
                }
                BookMeta handBook = (BookMeta) handMeta;
                // apply pages to book
                handBook.setPages(book.getPages());
                inHand.setItemMeta(handBook);
                session.getPlayer().setItemInHand(inHand);
                break;
            }
        case "MC|BSign":
            // read and verify stack
            ItemStack item = GlowBufUtils.readSlot(buf);
            //GlowServer.logger.info("BookSign [" + session.getPlayer().getName() + "]: " + item);
            if (item == null || item.getType() != Material.WRITTEN_BOOK) {
                return;
            }
            ItemMeta meta = item.getItemMeta();
            if (!(meta instanceof BookMeta)) {
                return;
            }
            BookMeta book = (BookMeta) meta;
            if (!book.hasPages() || !book.hasTitle()) {
                return;
            }
            // verify item in hand
            ItemStack inHand = session.getPlayer().getItemInHand();
            if (inHand == null || inHand.getType() != Material.BOOK_AND_QUILL) {
                return;
            }
            ItemMeta handMeta = inHand.getItemMeta();
            if (!(handMeta instanceof BookMeta)) {
                return;
            }
            BookMeta handBook = (BookMeta) handMeta;
            // apply pages, title, and author to book
            handBook.setAuthor(session.getPlayer().getName());
            handBook.setTitle(book.getTitle());
            handBook.setPages(book.getPages());
            inHand.setType(Material.WRITTEN_BOOK);
            inHand.setItemMeta(handBook);
            session.getPlayer().setItemInHand(inHand);
            break;
        case "MC|ItemName":
            if (session.getPlayer().getOpenInventory() == null) {
                break;
            }
            // check if player is in an anvil inventory
            if (session.getPlayer().getOpenInventory().getType() != InventoryType.ANVIL) {
                break;
            }
            // get the new name for the item
            String name;
            try {
                name = ByteBufUtils.readUTF8(buf);
            } catch (IOException e) {
                GlowServer.logger.log(Level.WARNING, "Error reading anvil item name by " + session, e);
                break;
            }
            ((GlowAnvilInventory) session.getPlayer().getOpenInventory().getTopInventory()).setRenameText(name);
            break;
        default:
            GlowServer.logger.info(session + " used unknown Minecraft channel: " + channel);
            break;
    }
    buf.release();
}
Also used : GlowAnvilInventory(net.glowstone.inventory.GlowAnvilInventory) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) ItemStack(org.bukkit.inventory.ItemStack) BookMeta(org.bukkit.inventory.meta.BookMeta) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)1 IOException (java.io.IOException)1 GlowAnvilInventory (net.glowstone.inventory.GlowAnvilInventory)1 ItemStack (org.bukkit.inventory.ItemStack)1 BookMeta (org.bukkit.inventory.meta.BookMeta)1 ItemMeta (org.bukkit.inventory.meta.ItemMeta)1