Jump to content

[1.8] Leaving an item in the craftMatrix


ilan321

Recommended Posts

Hello! I am rewriting my Transmutation mod, and I still cannot figure out how to leave an item in the craftMatrix. According to this post, I am supposed to call

matrix.setInventorySlotContents(int i, ItemStack k)

, but when I do so, the item does not stay in the crafting grid!

 

My event subscription method is as follows:

 

public class CraftingHandler
{
    @SubscribeEvent
    public void ItemCraftedEvent(PlayerEvent.ItemCraftedEvent e)
    {
        Transmutation.logger.info("Attempting to craft");
        for (int i = 0; i < e.craftMatrix.getSizeInventory(); i++)
        {
            if (e.craftMatrix.getStackInSlot(i) == null) continue;
            if (e.craftMatrix.getStackInSlot(i).getItem() == Recipes.tStone)
            {
                Transmutation.logger.info("Crafting with a transmutation stone");
                ItemStack stone = e.craftMatrix.getStackInSlot(i);
                if (!stone.hasTagCompound()) return; // debug stone
                Transmutation.logger.info("Crafting complete");
                stone.getTagCompound().setInteger("uses", stone.getTagCompound().getInteger("uses")-1);
                if (stone.getTagCompound().getInteger("uses") < 1) stone = new ItemStack(Blocks.air);
                else
                {
                    Transmutation.logger.info("Attempting to leave the itemstack in the matrix..");
                    e.craftMatrix.setInventorySlotContents(i, stone);
                }
                return;
            }
        }
    }
}

 

As you can see, I am calling

 e.craftMatrix.setInventorySlotContents(i, stone);

but the item does not stay in the crafting grid. How can I fix this?

 

 

On a side note, is there a way to "remove" an itemstack from the crafting grid? I don't know if setting it to an air block is something that's good or not.

Link to comment
Share on other sites

There is ItemStack version of getItemStack in the Item class:

    public ItemStack getContainerItem(ItemStack itemStack)

You can use it to migrate/modify NBT of the container item.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Link to comment
Share on other sites

Where do I edit the nbt while crafting? The craft event?

 

Edit:

 

Never mind! I figured it out myself! Apparently you override the getContainerItem method and do what you wish with it. Here's my end result:

    @Override
    public ItemStack getContainerItem(ItemStack itemStack)
    {
        int uses = itemStack.getTagCompound().getInteger("uses")-1;
        String owner = itemStack.getTagCompound().getString("owner");
        if (uses < 1)
        {
            return null;
        }
        ItemStack cItem = new ItemStack(getContainerItem());
        cItem.setTagCompound(new NBTTagCompound());
        cItem.getTagCompound().setInteger("uses", uses);
        cItem.getTagCompound().setString("owner", owner);
        return cItem;
    }

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.