Jump to content

addItemStackToInventory stops working after calling it 1 time.


ItsMartNotMert

Recommended Posts

This is the class that this topic is about:

 

public class TileRuneInfuser extends TileEntity {

    private ItemStack rune;
    private ItemStack modifier;


    public void onUse(ItemStack heldItem, EntityPlayer player, EnumHand hand){
        if(heldItem.getItem() instanceof ItemRune){
            if(rune == null){
                ItemStack heldItem2 = heldItem.copy();

                heldItem2.stackSize = 1;
                setRune(heldItem2);

                heldItem.stackSize--;
                player.setHeldItem(hand, heldItem);
                System.out.println("Rune Added");
            }
        }
        else{
            if(modifier == null){
                ItemStack heldItem2 = heldItem.copy();

                heldItem2.stackSize = 1;
                setModifier(heldItem2);

                heldItem.stackSize--;
                player.setHeldItem(hand, heldItem);
                System.out.println("Modifier Added");
            }
        }

        checkRecipe();
    }

    public void extractItem(EntityPlayer player, EnumHand hand){
        if(modifier != null){
            player.inventory.addItemStackToInventory(modifier);
            System.out.println("Modifier Extracted:" + modifier.getDisplayName());
            setModifier(null);
        }
        else if(rune != null) {
            player.inventory.addItemStackToInventory(rune);
            setRune(null);
            System.out.println("Rune Extraced");
        }
    }

    public void checkRecipe(){
        if(rune != null && modifier != null){
            for(BiMap.Entry<ItemStack, InfuserRecipeRegister.InfuserRecipe> b : InfuserRecipeRegister.getRecipes().entrySet()){
                if(modifier.getItem() == b.getKey().getItem()){

                    ItemStack output = b.getValue().getOutput();
                    setModifier(output);
                    System.out.println("Output Set to: " + output.getDisplayName());

                    setRune(null);


                    System.out.println("Rune Removed");
                    return;
                }
            }
        }
    }


    public ItemStack getModifier() {
        return modifier;
    }

    public void setModifier(ItemStack modifier) {
        this.modifier = modifier;
    }

    public ItemStack getRune() {

        return rune;
    }

    public void setRune(ItemStack rune) {
        this.rune = rune;
    }

    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound compound) {
        super.writeToNBT(compound);

        if(rune != null){
            NBTTagList tagList = new NBTTagList();
            NBTTagCompound itemCompound = new NBTTagCompound();
            rune.writeToNBT(itemCompound);
            tagList.appendTag(itemCompound);
            compound.setTag("rune", tagList);
        }

        if(modifier != null){
            NBTTagList itemList = new NBTTagList();
            NBTTagCompound modifierCompound = new NBTTagCompound();
            modifier.writeToNBT(modifierCompound);
            itemList.appendTag(modifierCompound);
            compound.setTag("modifier", itemList);
        }

        return compound;
    }
    
    @Override
    public void readFromNBT(NBTTagCompound compound) {
        super.readFromNBT(compound);

            NBTTagList tagList = (NBTTagList) compound.getTag("rune");
            NBTTagCompound tagCompound = tagList.getCompoundTagAt(0);
            rune = ItemStack.loadItemStackFromNBT(tagCompound);


            NBTTagList modifierList = (NBTTagList) compound.getTag("modifier");
            NBTTagCompound modifierCompound = modifierList.getCompoundTagAt(0);
            modifier = ItemStack.loadItemStackFromNBT(modifierCompound);


    }
}

 

 

So this is how it goes. I add an rune. It gets placed in the rune variable. I add a modifier like a diamond and it will get placed in the modifier variable. This all works with rightclicking

 

Then it looks for the recipe having the modifier and it finds it. It sets the rune value to null and the modifier to the item. I rightclick again and extract the item via the extractItem function and get the output.

 

Then when I do this all over again it does not seem to work. The item doesnt get added to my inventory anymore. It gives all the right information with the println's but just doesnt add the itemstack to the inventory.

 

Does anybody know a fix?

Link to comment
Share on other sites

Sounds like an issue I had a week or so ago, with one of my blocks.

Are you sure that the itemstack is a new itemstack?

You cannot really add something to your inventory, which already exists in your inventory, because it IS already in your inventory.

 

Compare the ItemStacks. Either run a few System.out.println(stack.toString) or compare the stacks directly, and see if they equal eachother.

Also previously known as eAndPi.

"Pi, is there a station coming up where we can board your train of thought?" -Kronnn

Published Mods: Underworld

Handy links: Vic_'s Forge events Own WIP Tutorials.

Link to comment
Share on other sites

Sounds like an issue I had a week or so ago, with one of my blocks.

Are you sure that the itemstack is a new itemstack?

You cannot really add something to your inventory, which already exists in your inventory, because it IS already in your inventory.

 

Compare the ItemStacks. Either run a few System.out.println(stack.toString) or compare the stacks directly, and see if they equal eachother.

 

Thanks, problem found!

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.