<commit_before>package amerifrance.guideapi.proxy;

import amerifrance.guideapi.api.GuideAPI;
import amerifrance.guideapi.api.IGuideItem;
import amerifrance.guideapi.api.impl.Book;
import amerifrance.guideapi.api.impl.abstraction.CategoryAbstract;
import amerifrance.guideapi.api.impl.abstraction.EntryAbstract;
import amerifrance.guideapi.api.util.NBTBookTags;
import amerifrance.guideapi.gui.GuiCategory;
import amerifrance.guideapi.gui.GuiEntry;
import amerifrance.guideapi.gui.GuiHome;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.IGuiHandler;

public class CommonProxy implements IGuiHandler {

    @Override
    public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
        return null;
    }

    @Override
    public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
        ItemStack stack = player.getHeldItemOffhand();
        if (stack == null || !(stack.getItem() instanceof IGuideItem))
            stack = player.getHeldItemMainhand();

        if (stack != null && stack.getItem() instanceof IGuideItem) {
            Book book = GuideAPI.BOOKS.getValues().get(ID);
            if (stack.hasTagCompound()) {
                NBTTagCompound tagCompound = stack.getTagCompound();
                if (tagCompound.hasKey(NBTBookTags.ENTRY_TAG) && tagCompound.hasKey(NBTBookTags.CATEGORY_TAG)) {
                    CategoryAbstract category = book.getCategoryList().get(tagCompound.getInteger(NBTBookTags.CATEGORY_TAG));
                    EntryAbstract entry = category.entries.get(new ResourceLocation(tagCompound.getString(NBTBookTags.ENTRY_TAG)));
                    int pageNumber = tagCompound.getInteger(NBTBookTags.PAGE_TAG);
                    GuiEntry guiEntry = new GuiEntry(book, category, entry, player, stack);
                    guiEntry.pageNumber = pageNumber;
                    return guiEntry;
                } else if (tagCompound.hasKey(NBTBookTags.CATEGORY_TAG)) {
                    CategoryAbstract category = book.getCategoryList().get(tagCompound.getInteger(NBTBookTags.CATEGORY_TAG));
                    int entryPage = tagCompound.getInteger(NBTBookTags.ENTRY_PAGE_TAG);
                    GuiCategory guiCategory = new GuiCategory(book, category, player, stack);
                    guiCategory.entryPage = entryPage;
                    return guiCategory;
                } else {
                    int categoryNumber = tagCompound.getInteger(NBTBookTags.CATEGORY_PAGE_TAG);
                    GuiHome guiHome = new GuiHome(book, player, stack);
                    guiHome.categoryPage = categoryNumber;
                    return guiHome;
                }
            }

            return new GuiHome(book, player, stack);
        }

        return null;
    }<commit_msg>Fix crash if book attempts to open non-existent page<commit_after>package amerifrance.guideapi.proxy;

import amerifrance.guideapi.api.GuideAPI;
import amerifrance.guideapi.api.IGuideItem;
import amerifrance.guideapi.api.impl.Book;
import amerifrance.guideapi.api.impl.abstraction.CategoryAbstract;
import amerifrance.guideapi.api.impl.abstraction.EntryAbstract;
import amerifrance.guideapi.api.util.NBTBookTags;
import amerifrance.guideapi.gui.GuiCategory;
import amerifrance.guideapi.gui.GuiEntry;
import amerifrance.guideapi.gui.GuiHome;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.IGuiHandler;

public class CommonProxy implements IGuiHandler {

    @Override
    public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
        return null;
    }

    @Override
    public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
        ItemStack stack = player.getHeldItemOffhand();
        if (stack == null || !(stack.getItem() instanceof IGuideItem))
            stack = player.getHeldItemMainhand();

        if (stack != null && stack.getItem() instanceof IGuideItem) {
            Book book = GuideAPI.BOOKS.getValues().get(ID);
            try {
                if (stack.hasTagCompound()) {
                    NBTTagCompound tagCompound = stack.getTagCompound();
                    if (tagCompound.hasKey(NBTBookTags.ENTRY_TAG) && tagCompound.hasKey(NBTBookTags.CATEGORY_TAG)) {
                        CategoryAbstract category = book.getCategoryList().get(tagCompound.getInteger(NBTBookTags.CATEGORY_TAG));
                        EntryAbstract entry = category.entries.get(new ResourceLocation(tagCompound.getString(NBTBookTags.ENTRY_TAG)));
                        int pageNumber = tagCompound.getInteger(NBTBookTags.PAGE_TAG);
                        GuiEntry guiEntry = new GuiEntry(book, category, entry, player, stack);
                        guiEntry.pageNumber = pageNumber;
                        return guiEntry;
                    } else if (tagCompound.hasKey(NBTBookTags.CATEGORY_TAG)) {
                        CategoryAbstract category = book.getCategoryList().get(tagCompound.getInteger(NBTBookTags.CATEGORY_TAG));
                        int entryPage = tagCompound.getInteger(NBTBookTags.ENTRY_PAGE_TAG);
                        GuiCategory guiCategory = new GuiCategory(book, category, player, stack);
                        guiCategory.entryPage = entryPage;
                        return guiCategory;
                    } else {
                        int categoryNumber = tagCompound.getInteger(NBTBookTags.CATEGORY_PAGE_TAG);
                        GuiHome guiHome = new GuiHome(book, player, stack);
                        guiHome.categoryPage = categoryNumber;
                        return guiHome;
                    }
                }
            } catch (Exception e) {
                // No-op: Catching if the saved page/entry/category doesn't exist anymore
            }

            return new GuiHome(book, player, stack);
        }

        return null;
    }
{'fraction_non_alphanumeric': 0.08687411151476859, 'fraction_numerical': 0.0, 'mean_word_length': 2.5473389355742295, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2493379', 'n_tokens_mistral': 1804, 'n_tokens_neox': 1744, 'n_words': 341}