So, I'm attempting to add a custom book to my HarvestCraft mod that just uses the vanilla MC book GUI and NBT system but has a specific texture (hence the need for a new item class).
Can anyone explain why this book can be created with its recipe (showing the correct texture, author, and title) but it won't open like a normal book?
package com.pam.harvestcraft;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemEditableBook;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraft.world.World;
public class ItemPamBook extends ItemEditableBook
{
public ItemPamBook()
{
this.setMaxStackSize(1);
}
private NBTTagList putTableOfContents(NBTTagList bookTagList)
{
NBTTagList bookPages = new NBTTagList();
bookPages.appendTag(new NBTTagString("Page 1"));
return bookTagList;
}
public void onUpdate(ItemStack itemStack, World world, Entity entity, int unknownInt, boolean unknownBool)
{
NBTTagList bookTagList = new NBTTagList();
bookTagList = putTableOfContents(bookTagList);
itemStack.setTagInfo("pages", bookTagList);
itemStack.setTagInfo("author", new NBTTagString("Pam"));
itemStack.setTagInfo("title", new NBTTagString("HarvestCraft Cookbook"));
}
@SideOnly(Side.CLIENT)
public boolean hasEffect(ItemStack par1ItemStack)
{
return false;
}
}
I realize this is now almost a year old, but I did find the solution thanks to the hint above. Thought I would post it here in case anyone else still has this problem.
I am doing this in forge 965, for 1.6.4, but the same should apply for the 1.7.10 listed above (since it was their hint that helped me solve it for 1.6.4).
Have your 'book item' extend 'ItemEditableBook' as MatrexsVigil does above.
Add the override (as she does) for:
public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
{
par3EntityPlayer.displayGUIBook(par1ItemStack);
return par1ItemStack;
}
But, replace the call to displayGUIBook with this:
if (player.worldObj.isRemote)
{
Minecraft.getMinecraft().displayGuiScreen(new GuiScreenBook(player, par1ItemStack, false));
}
I threw it inside a check on worldObj.isRemote just to be safe. It may not be necessary. My formerly unworking book suddenly will open and work!
Note: I've not done any 1.7.10 work, so the Minecraft.getMinecraft may or may not be different for 1.7.10. The rest is straight out of domi1819's detective work above. It does not require his workaround either.
Found the reason. Nothing is wrong with your code.
I've found that in EntityPlayerSP.class the interface will only open if the Item is an actual book.
This is one of the biggest limitations to modders -- in many places in the Minecraft code it checks if it something is equal to a specific instance, rather than using the instanceof operator. So if the Minecraft code is checking for an myItem == Items.book then if you extend ItemBook it won't be recognized as a book -- because only the one instance of vanilla book is checked for.
If they could replace every == with an instanceof, we'd have much, much easier time modding.
Can anyone explain why this book can be created with its recipe (showing the correct texture, author, and title) but it won't open like a normal book?
No. Even when I add @Override to onUpdate, nothing changes.
I realize this is now almost a year old, but I did find the solution thanks to the hint above. Thought I would post it here in case anyone else still has this problem.
I am doing this in forge 965, for 1.6.4, but the same should apply for the 1.7.10 listed above (since it was their hint that helped me solve it for 1.6.4).
Have your 'book item' extend 'ItemEditableBook' as MatrexsVigil does above.
Add the override (as she does) for:
public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
{
par3EntityPlayer.displayGUIBook(par1ItemStack);
return par1ItemStack;
}
But, replace the call to displayGUIBook with this:
if (player.worldObj.isRemote)
{
Minecraft.getMinecraft().displayGuiScreen(new GuiScreenBook(player, par1ItemStack, false));
}
I threw it inside a check on worldObj.isRemote just to be safe. It may not be necessary. My formerly unworking book suddenly will open and work!
Note: I've not done any 1.7.10 work, so the Minecraft.getMinecraft may or may not be different for 1.7.10. The rest is straight out of domi1819's detective work above. It does not require his workaround either.
Enjoy.
I would like to note to anyone who reads this post to find out how to make a book like this that it works in 1.8!
How do I do that in the book was something written ?
I would also like to know that
This is one of the biggest limitations to modders -- in many places in the Minecraft code it checks if it something is equal to a specific instance, rather than using the instanceof operator. So if the Minecraft code is checking for an myItem == Items.book then if you extend ItemBook it won't be recognized as a book -- because only the one instance of vanilla book is checked for.
If they could replace every == with an instanceof, we'd have much, much easier time modding.