// 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;
package amerifrance.guideapi.network;


public class PacketSyncHome implements IMessage, IMessageHandler<PacketSyncHome, IMessage> {

    public int page;

    public PacketSyncHome() {
        this.page = -1;
    }

    public PacketSyncHome(int page) {
        this.page = page;
    }

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

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

    @Override
    public IMessage onMessage(PacketSyncHome 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 && message.page != -1) {
            if (!book.hasTagCompound())
                book.setTagCompound(new NBTTagCompound());
            book.getTagCompound().setInteger(NBTBookTags.CATEGORY_PAGE_TAG, message.page);
            book.getTagCompound().removeTag(NBTBookTags.CATEGORY_TAG);
// 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/PacketSyncHome.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;

package amerifrance.guideapi.network;


public class PacketSyncHome implements IMessage, IMessageHandler<PacketSyncHome, IMessage> {

    public int page;

    public PacketSyncHome() {
        this.page = -1;
    }

    public PacketSyncHome(int page) {
        this.page = page;
    }

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

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

    @Override
    public IMessage onMessage(PacketSyncHome 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 && message.page != -1) {
            if (!book.hasTagCompound())
                book.setTagCompound(new NBTTagCompound());
            book.getTagCompound().setInteger(NBTBookTags.CATEGORY_PAGE_TAG, message.page);
            book.getTagCompound().removeTag(NBTBookTags.CATEGORY_TAG);
            book.getTagCompound().removeTag(NBTBookTags.ENTRY_TAG);