| 1 | package net.minecraft.item;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.minecraft.entity.player.EntityPlayer;
|
| 5 | import net.minecraft.entity.player.EntityPlayerMP;
|
| 6 | import net.minecraft.inventory.Slot;
|
| 7 | import net.minecraft.nbt.NBTTagCompound;
|
| 8 | import net.minecraft.nbt.NBTTagList;
|
| 9 | import net.minecraft.nbt.NBTTagString;
|
| 10 | import net.minecraft.network.play.server.S2FPacketSetSlot;
|
| 11 | import net.minecraft.stats.StatList;
|
| 12 | import net.minecraft.util.ChatComponentProcessor;
|
| 13 | import net.minecraft.util.ChatComponentText;
|
| 14 | import net.minecraft.util.EnumChatFormatting;
|
| 15 | import net.minecraft.util.IChatComponent;
|
| 16 | import net.minecraft.util.StatCollector;
|
| 17 | import net.minecraft.util.StringUtils;
|
| 18 | import net.minecraft.world.World;
|
| 19 |
|
| 20 | public class ItemEditableBook extends Item
|
| 21 | {
|
| 22 | public ItemEditableBook()
|
| 23 | {
|
| 24 | this.setMaxStackSize(1);
|
| 25 | }
|
| 26 |
|
| 27 | public static boolean validBookTagContents(NBTTagCompound nbt)
|
| 28 | {
|
| 29 | if (!ItemWritableBook.isNBTValid(nbt))
|
| 30 | {
|
| 31 | return false;
|
| 32 | }
|
| 33 | else if (!nbt.hasKey("title", 8))
|
| 34 | {
|
| 35 | return false;
|
| 36 | }
|
| 37 | else
|
| 38 | {
|
| 39 | String s = nbt.getString("title");
|
| 40 | return s != null && s.length() <= 32 ? nbt.hasKey("author", 8) : false;
|
| 41 | }
|
| 42 | }
|
| 43 |
|
| 44 | /**
|
| 45 | * Gets the generation of the book (how many times it has been cloned)
|
| 46 | */
|
| 47 | public static int getGeneration(ItemStack book)
|
| 48 | {
|
| 49 | return book.getTagCompound().getInteger("generation");
|
| 50 | }
|
| 51 |
|
| 52 | public String getItemStackDisplayName(ItemStack stack)
|
| 53 | {
|
| 54 | if (stack.hasTagCompound())
|
| 55 | {
|
| 56 | NBTTagCompound nbttagcompound = stack.getTagCompound();
|
| 57 | String s = nbttagcompound.getString("title");
|
| 58 |
|
| 59 | if (!StringUtils.isNullOrEmpty(s))
|
| 60 | {
|
| 61 | return s;
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | return super.getItemStackDisplayName(stack);
|
| 66 | }
|
| 67 |
|
| 68 | /**
|
| 69 | * allows items to add custom lines of information to the mouseover description
|
| 70 | */
|
| 71 | public void addInformation(ItemStack stack, EntityPlayer playerIn, List<String> tooltip, boolean advanced)
|
| 72 | {
|
| 73 | if (stack.hasTagCompound())
|
| 74 | {
|
| 75 | NBTTagCompound nbttagcompound = stack.getTagCompound();
|
| 76 | String s = nbttagcompound.getString("author");
|
| 77 |
|
| 78 | if (!StringUtils.isNullOrEmpty(s))
|
| 79 | {
|
| 80 | tooltip.add(EnumChatFormatting.GRAY + StatCollector.translateToLocalFormatted("book.byAuthor", new Object[] {s}));
|
| 81 | }
|
| 82 |
|
| 83 | tooltip.add(EnumChatFormatting.GRAY + StatCollector.translateToLocal("book.generation." + nbttagcompound.getInteger("generation")));
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | /**
|
| 88 | * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
|
| 89 | */
|
| 90 | public ItemStack onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn)
|
| 91 | {
|
| 92 | if (!worldIn.isRemote)
|
| 93 | {
|
| 94 | this.resolveContents(itemStackIn, playerIn);
|
| 95 | }
|
| 96 |
|
| 97 | playerIn.displayGUIBook(itemStackIn);
|
| 98 | playerIn.triggerAchievement(StatList.objectUseStats[Item.getIdFromItem(this)]);
|
| 99 | return itemStackIn;
|
| 100 | }
|
| 101 |
|
| 102 | private void resolveContents(ItemStack stack, EntityPlayer player)
|
| 103 | {
|
| 104 | if (stack != null && stack.getTagCompound() != null)
|
| 105 | {
|
| 106 | NBTTagCompound nbttagcompound = stack.getTagCompound();
|
| 107 |
|
| 108 | if (!nbttagcompound.getBoolean("resolved"))
|
| 109 | {
|
| 110 | nbttagcompound.setBoolean("resolved", true);
|
| 111 |
|
| 112 | if (validBookTagContents(nbttagcompound))
|
| 113 | {
|
| 114 | NBTTagList nbttaglist = nbttagcompound.getTagList("pages", 8);
|
| 115 |
|
| 116 | for (int i = 0; i < nbttaglist.tagCount(); ++i)
|
| 117 | {
|
| 118 | String s = nbttaglist.getStringTagAt(i);
|
| 119 | IChatComponent ichatcomponent;
|
| 120 |
|
| 121 | try
|
| 122 | {
|
| 123 | ichatcomponent = IChatComponent.Serializer.jsonToComponent(s);
|
| 124 | ichatcomponent = ChatComponentProcessor.processComponent(player, ichatcomponent, player);
|
| 125 | }
|
| 126 | catch (Exception var9)
|
| 127 | {
|
| 128 | ichatcomponent = new ChatComponentText(s);
|
| 129 | }
|
| 130 |
|
| 131 | nbttaglist.set(i, new NBTTagString(IChatComponent.Serializer.componentToJson(ichatcomponent)));
|
| 132 | }
|
| 133 |
|
| 134 | nbttagcompound.setTag("pages", nbttaglist);
|
| 135 |
|
| 136 | if (player instanceof EntityPlayerMP && player.getCurrentEquippedItem() == stack)
|
| 137 | {
|
| 138 | Slot slot = player.openContainer.getSlotFromInventory(player.inventory, player.inventory.currentItem);
|
| 139 | ((EntityPlayerMP)player).playerNetServerHandler.sendPacket(new S2FPacketSetSlot(0, slot.slotNumber, stack));
|
| 140 | }
|
| 141 | }
|
| 142 | }
|
| 143 | }
|
| 144 | }
|
| 145 |
|
| 146 | public boolean hasEffect(ItemStack stack)
|
| 147 | {
|
| 148 | return true;
|
| 149 | }
|
| 150 | }
|
| 151 |
|