Jump to content

Container item doesn't display 'lore' after being crafted with


ilan321

Recommended Posts

Hello, I am making a transmutation stone. It has a limited number of uses (stored in nbt). I have subscribed to the ItemCrafted event, and I can assign NBT to the item and have it display the 'lore' - being the number of uses. When the item is crafted, it shows the uses ('Uses: 128', etc). However, after crafting with the item (for example, the stone + 1 gold ingot = 4 iron ingot), it does not display any of the information.

The addInformation method: 

    @Override
    public void addInformation(ItemStack stack, EntityPlayer player,
                               List list, boolean par4)
    {
        if (stack.stackTagCompound != null)
        {
            String owner = stack.stackTagCompound.getString("owner");
            Integer uses = stack.stackTagCompound.getInteger("uses");
            list.add("Owner: " + owner);
            if (uses > 95)
            {
                list.add("Uses: " + EnumChatFormatting.GREEN + uses);
            }
            if ((uses > 63) && (uses < 96))
            {
                list.add("Uses: " + EnumChatFormatting.YELLOW + uses);
            }
            if ((uses < 64) && (uses > 2))
            {
                list.add("Uses: " + EnumChatFormatting.RED + uses);
            }
            if (uses.equals(1))
            {
                list.add("Uses: " + EnumChatFormatting.DARK_RED + uses);
            }
        }
    }

 

The code for the itemcrafted event:

        for (int i = 0; i < e.craftMatrix.getSizeInventory(); i++)
        {
            if (e.craftMatrix.getStackInSlot(i) != null)
            {
                if (e.craftMatrix.getStackInSlot(i).getItem() == Transmutation.bStone)
                {
                    if (e.craftMatrix.getStackInSlot(i).stackTagCompound != null)
                    {
                        e.craftMatrix.getStackInSlot(i).stackTagCompound.setInteger("uses", (e.craftMatrix.getStackInSlot(i).stackTagCompound.getInteger("uses") - 1));
                        if (e.craftMatrix.getStackInSlot(i).stackTagCompound.getInteger("uses") < 1)
                        {
                            e.craftMatrix.setInventorySlotContents(i, null);
                            return;
                        }
                    }
                    return;
                }
            }
        }

 

How can I reassign the extra info after the item is crafted with?

 

Link to comment
Share on other sites

I see, so you're telling it function like a bucket. But doesn't a bucket full of milk in a crafting recipe return an empty bucket? I think that is what is happening to your stone, you change the lore in the crafting table but since it's a container the crafting thing returns a new instance of the stone instead of one with lore. Maybe instead of having the stone be a container item, check to see if the stone was used in the recipe and if it was insert a new stone into the player's inventory with with one less use. I tried that myself with your code and it seemed to work fine.

 

edit: I wanted to avoid posting code but I may as well before I remove it. It's better if you try to follow the description and try to implement it yourself but if you're stumped on that:

@SubscribeEvent
public void onItemCraftEvent(ItemCraftedEvent e) {
	for (int i = 0; i < e.craftMatrix.getSizeInventory(); i++)
	{
		if (e.craftMatrix.getStackInSlot(i) != null)
		{
			if (e.craftMatrix.getStackInSlot(i).getItem() == SuperCool.superItem) //my item
			{
				if (e.craftMatrix.getStackInSlot(i).stackTagCompound != null)
				{
					e.craftMatrix.getStackInSlot(i).stackTagCompound.setInteger("uses", (e.craftMatrix.getStackInSlot(i).stackTagCompound.getInteger("uses") - 1));
					ItemStack superItem = e.craftMatrix.getStackInSlot(i); //stores the item with lore for future use
					if (e.craftMatrix.getStackInSlot(i).stackTagCompound.getInteger("uses") < 1)
					{
						e.craftMatrix.setInventorySlotContents(i, null);
					}else{
						InventoryPlayer inv = e.player.inventory; //no need for this, just wanted code to be more readable
						boolean placedItem = false;
                                                        //finds first empty slot, inserts the item with lore. 
						for (int j = 0; j < inv.getSizeInventory() && !placedItem; j++){
							if(inv.getStackInSlot(j) == null){
								inv.addItemStackToInventory(superItem);
								placedItem = true;
								e.player.inventory = inv;
							}
						}
						if(!placedItem) e.player.dropItem(superItem.getItem(), 1); //if all slots were full, drops item with lore on the ground
					}
				}
			}
		}
	}
}

Link to comment
Share on other sites

I think I found it, I have to override the onCraft method and set the slot contents manually. I'll test soon, I'm not home either :P

 

Edit: never mind, that was a method for an old version of Forge (it used ICraftingHandler). I tried removing the isContainer method and placing the transmutation stone in the crafting grid during the ItemCrafted event but it just disappears.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.