// Path: src/main/java/amerifrance/guideapi/api/IGuideItem.java
// public interface IGuideItem {
// 
//     Book getBook(ItemStack stack);
// }
// 
// Path: src/main/java/amerifrance/guideapi/api/util/NBTBookTags.java
// public class NBTBookTags {
// 
//     public static String BOOK_TAG = "G-API_Book";
//     public static String CATEGORY_TAG = "G-API_Category";
//     public static String CATEGORY_PAGE_TAG = CATEGORY_TAG + "_Page";
//     public static String ENTRY_TAG = "G-API_Entry";
//     public static String ENTRY_PAGE_TAG = ENTRY_TAG + "_Page";
//     public static String PAGE_TAG = "G-API_Page";
//     public static String KEY_TAG = "G-API_Key";
// }


import amerifrance.guideapi.api.IGuideItem;
import amerifrance.guideapi.api.util.NBTBookTags;
import io.netty.buffer.ByteBuf;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
    public PacketSyncCategory(int category, int page) {
        this.category = category;
        this.page = page;
    }

    @Override
    public void fromBytes(ByteBuf buf) {
        this.category = buf.readInt();
        this.page = buf.readInt();
    }

    @Override
    public void toBytes(ByteBuf buf) {
        buf.writeInt(category);
        buf.writeInt(page);
    }

    @Override
    public IMessage onMessage(PacketSyncCategory message, MessageContext ctx) {
        ItemStack book = ctx.getServerHandler().player.getHeldItemOffhand();
        if (book.isEmpty() || !(book.getItem() instanceof IGuideItem))
            book = ctx.getServerHandler().player.getHeldItemMainhand();

        if (!book.isEmpty() && book.getItem() instanceof IGuideItem) {
            if (message.category != -1 && message.page != -1) {
                if (!book.hasTagCompound())
                    book.setTagCompound(new NBTTagCompound());

                book.getTagCompound().setInteger(NBTBookTags.CATEGORY_TAG, message.category);
                book.getTagCompound().setInteger(NBTBookTags.ENTRY_PAGE_TAG, message.page);
// Path: src/main/java/amerifrance/guideapi/api/IGuideItem.java
// public interface IGuideItem {
// 
//     Book getBook(ItemStack stack);
// }
// 
// Path: src/main/java/amerifrance/guideapi/api/util/NBTBookTags.java
// public class NBTBookTags {
// 
//     public static String BOOK_TAG = "G-API_Book";
//     public static String CATEGORY_TAG = "G-API_Category";
//     public static String CATEGORY_PAGE_TAG = CATEGORY_TAG + "_Page";
//     public static String ENTRY_TAG = "G-API_Entry";
//     public static String ENTRY_PAGE_TAG = ENTRY_TAG + "_Page";
//     public static String PAGE_TAG = "G-API_Page";
//     public static String KEY_TAG = "G-API_Key";
// }


// Path: src/main/java/amerifrance/guideapi/network/PacketSyncCategory.java
import amerifrance.guideapi.api.IGuideItem;
import amerifrance.guideapi.api.util.NBTBookTags;
import io.netty.buffer.ByteBuf;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

    public PacketSyncCategory(int category, int page) {
        this.category = category;
        this.page = page;
    }

    @Override
    public void fromBytes(ByteBuf buf) {
        this.category = buf.readInt();
        this.page = buf.readInt();
    }

    @Override
    public void toBytes(ByteBuf buf) {
        buf.writeInt(category);
        buf.writeInt(page);
    }

    @Override
    public IMessage onMessage(PacketSyncCategory message, MessageContext ctx) {
        ItemStack book = ctx.getServerHandler().player.getHeldItemOffhand();
        if (book.isEmpty() || !(book.getItem() instanceof IGuideItem))
            book = ctx.getServerHandler().player.getHeldItemMainhand();

        if (!book.isEmpty() && book.getItem() instanceof IGuideItem) {
            if (message.category != -1 && message.page != -1) {
                if (!book.hasTagCompound())
                    book.setTagCompound(new NBTTagCompound());

                book.getTagCompound().setInteger(NBTBookTags.CATEGORY_TAG, message.category);
                book.getTagCompound().setInteger(NBTBookTags.ENTRY_PAGE_TAG, message.page);
                book.getTagCompound().removeTag(NBTBookTags.ENTRY_TAG);